0% found this document useful (0 votes)
7 views3 pages

C++ Programs for Common Calculations

The document contains solved assignment problems in C++ that include programs for calculating the volume of a sphere, converting Celsius to Fahrenheit, converting dollars to pesos, converting inches to centimeters, and swapping the values of two variables. Each problem is accompanied by a C++ code solution. The document serves as a tutorial for basic programming concepts and conversions.

Uploaded by

Girma Negash
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)
7 views3 pages

C++ Programs for Common Calculations

The document contains solved assignment problems in C++ that include programs for calculating the volume of a sphere, converting Celsius to Fahrenheit, converting dollars to pesos, converting inches to centimeters, and swapping the values of two variables. Each problem is accompanied by a C++ code solution. The document serves as a tutorial for basic programming concepts and conversions.

Uploaded by

Girma Negash
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

Solved Assignment Problems in C++ - Part1

Q1. Create a program to compute the volume of a sphere. Use the formula: V = (4/3) *pi*r3 where pi is
equal to 3.1416 approximately. The r is the radius of sphere. Display the result.

Sol: #include<iostream>
using namespace std;

#define pi 3.1416

int main()
{
//we need to give only one input to program i.e., radius of sphere r
int r;
float vol;

cout<<"Enter radius of sphere:"<<endl;


cin>>r;

vol = (4/3)*pi*r*r*r;
cout<<"Volume of sphere is:"<<vol;

return 0;
}

Q2. Write a program the converts the input Celsius degree into its equivalent Fahrenheit degree. Use the
formula: F = (9/5) *C+32.

Sol: #include<iostream>
using namespace std;

int main()
{
//we need to give only one input to program i.e., temparature in Celsius
float C;
float Fh;

cout<<"Enter temparature in Celsius:"<<endl;


cin>>C;

Fh = (1.8*C)+32;

cout<<"Converted Fahrenheit value is:"<<Fh;

return 0;
}

1|Page [Link]/EngineersTutor [Link]


Q3. Write a program that converts the input dollar to its peso exchange rate equivalent. Assume that the
present exchange rate is 51.50 pesos against the dollar. Then display the peso equivalent exchange rate.

Sol: #include<iostream>
using namespace std;

int main()
{
//we need to give only one input to program i.e., Number of Dollars
float dollar;

float peso;
cout<<"Enter amount of dollars to convert:"<<endl;
cin>>dollar;

peso = dollar*51.50;

cout<<"Equivalent peso is:"<<peso;

return 0;

Q4. Write a program that converts an input inch(es) into its equivalent centimeters. Take note that one inch
is equivalent to 2.54cms

Sol: #include<iostream>
using namespace std;

int main()
{
//we need to give only one input to program i.e., inches
float inch;

float cm;
cout<<"Enter inches:"<<endl;
cin>>inch;

cm = 2.54* inch;
cout<<"Equivalent peso is:"<<cm;

return 0;

2|Page [Link]/EngineersTutor [Link]


Q5. Write a program that exchanges the value of two variables: x and y. The output must be the value of
variable y will become the value of variable x, and vice versa.

Sol: #include<iostream>
using namespace std;

int main()
{
//we need to give 2 inputs: x and y;
int x, y;

int z;

cout<<"Enter values of x and y:"<<endl;


cin>>x>>y;

cout<<"Before swapping, values of x and y are:"<<x<<"\t"<<y;

cout<<"\n";
z = x;
x = y;
y =z;

cout<<"After swapping, values of x and y are:"<<x<<"\t"<<y;

return 0;
}

3|Page [Link]/EngineersTutor [Link]

You might also like