0% found this document useful (0 votes)
18 views45 pages

Solved Exercises in C++

The document presents 19 exercises in C++ to develop simple programs that perform operations such as: 1) Display the message "Hello World"; 2) Read a name and display a greeting; 3) Read the name and salary of an employee. The exercises range from mathematical operations to calculations with unit conversion and age validation for voting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views45 pages

Solved Exercises in C++

The document presents 19 exercises in C++ to develop simple programs that perform operations such as: 1) Display the message "Hello World"; 2) Read a name and display a greeting; 3) Read the name and salary of an employee. The exercises range from mathematical operations to calculations with unit conversion and age validation for voting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercises in C++

1) Write a program that displays the message 'Hello, World!' on the screen.

#include <iostream>
int main()
{
Hello, World!
return 0;
}
2) Write a program that reads a person's name and shows a welcome message.
for her: Ex: What is your name? João da Silva Hello João da Silva, it's a pleasure to meet you!

#include <iostream>
using namespace std;
int main()
{
What is your name?
string name;
cin >> name;
cout << "Hello " + name + ", it's a pleasure to meet you!";

return 0;
}
3) Create a program that reads the name and salary of an employee, showing at the end a
message. Ex: Employee Name: Maria Salary: 1850.45 Employee Maria has a
R$1850.45 in June.
#include <iostream>
using namespace std;
int main()
{
Employee's name:
string name;
cin >> name;
Salary:
double salary;
cin >> salary;
The employee
salary << " in June.";
return 0;
}
4) Develop an algorithm that reads two integers and shows the sum between them. Ex:
Enter a value: 8 Enter another value: 5 The sum between 8 and 5 is equal to 13.

#include <iostream>
using namespace std;
int main()
{
Enter a value:
double n1;
cin >> n1;
Enter another value:
double n2;
cin >> n2;
double soma = n1 + n2;
The sum between
soma
return 0;
}
5) Create a program that reads a student's two grades in a subject and displays them on the screen.
average in the subject. Ex: Grade 1: 4.5 Grade 2: 8.5 The average between 4.5 and 8.5 is equal to 6.5

#include <iostream>
using namespace std;
int main()
{
Note 1:
double n1;
cin >> n1;
Note 2:
double n2;
cin >> n2;
double media = (n1 + n2) / 2;
cout << "The average between " << n1 << " and " << n2 << " is equal to " <<
media
return 0;
}
6) Create a program that reads an integer and shows its predecessor and successor. E.g.:
Digite um número: 9 O antecessor de 9 é 8 O sucessor de 9 é 10.
#include <iostream>
using namespace std;
int main()
{
Enter a number:
int n1;
cin >> n1;
int successor = n1 + 1;
int predecessor = n1 - 1;
cout << "The successor of " << n1 << " is " << successor << endl;

The predecessor of
return 0;
}
7) Create an algorithm that reads a real number and displays its double and its third part on the screen. E.g.:
Digite um número: 3.5 O dobro de 3.5 é 7.0 A terça parte de 3.5 é 1.16666
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a number: ";
double n1;
cin >> n1;
double dobro = n1 * 2;
double thirdPart = n1 / 3;
The double of
The third of
return 0;
}
8) Develop a program that reads a distance in meters and shows the relative values in
other measurements. E.g.: Enter a distance in meters: 185.72 The distance of 85.7m corresponds
a: 0.18572Km 1.8572Hm 18.572Dam 1857.2dm 18572.0cm 185720.0mm
#include <iostream>
using namespace std;
int main()
{
Enter the distance in meters:
double distance;
cin >> distance;
double distance_km = distance / 1000;
double distance_hm = distance / 100;
double dam_distance = distance / 10;
double distance_dm = distance * 10;
double distance_cm = distance * 100;
double distance_mm = distance * 1000;
The distance of
endl;
cout << distance_km << " km." << endl;
cout << distance_hm << " hm." << endl;
cout << distance_dam << " dam." << endl;
cout << distance_dm << " dm." << endl;
cout << distance_cm << " cm." << endl;
cout << distance_mm << " mm." << endl;
return 0;
}
9) Create an algorithm that reads how much money a person has in their wallet (in R$) and shows
how many dollars can she buy. Consider US$1.00 = R$5.15.
#include <iostream>
using namespace std;
int main()
{
cout << "How many reais do you have? R$";
double reais;
cin >> reais;
double dollars = reais / 5.15;
With
return 0;
}
10) Create an algorithm that reads the width and height of a wall, calculates, and shows the area.
is painted and the amount of paint needed for the job, knowing that each liter of paint
paint an area of 2 square meters.
#include <iostream>
using namespace std;
int main()
{
What is the width of the wall?
double width;
cin >> width;
What is the height of the wall?
double height;
cin >> height;
double area = height * width;
double liters = area / 2;
To paint this wall you will need
liters of ink
return 0;
}
11) Develop a logic that reads the values of A, B, and C of a quadratic equation.
and show the value of Delta.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
Enter the value of A:
double a;
cin >> a;
Enter the value of B:
double b;
cin >> b;
Enter the value of C:
double c;
cin >> c;
double delta = b * b - (4*a*c);
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
delta = delta
x1 = x1 and x2 = x2
return 0;
}
12) Create a program that reads the price of a product, calculates, and shows its PRICE.
PROMOTIONAL, with a 5% discount.
#include <iostream>
using namespace std;
int main()
{
Price of the product:
double price;
cin >> price;
double discount = 0.05;
double promotional_price = price * (1 - discount);
Promotional price: R$
return 0;
}
13) Create an algorithm that reads an employee's salary, calculates, and shows their new one
salary, with a 15% increase.
#include <iostream>
using namespace std;
int main()
{
Employee's salary:
double salary;
cin >> salary;
double increase = 0.15;
double new_salary = salary * (1 + increase);
New salary: R$
return 0;
}
14) The car rental needs your help to charge for its services. Write a
program that asks for the number of kilometers driven by a rented car and the quantity
of days for which it was rented. Calculate the total price to be paid, knowing that the car costs
R$90 per day and R$0.20 per kilometer driven.

