0% found this document useful (0 votes)
9 views24 pages

C++ Exercises for Beginners

The document contains 10 coding exercises that demonstrate basic C++ programming concepts like input/output, loops, variables, arithmetic operations, and string manipulation. Each exercise includes the code to compile and run a simple program that performs calculations or displays output to the user. The exercises cover foundational skills for beginner C++ programmers.

Uploaded by

Amor Rodero
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

C++ Exercises for Beginners

The document contains 10 coding exercises that demonstrate basic C++ programming concepts like input/output, loops, variables, arithmetic operations, and string manipulation. Each exercise includes the code to compile and run a simple program that performs calculations or displays output to the user. The exercises cover foundational skills for beginner C++ programmers.

Uploaded by

Amor Rodero
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Activity 1

Exercise No. 1

#include <iostream>

using namespace std;

int main(){
int age;
cout << "Enter your age: ";
cin >> age;

cout << "You are " << age << " years old." << endl;
cout << "You are too young to play the game." << endl;

system("pause");
return 0;
}
Activity 1
Exercise No. 2

#include <iostream>

using namespace std;

int main(){
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
cout << "*";
}
cout << endl;
}

system("pause");
return 0;
}
Activity 1
Exercise No. 3

#include <iostream>

using namespace std;

int main(){
int x, y;
float z;
cout << "x: ";
cin >> x;
cout << "y: ";
cin >> y;
cout << "z: ";
cin >> z;

printf("x = %d y = %d z = %.1f\n", x, y, z);

system("pause");
return 0;
}
Activity 1
Exercise No. 4

# include <iostream>

using namespace std;

int main(){
char name [50];
cout << "Enter your name: ";
cin >> name;
cout <<"Hello " <<name <<endl;
system("pause");
return 0;
}
Activity 1
Exercise No. 5

#include <iostream>

using namespace std;

int main(){
int val_one, val_two, val_three;
cout << "val_one: ";
cin >> val_one;
cout << "val_two: ";
cin >> val_two;
cout << "val_three: ";
cin >> val_three;

int sum = val_one + val_two + val_three;


float ave = sum/5;

printf("The sum of %d + %d + %d = %d\n", val_one, val_two,


val_three,sum);
printf("The average is: %.0f\n", ave);

system("pause");
return 0;
}
Activity 2
Exercise No. 1

#include <iostream>

using namespace std;

int main(){
int numberone, numbertwo;
cout << "numberone: ";
cin >> numberone;
cout << "numbertwo: ";
cin >> numbertwo;

int sum = numberone + numbertwo;


printf("The sum of %d + %d = %d\n", numberone, numbertwo, sum);

system("pause");
return 0;
}
Activity 2
Exercise No. 2

#include <iostream>

using namespace std;

int main(){
float salesDiv, salesComp;
cout << "salesdiv: ";
cin >> salesDiv;
cout << "salescomp: ";
cin >> salesComp;

float totalSales = salesDiv * salesComp;

cout << "The total sales generated by the sales division is: $" <<
totalSales << " million" << endl;

system("pause");
return 0;
}
Activity 2
Exercise No. 3

#include <iostream>

using namespace std;

int main(){
float purchase, statetax, countrytax;

cout << "Purchase: ";


cin >> purchase;
cout << "State Tax: ";
cin >> statetax;
cout << "Country Tax: ";
cin >> countrytax;

statetax = statetax * purchase;


countrytax = countrytax * purchase;
float totalSalesTax = statetax + countrytax;

printf("The total sales tax on $%.2f purchase is $%.2f\n", purchase,


totalSalesTax);

system("pause");
return 0;
}
Activity 2
Exercise No. 4

#include <iostream>

using namespace std;

int main(){
float meal;
float tax;
float tip;
float total;

cout << "Input meal: ";


cin >> meal;
cout << "Input tax: ";
cin >> tax;
cout << "Input tip: ";
cin >> tip;

tax = tax * meal;


tip = (tax + meal) * tip;
total = meal + tax + tip;

cout << "The meal charge is: $" << meal << endl;
cout << "The tax amount is: $" << tax << endl;
cout << "The tip amount is: $" << tip << endl;
cout << "The total bill is: $" << total << endl;

system("pause");
return 0;
}
Activity 2
Exercise No. 5

#include <iostream>

using namespace std;

