a) What is control structure? Explain it’s classification.
b) And write a code for calculating age of a person.
c) Which control structure has used in this code.
a) What is control structure? Explain it’s classification.
C++ has three kinds of control structures such as: the selection statements (if, if...else and switch), the sequence statement, and the repetition statements (while, for and do...while).
- Selection(branching) - is used to execute different statements depending on certain conditions.
- Sequence - is a series of statements that execute one after another.
- Repetition(loop or iteration) - is used to repeat statements while certain conditions are met.
b) And write a code for calculating age of a person.
#include <iostream>
#include <string>
using namespace std;
int main (){
//aray of days in months
int month[] = { 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31 };
int dayNow;
int monthNow;
int yearNow;
//get the current date
cout<<"Enter the current day: ";
cin>>dayNow;
cout<<"Enter the current month: ";
cin>>monthNow;
cout<<"Enter the current year: ";
cin>>yearNow;
int personBirthDay;
int personBirthMonth;
int personBirthYear;
//get the person birthdate
cout<<"Enter the person birth day: ";
cin>>personBirthDay;
cout<<"Enter the person birth month: ";
cin>>personBirthMonth;
cout<<"Enter the person birth year: ";
cin>>personBirthYear;
if (personBirthDay > dayNow) {
dayNow = dayNow + month[personBirthMonth - 1];
monthNow = monthNow - 1;
}
if (personBirthMonth > monthNow) {
yearNow = yearNow - 1;
monthNow = monthNow + 12;
}
//calculate age
int ageDays = dayNow - personBirthDay;
int ageMonths = monthNow - personBirthMonth;
int ageYears = yearNow - personBirthYear;
//display age
cout<<"The age of the person is: "<<ageYears<<" year(s), "<<ageMonths<<" month(s),"<<ageDays<<" day(s).\n\n";
//delay
system("pause");
return 0;
}
c) Which control structure has used in this code.
I have used Selection(branching) control structure to create this program.
Comments
Dear Bigboss,
You're welcome. We are glad to be helpful.
If you liked our service please press like-button beside answer field. Thank you!
May god bless u..Thanks a lot
Leave a comment