0% found this document useful (0 votes)
36 views10 pages

C++ Programs for Various Calculations

The documents contain C++ programs that: 1) Calculate distance traveled in free fall given time in seconds. 2) Calculate volume of a globe given its radius. 3) Calculate new annual and monthly salary after an 11.5% compensation pay increase. 4) Calculate body mass index (BMI) given weight and height.

Uploaded by

sundar57567
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)
36 views10 pages

C++ Programs for Various Calculations

The documents contain C++ programs that: 1) Calculate distance traveled in free fall given time in seconds. 2) Calculate volume of a globe given its radius. 3) Calculate new annual and monthly salary after an 11.5% compensation pay increase. 4) Calculate body mass index (BMI) given weight and height.

Uploaded by

sundar57567
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

1. Develop a C++ program that allows the user to enter a time in seconds.

The output must


show how far an object in freefall for that length of time. There is no friction or resistance
in air. The acceleration of 32 feet / second and this is to be defined as a constant variable.
Equation to be used is distance = 0.5 * acceleration * time2

#include <iostream>
using namespace std;
const float g = 9.8;
int main(){
float distance,time;
cout<<"Time taken by object in free fall : ";
cin>>time;
distance = 0.5*g*time*time;
cout<<"The distance is : "<<distance<< "m";
return 0;
}
Output:

2. Ananya bought a model globe for a project work and that was medium sized. She found the
radius of the globe. But she would like to know the volume of that globe. Can you help her in
finding the Volume of the globe using the formula: (4.0/3.0) * π* r^3, where π = 3.14 is a
constant value.
#include <iostream>
#include <cmath>
using namespace std;
const double pie=3.14;
int main(){
double radii;
cout<<"What is the area of globe you want to give"<<endl;
cin>>radii;
double vol;
vol=(4.0/3.0)*pie*pow(radii,3);
cout<<"Volume of the globe is : "<<vol;
return 0;
}
Output:
3. ABC Corporation decides to give 11.5% for its workers as a compensation pay for a period of
six months. Develop a program to read employee’s previous annual salary. Output the new
annual salary, new monthly salary and old monthly salary. Also specify the exact amount
received as compensation pay. Use a variable declaration with the modifier const to express the
pay increase.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const float comp_pay_rate = 0.115;
float comp_pay, total_comp_pay, pre_aslry, new_aslry;
float pre_mslry, new_mslry;
cout << "Enter old annual salary: - ";
cin >> pre_aslry;
pre_mslry = pre_aslry / 12.0;
comp_pay = comp_pay_rate * pre_aslry;
total_comp_pay = comp_pay / 2.0;
new_aslry = pre_aslry + total_comp_pay;
new_mslry = new_aslry / 12.0;
cout << fixed << setprecision(2);
cout << "New Annual salary: - " << new_aslry << endl;
cout << "New Montly salary: - " << new_mslry << endl;
cout << "Old Monthly salary: - " << pre_mslry << endl;
cout << "Total Compensation pay(6 months): - " << total_comp_pay;
return 0;
}
Output:

4. Ganesh aims to become a police officer and so applies for the job of Inspector. When he
appeared for physical test, he failed in the test due to overweight. Now Ganesh decides to do
intense training to reduce weight. The trainer asks his BMI for deciding the level of training
required. Help him to calculate the BMI using the formula: BMI = weight / height 2
#include<iostream>
#include<cmath>
using namespace std;
int main(){
float weight , height;
cout<<"What is your weight sir (kg)"<<endl;
cin>>weight;
cout<<"what is your height sir (m)"<<endl;
cin>>height;
float bmi;
bmi=weight/(pow(height,2));
cout<<"your bmi is : "<<bmi;

return 0;
}
Output

