Ticker

6/recent/ticker-posts

Greater between 2 numbers and 3 numbers

1.Greater between between 2 numbers -
  • if else - 
#include <stdio.h>

int main()
{ int a,b;
    printf("Enter the numbers ");
    scanf("%d %d",&a,&b); //Input the numbers
    if(a>b)
{ printf("The greater no. is %d",a); }
     else
{ printf("The greater no. is %d",b); }
  return 0;
}

Output -

Enter the numbers 4
5
The greater no.is 5


  • Conditional (ternary) - 
#include <stdio.h>

int main()
{ int a,b,c;
    printf("Enter the numbers ");
    scanf("%d %d",&a,&b); //Input the numbers
    c=a>b?a:b;
    printf("The greater no. is %d",c);
  return 0;
}
Output -
Enter the numbers 4
5
The greater no.is 5



2.Greater between between 3 numbers -
  • if else - 
#include <stdio.h>

int main()
{ int a,b,c;
    printf("Enter the numbers ");
    scanf("%d %d %d",&a,&b,&c); //Input the numbers
    if(a>b)
{ printf("The greater no. is %d",a); }
    else if (b>c)
{ printf("The greater no. is %d",b); }
    else 
{ printf("The greater no. is %d",c); }
  return 0;
} 
Output -
Enter the numbers 4
5
6
The greater no.is 6

  • Conditional (ternary) - 
#include <stdio.h>

int main()
{ int a,b,c,d;
    printf("Enter the numbers ");
    scanf("%d %d %d",&a,&b,&c); //Input the numbers
    d=a>b?a:b>c?b:c;
    printf("The greatest no. is %d",d);
  return 0;
}
Output -
Enter the numbers 4
5
6
The greater no.is 6


Post a Comment

0 Comments