#include <iostream>
using namespace std;
int main()
{
Number of KM traveled:
double km;
cin >> km;
cout << "Number of days rented: ";
int days;
cin >> days;
double value = (90 * days) + (0.2 * km);
Value to pay: R$
return 0;
}
15) Create a program that reads the number of days worked in a month and shows the salary.
of an employee, knowing that he works 8 hours a day and earns R$25 per hour worked.
#include <iostream>
using namespace std;
int main()
{
Days worked:
int days;
cin >> days;
double salary = days * 8 * 25;
Salary: R$
return 0;
}
16) Write a program to calculate the reduction of a smoker's lifespan.
Ask about the number of cigarettes smoked per day and how many years he has been smoking. Consider
that a smoker loses 10 minutes of life for each cigarette. Calculate how many days of life a smoker
it will lose and display the total in days.

#include <iostream>
using namespace std;
int main()
{
Number of cigarettes smoked per day:
int cigarettes;
cin >> cigarettes;
cout << "Number of years smoking: ";
int years;
cin >> years;
total cigarettes = cigarettes * years * 365;
int min_lost = 10 * total_cigarettes;
int lost_days = lost_minutes / 1440;

This smoker has already lost


your life.
return 0;
}
17) Write a program that asks for the speed of a car. If it exceeds 80Km/h,
The user has been fined. In this case, display the amount of the fine.
charging R$5 for every km above the allowed speed.
#include <iostream>
using namespace std;
int main()
{
Car speed:
double speed;
cin >> speed;
if (speed > 80) {
double fine = (speed - 80) * 5;
You have been fined! Fine amount: R$
}
else {
Not fined.
}
}
18) Create a program that reads a person's year of birth, calculates their age and
then show if she can or cannot vote.
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
time_t seconds;
seconds = time (NULL);
int current_year = (seconds / 31557600) + 1970;

Enter the year you were born:


int birth_year;
cin >> birth_year;
int age = current_year - birth_year;

You are
if(age >= 16) {
You can vote!
}
else {
You cannot vote!
}
return 0;
}
19) Create an algorithm that reads a student's name and two grades, calculates their average and
show on the screen. In the end, analyze the average and show whether the student had a good one or not
utilization (if it is above the average of 7.0).

#include <iostream>
using namespace std;
int main()
{
cout << "Enter the first grade: ";
double n1;
cin >> n1;
Enter the second grade:
double n2;
cin >> n2;
double media = (n1 + n2) / 2;

Media:
if(media < 7) {
Student did not perform well.
}
else {
Student with good performance.
}
return 0;
}
20) Develop a program that reads an integer and shows whether it is EVEN or ODD.
#include <iostream>
using namespace std;
int main()
{
Enter a number:
int number;
cin >> number;
string EvenOrOdd = number % 2 == 0 ? "Even" : "Odd";
cout << "This number is..." << EvenOrOdd;
return 0;
}
Create an algorithm that reads a given year and shows whether it is a LEAP YEAR or not.

#include <iostream>
using namespace std;
int main()
{
Enter a year:
int year;
cin >> year;
if(year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
The year entered is a leap year.
}
else {
The specified year is not a leap year.
}
}
else {
The year provided is a leap year.
}
}
else {
The year entered is not a leap year.
}
return 0;
22) Write a program that reads a boy's year of birth and shows his
situation regarding military enlistment. - If before 18 years, show how many
years left for enlistment. - If you are already over 18 years old, show how many years you have already
they passed the enlistment.

