C Language Tutorial Home

# IF CONDITION PROGRAMS SET 5

Q. #21) Write a C program to take input for three number check and print largest number?( method:4)

#include <stdio.h>
int main()
{
int a,b,c,m;
printf(“Enter 3 nos “);
scanf(“%d %d %d”,&a,&b,&c);
m=(a>b && a>c)?a:(b>c)?b:c;
printf(“max no = %d”,m);

return 0;
}

Q. #22) Write a C program to take input for three number check and print lowest number?( method:4)

#include <stdio.h>
int main()
{
int a,b,c,m;
printf(“Enter 3 nos “);
scanf(“%d %d %d”,&a,&b,&c);
m=(a<b && a<c)?a:(b<c)?b:c;
printf(“min no = %d”,m);

return 0;
}

Q. #23) Write a C program to take input for three number check and print the following:
* Largest number
* Lowest number
* difference between largest and lowest number

#include <stdio.h>
int main()
{
int a,b,c,ma,mi,d;
printf(“Enter 3 nos “);
scanf(“%d %d %d”,&a,&b,&c);
/* max no */
if(a>b && a>c)
ma=a;
else
if(b>c)
ma=b;
else
ma=c;
/* min no */
if(a<b && a<c)
mi=a;
else
if(b<c)
mi=b;
else
mi=c;

/* diff */
d=ma-mi;

printf(“max no = %d min no = %d diff = %d”,ma,mi,d);

return 0;
}

Q. #24)Write a C program to Take input for 2 numbers and an operator (+,-,*,/) based on the entered operator calculate and print the result?

#include <stdio.h>
int main()
{
float a,b,c;
char op;
printf(“Enter 2 nos “);
scanf(“%f %f”,&a,&b);
printf(“Enter the operator (+,-,*,/) “);
fflush(stdin);
scanf(“%c”,&op);
if(op==’+’)
{
c=a+b;
printf(“Sum = %f”,c);
}
else
if(op==’*’)
{
c=a*b;
printf(“prod = %f”,c);
}
else
if(op==’-‘)
{
if(a>b)
c=a-b;
else
c=b-a;
printf(“diff = %f”,c);
}
else
if(op==’/’)
{
c=a/b;
printf(“div = %f”,c);
}
else
printf(“invalid operator”);

return 0;
}