Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Lab – 8

Working with sequential control flow using C++


Exercise

Problem statement 01 : Write a computer program in C ++ that accepts the base and height of a right angle
triangle from the user and display the area of the triangle.

Solution :
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int b, h;
float area;
cout<<"enter the value of base:";
cin>>b;
cout<<"enter the value of height:";
cin>>h;
area = (b*h)/2;
cout<<" Area of triangle is :"<<area<<endl;
getch();
return 0;
}
Result
Problem statement 02

A person is running in a circular ground.write a program in C ++ that asks the user to input radius of the ground
in meters and the number of rounds the person completes. The program should display the amount of distance
travelled by the person in meters.

Solution :

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float r, numberofrounds, PI, circum, distance;
cout<<"enter the radius of ground: ";
cin>>r;
cout<<"enter the number of rounds :";
cin>>numberofrounds;
PI = 3.14;
circum = 2*PI*r;
distance = (circum*numberofrounds);
cout<<"distance travelled by th person is :"<<distance<<endl;

getch();
return 0;
}
result
Problem statement 03

Write a program in c++ that asks the user to enter two integer numbers, stores them in variables numb1 and
num2 respectively. The program swaps the values of two variables with each other without using a third
variable and displays the values of both the variables after swapping.

Solution :

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num1, num2;
cout<<" Enter the number 1:"<<endl;
cin>>num1;
cout<<"Enter the number 2 :"<<endl;
cin>>num2;
cout<<"th output of number 1 is :"<<num2<<endl;
num2 = num1;
cout<<"the output of number2 is :"<<num1<<endl;
getch();
return 0;
}

RESULT
Problem statement 04 :Write a program in c++ that asks the user to enter a four digit integer number and
displays the average of all the four digits of number.

Solution :

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n;
float n1, n2, n3, n4;
float avrg;
cout<<"enter a four digit number:";
cin>>n;

n1=n % 10;
n= n/10;
n2 = n%10;
n = n/10;
n3 = n%10;
n = n/10;
n4 = n%10;
avrg =(n1+n2+n3+n4)/4;
cout<<"the average of four digits is :"<<avrg<<endl;
getch ();
return 0;
}
RESULT
Problem statement 05

Write a program in C++ that prompts the user to input a decimal number and outputs the number rounded to
the nearest integer.

Solution :

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float num;
cout<<"please enter a decimal number:";
cin>>num;

cout<<"Rounded number"<<static_cast<int>(num+0.5)<<endl;
getch();
return 0;
}
Result
Lab – 7
Getting familiar with fundamentals of programming
1. Write a single C++ statement to accomplish each of the following (assume that using
directives have not been used):
a. Declare the variables c, thisIsAVariable, q76354 and number to be of type int.
b. Prompt the user to enter an integer. End your prompting message with a colon (: ) followed by a
space and leave the cursor positioned after the space.
c. Read an integer from the user at the keyboard and store it in integer variable age.
d. Print the message This is a C++ program" on one line.
e. Print the message "This is a C++ program" on two lines. End the first line with C++.
f. Print the message "This is a C++ program" with each word on a separate line.
g. Print the message "This is a C++ program". Separate each word from the next by a tab.
Solution :
a. int c, thisisvariable, q76354, n;
b. cout<<"enter an integer : "
c. cin>>n;
d. cout<<"This is a C++ program"<<endl;
e. cout<<"This is a C++<<endl program"<<endl;
f. cout<<"This<<endl<< is<<endl<<a<<endl <<C++ <<endl<<program"<<endl;
g. cout<<"This \tis\t a\t C++\t program"<<endl;

2. Write a statement (or comment) to accomplish each of the following (assume that using
directives have been used for cin, cout and endl ):
a. State that a program calculates the product of three integers.

b. Declare the variables x, y, z and result to be of type int (in separate statements).
c. Prompt the user to enter three integers
d. Read three integers from the keyboard and store them in the variables x, y and z.
e. Compute the product of the three integers contained in variables x, y and z, and assign the result to
the variable result.
f. Print "The product is " followed by the value of the variable result.
g. Return a value from main indicating that the program terminated successfully.
Solution :
a. Write a program in C++ that calculates the product of three integers.
b. int x;
int y
int z;
c. Cout<<"Enter first number: ";
Cout<<"Enter second number: ";
Cout<<"Enter third number : ";
d. cin>>x>>y>>z;
e. cout <<(x*y*z);
f. cout<<”The product is :”<<(x*y*z);
g. Return 0;
3. Using the statements you wrote in Exercise 2, write a complete program that calculates and displays the
product of three integers. Add comments to the code where appropriate. [Note: You’ll need to write the
necessary using directives.]
Solution :

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x, y, z, product;
cout<<"enter first number :";
cin>>x;
cout<<"enter second number: ";
cin>>y;
cout<<"enter third number :";
cin>>z;
product = x*y*z;
cout<<"prouduct is :"<<product<<endl;
getch();
return 0;
}
RESULT
4. What, if anything, prints when each of the following C++ statements is performed? If
nothing prints, then answer “nothing.” Assume x = 2 and y = 3.

a. cout << x;
b. cout << x + x;
c. cout << "x=";
d. cout << "x = " << x;
e. cout << x + y << " = " << y + x;
f. z = x + y;
g. cin >> x >> y;
h. // cout << "x + y = " << x + y;
i. cout << "\n";
Solution :
a. 2

b. 4

c. x=

d. x =2

e. x+y = 5

f. nothing

g. nothing

h. nothing

i.nothing
5- Write a program that asks the user to enter two numbers, obtains the two numbers from the user
and prints the sum, product, difference, and quotient of the two numbers.

Solution :

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int n1,n2, sum, product,difference;

float quotient;

cout<<"enter first number:";

cin>>n1;

cout<<"cout enter second number :";

cin>>n2;

cout<<"sum is= “<<n1+n1<<endl<<”product = “<<n1*n2<<endl<<”difference = “<<n1-


n2<<endl<<”quotient = “<<n1/n2

getch();

return 0;

RESULT
6- Write a program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers
separated by one space. Do this several ways:

a.Using one statement with one stream insertion operator.

b. Using one statement with four stream insertion operators.

c. Using four statements.

solution :

a. cout<<”12 34”;

b. cout<<”1”<<”2 “<<”3”<<”4”;

c. cout<<”1”;

cout<<”2 ”;

cout<<”3”;

cout<<”4”;
7- Write a program that reads in the radius of a circle as an integer and prints the circle’s diameter,
circumference and area. Use the constant value 3.14159 for π. Do all calculations in output statements.

Solution :

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int radius;
float diameter,area, circumference, PI =3.14;
cout<<"ENTER THE RADIUS OF CIRCLE: ";
cin>>radius;
diameter = 2*radius;
cout<<"diameter:"<<diameter<<endl;
circumference = 2*PI*radius;
cout<<"circumference :"<<circumference<<endl;
area = PI*radius*radius;
cout<<"area :"<<area<<endl;
getch();
return 0;
}

8- Write a program that prints a box, an oval, an arrow and a diamond as follows:

Solution :
Result

9.. The formulas for calculating Body Mass Index (BMI) are
Create a BMI calculator application in C++ that reads the user’s weight in pounds and
height in inches (or, if you prefer, the user’s weight in kilograms and height in meters),
then calculates and displays the user’s body mass index.

#include<iostream>
using namespace std;
intmain()
{
float bmi, w, h;
cout<<"enter your weight in pound:";
cin>>w;
cout<<"enter your height in inch:";
cin>>h;
bmi = (w*703)/(h*h);
cout<<"your bmiis :"<<bmi<<endl;
if (bmi>25)
cout<<"overweight";
else if(bmi<25 &&bmi>18.5)
cout<<"optimal";
else
cout<<"underweight";
return 0;

}
Result

You might also like