Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Thursday 26 September 2013

C program of maximum of three numbers

1) Find maximum of three numbers using if condition




#include<stdio.h>

void main()
{
    int no1=12, no2=15, no3=30;
   
    if( no1 > no2 )
    {
        if( no1 > no3 )
        {
            printf("no1 is largest");
        }
        else
        {
            printf("no3 is largest");
        }
    }
    else
    {
        if( no2 > no3 )
        {
            printf("no2 is largest");
        }
        else
        {
            printf("no3 is largest");
        }
    }
}

2) Find maximum of three numbers using ternary / conditional operator

#include<stdio.h>

int main()
{
    int no1, no2, no3, max;
   
    printf("Enter No1 : ");
    scanf("%d",&no1);
   
    printf("Enter No2 : ");
    scanf("%d",&no2);
   
    printf("Enter No3 : ");
    scanf("%d",&no3);   


    max = no1 > no2 ? ( no1 > no3 ? no1 : no3 ) : ( no2 > no3 ? no2 : no3 );

    printf("Maximum number is %d",max);

    return 0;
}

0 comments :

Post a Comment