You are on page 1of 9

1. Write a program which takes the number of items as input.

It must output “You


have x items” if the number of items(x) is more than 1 and “You have 1 item”
otherwise. Use conditional operators.

Code :

#include<stdio.h>

int main()
{
int x;
printf("Enter a number of item(s) : ");
scanf("%d",&x);
x>1?printf("\nYou have %d items.\n",x):printf("\nYou have %d item.\n",x);
return 0;
}

Output :
2. Find the maximum among three numbers using the conditional operator.

Code :

#include<stdio.h>

int main()
{
int a,b,c,ans;
printf("\nEnter 3 numbers : ");
scanf(" %d %d %d",&a,&b,&c);
ans=((a>b)?((a>c)?a:c):((b>c)?b:c));
printf("\n%d is the largest among given three numbers.",ans);
return 0;
}

Output :
3. Take a number as input using scanf and check whether it is odd or even using
the conditional operator.

Code:

#include<stdio.h>

int main()
{
int x;
printf("Enter a number : ");
scanf("%d",&x);
(x%2==0)?printf("\n%d is an even number.\n",x):printf("\n%d is an odd
number.\n",x);
return 0;
}

Output :
4. What is the output of the following two programs?

a.
#include<stdio.h>

int main()
{
int a;
a = 1<0?2?3?4:5:6:7;
printf("%d",a);
return 0;
}

Output : 7

b.
#include<stdio.h>

int main()
{
int a;
a = 1?2?3?4:5:6:7;
printf("%d",a);
return 0;
}

Output : 4
5. Take two consecutive numbers(example 40 and 41) and write a simple program
that demonstrates bitwise logical operators(&,|,^,~) between them. Use << and
>> on 40 to demonstrate how they represent multiplication and division by
powers of 2.

Code :

#include<stdio.h>

int main()
{
unsigned int a,b;
printf("Enter two numbers : ");
scanf("%d %d",&a,&b);
printf("\n a = %d\tb = %d\n",a,b);
printf("\n a & b = %d\n",a&b);
printf(" a | b = %d\n",a|b);
printf(" a ^ b = %d\n",a^b);
printf(" ~a = %d\n",~a);
printf(" a<<1 = %d\n",a<<1);
printf(" a>>1 = %d\n",a>>1);
return 0;
}

Output :
6. What is the output in the following two code snippets and name what kind of
type conversion is taking place(explicit or implicit) ?

a.
#include<stdio.h>

int main()
{
int x=7, y=5;
float z;
z = x/y;
printf("%f",z);

return 0;
}

This is an implicit type conversion


Output : 1.000000

b.
#include<stdio.h>

int main()
{
int x=7, y=5;
float z;
z = (float)x/(float)y;
printf("%f",z);

return 0;
}

This is an explicit type conversion.


Output : 1.400000
7. You are given a person’s weight (w) as a floating number input. Find the weight
that immediately follows this value.(Eg: if w=60.3 or 60.7, next weight is 61)
Make use of type casting.

Code :

#include<stdio.h>

int main()
{
float w;
printf("\nEnter your Weight : ");
scanf("%f",&w);
int x=(float)(w+1);
printf("\nNext wieght is %d\n",x);

return 0;
}

Output :
8. Take two float numbers x= 3.52691 and y= 12.43156 as input and print their sum
with a) precision 2, b) a width of 7 and precision 3, c) a width of 12 and
precision 3 and the extra positions padded with 0.

Code :

#include<stdio.h>

int main()
{
float x,y;
printf("\nEnter two floating number : ");
scanf("%f %f",&x,&y);
printf("\nx = %f\ty = %f\n",x,y);
float sum =x+y;
printf("\nsum of x & y = %.2f",sum);
printf("\nsum of x & y = %7.3f",sum);
printf("\nsum of x & y = %012.03f",sum);

return 0;
}

Output :
9. Write a program which inputs day of birth(d) and birth month(m) and outputs it
in the format “Your birthday is on dd/mm” where extra places are padded with
zero. (If day of birth is 3,it should output day as 03 and same for month)

Code :

#include<stdio.h>

int main()
{
int d,m;
printf("\nEnter your date of birth : ");
scanf("%d",&d);
printf("Enter your birth month(in numeric) : ");
scanf("%d",&m);
printf("\nYour birthday is on %02d/%02d",d,m);

return 0;
}
Output :

You might also like