Find elements

C Program to largest and smallest among three numbers

C Program to smallest and largest among three numbers

In this tutorial, we discuss C Program to the smallest and largest among three numbers

In this program, we will discuss a simple concept of the program to find the smallest and largest among three numbers in the C programming language.

In this topic, we learn how to find the smallest and largest number from given three numbers using if statements.

Program to find the smallest and largest among three numbers

Find smallest and largest among three integer numbers

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,num3;//variable declaration
    printf("Enter three numbers\n");
    scanf("%d %d %d",&num1,&num2,&num3);
    //Takse three input num1,num2,num3 from user

//check smallest
    if(num1<num2 && num1<num3){
            //check whether num1 is smallest than num2 and num3
        printf("\n%d is the smallest",num1);
    }//if it is truen this statement is displayed
    else if(num2<num3){
        //when it is false then check num2 or num 3 to small
        printf("\n%d is the smallest",num2);//when it is true this statements is displayed
    }
    else{
        printf("\n%d is the smallest",num3);
    }//all statements are false this statement is displayed
//check largest
    if(num1>num2 && num1>num3){//check largest
            //check whether num1 is largest than num2 and num3
        printf("\n%d is largest",num1);
    }//if it is true this statement is displayed
    else if(num2>num3){
        //when it is false then check num2 or num 3 to large
        printf("\n%d is largest",num2);//when it is true this statements is displayed
    }
    else{
        printf("\n%d is largest",num3);
    }//all conditions are false this statement is displayed
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter three numbers
45
23
67

23 is the smallest
67 is largest

 

 

Find smallest and largest among three float numbers

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float num1,num2,num3;//variable declaration
    printf("Enter tree numbers\n");
    scanf("%f %f %f",&num1,&num2,&num3);
    //Takse three input num1,num2,num3 from user

//check smallest
    if(num1<num2 && num1<num3){
            //check whether num1 is smaller than num2 and num3
        printf("\n%.2f is smallest",num1);
    }//if it is truen this statement is displayed
    else if(num2<num3){
        //when it is false then check num2 or num 3 small
        printf("\n%.2f is smallest",num2);//when it is true this statements is displayed
    }
    else{
        printf("\n%.2f is smallest",num3);
    }//all statements are false this statement is displayed
//check largest
    if(num1>num2 && num1>num3){
            //check whether num1 is largest than num2 and num3
        printf("\n%.2f is largest",num1);
    }//if it is true this statement is displayed
    else if(num2>num3){
        //when it is false then check num2 or num3 large
        printf("\n%.2f is largest",num2);//when it is true this statements is displayed
    }
    else{
        printf("\n%.2f is largest",num3);
    }//all statements are false this statement is displayed
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter three numbers
34.56
12.34
67.5

12.34 is smallest
67.50 is largest

Suggested for you

Operator in C language

If statements in C language

Variable in C language

Data type in C language

 

C Program to display smallest among three numbers
Java program to find middle of three numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago