Recents in Beach

C Program to Find Largest Of Three Numbers


C program to find largest of three numbers using Nested if...else statement

#include <stdio.h>

int main()
{
    int fn,sn,tn;
    printf("Enter three numbers:\n");
    scanf("%d %d %d",&fn,&sn,&tn);
    if(fn>sn){
        if(fn>tn){
            printf("%d is largest of three numbers.",fn);
        }
        else{
            printf("%d is largest of three numbers.",tn);
        }
    }
    else if(sn>tn){
        printf("%d is largest of three numbers.",sn);
    }
    else{
        printf("%d is largest of three numbers.",tn);
    }

    return 0;
}


C program to find largest of three using ternary operator

#include <stdio.h>

int main()
{
    int fn,sn,tn,lg;
    printf("Enter three numbers:\n");
    scanf("%d %d %d",&fn,&sn,&tn);
    
    lg = fn>sn?(fn>tn?fn:tn):(sn>tn?sn:tn);
    
    printf("%d is largest of three numbers.",lg);
    
    return 0;
}

C program to find largest of three using functions

#include <stdio.h>

int largest(int fn, int sn, int tn);

int main()
{
    int fn,sn,tn;
    printf("Enter three numbers:\n");
    scanf("%d %d %d",&fn,&sn,&tn);
    
    lg = largest(fn,sn,tn);
    
    printf("%d is largest of three numbers.",lg);
    
    return 0;
}

int largest(int fn, int sn, int tn){
    int lg;
    lg = fn>sn?(fn>tn?fn:tn):(sn>tn?sn:tn);
    return lg;
}

Post a Comment

0 Comments