#include <iostream>
#include <time.h>
using namespace std;
int main()
{
time_t seconds;
seconds = time (NULL);
int current_year = (seconds / 31557600) + 1970;

Enter the year you were born:


int year_of_birth;

cin >> birth_year;


int age = current_year - birth_year;

You are
if(age < 18) {
You will have to enlist in
years.
}
else if(age > 18) {
You should have registered
years.
}
else {
You must enlist this year.
}
return 0;
}
23) In an exclusive promotion for International Women's Day, a store wants to offer discounts for
everyone, but especially for women. Create a program that reads name, gender and the value of
customer purchases and calculate the price with discount.
Knowing that: - Men receive a 5% discount - Women receive a 13% discount
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char name[50];
cout << "Name: ";
cin.getline(name, 50);
char gender[10];
cout << "Sex [M/F]: ";
cin.getline(gender, 10);
sex[0] = toupper(sex[0]);
Value of purchases: R$
double purchase_value;
cin >> purchase_value;
Gender is Male or Female
double discount = gender[0] == 'M' ? 0.05 : 0.13;
cout << "Hello, " << name << ", the total amount of your purchases was "
of R$
cout << "For being of sex " << sexoMF << ", you have a discount of ";
Discount:
The total amount after the discount applied is equal to R$
purchase_value * ( 1 - discount);
return 0;
}
24) Create an algorithm that asks the distance a passenger wishes to travel in Km.
Calculate the ticket price, charging R$0.50 per Km for trips up to 200Km and R$0.45 for
longer trips.
#include <iostream>
using namespace std;
int main()
{
What is the distance to be traveled, in kilometers?
double distance;
cin >> distance;
double value = distance > 200 ? 0.45 : 0.5;
According to the distance provided, your ticket will
cost: R$
}
25) Create a program that reads the length of three line segments. Analyze their
measurements and say if it is possible to form a triangle with these lines. Mathematically,
for three segments to form a triangle, the length of each side must be less than the
sum of the other two.
#include <iostream>
using namespace std;
int main()
{
double l1, l2, l3;
Side 1:
cin >> l1;
Side 2:
cin >> l2;
Side 3:
cin >> l3;
isTriangle = l1 < l2 + l3 && l2 < l1 + l3 && l3 < l1 + l2
YES
cout << "Is it a triangle? " << ehTriangulo;
}
26) Write an algorithm that reads two integers and compares them, displaying on the screen.
uma das mensagens abaixo: - O primeiro valor é o maior - O segundo valor é o maior - Não
there is no greater value, both are equal
#include <iostream>
using namespace std;
int main()
{
double n1, n2;
First value:
cin >> n1;
Second value:
cin >> n2;
if(n1 > n2) {
The first value is the greatest.
}
else if(n2 > n1) {
The second value is the largest.
}
else {
There is no greater value, both are equal.
}
}
27) Create a program that reads two grades of a student and calculates their average, showing
uma mensagem no final, de acordo com a média atingida: - Média até 4.9: REPROVADO - Média
between 5.0 and 6.9
#include <iostream>
using namespace std;
int main()
{
double grade1, grade2;
First grade:
cin >> grade1;
Second grade:
cin >> grade2;
double average = (grade1 + grade2) / 2;
cout << "Average: " << media << endl;
if(media < 5) {
FAILED
}
else if(media >= 5 && media < 7) {
RECOVERY
}
else {
APPROVED
}
}
28) Create a program that reads the width and length of a rectangular plot of land,
calculating and displaying its area in m². The program should also show the classification
of this land, according to the list below: - Below 100m² = POPULAR LAND - Between
100m² and 500m² = MASTER LAND - Above 500m² = VIP LAND
#include <iostream>
using namespace std;
int main()
{
double
Length of the land:
cin >> length;
Land width:
cin >> width;
length * width;
Area:
if(area < 100) {
POPULAR LAND
}
else if(area >= 100 && area < 500) {
LAND MASTER
}
else {
VIP LAND
}
}
29) Develop a program that reads the name of an employee, their salary, how many years
he works at the company and shows his new salary, adjusted according to the table below:
- Up to 3 years at the company: 3% increase - between 3 and 10 years: 12.5% increase - 10 years or
but: 20% increase
#include <iostream>
using namespace std;
int main()
{
char name[50];
cout << "Name: ";
cin.getline(name, 50);
double salary, increase;
Salary of
cin >> salary;
How many years has
int years;
cin >> years;
if (years <= 3) {
aumento = 0.03;
}
else if(years >= 10) {
aumento = 0.2;
}
else {
aumento = 0.125;
}
double new_salary = salary * (1 + increase);
cout << "The new salary of " << name << " is equal to R$" <<
new_salary
return 0;
}
30) Redo algorithm 25, adding the feature to show what type of triangle it will be.
formado: - EQUILÁTERO: todos os lados iguais - ISÓSCELES: dois lados iguais - ESCALENO: todos
the different sides
#include <iostream>
using namespace std;
int main()
{
double l1, l2, l3;
Side 1:
cin >> l1;
Side 2:
cin >> l2;
Side 3:
cin >> l3;
string isTriangle = l1 < l2 + l3 && l2 < l1 + l3 && l3 < l1 + l2
YES
cout << "Is it a triangle? " << isTriangle << endl;
if(ehTriangulo == "YES") {
if(ehTriangulo == "YES" && l1 == l2 && l2 == l3) {
Let's see an equilateral triangle.
}
else if(triangle == "YES" && l1 != l2 && l1 != l3 && l2 != l3)
{
It's a scalene triangle.
}
else {
This is an isosceles triangle.
return 0;
}
}
}
31) Create a game of Rock-Paper-Scissors
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int drawn_number = rand() % 3;
string opcoes[3] = {"PEDRA", "PAPEL", "TESOURA"};
int my_option;
STONE [0]... PAPER [1] or SCISSORS [2]....
cin >> my_option;
The computer played...
endl;
You chose ...
if((drawn_number == 0 && my_choice == 2) ||
(drawn_number == 1 && my_option == 0) ||
(drawn_number == 2 && my_option == 1)) {
THE COMPUTER WON!
}
else if(sorteed_number == my_option) {
DRAW!
}
else {
YOU WON!
}
return 0;
}
32) Create a game where the computer will draw a number between 1 and 5, and the player will try.
discover what the drawn value was.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int drawn_number = rand() % 6;
int my_option;
cout << "Choose a number from 1 to 5...";
cin >> my_option;
The computer chose...
You chose ...
if(draw_number == my_option) {
YOU GOT IT RIGHT!
}
else {
YOU LOST!
}
return 0;
}
33) Write a program to approve or not the bank loan for the purchase of
a house. The program will ask for the value of the house, the buyer's salary, and how many years
he will pay. Calculate the monthly payment amount, knowing that it cannot exceed 30% of
salary or else the loan will be denied.
#include <iostream>
using namespace std;
int main()
{
double house_value, salary;
House value: R$
cin >> house_value;
Salary: R$
cin >> salary;
int years;
Years to pay:
cin >> years;
double installment_value = house_value / (years * 12);
string approval = installment_value <= salary * 0.3 ? "Approved" :
Denied
if(approval == "Approved") {
Loan approved, installment amount: R$
installment value
}
else {
cout << "Loan " << approval;
}
return 0;
}
34) The Body Mass Index (BMI) is a value calculated based on height and weight.
a person. According to the value of the BMI, we can classify the individual within certain
Underweight
between 30 and 40: Obesity - above 40: Morbid obesity. Note: The BMI is calculated by
weight/height² (weight divided by the square of height)
#include <iostream>
using namespace std;
int main()
{
double height
How tall are you, in meters?
cin >> height;
What's your weight in kg?
cin >> mass;
weight / (height * height);
cout << imc << endl;
if(imc < 18.5) {
below 18.5: Underweight
}
else if (18.5 < bmi && bmi <= 25) {
Between 18.5 and 25: Ideal weight
}
else if (25 < imc && imc <= 30) {
Between 25 and 30: Overweight
}
else if(30 < imc && imc <= 40) {
Between 30 and 40: Obesity
}
else {
above 40: Morbid obesity
}
return 0;
}
35) A car rental company needs to charge for its services. The rental of a
the car costs R$90 per day for an economy car and R$150 per day for a luxury car. In addition, the
customer pays per km traveled. Create a program that reads the type of car rented (popular or
luxury), how many days of rental and how many kilometers were traveled. In the end, show the price to be
Payment according to the table below: - Popular cars (rental of R$90 per day) - Up to 100Km
traveled: R$0.20 per km - Above 100 km traveled: R$0.10 per km - Luxury cars
(rental of R$150 per day) - Up to 200Km traveled: R$0.30 per Km - Above 200Km
traveled: R$0.25 per Km
#include <iostream>
using namespace std;
int main()
{
cout << "Type of rented car, POPULAR[0] or LUXURY[1]: ";
type
cin >> type;
Rental days:
cin >> days;
double km, total_value;
cout << "How many kilometers driven: ";

cin >> km;


if(tipo == 0) {
You chose a POPULAR car.
if(km <= 100) {
(90 * days) + (0.2 * km);
}
else {
(90 * days) + (0.1 * km);
}
}
else {
You chose a LUXURY car.
if(km <= 200) {
(150 * days) + (0.3 * km);
}
else {
(150 * days) + (0.25 * km);
}
}
cout << "Total value: R$ " << total_value;
return 0;
}
36) A healthy living program wants to award points for physical activities that can be
exchanged for money. The system works like this: - Each hour of physical activity in the month is worth
points - up to 10 hours of activity in the month: earns 2 points per hour - from 10 hours up to 20 hours of activity in
month: earns 5 points per hour - above 20 hours of activity in the month: earns 10 points per hour - A
For each point earned, the customer earns R$0.05 (5 cents) Create a program that reads how many
hours of activity a person had per month, calculate and show how many points they had and how much
she managed to earn money.
#include <iostream>
using namespace std;
int main()
{
cout << "How many hours of activities this month: ";
int hours;
double points, prize;
cin >> hours;
if(hours < 10) {
points = 2 * hours;
}
else if(hours >= 10 && hours < 20) {
points = 5 * hours;
}
else {
points = 10 * hours;
}
prize = points * 0.05;
you will receive R$
return 0;
}
37) A company needs to adjust the salaries of its employees, giving a raise of
according to some factors. Make a program that reads the current salary, the employee's gender and
How many years has this employee worked at the company? In the end, show their new salary.
based on the table below: - Women - less than 15 years with the company: +5% - from 15 to 20 years
de empresa: +12% - mais de 20 anos de empresa: +23% - Homens - menos de 20 anos de
empresa: +3% - de 20 até 30 anos de empresa: +13% - mais de 30 anos de empresa: +25%.
#include <iostream>
using namespace std;
int main()
{
double salary
Current salary: R$
cin >> salary;
int gender, years;
Genre - MALE[0] / FEMALE[1]:
cin >> genre;
Years in the company:
cin >> years;
if(gender == 1) {
if(years < 15) {
increase = 0.05;
}
else if(years >= 15 && years <= 20) {
aumento = 0.12;
}
else {
aumento = 0.23;
}
}
else {
if(years < 20) {
increase = 0.03;
}
else if(years > 30) {
increase = 0.25;
}
else {
aumento = 0.13;
}
}
cout << "New salary: R$ " << salary * (1 + increase);
return 0;
}
38) Write a program that displays the following count on the screen: 6 7 8 9 10 11 Done!
#include <iostream>
using namespace std;
int main()
{
for(int i = 6; i <= 11; i++) {
cout << i << " ";
}
Finished!
return 0;
}
Create an algorithm that displays the following countdown on the screen: 10 9 8 7 6 5 4 3 Done!

