Grupo
Control de Flujo en C++
Ojetivo
1. Pida al usuario que ingrese tres números.
2. Use sentencias condicionales para comparar los números.
3. Introduzca el número mayor de los tres.
4. Si dos o más números son iguales y el mayor es el mayor, introduzca el mensaje correspondiente.
Escriba un programa que compare tres números y encuentre el mayor.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the iostream library for input and output
using namespace std; // Use the standard namespace
// Main function - the entry point of the program
int main() {
double num1, num2, num3; // Variables to store the three numbers
// Ask the user for input
cout << "Enter first number: ";
cin >> num1; // Read the first number
cout << "Enter second number: ";
cin >> num2; // Read the second number
cout << "Enter third number: ";
cin >> num3; // Read the third number
// Compare the numbers using if-else statements to find the largest
if (num1 >= num2 && num1 >= num3) { // Check if num1 is the largest
cout << "The largest number is: " << num1 << endl;
}
else if (num2 >= num1 && num2 >= num3) { // Check if num2 is the largest
cout << "The largest number is: " << num2 << endl;
}
else { // If the previous conditions are false, num3 must be the largest
cout << "The largest number is: " << num3 << endl;
}
return 0; // Return 0 to indicate that the program executed successfully
}
Salida
Enter first number: 12
Enter second number: 7
Enter third number: 19
The largest number is: 19
//Or in case of equal numbers:
Enter first number: 8
Enter second number: 8
Enter third number: 6
The largest number is: 8
Comparte este ejercicio C++