5. Demonstrates dynamic memory allocation using new to create an array of integers. It assigns
values to the dynamically allocated array, prints them, and deallocates the memory using
delete[].
#include <iostream>
using namespace std;
int main()
{
int *array = new int[5];
for (int i = 0; i < 5; i++)
{
array[i] = i * 2;
}
cout << "Array: ";
for (int i = 0; i < 5; i++)
{
cout << array[i] << " ";
}
cout << endl;
delete[] array;
cout << "Memory deallocated successfully!" << endl;
return 0;
}
Output:
6. A shop will give discount of 10% if the cost of purchased quantity is more than 1000. Ask
user for quantity Suppose, one unit will cost 100. Judge and print total cost for user.
#include <iostream>
using namespace std;
int main(){
int unit;
cout<<"Welcome to the shop.\nHow much units you want to buy: ";
cin>>unit;
float cost;
cost = unit*100;
if (cost>=1000){
cout<<"Your price is : $"<<cost-(cost*10/100)<<" after discount";
}
else{
cout<<"Your price is : $"<<cost;
}
return 0;
Output

7. A school has following rules for grading system:

a. Below 25 – F

b. 25 to 45 – E

c. 45 to 50 – D

d. 50 to 60 - C

e. 60 to 80 - B

f. Above 80 - A

Ask user to enter marks and print the corresponding grade.


include <iostream>
using namespace std;
int main(){
float marks;
cout<<"welcome to grade calculator\nPlease tell your marks\n";
cin>>marks;
switch(static_cast<int>(marks)){
case 0 ... 24:
cout<<"Grade F";
break;
case 25 ... 44:
cout<<"Grade E";
break;
case 45 ... 49:
cout<<"Grade D";
break;
case 50 ... 59:
cout<<"Grade C";
break;
case 60 ... 79:
cout<<"Grade B";
break;
default:
cout<<"Grade A";

}
return 0;
}
Output

8. As activity directory at Ocean Breeze Resort, it is your job to suggest appropriate activities to
guests based on the weather:

temp>= 80: swimming

60 <= temp < 80:tennis

40 <= temp < 60:golf


temp < 40: skiing

#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to the activity adivisior" << endl<< "What is the temprature
outside : ";
float temp;
cin >> temp;
cout << "The activity you can do in this temprature is : ";
if (temp >= 80)
cout
<< "Swimming";
else if (temp < 80 && temp >= 60)
cout
<< "tennis";
else if (temp < 60 && temp >= 40)
cout
<< "golf";
else
cout << "skiing";

return 0;
}
Output:

9. Write the program to determine the raise and new salary for an employee to compute the raise.
The input to the program includes the current annual salary for the employee and a number
indicating the performance rating (1=excellent, 2=good, and 3=poor). An employee with a rating
of 1 will receive a 6% raise, an employee with a rating of 2 will receive a 4% raise, and one with
a rating of 3 will receive a 1.5% raise.

Answer
#include <iostream>
using namespace std;
int main()
{
float salary;
int rating;
cout << "What is the employee salary now : ";
cin >> salary;
cout << "How much you will rate the employee (out of 4): ";
cin >> rating;
float newsalary;
float percent_increment;
switch (rating)
{
case 1:
percent_increment = 6;
newsalary = salary + (salary * percent_increment) / 100;
cout << "The new salary is: ";
cout << newsalary;
break;
case 2:
percent_increment = 4;
newsalary = salary + (salary * percent_increment) / 100;
cout << "The new salary is: ";
cout << newsalary;
break;
case 3:
percent_increment = 1.5;
newsalary = salary + (salary * percent_increment) / 100;
cout << "The new salary is: ";
cout << newsalary;
break;
default:
percent_increment = 0;
newsalary = salary + (salary * percent_increment) / 100;
cout << "Same as previous salary: ";
cout << newsalary;
break;
}

return 0;
}
Output:
10. Write a C++ program that will display if a students is pass or not in his exam. (50% or more
is pass). If the student is Pass than your program should display which letter the student has
obtained.

85% or more E for excellent

75% or more but less than 85% O for Outstanding

65% or more but less than 75% G for good

Less than 65% S for satisfactory

If however the student is Fail (below 50% marks) your program should display 1

whether the student should Resit or Redo depending on the following criteria.