#include <iostream>
using namespace std;
int main()
{
for(int i = 10; i >= 3; i--) {
cout << i << " ";
}
It's over!
return 0;
}
Create an application that displays the following count on the screen: 0 3 6 9 12 15 18 Finished!

#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i <= 18; i+=3) {
cout << i << " ";
}
Finished!
return 0;
}
41) Develop a program that displays the following count on the screen: 100 95 90 85 80 ... 0
It's over!
#include <iostream>
using namespace std;
int main()
{
for(int i = 100; i >= 0; i-=5) {
cout << i << ' ';
}
Finished!
return 0;
}
42) Create an algorithm that asks the user for any positive integer.
show a count up to this value: Ex: Enter a value: 35 Count: 1 2 3 4 5 6 7 ... 33 34 35
It's over!
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Enter a value: ";
cin >> x;
for(int i = 1; i <= x; i++) {
cout << i << " ";
}
Finished!
return 0;
}
43) Develop an algorithm that shows a countdown from 30 to 1, marking
the numbers that are divisible by 4, exactly as shown below: 30 29 [28] 27 26 25
[24] 23 22 21 [20] 19 18 17 [16]...
#include <iostream>
using namespace std;
int main()
{
for(int i=30; i>=1; i--) {
if(i%4==0) {
[i]
}
else {
cout << i << " ";
}
}
return 0;}
44) Create an algorithm that reads the initial counting value, the final value, and the increment.
showing all values in the interval: Ex: Enter the first Value: 3 Enter the
último Valor: 10 Digite o incremento: 2 Contagem: 3 5 7 9 Acabou!
#include <iostream>
using namespace std;
int main()
{
int first, last, increment;
Enter the first term:
cin >> first;
Enter the last term:
cin >> last;
Enter the increment:
cin >> increment;
for(int i = first; i <= last; i += increment) {
cout << i << " ";
}
It's over!
return 0;
}
45) The above program will have a problem when we enter the first value greater than the
last. Solve this problem with a code that works in any situation.
#include <iostream>
using namespace std;
int main()
{
int first, last, increment;
Enter the first term:
cin >> first;
Enter the last term:
cin >> last;
Enter the increment:
cin >> increment;
for(int i = first; i <= last; i += increment) {
cout << i << " ";
}
if(first > last) {
for(int i = last; i <= first; i+= increment) {
cout << i << " ";
}
}
Done!
return 0;
}
46) Create a program that calculates and displays the result of the sum of 6 + 8 + 10 +
12 + 14 + ... + 98 + 100.
#include <iostream>
using namespace std;
int main()
{
int sum;
for(int i = 6; i <= 100; i+= 2) {
cout << i << ' ';
soma += i;
}
cout << "Sum: " << sum;
return 0;
}
47) Develop an application that displays the result of the expression 500 + 450 + 400 on the screen
+ 350 + 300 + ... + 50 + 0
#include <iostream>
using namespace std;
int main()
{
int sum;
for(int i = 500; i >= 0; i-= 50) {
cout << i << " ";
soma += i;
}
cout << "Sum: " << sum;
return 0;
}
48) Create a program that reads 7 integer numbers and at the end shows the sum between them.
#include <iostream>
using namespace std;
int main()
{
int number, sum;
for(int i = 1; i <= 7; i++) {
Number
cin >> number;
sum += number;
}
cout << "Sum: " << sum;
ret
urn 0;
}
49) Create a program that reads 6 integers and at the end shows how many of them are even.
and how many are odd.
#include <iostream>
using namespace std;
int main()
{
int number;
int pairs = 0;
int oddNumbers = 0;

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


Number
cin >> number;
if(number % 2 == 0) {
pairs++;
}
else {
odds++;
}
}
Even:
Odd:
return 0;
}
50) Develop a program that draws 20 numbers between 0 and 10 and shows it on
What were the drawn numbers?
numbers are divisible by 3
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
int numbers_greater_than_five = 0;
int divisible_by_three = 0;
srand(time(NULL));
for(int i = 1; i <= 20 ; i++){
int drawn_number = rand() % 11;
Number
endl;
if (drawn_number > 5) {
numbers_greater_than_five++;
}
if(number_drawn % 3 == 0) {
divisible_by_three++;
}
}
cout << "Numbers greater than 5: " << numbers_greater_than_five <<
endl;
cout << "Numbers divisible by 3: " << divisiveis_tres;
return 0;
}
51) Create an application that reads the price of 8 products. In the end, display on the screen what was the
what was the highest and the lowest price entered.

