0% found this document useful (0 votes)
36 views35 pages

Day 1 Placement Training

The document contains a series of programming tasks and their corresponding C code implementations. These tasks cover various topics such as ASCII value conversion, float value formatting, profit calculation, number reversal, Tic-Tac-Toe position conversion, trendy number checking, mango tree identification, prime number generation, hollow square pattern printing, unique product ID summation, unique character counting in strings, factorial calculation, and triangle printing. Each task includes sample input and output for clarity.

Uploaded by

bharatiyaboy1267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views35 pages

Day 1 Placement Training

The document contains a series of programming tasks and their corresponding C code implementations. These tasks cover various topics such as ASCII value conversion, float value formatting, profit calculation, number reversal, Tic-Tac-Toe position conversion, trendy number checking, mango tree identification, prime number generation, hollow square pattern printing, unique product ID summation, unique character counting in strings, factorial calculation, and triangle printing. Each task includes sample input and output for clarity.

Uploaded by

bharatiyaboy1267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1.

Write a program to get a character as an input and print


its ASCII value.
Sample Input:
a
Sample output:
97

Program:

#include <stdio.h>
#include<math.h>
int main()
{
int a=99;
printf("%c",(char)a);

}
2.
Write a program to get a float value from the user and
display it in the below-mentioned format.
HINT: Use ceil() and floor() functions from the header file.

Sample Input:
54.5
Sample output:
54 // o.value
55 // round up
54 // round down

Program :
#include <stdio.h>
#include<math.h>
int main()
{
float a=54.5;
printf("%d\n",(int)a);
printf("%d\n",(int)ceil(a));
printf("%d\n",(int)floor(a));

}
3.
Each Saturday, The Herald sells 'a' copies of a special
edition newspaper for Rs. b per copy. The cost to him for
printing each newspaper is Rs. c. He pays a fixed cost of
Rs.100 per Saturday for storage, delivery, and so on. He
wants to calculate the profit which it obtains only on
Saturdays. Can you please help him out by writing a
program to compute the profit if a, b, and c are given?
Sample Input:
1000
2
1
Sample output:
900

Program:
#include <stdio.h>
int main()
{
int a,b,c,o1,o2,res=0;
scanf("%d%d%d",&a,&b,&c);
o1=a*b;
o2=a*c;
res=o1-o2-100;
printf("%d",res);
}
4.
Write a program to reverse the 3 digit number.
Sample Input:
456
Sample output:
654

Program:

#include <stdio.h>
int main()
{
int number,reverse=0,rem=0;
scanf("%d",&number);
while(number != 0)
{
int rem = number % 10;
reverse = reverse * 10 + rem;
number = number/10;
}
printf("%d",reverse);

}
4. Pradeep is designing the input interaction for the X-O
game (a.k.a TIC-TAC-TOE). The user identifies the
positions as 1|2|3 4|5|6| 7|8|9. Since the board is a
2-dimensional layout (3x3), Pradeep uses a 2-dimensional
array to store board status and he identifies positions by
row, and column as for first-row elements -(0,0),(0,1),(0,2)
and for second-row elements - (1,0),(1,1),(1,2) and for 3rd
row elements - (2,0),(2,1),(2,2). Help Pradeep to solve the
conversion from user's input (1-9) to (Row, Column)
(0-2),(0-2) respectively.
Note - Indexing for rows and columns is 0-based.

Sample Input:
6
Sample output:
12

Program:
#include <stdio.h>

int main()

int n;
scanf("%d",&n);

int i=(n-1)/3;

int j=(n-1)%3;

printf("%d %d",i,j);

5. Joey Tribbiani wanted to buy a meatball Sandwich and


went to the nearby sandwich shop. There was a poster that
said if anyone comes up with a program for Trendy
Number they can get free Sandwiches for a lifetime as a
gift. Can you help Joey to write a program to check
whether the given number is a trendy number or not?

A number is said to be a trendy number if it has 3 digits


and the middle digit is divisible by 3.

Sample Input:
791
Sample output:
Trendy number
Program:
#include <stdio.h>

int main()

int n,sl;

scanf("%d",&n);

if((n>=100)&&(n<1000))

sl=(n/10)%10;

if(sl%3==0)

printf("Trendy number");

}else

