To compare between two number follow the logic
e.g- if a>b is true then a is greater else b is greater.
using if else
#include <stdio.h>int main()
{
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
if(a>b)
printf("%d is greatest\n",a);
else
printf("%d is greatest\n",b);
return 0;
}
OUTPUT:
Enter two numbers
11
22
22 is greatest
using conditional operator
#include <stdio.h>int main()
{
int a,b;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
(a>b)?printf("%d is greatest\n",a):printf("%d is greatest\n",b);
return 0;
}
OUTPUT:
Enter two numbers
47
24
47 is greatest
Enter two numbers
47
24
47 is greatest
c program to find the greatest between three number
To find the greatest between three number follow the logic
consider three numbers a,b,c.
step-1: first compare a with b.
step-2: if step-1 is true compare a with c
step-3: if step-2 is true, print a is greater or if step-2 false print c is greater.
step-4: if step-1 is false, compare b with c.
step-5: if step-4 is true, print b is greater or if step-4 is false print c is greater.
consider three numbers a,b,c.
step-1: first compare a with b.
step-2: if step-1 is true compare a with c
step-3: if step-2 is true, print a is greater or if step-2 false print c is greater.
step-4: if step-1 is false, compare b with c.
step-5: if step-4 is true, print b is greater or if step-4 is false print c is greater.
using if else
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("%d is greatest\n",a);
else
printf("%d s greatest\n",c);
}
else
{
if(b>c)
printf("%d is greatest\n",b);
else
printf("%d is greatest\n",c);
}
return 0;
}
OUTPUT:
Enter three numbers
15
18
17
18 is greatest
using conditional operator
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
(a>b)?
((a>c)?printf("%d is greatest\n",a):printf("%d s greatest\n",c)):
((b>c)?printf("%d is greatest\n",b):printf("%d is greatest\n",c));
return 0;
}
OUTPUT:
Enter three numbers
24
32
49
49 is greatest
c program to find the greatest between four number
To find the greatest between four numbers follow the logic
consider four numbers a,b,c,d.
step-1: first compare a with b(a>b).
step-2: if step-1 is true compare a with c(a>c).
step-3: if step-2 is true compare a with d(a>d).
step-4: if step-3 is true, print a is greater or if step-3 false print d is greater.
step-5: if step-2 is false, compare c with d.
step-6: if step-5 is true, print c is greater or if step-5 is false print d is greater.
step-7: if step-1 is false compare b with c(b>c)
step-8: if step-7 is true compare b with d(b>d).
step-9: if step-8 is true, print b is greater or if step-8 false print d is greater.
step-10: if step-7 is false compare c with d(c>d).
step-11: if step-10 is true, print c is greater or if step-10 false print d is greater.
consider four numbers a,b,c,d.
step-1: first compare a with b(a>b).
step-2: if step-1 is true compare a with c(a>c).
step-3: if step-2 is true compare a with d(a>d).
step-4: if step-3 is true, print a is greater or if step-3 false print d is greater.
step-5: if step-2 is false, compare c with d.
step-6: if step-5 is true, print c is greater or if step-5 is false print d is greater.
step-7: if step-1 is false compare b with c(b>c)
step-8: if step-7 is true compare b with d(b>d).
step-9: if step-8 is true, print b is greater or if step-8 false print d is greater.
step-10: if step-7 is false compare c with d(c>d).
step-11: if step-10 is true, print c is greater or if step-10 false print d is greater.
using if else
#include <stdio.h>
int main()
{
int a,b,c,d;
printf("Enter four number\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b)
{
if(a>c)
{
if(a>d)
printf("%d is greatest\n",a);
else
printf("%d is greatest\n",d);
}
else
{
if(c>d)
printf("%d is greatest\n",c);
else
printf("%d is greatest\n",d);
}
}
else
{
if(b>c)
{
if(d>b)
printf("%d is greatest\n",d);
else
printf("%d is greatest\n",b);
}
else
{
if(c>d)
printf("%d is greatest\n",c);
else
printf("%d is greatest\n",d);
}
}
return 0;
}
OUTPUT:
Enter four number
14
78
25
34
78 s greatest
using conditional operator
#include <stdio.h>
int main()
{
int a,b,c,d;
printf("Enter four number\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
(a>b)?
(
(a>c)?((a>d)?printf("%d is greatest\n",a):printf("%d is greatest\n",d)):
((c>d)?printf("%d is greatest\n",c):printf("%d is greatest\n",d))
):
((b>c)?((d>b)?printf("%d is greatest\n",d):printf("%d is greatest\n",b)):
((c>d)?printf("%d is greatest\n",c):printf("%d is greatest\n",d))
);
return 0;
}
OUTPUT:
Enter four number
41
65
52
34
65 is greatest