Thursday, 18 June 2015

C program to find greatest among 3 numbers

To find the greatest among 3 numbers 

a)using if else statements.
 
Program:
 
#include <stdio.h>
#include<conio.h>

void main()
{
      float a, b, c;  //Declaring Variable.
      

      printf("Enter three numbers: ");  //For printing
      scanf("%f %f %f", &a, &b, &c);  //For Input.

      if (a>=b && a>=c)
            printf("Largest number = %.2f",a);  //For printing

      else if(b>=a && b>=c)
            printf("Largest number = %.2f",b);  //For printing
     
      else 
            printf("Largest Number = %.2f",c);   //For printing

      getch();  //Getting a character or waiting for key press
}
Output:

b)To find greatest among three numbers using &&.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,c,big;  //Declaring Variable.
printf("Enter the Three Numbers"); //For printing
scanf("%d%d%d",&a,&b,&c);  //For Input.
big=(a>b && a>c ? a:b>a && b>c ? b:c );
printf("Biggest Number = %d",big); //For printing
getch();   //Getting a character or waiting for key press
}
Output:

No comments:

Post a Comment