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:”<<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., temperature in Celsius
float C;
float Fh;
cout<<“Enter temperature in Celsius:”<<C;
Fh = (1.8*C)+32;
cout<<“Converted Fahrenheit value is:”<<Fh;
return 0;
}

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:”<<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:”<<inch;
cm = 2.54* inch;
cout<<“Equivalent peso is:”<<cm;
return 0;
}

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:”<<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;
}

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »