C++ Program
C++ Program
1.. Write a program that inputs age in years and displays age in days and months.
#include <iostream>
using namespace std;
int main()
{
int ageInYears, ageInMonths, ageInDays;
cout << "Enter your age in years: ";
cin >> ageInYears;
ageInMonths = ageInYears * 12;
ageInDays = ageInYears * 365;
cout << "Age in months: " << ageInMonths << " months" << endl;
cout << "Age in days: " << ageInDays << " days" << endl;
return 0;
}
Output:
Enter your age in years: 25
2.. Write a program to calculate the average score of 5 students. The program should take the
total score as input and divide it by 5. The scores are stored as floating-point numbers.
#include <iostream>
int main()
cout << "The average score of 5 students is: " << averageScore << endl;
return 0;
Output:
3.. Write a program that inputs a number from user and displays its square and cube.
#include <iostream>
int main()
cout << "Square of " << number << " is: " << square << endl;
cout << "Cube of " << number << " is: " << cube << endl;
return 0;
}
Output:
Enter a number: 3
Square of 3 is: 9
Cube of 3 is: 27
4.. Write a program that inputs total pages of a book, number of pages a person reads in one
day and number of days a person has read the book. It displays number of pages that have
been read and number of pages remaining.
#include <iostream>
int main()
cout << "Enter the total number of pages in the book: ";
cout << "Enter the number of pages you read in a day: ";
cout << "Enter the number of days you have read the book: ";
cout << "You have read " << pagesRead << " pages." << endl;
cout << "You have " << pagesRemaining << " pages remaining." << endl;
return 0;
Output:
5.. A car can travel 5.3 miles in 1 liter. Write a program that input petrol in liters and
displays how much distance the car can cover using the available petrol.
#include <iostream>
int main()
cout << "The car can travel " << totalDistance << " miles with " << petrolInLiters << " liters
of petrol." << endl;
return 0;
}
Output:
6.. Write a program that inputs total number of student in a class and fee per student. It
displays total fee collected from the class.
#include <iostream>
int main()
int totalStudents;
cout << "Enter the total number of students in the class: ";
cout << "The total fee collected from the class is: " << totalFee << " units." << endl;
return 0;
Output:
The total fee collected from the class is: 15000 units.
The program inputs code for movie type and displays its category. For example if the user
enters h, it displays “Horror Movies”. The program should also display a menu of movie
categories.
#include <iostream>
int main() {
char code;
cout << "Enter the code for the movie category: ";
cin >> code;
} else {
return 0;
Output:
A - Adventure Movies
B - Comedy Movies
C - Family Movies
H - Horror Movies
Horror Movies
8.. Write a program to get three numbers from user for integer variables a, b and c. If a is not
zero, find out whether it is the common divisor of b and c.
#include <iostream>
int main() {
int a, b, c;
if (a != 0) {
if (b % a == 0 && c % a == 0) {
cout << a << " is a common divisor of " << b << " and " << c << endl;
} else {
cout << a << " is not a common divisor of " << b << " and " << c << endl;
} else {
cout << "Cannot check for common divisor, since " << a << " is zero" << endl;
return 0;
}
Output:
9.. Write a program that accepts the code number as an input and display the correct disk
drive manufacture as follow:
1 Western Digital
2 3M Corporation
3 Maxell Corporation
4 Sony Corporation
5 Verbatim Corporation
#include <iostream>
int main() {
int code;
if (code == 1) {
} else if (code == 2) {
} else if (code == 3) {
} else if (code == 4) {
} else if (code == 5) {
} else {
return 0;
Output:
1 - Western Digital
2 - 3M Corporation
3 - Maxell Corporation
4 - Sony Corporation
5 - Verbatim Corporation
Sony Corporation
10.. Write a program that could weather the number entered through keyboard is odd or even
and should also tell that whether it is prime of not. The program should keep on taking the value
till the user ends and before termination should find the total number of odds, evens and primes
entered.
#include <iostream>
int main()
char choice;
do {
if (num % 2 == 0) {
evens++;
} else {
if (num > 1) {
if (num % i == 0) {
isPrime = false;
break;
} else {
isPrime = false;
if (isPrime) {
primes++;
} else {
cout << num << " is not a prime number." << endl;
return 0;
Output:
Enter a number: 10
10 is even.
Enter a number: 7
7 is odd.
7 is a prime number.
Enter a number: 15
15 is odd.
Total evens: 1
Total primes: 1
11. Write a program that inputs marks of a student in three subjects and displays the sum
and average marks. It also displays PASS or FAIL where the passing marks of each subject
is 40. It displays "Pass" only if marks of each subject is 40 or more.
#include <iostream>
int main() {
int sum;
float average;
average = sum / 3;
} else {
return 0;
Output:
Average of marks: 60
Result: PASS
12. Write a program that input a number from the user and displays its factorial. It asks
the user whether he wants to calculate another factorial or not. If the user inputs 1, it again
inputs number and calculates factorial. If user inputs 0, program terminates.
#include <iostream>
int main()
factorial = 1;
factorial *= i;
cout << "Factorial of " << num << " is: " << factorial << endl;
cout << "Do you want to calculate another factorial? (1 for yes, 0 for no): ";
return 0;
Output:
Enter a number: 5
Enter a number: 3
Factorial of 3 is: 6