printf("Not Trendy number");

}else
{

printf("Invalid Number");

6.In the kingdom of Terebinthia, Leslie Burke is so much


interested in gardening and hence she plants more trees in
her garden. She plants trees in a rectangular fashion with
the order of rows and columns and numbers the trees in a
row-wise order. She planted the mango trees only in the
1st row, 1st column, and last column. So, given the tree
number, your task is to find out whether the given tree is a
mango tree or not. Now, write a program to check whether
the given number denotes a mango tree or not.

Sample Input:
5
5
11
Sample output:
yes
Program:

#include <stdio.h>

int main()

int r,c,t;

scanf("%d%d%d",&r,&c,&t);

if(t<=c||(t%c==1)||(t%c==0))

printf("Yes");

}else

printf("No");

}
7. A prime number is divisible only by 1 and itself. You are
given a positive integer. Write an algorithm to find all the
prime numbers from 2 to the given positive number.

Sample Input:
11
Sample output:
2 3 5 7 11

Program:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=2;i<=n;i++)
{
int flag=1;
int m=i/2;
for(int j=2;j<=m;j++)
{
if(i%j==0)
{
flag=0;
break;
}
}
if(flag==1)
{
printf("%d ",i);
}
}
}

9.Write a program to print the hollow square pattern.

Input:

Output:

* * * * *

* *

* *

* *

* * * * *

Program:

#include<stdio.h>

int main()
{

int n;

scanf("%d",&n);

for(int i=0;i<n;i++)

for(int j=0;j<n;j++)

if(i==0||j==0||i==n-1||j==n-1)

printf("*");

}else

printf(" ");

printf("\n");
}

10.

Calculating the Total of Unique Products Sold

You are working as an inventory manager for an e-commerce


company. The company tracks the products sold across
different regions. However, some products might be sold in
multiple regions, and they want to calculate the total sales of
products that have only been sold in one region. This means
you need to find the sum of product IDs that appear only once
in the list.

You are given an array of product IDs, where each product ID


represents a product sold. Your task is to write a program that
calculates the sum of unique product IDs, i.e., those that
appear exactly once in the array. If no product ID is unique
(i.e., all appear more than once), return 0.

Function Signature

int SumUniqueElements(int[] arr, int length);

Assumptions
The length of the array is always greater than 0.

The product IDs in the array are positive integers.

You need to sum only those product IDs which appear exactly
once.

Input Format

The first input is the size of the array, length.

The second input is the array arr which contains the product
IDs.

Output Format

You need to output the sum of all unique product IDs.

Constraints

1 ≤ length ≤ 10000

The product IDs in the array are positive integers within the
integer range.

Example 1

Sample Input 1

8
25479248

Sample Output 1

29

#include <stdio.h>

