Smallest of three numbers
Table of Contents
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.
In this code, we will find smallest number out of three numbers using if statements in C++ language
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2,num3; //declare the variables
cout<<"Enter three numbers: ";
//Ask input from the user
cin>>num1>>num2>>num3;
//Reading the input from user for numbers
if(num1<=num2 && num1<=num3){
//compare num1 with num2 and num3
cout<<"\n The smallest number is: "<<num1;
}
if(num2<=num1 && num2<=num3){
//compare num2 with num1 and num3
cout<<"\n The smallest number is:"<<num2;
}
if(num3<=num1 && num3<=num2){
//compare num3 with num1 and num2
cout<<"\n The smallest number is: "<<num3;
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter three numbers 56 78 23 The smallest number is: 23
In this code, we will find smallest number out of three numbers using if else-if statements in C++ language
Program 2
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2,num3; //declare the variables
cout<<"Enter three numbers: ";
//Ask input from the user
cin>>num1>>num2>>num3;
//Reading the input from user for numbers
if(num1<=num2 && num1<=num3){
//compare num1 with num2 and num3
cout<<"\n The smallest number is: "<<num1;
}
else if(num2<=num1 && num2<=num3){
//compare num2 with num1 and num3
cout<<"\n The smallest number is:"<<num2;
}
else{
//compare num3 with num1 and num2
cout<<"\n The smallest number is: "<<num3;
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter three numbers 27 67 89 The smallest number is: 27
In this code, we will find smallest number out of three numbers using nested if statements in C++ language
Program 3
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{int num1,num2,num3; //declare the variables
cout<<"Enter three numbers: ";
//Ask input from the user
cin>>num1>>num2>>num3;
//Reading the input from user for numbers
if(num1<num2){//compare num1 and num2
if(num1<num3){//compare num1 and num3
cout<<"\n The smallest number is: "<<num1;
}
else{
cout<<"\n The smallest number is: "<<num3;
}
}
else{
if(num2<num3){//compare num2 and num3
cout<<"\n The smallest number is: "<<num2;
}
else{
cout<<"\n The smallest number is: "<<num3;
}
}
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter three numbers 34 67 98 The smallest number is: 34
In this code, we will find smallest number out of three numbers using ternary operator in C++ language
Program 4
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2,num3; //declare the variables
cout<<"Enter three numbers: ";
//Ask input from the user
cin>>num1>>num2>>num3;
//Reading the input from user for numbers
int result=num3<(num1<num2?num1:num2)?num3:((num1<num2)? num1:num2);
cout<<"\n The Smallest number is: %d "<<result;
getch();
return 0;
} When the above code is executed, it produces the following result
Enter three numbers 45 32 56 The smallest number is: 32
Program 5
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2,num3; //declare the variables
cout<<"Enter three numbers: ";
//Ask input from the user
cin>>num1>>num2>>num3;
//Reading the input from user for numbers
int temp=((num1<num2)? num1:num2);
int result=num3<temp?num3:temp;
cout<<"\n The Smallest number is: %d "<<result;
getch();
return 0;
}
When the above code is executed, it produces the following result
Enter three numbers 46 76 21 The smallest number is: 21
In this code, we will find smallest number out of three numbers using function in C++ language
Program 6
#include <iostream>
#include <conio.h>
using namespace std;
int smallestNum(int, int,int);//function prototype
int main()
{
int num1,num2,num3;
//declare the variables
cout<<"Enter three number: ";
//ask input from the user
cin>>num1>>num2>>num3;
//reading input from user for 3 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
cout<<"\nSmallest number is:" <<num1;
}
else{
cout<<"\nSmallest number is:"<<num3;
}
}
else{
if(num2<num3){//compare num2 and num1
cout<<"\nSmallest number is:"<<num2;
}
else{
cout<<"\nSmallest number is:"<<num3;
}
}
}
When the above code is executed, it produces the following result
Enter three numbers 12 78 45 The smallest number is: 12
Suggested post
Nested if statement in C++ language
Similar post
Java code to find middle number of three
C code to find middle number of three
C++ code to find middle number of three
Python code to find middle number 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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.