TechyQuest.xyz - Learn With Coding Examples

Learn programming with the examples of programs. Techyquest provides lot of programming examples to learn programming and logic.

Recents in Beach

LightBlog

Breaking

Saturday, 25 January 2020

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;
}

No comments:

Post a Comment