The following code provides a solution for printing a sequence of values using a for loop in C++. This code demonstrates a simple yet effective way to print a series of numbers that follow a specific pattern, in this case, the sequence of odd numbers from 1 to 13.
Which Compiler is best?
- For Laptop, Offline or PC Devices use Visual Studio Code or CFree , Dev C++.
- For Mobile Devices you can use online Compilers such as w3shools.
Code Elements:
- Age Calculator
- Zodiac Sign Calculator
- Age Roles
The code is a simple program that calculates the age of a person based on the information about their date of birth, month of birth and year of birth. The user is asked to input this information and the current year. The age of the user is then calculated by subtracting the year of birth from the current year.
The code takes into account if the user’s birthday has already occurred in the current year or not. If the birthday has occurred, then the age is calculated as the difference between the current year and the year of birth. If the birthday has not occurred yet, then one is subtracted from the age.
Age Calculator
#include <iostream>
using namespace std;
int main() {
int day, month, year, current_year, age;
cout << "Enter your day of birth: ";
cin >> day;
cout << "Enter your month of birth (in numbers): ";
cin >> month;
cout << "Enter your year of birth: ";
cin >> year;
cout << "Enter current year: ";
cin >> current_year;
age = current_year - year;
if (month > 2) { // checking if birthday has occurred in current year or not
age--;
} else if (month == 2 && day >= 29) {
age--;
}
return 0;
}
Zodiac Sign Calculator using C++
In this code, the user is prompted to enter their birth day and month. The day and month are stored in the day
and month
variables respectively. A switch statement is then used to determine the zodiac sign based on the user’s birth date. The switch statement checks the value of the month
variable, and based on the month, it checks the value of the day
variable to determine the zodiac sign. If the user’s birth date is not a valid date, an error message is displayed.
#include <iostream>
using namespace std;
int main() {
int day, month;
cout << "Enter your birth day (1-31): ";
cin >> day;
cout << "Enter your birth month (1-12): ";
cin >> month;
switch(month) {
case 1:
if (day >= 1 && day <= 19) {
cout << "Your zodiac sign is Capricorn." << endl;
} else if (day >= 20 && day <= 31) {
cout << "Your zodiac sign is Aquarius." << endl;
} else {
cout << "Invalid date." << endl;
}
break;
case 2:
if (day >= 1 && day <= 18) {
cout << "Your zodiac sign is Aquarius." << endl;
} else if (day >= 19 && day <= 29) {
cout << "Your zodiac sign is Pisces." << endl;
} else {
cout << "Invalid date." << endl;
}
break;
case 3:
if (day >= 1 && day <= 20) {
cout << "Your zodiac sign is Pisces." << endl;
} else if (day >= 21 && day <= 31) {
cout << "Your zodiac sign is Aries." << endl;
} else {
cout << "Invalid date." << endl;
}
break;
case 4:
if (day >= 1 && day <= 19) {
cout << "Your zodiac sign is Aries." << endl;
} else if (day >= 20 && day <= 30) {
cout << "Your zodiac sign is Taurus." << endl;
} else {
cout << "Invalid date." << endl;
}
break;
// ... and so on, for all 12 months ...
default:
cout << "Invalid month." << endl;
break;
}
return 0;
}
User Role:
After calculating the age and Zodiac Sign, the code uses an if-else statement to determine if the user is a child (age 0 to 14), youth (age 15 to 24), adult (age 25 to 64), senior (age 65 and over), or if the input is invalid.
#include <iostream>
using namespace std;
int main() {
int day, month, year, current_year, age;
cout << "Enter your day of birth: ";
cin >> day;
cout << "Enter your month of birth (in numbers): ";
cin >> month;
cout << "Enter your year of birth: ";
cin >> year;
if (age >= 0 && age <= 14) {
cout << "You are a Children" << endl;
} else if (age >= 15 && age <= 24) {
cout << "You are a Youth" << endl;
} else if (age >= 25 && age <= 64) {
cout << "You are an Adult" << endl;
} else if (age >= 65) {
cout << "You are a Senior" << endl;
} else {
cout << "Invalid input" << endl;
}
return 0;
}