33% or more Resit in exam

Less than 33% Redo course


#include <iostream>
using namespace std;
int main()
{
int percentage_scored;
cout << "What is your percentage : ";
cin >> percentage_scored;
if (percentage_scored <= 50)
{
if (percentage_scored >= 33)
{
cout << "You have to Reapear the exam again";
}
else
{
cout << "You have to repeat the same course again";
}
}
else
{
switch (percentage_scored)
{
case 85 ... 100:
cout << "You are Pass\n";
cout << "E";
break;
case 75 ... 84:
cout << "You are Pass\n";
cout << "O";
break;
case 65 ... 74:
cout << "You are Pass\n";
cout << "G";
break;
default:
cout << "You are Pass\n";
cout << "S";
}
}

return 0;
}
Output

Common questions

Powered by AI

A program determining student grades based on score ranges uses a switch-case structure within certain boundaries. Input scores are matched to specific ranges, leading to the following grading: Below 25 – F, 25 to 45 – E, 45 to 50 – D, 50 to 60 – C, 60 to 80 – B, Above 80 – A. Specific conditions in switch cases efficiently map scores to assigned grades .

Conditional structures in C++ can efficiently guide activity suggestions based on temperature. Using a series of 'if', 'else if', and 'else' statements, the program evaluates the input temperature and advises activities like swimming if the temperature is 80°F or higher, tennis if it's between 60°F and 79°F, golf for 40°F to 59°F, and skiing if below 40°F. This demonstrates the use of branching in decision-making processes .

In C++, the program calculating falling distance applies the physics law d = 0.5 * g * t^2, where g is the acceleration due to gravity (9.8 m/s²). The user inputs time in seconds, and the formula computes and outputs the distance. By using concepts of physics, this program exemplifies real-world application of the laws of motion in computer science .

The program checks if a purchase qualifies for a discount by calculating the cost as unit price times quantity. If the total exceeds 1000 currency units, a 10% discount is applied. Conditional 'if-else' logic is used to decide in real-time whether the final cost will be reduced, ultimately outputting the discounted or full price based on the total cost .

Performance-based salary adjustments in C++ are determined by first obtaining the current annual salary and a performance rating input from the user. The switch statement is used to apply different raise percentages: a 6% raise for excellent performance (rating 1), a 4% raise for good performance (rating 2), and a 1.5% raise for poor performance (rating 3). The resulting new salary is calculated using these percentages and displayed to the user .

To determine the volume of a globe in C++, the formula (4.0/3.0) * π * r^3 is used, where π (pi) is approximately 3.14, and r is the radius of the globe. The program prompts the user to input the radius, calculates the volume using the stated formula, and outputs the result. The 'pow' function from the <cmath> library can be used to compute the cube of the radius .

In the C++ exam program, a student’s pass/fail status is determined by their score percentage, where 50% or more means pass. A switch statement assigns different letter grades based on higher score brackets. For failing scores below 50%, the program advises 'Resit' for scores between 33-49% or 'Redo course' for less than 33%. This program uses conditional logic to guide educational improvements .

Dynamic memory allocation in C++ can be demonstrated by using the 'new' operator to create an array of integers. In the program, memory for an array of integers is allocated dynamically, values are assigned, the array is printed, and finally the memory is deallocated using 'delete[]'. This ensures efficient memory usage, especially for variable-size data .

BMI (Body Mass Index) is calculated programmatically using the formula BMI = weight / height^2, where weight is in kilograms and height is in meters. This computation requires taking inputs of weight and height, squaring the height using the 'pow' function, and dividing the weight by this squared value. The calculated BMI helps in determining the intensity of training required for a person based on their body composition .

Compensation pay adjustments for employees in C++ are computed by first reading an employee's previous annual salary, then calculating a compensation increase at a fixed rate of 11.5%. The formula total_comp_pay = comp_pay / 2.0 is used to determine the final compensation pay over six months. The new annual salary and monthly salaries are adjusted accordingly by adding the total compensation pay to the old salary .

You might also like