int main(){
int val_one, val_two, val_three, val_four, val_five;
cout << "val_one: ";
cin >> val_one;
cout << "val_two: ";
cin >> val_two;
cout << "val_three: ";
cin >> val_three;
cout << "val_four: ";
cin >> val_four;
cout << "val_five: ";
cin >> val_five;

int sum = val_one + val_two + val_three + val_four + val_five;


float ave = sum/5;

printf("The sum of %d + %d + %d + %d + %d = %d\n", val_one, val_two,


val_three, val_four, val_five, sum);
printf("The average is: %.0f\n", ave);

system("pause");
return 0;
}
Activity 2
Exercise No. 6

#include <iostream>

using namespace std;

int main(){
float payAmount, payPeriods, annualPay;
cout << "Pay Amount: ";
cin >> payAmount;
cout << "Pay Periods: ";
cin >> payPeriods;

annualPay = payAmount * payPeriods;

printf("The employee earns $%.2f each pay period.\n", payAmount);


printf("There are %.0f in a year.\n", payPeriods);
printf("The annual pay is: $%.2f.\n", annualPay);

system("pause");
return 0;
}
Activity 2
Exercise No. 7

#include <iostream>

using namespace std;

int main(){
float currentLevel, risingLevel;
cout << "Current Level: ";
cin >> currentLevel;
cout << "Rising Level: ";
cin >> risingLevel;

printf("In 5 years, the ocean's level will be %f higher than the current
level. \n", currentLevel + (risingLevel*5));
printf("In 7 years, the ocean's level will be %f higher than the current
level. \n", currentLevel + (risingLevel*7));
printf("In 10 years, the ocean's level will be %f higher than the current
level. \n", currentLevel + (risingLevel*10));

system("pause");
return 0;
}
Activity 2
Exercise No. 8

#include <iostream>

using namespace std;

int main(){
char month1[10], month2[10], month3[10];
float rain1, rain2, rain3;

cout << "Enter the name of the month: ";


cin >> month1;
cout << "Enter the rainfall (in inch): ";
cin >> rain1;
cout << "Enter the name of the month: ";
cin >> month2;
cout << "Enter the rainfall (in inch): ";
cin >> rain2;
cout << "Enter the name of the month: ";
cin >> month3;
cout << "Enter the rainfall (in inch): ";
cin >> rain3;

float ave = (rain1 + rain2 + rain3)/3;


printf("The average rainfall for %s, %s, and %s is: %.2f\n", month1,
month2, month3, ave);

system("pause");
return 0;
}
Activity 2
Exercise No. 9

#include <iostream>

using namespace std;

int main(){
char c;
int i;
float f;
double d;

cout << "The size of a char is: 1" << endl;


cout << "The size of an int is: 4" << endl;
cout << "The size of a float is: 4" << endl;
cout << "The size of a double is: 8" << endl;

system("pause");
return 0;
}
Activity 2
Exercise No. 10

#include <iostream>

using namespace std;

int main(){
float gallons, miles, MPG;
cout << "Enter the number of size of the tank (gallons): ";
cin >> gallons;
cout << "Enter the number of miles per tank of gas: ";
cin >> miles;
MPG = miles/gallons;

cout << "A car that holds " << gallons <<" gallons of gasoline and travel
" << miles << " miles before refueling gets " << MPG << "MPG" << endl;

system("pause");
return 0;
}
Activity 3
Exercise No. 1

#include <iostream>

using namespace std;

int main(){
float capacity, miles, average;
cout << "Enter the number of size of the tank (gallons): ";
cin >> capacity;
cout << "Enter the number of miles per tank of gas: ";
cin >> miles;
average = miles/capacity;

cout << "The car's MPG is: " << average << endl;

system("pause");
return 0;
}
Activity 3
Exercise No. 2

#include <iostream>

using namespace std;

int main(){
int classA, classB, classC;
cout << "How many tickets were sold for classA? ";
cin >> classA;
cout << "How many tickets were sold for classB? ";
cin >> classB;
cout << "How many tickets were sold for classC? ";
cin >> classC;

int numberA = classA, numberB = classB, numberC = classC;


int total = (numberA * classA) + (numberB * classB) + (numberC *
classC);

printf("Sales for Class A: $%d.00\n", numberA*classA);


printf("Sales for Class B: $%d.00\n", numberB*classB);
printf("Sales for Class C: $%d.00\n", numberC*classC);
printf("Total sales generated: $%d.00\n", total);

system("pause");
return 0;
}
Activity 3
Exercise No. 3

#include <iostream>

using namespace std;

int main(){
float score1, score2, score3, score4, score5;
cout << "Enter score 1: ";
cin >> score1;
cout << "Enter score 2: ";
cin >> score2;
cout << "Enter score 3: ";
cin >> score3;
cout << "Enter score 4: ";
cin >> score4;
cout << "Enter score 5: ";
cin >> score5;

float ave = (score1 + score2 + score3 + score4 + score5)/5;


printf("The average score is: %f\n", ave);

system("pause");
return 0;
}
Activity 3
Exercise No. 5

#include <iostream>
#include <string>

using namespace std;

int main(){
char movie[50];
float adultTickets, childTickets;
float adultPrice = 6.00, childPrice = 3.00;
float grossBox, netBox, distributorCut;

cout << "Enter the name of the movie: ";


[Link](movie, 50);
cout << "Enter number of adult tickets sold: ";
cin >> adultTickets;
cout << "Enter number of child tickets sold: ";
cin >> childTickets;

grossBox = (adultTickets * adultPrice) + (childTickets * childPrice);


netBox = grossBox * 0.20;
distributorCut = grossBox - netBox;

cout << "Movie Name: " << movie << endl;


cout << "Adult Tickets Sold: " << adultTickets << endl;
cout << "Child Tickets Sold: " << childTickets << endl;
printf("Gross Box Office Profit $%.2f\n", grossBox);
printf("Net Box Office Profit $%.2f\n", netBox);
printf("Amount Paid to Distributor:$%.2f\n", distributorCut);

system("pause");
return 0;
}
Activity 3
Exercise No. 6

#include <iostream>
#include <math.h>

using namespace std;

int main(){
float widgetWeight = 9.2;
float palletEmpty, palletWeight;

cout << "How much does the empty pallet weight (pound): ";
cin >> palletEmpty;
cout << "How much does the pallet weight (with widgets): ";
cin >> palletWeight;

float widgetNumber = ceil((palletWeight - palletEmpty) /


widgetWeight);
cout << "***************************************" << endl;
cout << "There are " << widgetNumber << " widgets on the pallet" <<
endl;

system("pause");
return 0;
}
Activity 3
Exercise No. 7

#include <iostream>

using namespace std;

int main(){
int cookies;
cout << "Enter the number of cookies you ate: ";
cin >> cookies;

int totalCalories = cookies * 75;


cout << "*******************************" << endl;
cout << "You consumed: " << totalCalories << " Calories" << endl;

system("pause");
return 0;
}
Activity 3
Exercise No. 8

#include <iostream>

using namespace std;

int main(){
int repCost;
cout << "Enter the replacement cost of your building: ";
cin >> repCost;

cout << " *******************************" << endl;


cout << " \"Minimum insurance recommended\"" << endl;
cout << " *******************************" << endl;
cout << "Enter the replacement cost of your building: $" << repCost <<
endl;
cout << " The minimum insurance you should buy is: $8000" << endl;

system("pause");
return 0;
}
Activity 3
Exercise No. 9

#include <iostream>

using namespace std;

int main() {
float loanPayment, monthlyTotal, insurance, gas, oil, tires,
maintenance;

cout << "Enter your monthly loan payment: ";


cin >> loanPayment;
cout << "Enter your monthly insurance: ";
cin >> insurance;
cout <<"Enter your monthly gas expenses: ";
cin >> gas;
cout <<"Enter your monthly oil expenses: ";
cin >> oil;
cout <<"Enter your monthly tires expenses: ";
cin >> tires;
cout <<"Enter your monthly maintenance expenses: ";
cin >> maintenance;

float montlhlyTotal = loanPayment + insurance + gas + oil + tires +


maintenance;
float annualTotal = monthlyTotal * 12;

cout << "*********************************************" <<


endl;
printf("Your total monthly expenses are: $%.2f\n", monthlyTotal);
printf("Your annual expenses are: $%.2f\n", annualTotal);
}
Activity 3
Exercise No. 10

#include <iostream>

using namespace std;

int main(){
float celsius;
cout << "Enter Celsius Temperature: ";
cin >> celsius;
float fahrenheit = (1.8 * celsius) + 32;
cout << celsius << " Celsius = " << fahrenheit << " Fahrenheit" << endl;
system("pause");
return 0;
}

Common questions

Powered by AI

In C++, the `cin` object is used to receive user input from the standard input device. It reads data from the keyboard and stores it in the variables provided, as seen in several activities. For example, in Activity 1 Exercise No. 1, `cin` is used to get the user's age: `cin >> age;`. Similarly, in Activity 2 Exercise No. 6, it reads `payAmount` and `payPeriods`: `cin >> payAmount; cin >> payPeriods;` .

In Activity 3 Exercise No. 5, the gross box office profit is calculated by multiplying the number of adult and child tickets sold by their respective prices: `grossBox = (adultTickets * adultPrice) + (childTickets * childPrice);`. The net box office profit is determined as 20% of the gross profit: `netBox = grossBox * 0.20;`. The distributor's cut, which is the portion of the gross profit retained by the distributor, is calculated by subtracting the net profit from the gross profit: `distributorCut = grossBox - netBox;`. This calculation provides an understanding of the flow of ticket sales revenue .

The average calculation in Activity 1 Exercise No. 5 is incorrect because the total sum is divided by 5, even though the variable `sum` is derived from the sum of three integers (val_one, val_two, val_three). This leads to a logical error, displaying the average of the three values as if they were five. The correct formula should be `ave = sum/3` to reflect the actual number of variables summed .

The nested loop structure in Activity 1 Exercise No. 2 consists of two `for` loops. The outer loop iterates five times (`for(int i = 0; i < 5; i++)`) and, for each iteration, the inner loop also iterates five times (`for(int j = 0; j < 5; j++)`). During each inner loop iteration, an asterisk `*` is printed. Thus, this nested structure results in a 5x5 grid of asterisks being printed, forming a square block on the console .

The program in Activity 3 Exercise No. 6 estimates the number of widgets on a pallet by first calculating the difference between the total pallet weight with widgets and the empty pallet weight. This difference is then divided by the weight of a single widget to estimate the number of widgets: `widgetNumber = ceil((palletWeight - palletEmpty) / widgetWeight);`. The `ceil` function ensures that the number of widgets is rounded up to the nearest whole number, accounting for any partial widgets resulting from the division and thus reflecting a realistic inventory count .

The program in Activity 3 Exercise No. 7 calculates the total calories consumed by multiplying the number of cookies eaten by a constant calorie count per cookie. Specifically, it uses `totalCalories = cookies * 75;` where `cookies` is the user input for the number of cookies consumed and 75 represents the assumed number of calories per cookie. This simple multiplication provides immediate feedback on caloric intake based on user consumption .

In Activity 2 Exercise No. 6, the calculation of an employee's annual pay is accomplished through two main data inputs: `payAmount` and `payPeriods`. First, the employee's pay per period is input and stored: `cin >> payAmount;`. Next, the number of such pay periods in a year is attained: `cin >> payPeriods;`. Finally, the annual pay is derived by multiplying the per-period amount by the number of periods: `annualPay = payAmount * payPeriods;`. This process highlights the modular use of variables to compute cumulative annual earnings .

The `system("pause")` command is used in the C++ programs to pause the execution of the program and wait for the user to press a key. This allows users to see the output on the console window before it closes, particularly useful when running programs in environments where the console window closes automatically upon program termination. It is seen in each exercise as a way to facilitate user interaction by allowing them to review the program's output before proceeding .

The total sales tax in Activity 2 Exercise No. 3 is calculated by multiplying the purchase amount by each tax rate: `statetax = statetax * purchase; countrytax = countrytax * purchase;`. These are then summed to get `totalSalesTax`. However, a potential improvement involves confirming the integrity of tax rate values input by the user, as incorrect rate inputs directly lead to erroneous tax calculations. Additionally, using specific data types like `double` for monetary calculations could improve precision over `float`, reducing potential round-off errors .

The rainfall average calculation program in Activity 2 Exercise No. 8 collects names and rainfall data for three months. The logical flow starts with user inputs for each month's name and rainfall: `cin >> month1; cin >> rain1;` and so on. The total rainfall from all three months is averaged using `ave = (rain1 + rain2 + rain3)/3;`. This design is user-friendly as it allows easy input of data and delivers an immediate average, although it could be enhanced by including error checking for invalid inputs or non-numeric data .

You might also like