0% found this document useful (0 votes)
14 views

C++ Program

Uploaded by

syedsahib2922
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

C++ Program

Uploaded by

syedsahib2922
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to Computing (Programs)

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

Age in months: 300 months

Age in days: 9125 days

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>

using namespace std;

int main()

float totalScore, averageScore;

cout << "Enter the total score of 5 students: ";


cin >> totalScore;

averageScore = totalScore / 5.0;

cout << "The average score of 5 students is: " << averageScore << endl;

return 0;

Output:

Enter the total score of 5 students: 250

The average score of 5 students is: 50

3.. Write a program that inputs a number from user and displays its square and cube.
#include <iostream>

using namespace std;

int main()

int number, square, cube;

cout << "Enter a number: ";

cin >> number;

square = number * number;

cube = number * number * number;

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>

using namespace std;

int main()

int totalPages, pagesPerDay, daysRead, pagesRead, pagesRemaining;

cout << "Enter the total number of pages in the book: ";

cin >> totalPages;

cout << "Enter the number of pages you read in a day: ";

cin >> pagesPerDay;

cout << "Enter the number of days you have read the book: ";

cin >> daysRead;

pagesRead = pagesPerDay * daysRead;

pagesRemaining = totalPages - pagesRead;

cout << "You have read " << pagesRead << " pages." << endl;
cout << "You have " << pagesRemaining << " pages remaining." << endl;

return 0;

Output:

Enter the total number of pages in the book: 300

Enter the number of pages you read in a day: 20

Enter the number of days you have read the book: 10

You have read 200 pages.

You have 100 pages remaining.

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>

using namespace std;

int main()

float petrolInLiters, distancePerLiter = 5.3, totalDistance;

cout << "Enter the amount of petrol in liters: ";

cin >> petrolInLiters;

totalDistance = petrolInLiters * distancePerLiter;

cout << "The car can travel " << totalDistance << " miles with " << petrolInLiters << " liters
of petrol." << endl;

return 0;
}

Output:

Enter the amount of petrol in liters: 10

The car can travel 53 miles with 10 liters of petrol.

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>

using namespace std;

int main()

int totalStudents;

double feePerStudent, totalFee;

cout << "Enter the total number of students in the class: ";

cin >> totalStudents;

cout << "Enter the fee per student: ";

cin >> feePerStudent;

totalFee = totalStudents * feePerStudent;

cout << "The total fee collected from the class is: " << totalFee << " units." << endl;

return 0;

Output:

Enter the total number of students in the class: 30


Enter the fee per student: 500

The total fee collected from the class is: 15000 units.

7.. Write a program that uses the following categories of movies:

A for Adventure movies

B for Comedy movies

C for Family movies

H for Horror movies

S for Science Fiction movies

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>

using namespace std;

