Write a program to find the largest and smallest of the given two numbers using if else

Here we will learn about how to find and print smallest between any two given number (by user at run-time) with and without using user-defined function. At last, we will also use Ternary operator to find and print smallest between the two numbers.

Find Smallest of Two Numbers in C

Here is the program that will ask from the user to enter any two number to find out and print the smallest one as shown here:

#include<stdio.h> #include<conio.h> int main() { int a, b, small; printf("Enter any two number: "); scanf("%d%d", &a, &b); if(a<b) small=a; else small=b; printf("\nSmallest of the two number is: %d", small); getch(); return 0; }

The program was written under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run of above program:

Write a program to find the largest and smallest of the given two numbers using if else

C Program To Find The Largest of 2 Numbers Using if else

Algorithm -:

  1. Program Start
  2. Variable Declaration
  3. Input two number
  4. Check the condition, ternary (conditional) operator.
  5. Display answer according the condition
  6. Program End

Program -:

//C program to find Largest among two numbers using if else #include<stdio.h> void main() { // Variable declaration int a,b,larg; printf("Enter two number\n"); scanf("%d %d",&a,&b); // Smallest among a and b if(a>b) larg = a; else larg = b; //Display largest number printf("largest among 2 Number is : %d",larg); }

Output -:

Enter two numbers 10 8 Largest among 2 number is : 10

C Program To Find The Largest of 3 Numbers Using if else

Program -:

//C program to find Largest among three numbers Using if else #include<stdio.h> void main() { // Variable declaration int a,b,c, larg; printf("Enter Three Number\n"); scanf("%d %d %d",&a,&b,&c); // larg among a, b and c if(a>b) { if(a>c) larg = a; else larg = c; } else { if(b>c) larg = b; else larg = c; } //Display Largest Number printf("%d is largest number",larg); return 0; }

Output -:

Enter Three Numbers 9 15 19 19 is largest number

C Program To Find The Largest of 4 Numbers Using if else

Program -:

//C program to find Largest of four numbers Using If Else #include<stdio.h> int main() { // Variable declaration int a,b,c,d, larg; //input numbers printf("Enter four Numbers\n"); scanf("%d %d %d %d",&a,&b,&c,&d); // larg among a, b, c and d if(a>b) { if(a>c) { if(a>d) { larg = a; } else { larg = d; } } else { if(c>d) larg = c; else larg = d; } } else { if(b>c) { if(b>d) { larg = b; } else { larg = d; } } else { if(c>d) { larg = c; }18 else { larg = d; } } } //Display Largest number printf("%d is Largest Number",larg); return 0; }

Output -:

Enter four Numbers 10 28 129 18 129 is Largest Number

C Program To Find The Largest of 5 Numbers Using if else

Program -:

//C program to find Largest of five numbers Using If Else #include<stdio.h> int main() { // Variable declaration int a,b,c,d,e, larg; printf("Enter five Numbers\n"); scanf("%d %d %d %d %d",&a,&b,&c,&d, &e); // larg among a, b, c, d and e if(a>b) { if(a>c) { if(a>d) { if(a>e) larg = a; else larg = e; } else { if(d>e) larg = d; else larg = e; } } else { if(c>d) { if(c>e) larg = c; else larg = e; } else { if(d>e) larg = d; else larg = e; } } } else { if(b>c) { if(b>d) { if(b>e) larg = b; else larg = e; } else { if(d>e) larg = d; else larg = e; } } else { if(c>d) { if(c>e) larg = c; else larg = e; } else { if(d>e) larg = d; else larg = e; } } } //Display Largest number printf("%d is Largest Number",larg); return 0; }

Output -:

Enter five numbers 18 32 28 281 828 828 is Largest Number

Conclusion

In this article, we have created all the following programs -:

  • C Program To Find The Largest of 2 Numbers Using If Else
  • C Program To Find The Largest of 3 Numbers Using If Else
  • C Program To Find The Largest of 4 Numbers Using If Else
  • C Program To Find The Largest of 5 Numbers Using If Else

If you have difficulty to understanding any program or have any doubts or questions, then tell me in the comment below.