Thursday, 21 August 2014

Filled Under: ,

C Program to Find the Largest Number Among Three Numbers

Share

In this program, user is asked to enter three numbers and this program will find the largest number among three numbers entered by user. This program can be solved in more than one way.

Example of C Program to Find the Largest Number Among Three Numbers

Program 1
#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %.2f", a);
      if(b>=a && b>=c)
         printf("Largest number = %.2f", b);
      if(c>=a && c>=b)
         printf("Largest number = %.2f", c);
      return 0;
}

Program 2

#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if (a>=b)
      {
          if(a>=c)
            printf("Largest number = %.2f",a);
          else
            printf("Largest number = %.2f",c);
      }
      else
      {
          if(b>=c)
            printf("Largest number = %.2f",b);
          else
            printf("Largest number = %.2f",c);
      }
      return 0;
}

Program 3

#include <stdio.h>
int main(){
      float a, b, c;
      printf("Enter three numbers: ");
      scanf("%f %f %f", &a, &b, &c);
      if(a>=b && a>=c)
         printf("Largest number = %.2f", a);
      else if(b>=a && b>=c)
         printf("Largest number = %.2f", b);
      else
         printf("Largest number = %.2f", c);
      return 0;
}
Above are the three different way to find the greater number among 3 number.We get same result in each way.
Output
Enter three numbers: 42.2
33.452
19.193
Largest number = 42.2