Let's find which number is largest among three numbers by using this simple
  C program.
  Here, we have taken all three numbers input from user at a time and then
  by using and operator ( && ) we have checked all conditions.
  Here, else if is used if we need to check more than one conditions if else will
  help to get straight to the output as soon as it finds the condition true,
  it will not check further while if you have used If  instead of else if  then it will
  check all conditions also when first if statement is true which is not good
  what if we have to a 1000 of conditions then it will waste time in checking all.
 
 Logic:- We will pick one number and compare it with other two and then
 pick next and do the same and at last it will be automatically third number
 if two of them gets false.

  Program
 
  #include <stdio.h>
  #include <conio.h>
  void main()
  {
    int n1,n2,n3;
    printf("Enter three numbers = ");
    scanf("%d%d%d",&n1,&n2,&n3);
    if( n1 > n2 &&  n1 > n3)
      {
        printf("%d is the largest no among three....",n1);
       }
    else if (n2 > n1 && n2 > n3)
      {
       printf("%d is the largest no...",n2);
      }
    else
     {
       printf("%d is the largest no...",n3);
      }
    }
  Output:


Post a Comment

Previous Post Next Post