You are on page 1of 4

Computer Programming – I (MCT-143)

Lab Session 4
First of all below is the explanation of last two tasks of the last Lab Session to guide you to proceed step by
step.

Task 3:
Write a C++ program that will take a three digit number as input and will display three digits in a column
and also show their sum. (At present we can assume that user is sensible enough and will enter three digits
number. Later on in coming lab sessions we will see how to check that user has actually entered three digits
number and if it is not a three digits number, how to ask again for the number)
Sample output is:

Enter a three digits number: 432


4
3
2
4+3+2=9

How to do it:
Whenever you are given any program to write, firstly break that problem into simple parts and then try
implementing each part. For example in the above case first of all just think about how to take input from
user. It’s very simple, you will declare a variable int and store the 3 digit number from user in that variable:

int x;
cout << “Enter a 3 digit number”;
cin >> x;

Now just think the logic to separate the three digits and save them individually in some variables. First of
all you can decide name of three variables in which you will store three digits separately. It’s also better to
implement the logic thinking of some three digit number. Say it is 597. Now you decide three variable
names for example variable a for tens digit i.e. 7 in case of 597, variable b for tens digit i.e. 9 and variable c
for hundredth digit i.e. 5 in case of 597.

Now you should declare three int variable a, b and c at the top of the code where you declared variable x.

Now implement the logic to extract tens digit i.e. 7 from 597. It can be done using remainder division. So
you will write:
a = x%10;

So you are done with the first part. Now think how to extract hundredth digit from 597. It can’t be done
directly. Firstly you need to separate 59 from 597 which can be done using simple division. And to save 59
in some variable you need some variable. So add another int variable at the top of the code. Say you
declared a variable m at the top to save 59. Do it and then you can separate 59 as:
m= x/10;

Now m variable contains value 59. You can separate 5 and 9 very easily. You will write:
b=m%10;

So b will get the value 9. And in the end you will write:
c=m/10;
c will get 5.

So you have successfully separated the three digits. Major task has been done. Rest is just to display as
described in sample output. To display three variable a, b and c values on a new line, you should know how
to do that. Like:
cout << a <<endl<<b << endl <<c <<endl;

Finally you want to display 5+9+7=21.


5, 9 and 7 are saved in variables a, b and c. So it can be done like:

cout << a << “+” << b << “+” << c << “=” <<a+b+c;

You should understand above cout command.

Task 4:
Write a C++ program that will take two integers as input and will display first integer as a percentage of
other.
Sample output is:

Enter 1st integer: 20


Enter 2nd integer: 30
20 is 66.6667% of 30.
How to do it:
Taking two integers as input is a simple task. You can use two variables say a and b to save the values of
two input integers.

Declare a variable that will hold the percentage of a as b. You should declare it as float so that fractional
part can be saved. Calculation of a as percentage of b is a simple formula which you should know from your
previous knowledge.

c=a/b*100;

Do you think above line will generate correct result??


Again there is problem that a and b are two integers, so when they will be divide, result will be integer.
This is same problem you faced in calculating 4/3 in Task1 for volume calculations of the sphere. There you
solved it using 1.333 value or using 4.0/3. But in this case you can see the value are stored in variables a
and b and aforementioned methods cannot be applied.
What to do now.

It can be handled if you declare variable c as float and initialize it as 1.0 as:
float c=1.0;
And then instead of using:
c=a/b*100;

You should use now:


c=c*a/b*100;

There is yet another method to do it in simpler way without initializing c and using the method shown
below:
c=a*0.1/b*100;

After calculating the value c you should display the last message.

Today’s Tasks

Task 1:
Write a program that will take a floating input from the user and will display its non-fractional and
fractional part separately.
Sample output is:

Enter a floating number: 2.34


Non-Fractional part is: 2
Fractional part is: 0.34

How to do it:
Taking a float input should not be any issue. Suppose you have saved it in a variable a. To get the non-
fractional part, just declare a variable of data type int say x and use:

x=a;

As x is of int data type so only int part (which is non-fractional) will be saved in x. So if user enters 2.34 as
input, x will contain 2. Now you have 2.34 stored in variable a and 2 stored in variable x. Just subtract both
to get the 0.34. You should be able to do that.
You may get some precision problem at fractional part e.g. instead of 0.34 if you will get 0.339.
Task 2:
Write a program that will add two fractions and will display the result.

Sample output is:


Enter num of 1st fraction: 3
Enter den of 1st fraction: 4

Enter num of 2nd fraction: 2


Enter den of 2nd fraction: 3

3/4 + 2/3 = 17/12

As you can see the above sample is simple to implement but doesn’t look professional. A better approach
can be as under:

Sample output is:


Enter 1st fraction: 12/5
Enter 2nd fraction: 1/4

12/5 + 1/4 = 53/20

Use character variable to implement the task as explained in the lecture. Also at the moment do not worry
for the simplification of the fraction.

Task 3:
Write a program that will take three sides of the triangle as input and will display the area of the triangle at
the output using hero’s formula. If a, b and c are sides of triangle, area of the triangle is:
√ ( )( )( )

You have to include math.h header file as you include iostream.h and to calculate the square root you
will use sqrt(a) command where a is the variable of which you want to find the square root.

Sample output is:


Enter 1st side of triangle: 4
Enter 2nd side of triangle: 3
Enter 3rd side of the triangle: 5
The area of triangle is: 6

Note: The side can be in decimal.

You might also like