int unique(int a[], int n) {

int sum = 0;

for (int i = 0; i < n; i++) {

int d = 0;

for (int j = 0; j < n; j++) {

if (i != j && a[i] == a[j]) {

d = 1;

break;

}
}

if (d==0) {

sum += a[i];

return sum;

int main() {

int n;

scanf("%d", &n);

int a[n];

for (int i = 0; i < n; i++) {

scanf("%d", &a[i]);

int res = unique(a, n);

printf("%d", res);
return 0;

11.

Unique Character Analysis

Imagine you are designing a security system for an


organization where unique character analysis of password-like
inputs is essential. In this system, each string input (such as a
password or security phrase) needs to be evaluated to
determine the count of unique characters. This feature helps in
assessing the complexity of passwords for security purposes.
You are required to write a Java program to calculate the
number of unique characters in a given string.

You are required to implement a method that takes a string str


as input and returns the number of unique characters in the
string. If the input string is null, return -1. The string contains
only lowercase alphabets and has a length of up to 100,000
characters.

Function Signature

int numberUniqueCharacters(String str)


Input Format

A string str containing only lowercase alphabets.

Output Format

An integer representing the number of unique characters in


the string.

Constraints

The length of str is less than or equal to 100,000 characters.

str contains only lowercase alphabets.

Example 1

Sample Input 1

abcdabc

Sample Output 1

#include <stdio.h>
#include <string.h>

#include <ctype.h>

int numberUniqueCharacters(char str[])

if (str == NULL)

return -1;

int count[26] = {0};

for (int i = 0; str[i] != '\0'; i++)

if (str[i] >= 'a' && str[i] <= 'z')

count[str[i] - 'a']++;

int cnt = 0;

for (int i = 0; i < 26; i++)

if (count[i] > 0)
cnt++;

return cnt;

int main()

char str[1000001];

scanf("%s", str);

printf("%d\n", numberUniqueCharacters(str));

return 0;

Calculating Factorials for the Space Expedition

In the futuristic world of 2140, a team of astronauts is


preparing for a journey to Mars. As part of their training, they
are required to calculate various mathematical functions, one
of which is the factorial of a number. The factorial of a
non-negative integer n is the product of all positive integers
less than or equal to n.
To streamline their calculations, the team decided to
implement a program that uses recursion to compute the
factorial. Your task is to help them write a program that
calculates the factorial of a given number using a recursive
approach.

Write a program that takes a non-negative integer as input and


returns its factorial using recursion.

Input Format

The input consists of a single non-negative integer n.

Output Format

Print the factorial of the number.

Example 1

Sample Input 1

Sample Output 1

120

#include <stdio.h>

int fact(int n)
{

int fact=1;

for(int i=1;i<=n;i++)

fact*=i;

return fact;

int main()

int n;

scanf("%d",&n);

printf("%d",fact(n));

return 0;

Right Angled triangle:


#include <stdio.h>

int main()

int n;

scanf("%d",&n);

for(int i=0;i<n;i++)

for(int j=0;j<i+1;j++)

printf("*");

printf("\n");

}
Left Angled Triangle:

#include <stdio.h>

int main()

int n;

scanf("%d",&n);

for(int i=0;i<n;i++)

for(int j=0;j<n-i-1;j++)

printf(" ");

for(int j=0;j<i+1;j++)

printf("*");

}
printf("\n");

}​
Triangle:

#include <stdio.h>

int main()

int n;

scanf("%d",&n);

for(int i=0;i<n;i++)

for(int j=0;j<n-i-1;j++)

printf(" ");

for(int j=0;j<i+1;j++)

{
printf("* ");

printf("\n");

Write a program to count the number of vowels in the given


string.

Write a program to find whether the given string is a palindrome or


not without using string library functions.

Note: The string reads the same backward and forward.

Input:

Alliance university

Output:

University alliance
/***********************************************************
*******************

Online C Compiler.

Code, Compile, Run and Debug C program online.

Write your code in this editor and press "Run" button to compile and
execute it.

************************************************************
*******************/

// Write a program to count the number of vowels & consonants in


the given string.

#include <stdio.h>

#include<string.h>

//example: Alliance =8 4 anekal\0

// int main()
// {

// char a[100];

// scanf("%[^\n]s",a);

// int l=strlen(a);

// int count=0,consonants=0;

// for(int i=0;i<l;i++)

// {

// char ch=a[i];

//
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'|
|ch=='O'||ch=='U')

// {

// count++;

// }else{

// consonants++;

// }

// }

// printf("%d %d",count,consonants);

// }
// Write a program to find whether the given string is a palindrome or
not without using string library functions.

// Note: The string reads the same backward and forward.

// example: mam

// int main()

// {

// char a[100],rev[100];

// scanf("%[^\n]s",a);

// int l=0;

// for(int i=0;a[i]!='\0';i++)

// {

// l++;

// }

// // j=5,4,3,2,1 mam
// // k = 1 2 3 4 5 rev[0]=a[2] rev[m ]

// // rev[1]=a[1] rev[m a m]

// int k=0;

// for(int j=l-1;j>=0;j--)

// {

// rev[k]=a[j];

// k++;

// }

// for(int i=0;i<l;i++)

// {

// if(rev[i]!=a[i]) // ankel

// {

// printf("Not a palindrome");

// return 0;

// }

// }

// printf("Palindrome");

// }
// string reverse for the sentence

//example: this is a pen output: pen a is this

int main()

char str[100],word[100];

scanf("%[^\n]s",str);

int count=0;

int l=strlen(str);

int i,j=0;

// for(int i=l-1;i>=0;i--)

// {

// if(str[i]==" "|| str[i]=='\0') // this is a pen

// {

// word[j++]=" ";

// }else
// {

// word[j++]=str[i];

// }

// }

// printf("%s",word);

for(int i=l-1;i>=0;i--)

if(str[i]==" "|| str[i]=='\0') // this is a pen

word[j++]=" ";

}else

word[j++]=str[i];

}
int k=0,count1=0;

char rev[100];

for(int i=0;word[i]!="\0";i++)

if(word[i]!=" ")

count1++;

for(int y=count1;y>=0;y--)

rev[k++]=word[count1];

printf("%s",rev);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy