Here's the program to read two numbers from the keyboard and display the larger value on the screen:
c++
Copy code
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
if (num1 > num2) {
cout << "Larger number is: " << num1 << endl;
}
else {
cout << "Larger number is: " << num2 << endl;
}
return 0;
}
Sample output:
Larger number is: 45
Explanation:
- The program starts by including the necessary header file iostream which contains the standard input/output library in C++.
- using namespace std; statement is used to avoid typing std:: before standard library objects.
- Two integer variables num1 and num2 are declared to store the numbers entered by the user.
- The user is prompted to enter the first number using the cout statement.
- The cin statement is used to read the input from the user and store it in the variable num1.
- Similarly, the user is prompted to enter the second number and its input is read and stored in the variable num2.
- An if-else statement is used to check which of the two numbers is larger. If num1 is larger, the program displays num1 as the larger number. If num2 is larger, the program displays num2 as the larger number.
- The larger number is displayed using the cout statement.
- Finally, the return 0; statement ends the program.
No comments:
Post a Comment
If you have any doubts, please let me know