C Language

C program to Find Smallest of three numbers

C program to Find Smallest of three numbers

In this article, we will discuss the concept of C program to Find Smallest of three numbers

In this post, we are going to learn how to write a program to find smallest number out of three numbers using different methods in C program.

Program to find smallest numbers

Code to find smallest numbers  using if statements

In this code, we will find smallest number out of three numbers using if statements in C language

Program 1

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num1,num2,num3; //declare the variables
    printf("Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);
    //Reading the three input from user for numbers
       if(num1<=num2 && num1<=num3){
            //compare num1 with num2 and num3
    printf("\n The smallest number is: %d ",num1);
}
if(num2<=num1 && num2<=num3){
        //compare num2 with num1 and num3
     printf("\n The smallest number is: %d ",num2);
}
if(num3<=num1 && num3<=num2){
    //compare num3 with num1 and num2
     printf("\n The Smallest number is: %d ",num3);
}
    getch();
    return 0;
}




When the above code is executed, it produces the following result

Enter three numbers: 200
100
300

The smallest number is :100

 

Code to find smallest numbers  using if else-if statements

In this code, we will find smallest number out of three numbers using if else-if statements in C language

Program 2

#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);
    //Takes three input for num1,num2,num3
    if(num1<num2 && num1<num3){
            //check whether num1 is smaller than num2 and num3
        printf("\n%d is smallest",num1);
    }//if it is true this statement is displayed
    else if(num2<num3){
        //then check num2 or num3 is small
        printf("\n%d is smallest",num2);//when it is true this statements is displayed
    }
    else{
        printf("\n%d is smallest",num3);
    }//all statements are false this statement is displayed
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter three numbers
45
67
87

45 is smallest

 

Code to find smallest numbers  using Nested if statements

In this code, we will find smallest number out of three numbers using Nested-if statements in C language

Program 3

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num1,num2,num3; //declare the variables
    printf("Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);//Reading the input from user for numbers
       if(num1<num2){//compare num1 and num2
            if(num1<num3){//compare num1 and num3
         printf("\nSmallest number is: %d\n",num1);
            }
            else{
                printf("\nSmallest number is: %d\n",num3);
            }
    }
    else{
        if(num2<num3){//compare num2 and num1
            printf("\nSmallest number is: %d\n",num2);
        }
        else{
            printf("\nSmallest number is: %d\n",num3);
        }

    }
    getch();
    return 0;
}



When the above code is executed, it produces the following result

Enter three numbers: 23
89
65

Smallest number is 23

 

Code to find smallest numbers  using ternary operator

In this code, we will find smallest number out of three numbers using ternary operator in C language

Program 4

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num1,num2,num3; //declare the variables
    printf("Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);
    //Reading the input from user for numbers
       int result=num3<(num1<num2?num1:num2)?num3:((num1<num2)? num1:num2);
        printf("\n The Smallest number is: %d ",result);
        getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter three numbers: 78
54
32

Smallest number is 32

 

Program 5

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num1,num2,num3; //declare the variables
    printf("Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);
    //Reading the input from user for numbers
       int temp=((num1<num2)? num1:num2);

int result=num3<temp?num3:temp;
        printf("\n The Smallest number is: %d ",result);
        getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter three numbers: 999
777
888

Smallest number is 777

 

 

Code to find smallest numbers  using function

In this code, we will find smallest number out of three numbers using  function in C language

Program 6

#include <stdio.h>
#include <stdlib.h>
int smallestNum(int, int,int);//function prototype
int main()
{
    int num1,num2,num3; //declare the variables
    printf("Enter three numbers: ");
    //Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);//Reading the input from user for numbers
    smallestNum(num1,num2,num3);//Calling the function
    getch();
    return 0;
}

    int smallestNum(int num1,int num2,int num3){//function definition
    if(num1<num2){//compare num1 and num2
            if(num1<num3){//compare num1 and num3
         printf("\nSmallest number is: %d\n",num1);
            }
            else{
                printf("\nSmallest number is: %d\n",num3);
            }
    }
    else{
        if(num2<num3){//compare num2 and num1
            printf("\nSmallest number is: %d\n",num2);
        }
        else{
            printf("\nSmallest number is: %d\n",num3);
        }

    }
    }


When the above code is executed, it produces the following result

Enter three numbers: 321
543
765

Smallest number is 321

 

Suggested post

Operator in C language

if statements in C language

Nested if statement in C language

Array in C language

Function in C language

 

Similar post

Java code to find middle of three

C code to find middle of three

C++ code to find middle of three

Python code to find middle of three

 

Java program to Find largest of three numbers

C program to Find largest of three numbers

C++ program to Find largest of three numbers

Python program to Find largest of three numbers

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.