Professional Documents
Culture Documents
Mechanical Engineering
FINAL PAPER Session 2016
1|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
No. of Junior Salesperson;s Salary per Senior Sales Person;s Salary per
Weeks week week
<=4 1500 2000
<=8 1750 2500
>8 2000 2750
(Marks = 7)
Question #6: Write a program that converts the angle from degree to radian and displays each
value. (Marks = 7)
Question #7: Write a program that inputs a sentence from the user and counts the number of words
and characters in the sentence. (Marks = 8)
Question #8: Write a program that inputs a number and checks whether it is a perfect number or
not. A perfect number is the number that is numerically equal to the sum of its divisors. For
example, 6 is a perfect number because the divisors of 6 are 1, 2, 3 and 1+2+3=6 (Marks = 8)
Question #9: Write a program to print the following numbers: (Marks = 9)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Question # 10: Write a program to print the following output: (Marks = 10)
BBBBBBBBB
BBBBBBB
BBBBB
BBB
B
Question #11: Write a program that inputs ten numbers from the user in an array and displays the
minimum number. (Marks = 7)
Question #12: Write a program to check whether a number entered by a user is prime number,
even number or odd number using function. (Marks = 13)
2|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
3|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
ANSWERS
Question #1: Write a program that inputs age in years and displays age in days and months.
Answer:
Source Code:
#include<iostream>
#include<conio.h>
int main()
int AgeInYears,
int AgeInDays,
int AgeInMonths;
cin>>AgeInYears;
AgeInMonths = AgeInYears*12;
AgeInDays = AgeInYears*365;
return 0;
4|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
5|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #2: Write a program that inputs two times in hh:mm:ss format, adds both times and
displays the total time. (Marks = 8)
Answer:
Source Code:
#include<iostream>
#include<conio.h>
int main()
int hh1,
int hh2,
int mm1,
int mm2,
int ss1,
int ss2,
int hh=0,
int mm=0,
int ss=0;
cin>>hh1>>mm1>>ss1;
cin>>hh2>>mm2>>ss2;
ss = ss1 + ss2;
6|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
mm = ss / 60;
ss = ss % 60;
mm = mm + (mm1 + mm2);
hh = mm / 60;
mm = mm % 60;
hh = hh + (hh1 + hh2);
return 0;
7|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #3: Write a program that inputs salary and grade, it adds 50% bonus if the grade is less
than 15 and 25% bonus if the grade is above. Display basic salary, bonus and net salary.
(Marks=7)
Answer:
Source Code:
#include <iostream>
#include <conio.h>
int main()
int grde;
cin>>slry;
cin>>grde;
if(grde<15)
bonus=slry*50.0/100.0;
else
bonus=slry*25.0/100.0;
8|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
slry=slry+bonus;
getch();
9|Page
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #4: Write a program that calculates the electricity bill. The rates of electricity per unit
are as follows:
If the units consumed are <=300, then the cost is Rs. 12 per unit.
If the units consumed are >300 and <=500, then the cost is Rs. 17 per unit.
If the units consumed exceed 500 then the cost per unit is Rs. 22
A line rent of Rs. 1500 is also added to the total bill and surcharges of 5% extra if the bill
exceeds Rs. 15000. Calculate the total bill with all the conditions given above. (Marks=8)
Answer:
Source Code:
#include <iostream>
int main ()
int a,b,n;
cin>> a;
if (a<=300)
b=a*12;
cout<< b;
10 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
else if (a<=500)
b=(a*17);
cout<< b;
else if (a>500)
b=(a*22);
cout<< b;
if (b<=15000)
n=b;
cout<<" \n\n Your bill is less than or equal to 15000 therefore 5% surcharge and 1500 tax
is not applicable on the total amount which is = ";
11 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
cout<<n;
else if (b>15000)
n=(b+1500+(b*0.05));
cout<<" \n\n Your bill is greater than 15000 therefore with 5% surcharge and 1500 tax, it
is = ";
cout<<n;
return 0;
12 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
13 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #5: Senior salesperson and junior salesperson are paid per a week. Write a program that
accepts as input a salesperson’s position in the character variable and no. of week he did hard
work. If value is ‘S’ or ‘s’, the senior person’s salary should be computed and displayed, if value
is ‘J’ or ‘j’, the junior person’s salary should be calculated and displayed, otherwise display an
error message. Marks=10
No. of Junior Salesperson’s Salary per Senior Sales Person’s Salary per
Weeks week week
<=4 1500 2000
<=8 1750 2500
>8 2000 2750
(Marks = 7)
Answer:
Source Code:
#include<iostream>
#include<conio.h>
int main ()
char status;
cout<<"Enter your status\nEnter \'s\' or \'S\' for senior person\'s salary\nEnter \'j\' or \'J\' for
junior person\'s salary."<<endl;
cin>>status;
switch (status)
case 's':
14 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
case 'S':
cin>>weeks;
if (weeks<=4)
salary=(weeks*2000);
cout<<salary;
else if (weeks<=8)
salary=(weeks*2500);
cout<<salary;
else if (weeks>8)
salary=(weeks*2750);
cout<<salary;
15 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
}
break;
case 'j':
case 'J':
cin>>weeks;
if (weeks<=4)
salary=(weeks*1500);
cout<<salary;
else if (weeks<=8)
salary=(weeks*1750);
cout<<salary;
16 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
else if (weeks>8)
salary=(weeks*2000);
cout<<salary;
break;
default:
cout<<endl<<"Invalid input.";
return 0;
17 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
18 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
19 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #6: Write a program that converts the angle from degree to radian and displays each
value. Marks: 7
Answer:
Source Code:
#include<iostream>
int main()
float rad,degree;
cin>>degree;
rad=degree*22/(180*7);
return 0;
20 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #7: Write a program that inputs a sentence from the user and counts the number of
words and characters in the sentence. Marks=8
Answer:
Source Code:
#include<iostream>
#include<string.h>
int main ()
char str[50];
int count = 0, i;
gets(str);
count++;
cout << "\nTotal number of words in the sentence are: " << count + 1 <<endl;
count = 0;
count++;
21 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
}
cout << "\nTotal number of characters in the sentence are: " << count <<endl;
return 0;
22 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #8: Write a program that inputs a number and checks whether it is a perfect number or
not. A perfect number is the number that is numerically equal to the sum of its divisors. For
example, 6 is a perfect number because the divisors of 6 are 1, 2, 3 and 1+2+3=6 (Marks = 8)
Answer:
Source Code:
#include<iostream>
int main ()
div = num % i;
if (div == 0)
sum = sum + i;
if (sum == num)
else
return 0;
23 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
24 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #9: Write a program to print the following numbers: (Marks = 9)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Answer:
Source Code:
#include<iostream>
#include<conio.h>
int main()
int i, j, k=8;
cout<<" ";
k=k-2;
25 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
cout<<"\n";
getch();
26 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #10: Write a program to print the following output: (Marks = 10)
BBBBBBBBB
BBBBBBB
BBBBB
BBB
B
Answer:
Source Code:
#include<iostream>
#include<conio.h>
int main()
int i, k, m, l;
for(i=0;i<=4;i++)
for(k=i;k>0;k--)
cout<<" ";
for(l=i;l<9-i;l++)
cout<<"B";
27 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
}
cout<<"\n";
getch();
28 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #11: Write a program that inputs ten numbers from the user in an array and displays the
minimum number. (Marks = 7)
Answer:
Source Code:
#include<iostream>
int main ()
cin >> n;
max = arr[0];
min = arr[0];
min = arr[i];
29 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
return 0;
30 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
Question #12: Write a program to check whether a number entered by a user is prime number,
even number or odd number using function. (Marks = 13)
Answer:
Source Code:
#include <iostream>
using namespace std;
bool checkPrimeNumber(int);
int main()
{
int num;
if (checkPrimeNumber(num))
cout << "\n" << num << " is a prime number.";
else
cout << "\n" << num << " is not a prime number.";
if(num % 2 == 0)
printf("\n%d is even.", num);
else
printf("\n%d is odd.", num);
return 0;
31 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
bool checkPrimeNumber(int num)
{
bool isPrime = true;
if (num == 0 || num == 1)
{
isPrime = false;
}
else {
for (int i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
}
return isPrime;
}
32 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
33 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
34 | P a g e