Write a C program to find the largest of 3 numbers.

Procedure 1:
int main() 
{
   int a, b, c;
   printf("Enter the value for a:\n");
   scanf("%d", &a);
   printf("Enter the value for b:\n");
   scanf("%d", &b);
   printf("Enter the value for c:\n");
   scanf("%d", &c);
   
   if ((a > b) && (a > c)) 
   {
      printf("\n The big one is a= %d", a);
   } 
   else if (b > c) 
   {
      printf("\n The big one is b= %d", b);
   } 
   else 
   {
      printf("\n The big one is c= %d", c);
   }
   return 0;
}
Procedure 2:
void main()
{
 
   int a,b,c;
   clrscr();
   printf("Enter the value of a:");
   scanf("%d",&a);
   printf("\nEnter the value of b:");
   scanf("%d",&b);
   printf("\nEnter the value of c:");
   scanf("%d",&c);
   
   if(a>b)
   {
      if(a>c)
          printf("a is largest");
      else
          printf("c is largest");
   }
   else
   {
       if(b>c)
          printf("b is largest");
       else
          printf("c is largest");
   }
getch();
}
Using Functions:
int function(int x,int y,int z)
{
    if(x>y && x>z)
            return x;
    else if(y>z)
            return y;
    else
            return z;
}
void main()
{
int a=10,b=39,c=30;
int res;
res=function(a,b,c);
printf("Res %d:",res);
}
Using Conditional Operator:
int main()
{
  int a,b,c,big;
  printf("\nEnter 3 numbers:");
  scanf("%d %d %d",&a,&b,&c);

  big=(a>b&&a>c?a:b>c?b:c);
  printf("\nThe biggest number is: %d",big);

  return 0;
}

No comments:

Post a Comment