#include <iostream>
using namespace std;

int main()
{
double price, greater, lesser;
for(int i = 1; i <= 8; i++) {
Product
cin >> price;
if(i == 1) {
greater = price;
lesser = price;
}
if(price < lower) {
price
}
if(price > highest) {
greater = price;
}
}
Highest price: R$
Lowest price: R$
return 0;
}
52) Create an algorithm that reads the age of 10 people, showing in the end: a) What is the average
de idade do grupo b) Quantas pessoas tem mais de 18 anos c) Quantas pessoas tem menos de 5
years d) What was the highest age read
#include <iostream>
using namespace std;

int main()
{
int age;
int sum = 0;
0
int below_five = 0;
int maior = 0;
for(int i = 1; i <= 10; i++) {
Age of person
cin >> age;
sum += age;
if(age > 18) {
over_eighteen++;
}
if(age < 5) {
below_five++;
}
if(age > greater) {
older = age;
}
}
double media = sum / 10;
Average age:
People over 18 years old:
People under 5 years old:
Largest age entered:
return 0;
}
53) Make a program that reads the age and sex of 5 people, showing in the end: a)
How many men were registered
age of the group
#include <iostream>
using namespace std;
int main()
{
int age;
string gender;
int sum = 0;
int sum_age_men = 0;
int women = 0;
int men = 0;
int women_above_twenty = 0;
for(int i = 1; i <= 5; i++) {
Gender of person
cin >> sex;
Age of person
cin >> age;
sum += age;
if(sex == "F") {
women++
if(age > 20) {
woman_above_twenty++
}
}
if(sex == "M") {
men++;
soma_idade_homens += age;
}
}
double overall_average = sum / 5;
double average_age_men = sum_age_men / men;
cout << "Overall average age: " << media_geral << endl;
Registered men:
Registered women:
cout << "Average age of men: " << media_homens << endl;
Women over 20 years old:
return 0;
}
54) Develop an application that reads the weight and height of 7 people, showing at the end: a)
What was the average height of the group
those weighing less than 50kg are shorter than 1.60m d) How many people are over 1.90m tall
weigh more than 100Kg.
#include <iostream>
using namespace std;
int main()
{
double mass, height;
double soma_altura = 0;
double above_ninety = 0;
double less_50kg_less_160cm = 0;
double more_than_190cm_more_than_100kg = 0;

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


Weight of person
cin >> mass;
Height of person
cin >> height;
soma_altura += height;
if(mass > 90) {
above_ninety++;
}
if(mass < 50 && height < 1.6) {
less_than_50kg_less_than_160cm++
}
if(mass > 100 && height > 1.9) {
more_than_190cm_more_than_100kg++;
}
}
double average_height = sum_height / 7;
The average height of the group:
People weigh more than 90Kg:
People who weigh less than 50Kg are less than 1.60m:
less_than_50kg_less_than_160cm
People who are over 1.90m tall weigh more than 100Kg:
more_190cm_more_100kg
return 0;
}
55) Let's improve the game we made in exercise 32. From now on, the computer will draw
a number between 1 and 10 and the player will have 4 attempts to try to get it right.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int drawn_number = rand() % 10;
int guess;
You have 4 attempts to try to guess the number
drawn (1 to 10)
for(int i = 1; i <= 4; i++) {
cout << "Attempt " << i << ": ";
cin >> guess;
if (guess == drawn_number) {
You got it right!
break;
}
else {
cout << "Error" << endl;
}
}
return 0;
}
56) Create a program that reads several numbers from the keyboard and shows at the end the sum between
them. Note: The program will be interrupted when the number 1111 is entered.
#include <iostream>
using namespace std;
int main()
{
int number;
int sum = 0;
do {
sum += number;
Enter a number:
cin >> number;
} while (number != 1111);
Sum:
return 0;
}
57) Develop an application that reads the salary and the gender of several employees. In the end,
show the total salaries paid to men and the total paid to women. The program will
ask the user if they want to continue or not whenever reading employee data.
#include <iostream>
using namespace std;
int main()
{
int number;
bool continue = 1;
double salary, men's salary, women's salary;
int sum = 0;
string gender;
while(continue) {
Salary: R$
cin >> salary;
Sex [M/F]:
cin >> gender;
Do you want to continue? YES[1] / NO[0]:
cin >> continue;
if(gender == "M") {
salary_men += salary;
}
if(gender == "F") {
female_salary += salary;
}
}
Sum of the men's salary: R$
endl;
Sum of women's salaries: R$
endl;
return 0;
}
58) Create an algorithm that reads the age of several students in a class. The program will
stop when the age 999 is entered. In the end, show how many students are in the class and which ones.
it is the average age of the group.

