You are on page 1of 34

SUMMER 2020 B.Sc.

Mechanical Engineering
FINAL PAPER Session 2016

Name: Muhammad Usama Course Instructor: Engr. Aniqa Ahmad


Roll No: 16-M-804 Date: 7th September, 2020
Subject: Computer Systems and Programming Total Marks: 50
Summer Semester 2020 | Final Paper
B.Sc. Mechanical Engineering | Session 2016
Faculty of Engineering | Lahore Leads University
DEPARTMENT OF MECHANICAL ENGINEERING
QUESTIONS
Question #1: Write a program that inputs age in years and displays age in days and months.
Question #2: Write a program that inputs two times in hh:mm:ss format, adds both times and
displays the total time. (Marks = 8)
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)
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)
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)

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

(ANSWERS ON NEXT PAGE)

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>

using namespace std;

int main()

int AgeInYears,

int AgeInDays,

int AgeInMonths;

cout<<"Enter your age in years: ";

cin>>AgeInYears;

AgeInMonths = AgeInYears*12;

cout<<"Your age in months: "<<AgeInMonths<<endl;

AgeInDays = AgeInYears*365;

cout<<"Your age in days :"<<AgeInDays<<endl;

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>

using namespace std;

int main()

int hh1,

int hh2,

int mm1,

int mm2,

int ss1,

int ss2,

int hh=0,

int mm=0,

int ss=0;

cout<<"Enter time 1 in hh:mm:ss format : ";

cin>>hh1>>mm1>>ss1;

cout<<"Enter time 2 in hh:mm:ss format : ";

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);

cout<<"Sum of two times is : "<<hh<<":"<<mm<<":"<<ss<<endl;

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>

using namespace std;

int main()

float slry, bonus;

int grde;

cout<<"Enter Your Salary =";

cin>>slry;

cout<<"Enter Your Grade =";

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;

cout<<"Your Total Salary Is ="<<slry;

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>

using namespace std;

int main ()

int a,b,n;

cout<<" Enter your units = ";

cin>> a;

if (a<=300)

b=a*12;

cout<<"\n\n Your bill falls in 1st Slab and is = ";

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<<"\n\n Your bill falls in 2nd Slab and is = ";

cout<< b;

else if (a>500)

b=(a*22);

cout<<"\n\n Your bill falls in 3rd Slab and is = ";

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>

using namespace std;

int main ()

int weeks, salary;

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':

cout<<endl<<"Enter total number of weeks worked: ";

cin>>weeks;

if (weeks<=4)

salary=(weeks*2000);

cout<<"Your Salary is: ";

cout<<salary;

else if (weeks<=8)

salary=(weeks*2500);

cout<<"Your Salary is: ";

cout<<salary;

else if (weeks>8)

salary=(weeks*2750);

cout<<"Your Salary is: ";

cout<<salary;

15 | P a g e
SUMMER 2020 B.Sc. Mechanical Engineering
FINAL PAPER Session 2016
}

break;

case 'j':

case 'J':

cout<<endl<<"Enter total number of weeks worked: ";

cin>>weeks;

if (weeks<=4)

salary=(weeks*1500);

cout<<"Your Salary is: ";

cout<<salary;

else if (weeks<=8)

salary=(weeks*1750);

cout<<"Your Salary is: ";

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<<"Your Salary is: ";

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>

using namespace std;

int main()

float rad,degree;

cout<<"Enter the value in degree ";

cin>>degree;

rad=degree*22/(180*7);

cout<<degree<<" degree = "<<rad<<" radian";

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>

using namespace std;

int main ()

char str[50];

int count = 0, i;

cout << "Enter a Sentence: ";

gets(str);

for (i=0; str[i]!='\0';i++)

if (str[i] == ' ')

count++;

cout << "\nTotal number of words in the sentence are: " << count + 1 <<endl;

count = 0;

for (i=0; str[i]!='\0'; i++)

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>

using namespace std;

int main ()

int i, num, div, sum=0;

cout << "Enter The Number : ";

cin >> num;

for (i=1; i < num; i++)

div = num % i;

if (div == 0)

sum = sum + i;

if (sum == num)

cout << "\n" << num <<" is a perfect number.";

else

cout << "\n" << num <<" is not a perfect number.";

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>

using namespace std;

int main()

int i, j, k=8;

for(i=1; i<6; i++)

for(j=0; j<k; j++)

cout<<" ";

k=k-2;

for(j=1; j<=i; j++)

cout<< j << " ";

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>

using namespace std;

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>

using namespace std;

int main ()

int arr[10], n, i, max, min;

cout << "Enter the size of the array : ";

cin >> n;

cout << "Enter the elements of the array : ";

for (i = 0; i < n; i++)

cin >> arr[i];

max = arr[0];

min = arr[0];

for (i = 0; i < n; i++)

if (min > arr[i])

min = arr[i];

cout << "Smallest element : " << min;

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;

cout << "Enter an integer: ";


cin >> 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

You might also like