Table of Contents
In this article, we will discuss the concept of C++ program to Find Largest of three numbers
In this post, we are going to learn how to write a program to find largest number out of three numbers using different methods in C++ program.
In this code, we will find largest 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<<"Please Enter three numbers for check largest: "; //Ask input from the user cin>>num1>>num2>>num3;//Reading input from user for num1, num2, num3 if(num1>=num2 && num1>=num3){ cout<<"Largest number is: "<<num1; //Checking the num1 is largest } if(num2>=num1 && num2>=num3){ cout<<"Largest number is: "<<num2; //Checking the num2 is largest } if(num3>=num1 && num3>=num2){ cout<<"Largest number is: "<<num3; //Checking the num3 is largest } getch(); return 0; }
When the above code is executed, it produces the following result
Please Enter three numbers for check largest: 76 564 45 Largest number is: 564
In this code, we will find largest 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 to find large: "; //Ask input from the user cin>>num1>>num2>>num3; //Reading input from user for num1, num2,num3 if(num1>=num2 && num1>=num3){ cout<<"\nLargest number is: "<<num1; }//num1 compare num 2 and num 3 else if(num2>=num1 && num2>=num3){ cout<<"\nLargest number is: "<<num2; }//num2 compare num1 and num3 else{ cout<<"\nLargest number is: "<<num3; } getch(); return 0; }
When the above code is executed, it produces the following result
Enter three numbers to find large: 789 146 1030 Largest number is 1030
In this code, we will find largest 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<<"Please Enter three numbers: "; //Ask input from the user cin>>num1>>num2>>num3; //Reading input from user for num1, num2,num3 if(num1>=num2){//checking num1 and num2 if(num1>=num3){//checking num1 and num3 cout<<"\nLargest number is:"<<num1; } else{ cout<<"\nLargest number is:"<<num2; } } else{ if(num2>=num3){//checking num2 and num1 cout<<"\nLargest number is:"<<num2; } else{ cout<<"\nLargest number is:"<<num3; } } getch(); return 0; }
When the above code is executed, it produces the following result
Please Enter three numbers: 7500 2500 4000 Largest number is :7500
In this code, we will find largest number out of three numbers using function in C++ language
Program 4
#include <iostream> #include <conio.h> using namespace std; int findBiggest(int,int,int); //function prototype int main() { double num1, num2,num3; //declare the variables cout<<"Enter three numbers to find large: "; cin>>num1>>num2>>num3;//Reading input from user for num1, num2,num3 int result=findBiggest(num1,num2,num3);//Calling the method cout<<"Biggest number is: "<<result;//Display result on the screen getch(); return 0; } int findBiggest(int num1, int num2, int num3){//method definition int biggest; if(num1>=num2){ if(num1>=num3){ return num1; } else{ return num3; } } else{ if(num2>num3){ return num2; } else{ return num3; } } }
When the above code is executed, it produces the following result
Enter three numbers to find large: 100 50 25 Biggest number is 100
In this code, we will find largest number out of three numbers using ternary operator in C++ language
Program 5
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2,num3; //declare the variables cout<<"Please Enter three numbers: "; //Ask input from the user cin>>num1>>num2>>num3; //Reading the input from user and store the variables int result=num3>(num1>num2?num1:num2)?num3:((num1>num2)? num1:num2); //find largest using ternary operator cout<<"Largest number is: "<<result; //Display result on the screen getch(); return 0; }
When the above code is executed, it produces the following result
Please Enter three numbers: 12 76 45 Largest number is: 76
Program 6
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2,num3; //declare the variables cout<<"Please Enter three numbers: "; //Ask input from the user cin>>num1>>num2>>num3; //Reading the input from user and store the variables int temp=(num1>num2)? num1:num2; //Compare num1 and num2 using ternary operator int largest= num3>temp?num3:temp; //Compare num3 and temp variable using ternary operator //find largest using ternary operator cout<<"Largest number is: "<<largest; //Display result on the screen getch(); return 0; }
When the above code is executed, it produces the following result
Please Enter three numbers: 125 345 575 Largest number is: 575
In this code, we will find largest number out of three numbers using array in C++ language
Program 7
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[10], size,i; cout<<"Enter the number of elements in an array\n"; cin>>size; cout<<"Enter "<<size<< " integers \n"; for(i=0; i<size; i++){ cout<<"Enter the elements "<<(i+1)<<": "; cin>>arr[i];//takes input from user for array } int max=arr[0]; for(i=0; i<size; i++){ if(max<arr[i]){ max=arr[i]; } } cout<<"\nThe largest value is:"<<max; getch(); return 0; }
Enter the number of elements in an array 3 Enter 3 integers Enter the element 1: 1250 Enter the element 1: 3000 Enter the element 1: 2500 The largest value is : 3000
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.