Write a program in C++ that takes age of five persons and then just display the age of each person usingarrays
Question
Write a program in C++ that takes age of five persons and then just display the age of each person using arrays
Solution
Here is a simple C++ program that takes the ages of five persons and then displays their ages using arrays.
#include <iostream>
using namespace std;
int main() {
// Step 1: Declare an array to hold the ages and a variable for the number of persons
const int NUMBER_OF_PERSONS = 5;
int ages[NUMBER_OF_PERSONS];
// Step 2: Input ages for each person
cout << "Enter the age of " << NUMBER_OF_PERSONS << " persons:" << endl;
for (int i = 0; i < NUMBER_OF_PERSONS; i++) {
cout << "Person " << (i + 1) << ": ";
cin >> ages[i]; // Store age in the array
}
// Step 3: Display ages of each person
cout << "\nThe ages of the persons are:" << endl;
for (int i = 0; i < NUMBER_OF_PERSONS; i++) {
cout << "Person " << (i + 1) << ": " << ages[i] << " years old" << endl;
}
return 0;
}
Explanation of the Code:
- Array Declaration: An array
ages
of size 5 is declared to hold the ages of the persons. - Input Loop: A loop is used to input ages from the user and store them in the array.
- Display Loop: Another loop is used to display the ages stored in the array.
Final Note:
Make sure to compile the code using a C++ compiler and run the program to see the results.
Similar Questions
Write a program in C++ that takes age of five persons and then just display the age of each person usingarrays
Write a program that declares an array of size 5. Input values in the array from user, and then display allvalues of array
The sum of present ages of 2 persons A and B is 60.If the age of A is twice that of B,Find the sum of their ages 5 years hence.A50B60C70D80
write c program to insert the values at the specific index in the array without using functions
The ages of A, B, and C are in the ratio 3:5:7. If the sum of their ages is 90 years, how old is B? Options 20 30 25 35
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.