Comparing age of two friends of different age
With this program we can find who is elder amongst the friends of different age.
Learning Objectives
- Seeing an if-else condition handle different items only. Handling equality is not there.
Source Code
|
Related (?) : |
Run Output
Code Understanding
int sa,fa; cout<<“Enter your age : “;cin>>sa; cout<<“Enter your friend’s age : “;cin>>fa;
Two ages are being collected here.
if(sa>fa) cout<<“You are older than your friend.”<<endl;
This is when you are older.
else cout<<“You friend is older than you.”<<endl;
This is when your friend is older.
Notes
- This program can not handle the case of equal age of two persons. We can see further in next topic that how this case can not be handled without making a nested comparison.
- A program which uses the if-else-if construct with a solution to above problem can be seen at
http://computersirkiclass.com/?codesheet=if-else-if-simple-if-ladder-2&tag=cp010-multiple-nested-ladder-of-if-else
Common Errors
- Since this is not a type of if-else ladder, so multiple else if conditions are not checked here and hence the case of same age of two friends will not give correct output.
Suggested Filename(s): agecomparison.cpp, unequalage.cpp
sunmitra| Created: 23-Dec-2017 | Updated: 15-Sep-2018|