#include <iostream>
using namespace std;
int main()
{
int age;
int sum_ages = 0;
int total_students = 0;
while(true) {
Student's age:
cin >> age;
if(age == 999) {
break;
}
soma_idades += age;
soma_alunos++;
}
double media = sum_ages / sum_students;
Total number of students:
Average of the ages:
return 0;
}
59) Create a program that reads the gender and age of several people. The program will ask
whether the user wants to continue or not for each person. In the end, show: a) what is the highest age read
how many men were registered
of age among men
#include <iostream>
using namespace std;
int main()
{
int age, continue;
int maior_idade = 0;
int total_men = 0;
youngest_woman_age
int sum_age_men = 0;
string gender;
while(true) {
cout << "Age: ";
cin >> age;
Gender [M/F]:
cin >> sex;
Would you like to continue? YES [0] / NO [1]:
cin >> continue;
if(age > maximum_age) {
age
}
if(gender == "M") {
total_men++;
soma_idade_homens += age;
}
if(sex == "F" && age < youngest_woman_age) {
youngest_woman_age
}
if(continue == 1) {
break;
}
}
double average_age_men = sum_age_men / total_men;
Largest age:
Registered men:
cout << "Youngest woman's age: " << youngest_woman_age <<
endl;
cout << "Average age among men " << average_age_men;
return 0;
}
60) Develop an algorithm that reads the name, age, and gender of several people.
the program will ask if the user wants to continue or not. In the end, show: a) The name of
oldest person
men are over 30 years old and) How many women are under 18 years old
#include <iostream>
using namespace std;
int main()
{
name
int people = 0;
int maior_idade = 0;
int continue;
int age;
int sum_ages = 0;
int men_above_30 = 0;
int women_below_18 = 0;
int idade_mulher_mais_jovem = 999;
while (true) {
cout << "Name: ";
cin >> name;
cout << "Age: ";
cin >> age;
if(age > adult_age) {
age;
older
}
Gender [M/F]:
cin >> gender;
sum_ages += age;
people++;
if (gender == "F" && age < youngest_female_age) {
youngest_woman_age = age;
name
}
if(sex == "M" && age > 30) {
men_above_30++
}
if(sex == "F" && age < 18) {
women_below_18++
}
Do you want to continue? YES[0] or NO[1]:
cin >> continue;
if(continue == 1) {
break;
}
}
double media = sum_ages / people;
Oldest person:
Youngest woman:
Average of ages:
Men over 30 years old:
cout << "Women under 18 years old: " << women_under_18 <<
endl;
return 0;
}
61) Create a program that displays the following count on the screen, using the structure 'do'
while 0 3 6 9 12 15 18 21 24 27 30 It's over!
#include <iostream>
using namespace std;
int main()
{
int value = 0;
do {
cout << value << " ";
value += 3;
} while (value <= 30);
Finished!
return 0;
}
62) Create a program using the 'do while' structure that reads the age of many
people. With each loop, you should ask the user if they want to continue or not.
Enter data. In the end, when the user decides to stop, display on the screen: a) How many ages were entered.
b) What is the average of the entered ages c) How many people are 21 years old or older.
#include <iostream>
using namespace std;
int main()
{
int age, continue;
int entered_ages = 0;
double sum_ages = 0;
int people_above_20 = 0;
do {
Age:
cin >> age;
soma_idades += age;
ages_entered++;
if(age > 20) {
people_over_20++;
}
Continue [YES] = 0 / [NO] = 1:
cin >> continue;
} while (continue != 1);
double media = sum_of_ages / ages_entered;
cout << "Number of ages entered: " << entered_ages <<
endl;
cout << "Average age: " << media << endl;
People over 20 years old:
return 0;
}
63) Create a program using the 'do while' structure that reads several numbers. At each
loop, ask if the user wants to continue or not. In the end, display on the screen: a) The sum
What was the lowest value entered
How many values are even?
#include <iostream>
using namespace std;
int main()
{
int value, continue;
int values_entered = 0;
double soma = 0;
int min_value = 99999;
int even_values = 0;
do {
cout << "Value: ";
cin >> value;
sum += value;
entered_values++;
if(value < lesser_value) {
min_value = value;
}
if(value % 2 == 0) {
even_values++;
}
Continue [YES] = 0 / [NO] = 1:
cin >> continue;
} while (continue != 1);
double media = sum / values_entered;
cout << "Sum of the values " << sum << endl;
Lowest value entered:
cout << "Average of the values: " << media << endl;
cout << "Even values: " << even_values;
return 0;
}
64) Develop a program using the 'for' structure that displays the following on the screen
counting: 0 5 10 15 20 25 30 35 40 Finished!
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i <= 40; i+=5) {
cout << i << " ";
}
finished!
return 0;
}
65) Develop a program using the 'for' structure that displays the following on the screen
countdown: 100 90 80 70 60 50 40 30 20 10 0 Done!
#include <iostream>
using namespace std;
int main()
{
for(int i = 100; i >= 0; i-=10) {
cout << i << " ";
}
finished!
return 0;
}
66) Write a program that reads any number and shows the multiplication table of that number.
using the structure 'for'. Ex: Enter a value: 5 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ...
#include <iostream>
using namespace std;
int main()
{
int number;
Show the multiplication table of which number?
cin >> number;
for(int i = 1; i <= 10; i++) {
cout << number << " x " << i << " = " << (number * i) << endl;
}
finished!
return 0;
}
67) Create a program using the 'for' structure that reads a positive integer.
show on the screen a count from 0 to the entered value: Ex: Enter a value: 9 Count: 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, FIN!
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a value: ";
cin >> number;
for(int i = 0; i <= number; i++) {
cout << i << " ";
}
finished!
return 0;
}
68) Create a program that reads the gender and weight of 8 people, using the 'for' structure. In the
How many women were registered
de 100Kg c) A média de peso entre as mulheres d) O maior peso entre os homens
#include <iostream>
using namespace std;
int main()
{
string gender;
double mass;
int women = 0;
int men_above_100 = 0;
double women_mass_sum = 0;
double largest_mass_man = 0;
for(int i = 1; i <= 8; i++) {
Sex of the person
cin >> gender;
Weight of person
cin >> mass;
if(sex == "F") {
women++;
total_mass_women += mass;
}
if(gender == "M" && weight > 100) {
men_above_100++;
}
if(gender == 'M' && weight > greatest_male_weight) {
highest_mass_man = mass;
}
}
double average_mass_women = sum_mass_women / women;
Registered women:
Men over 100kg:
cout << "Mass media among women: " << mass_media_women

Largest mass among men:


endl;
return 0;
}
69) Develop a program that reads the first term and the reason of an AP (Arithmetic Progression)
Arithmetic), displaying on the screen the first 10 elements of the arithmetic progression and the sum of all the values
follow along.
#include <iostream>
using namespace std;
int main()
{
int first_term, ratio;
int sum = 0;
First term of the arithmetic progression:

cin >> first_term;


Reason:
cin >> ratio;
for(int i = 1; i <= 10; i++){
Term
reason << endl;
sum += first_term + (i - 1) * ratio;
}
cout << "The sum of the terms: " << sum;
return 0;
}
70) Create a program that shows the first 10 elements of the Fibonacci Sequence: 1
1, 2, 3, 5, 8, 13, 21...

#include <iostream>
using namespace std;
int main()
{
int n1 = 0;
int n2 = 1;
cout << n2 << " ";
for(int i = 1; i <= 10; i++) {
int n3 = n1 + n2;
cout << n3 << ' ';
n1 = n2;
n2 = n3;
}
return 0;
}

You might also like