int main() {

char code;

cout << "Movie Categories Menu:" << endl;

cout << "A - Adventure Movies" << endl;

cout << "B - Comedy Movies" << endl;

cout << "C - Family Movies" << endl;

cout << "H - Horror Movies" << endl;

cout << "S - Science Fiction Movies" << endl;

cout << "Enter the code for the movie category: ";
cin >> code;

if (code == 'A' || code == 'a') {

cout << "Adventure Movies" << endl;

} else if (code == 'B' || code == 'b') {

cout << "Comedy Movies" << endl;

} else if (code == 'C' || code == 'c') {

cout << "Family Movies" << endl;

} else if (code == 'H' || code == 'h') {

cout << "Horror Movies" << endl;

} else if (code == 'S' || code == 's') {

cout << "Science Fiction Movies" << endl;

} else {

cout << "Invalid movie category code" << endl;

return 0;

Output:

Movie Categories Menu:

A - Adventure Movies

B - Comedy Movies

C - Family Movies
H - Horror Movies

S - Science Fiction Movies

Enter the code for the movie category: h

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>

using namespace std;

int main() {

int a, b, c;

cout << "Enter three numbers: ";

cin >> 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:

Enter three numbers: 2 4 6

2 is a common divisor of 4 and 6

9.. Write a program that accepts the code number as an input and display the correct disk
drive manufacture as follow:

Code Disk drive manufacture

1 Western Digital
2 3M Corporation
3 Maxell Corporation
4 Sony Corporation
5 Verbatim Corporation

#include <iostream>

using namespace std;

int main() {

int code;

cout << "Disk Drive Manufacturers Code List:" << endl;

cout << "1 - Western Digital" << endl;

cout << "2 - 3M Corporation" << endl;

cout << "3 - Maxell Corporation" << endl;

cout << "4 - Sony Corporation" << endl;

cout << "5 - Verbatim Corporation" << endl;

cout << "Enter the code number: ";


cin >> code;

if (code == 1) {

cout << "Western Digital" << endl;

} else if (code == 2) {

cout << "3M Corporation" << endl;

} else if (code == 3) {

cout << "Maxell Corporation" << endl;

} else if (code == 4) {

cout << "Sony Corporation" << endl;

} else if (code == 5) {

cout << "Verbatim Corporation" << endl;

} else {

cout << "Invalid code number" << endl;

return 0;

Output:

Disk Drive Manufacturers Code List:

1 - Western Digital

2 - 3M Corporation

3 - Maxell Corporation
4 - Sony Corporation

5 - Verbatim Corporation

Enter the code number: 4

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>

using namespace std;

int main()

int num, odds = 0, evens = 0, primes = 0;

char choice;

do {

cout << "Enter a number: ";

cin >> num;

if (num % 2 == 0) {

cout << num << " is even." << endl;

evens++;

} else {

cout << num << " is odd." << endl;


odds++;

bool isPrime = true;

if (num > 1) {

for (int i = 2; i <= num / 2; i++) {

if (num % i == 0) {

isPrime = false;

break;

} else {

isPrime = false;

if (isPrime) {

cout << num << " is a prime number." << endl;

primes++;

} else {

cout << num << " is not a prime number." << endl;

cout << "Do you want to continue? (y/n): ";

cin >> choice;


} while (choice == 'y' || choice == 'Y');

cout << "Total odds: " << odds << endl;

cout << "Total evens: " << evens << endl;

cout << "Total primes: " << primes << endl;

return 0;

Output:

Enter a number: 10

10 is even.

10 is not a prime number.

Do you want to continue? (y/n): y

Enter a number: 7

7 is odd.

7 is a prime number.

Do you want to continue? (y/n): y

Enter a number: 15

15 is odd.

15 is not a prime number.

Do you want to continue? (y/n): n


Total odds: 2

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>

using namespace std;

int main() {

int marks1, marks2, marks3;

int sum;

float average;

cout << "Enter marks of subject 1: ";

cin >> marks1;

cout << "Enter marks of subject 2: ";

cin >> marks2;

cout << "Enter marks of subject 3: ";

cin >> marks3;

sum = marks1 + marks2 + marks3;

average = sum / 3;

cout << "Sum of marks: " << sum << endl;

cout << "Average of marks: " << average << endl;


if (marks1 >= 40 && marks2 >= 40 && marks3 >= 40) {

cout << "Result: PASS" << endl;

} else {

cout << "Result: FAIL" << endl;

return 0;

Output:

Enter marks of subject 1: 50

Enter marks of subject 2: 60

Enter marks of subject 3: 70

Sum of marks: 180

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>

using namespace std;

int main()

int num, choice, factorial;


do {

cout << "Enter a number: ";

cin >> num;

factorial = 1;

for (int i = 1; i <= num; i++) {

factorial *= i;

cout << "Factorial of " << num << " is: " << factorial << endl;

cout << "Do you want to calculate another factorial? (1 for yes, 0 for no): ";

cin >> choice;

} while (choice == 1);

return 0;

Output:

Enter a number: 5

Factorial of 5 is: 120

Do you want to calculate another factorial? (1 for yes, 0 for no): 1

Enter a number: 3

Factorial of 3 is: 6

Do you want to calculate another factorial? (1 for yes, 0 for no): 0

You might also like