Followers

Monday, April 24, 2023

Program To Find The Largest Number Amongst Three Numners

Program:
 #include <iostream>  
 using namespace std;  
 int main() {  
      int a, b, c, lg_num;  
      cout<<"Enter first number: ";  
      cin>>a;  
      cout<<"Enter second number: ";  
      cin>>b;  
      cout<<"Enter third number: ";  
      cin>>c;   
      if(a>b && a>c ){  
           lg_num = a;  
      }else if(b>a && b>c ){  
           lg_num = b;  
      }else if(c>a && c>b ){  
           lg_num = c;  
      }  
      cout<<"Largest Number: "<<lg_num<<endl;  
      return 0;  
 }  
Output: 












Working Of The Program:

1. First, I declare the variables a, b, c, and lg_num.
2. Then, I prompt the user to enter the first number using cout and read it in using cin.
3. Next, I prompt the user to enter the second number and read it in using cin.
4. Finally, I prompt the user to enter the third number and read it in using cin.
5. I compare a, b, and c using if-else statements to find the largest number among them.
6. If a is greater than b and c, then I set lg_num to a.
7. If b is greater than a and c, then I set lg_num to b.
8. If c is greater than a and b, then I set lg_num to c.
9. I print out the value of lg_num using cout.
10. Finally, I return 0 to indicate that the program has completed successfully.

No comments:

Post a Comment

Program to Find the Length of a String

Program: #include <iostream> using namespace std; int main() { string a; cout<<"Enter the string...