0% found this document useful (0 votes)
31 views7 pages

Assignment 1

Uploaded by

misbahiqbal283
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

Assignment 1

Uploaded by

misbahiqbal283
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment-1 (week5)

Course Title:
Programming fundamentals theory
Submitted to: Sir Junaid Abdullah Mansoor
Submitted by: Misbah Aiman
ID: F20232661169
Section: V24
Course code: CC1021
Program name: BS (CS)

University of Management and


Technology, Lahore
Question no. 1: Write a program that encourages the user to enter two fractions, and then
displays their sum in fractional form.

Code:
#include <iostream>

#include <cmath>

using namespace std;

int main()

/*Write a program that encourages the user to enter two fractions, and then displays their sum
in fractional form.*/

int numerator1, denumerator1, numerator2, denumerator2;

char op='/';

cout << "Enter the first fraction in the form of a/b: " ;

cin >> numerator1 >> op >> denumerator1 ;

cout << "Enter the second fraction in the form of a/b: " ;

cin >> numerator2 >> op >> denumerator2 ;

int answer_numerator = numerator1 * denumerator2 + numerator2 * denumerator1 ;

int answer_denumerator = denumerator1 * denumerator2;

cout << "The sum of the fractions is: " << answer_numerator << "/" << answer_denumerator << endl;

return 0;

Output:
Enter the first fraction in the form of a/b: 6/7

Enter the second fraction in the form of a/b: 3/6

The sum of the fractions is: 57/42

Question no. 2: If cost price and selling price of an item is input through the keyboard, write
a program to determine whether the seller has made profit or incurred loss. Also determine
how much profit he made or loss he incurred.
Code:
#include <iostream>

#include <cmath>

using namespace std;

int main()

/*If cost price and selling price of an item is input through the keyboard, write a program to
determine whether the seller has made profit or incurred loss. Also determine how much profit he made
or loss he incurred.*/

float costPrice, sellingPrice, profit, loss;

cout << "Enter the cost price: ";

cin >> costPrice;

cout << "Enter the selling price: ";

cin >> sellingPrice;

if (sellingPrice>costPrice)

profit = sellingPrice-costPrice;

cout << "The profit made by the seller is $" << profit << endl;

else if (costPrice>sellingPrice)

loss = costPrice-sellingPrice;

cout << "The loss incurred by the seller is $" << loss << endl;

else

cout << "The seller has neither made a profit nor incurred a loss." << endl;

}
return 0;

Output:

Enter the cost price: 5768

Enter the selling price: 6557

The profit made by the seller is $789

Question no. 3: Any integer is input through the keyboard. Write a program to find out
whether it is an odd number or even number.

Code:
#include <iostream>

using namespace std;

int main()

/*Any integer is input through the keyboard. Write a program to find out whether it is an odd
number or even

number.*/

int number;

cout << "enter the number: " ;

cin >> number;

if (number%2==0)

cout << "given number is even." << endl;

else if (number%2!=0)

cout << "given number is odd." << endl;

else
{

cout << "invalid input" << endl;

return 0;

Output:
enter the number: 670

given number is even.

Question no. 4:
Code:
#include <iostream>

#include <cmath>

using namespace std;

int main()

//program 4

int num1, num2, num3, average;

num1=125, num2=28, num3=-25 ;

average= (num1+num2+num3)/3;

cout << "average of given values is: " << average <<endl;

return 0;

Output:
average of given values is: 42

Question no. 5:
code:
#include <iostream>
#include <cmath>

using namespace std;

int main()

//program 5

float a, b, c, d, e ;

cout << "Enter first decimal: " ;

cin >> a;

cout << "Enter second decimal: " ;

cin >> b;

cout << "Enter third decimal: " ;

cin >> c;

cout << "Enter fourth decimal: " ;

cin >> d;

cout << "Enter fifth decimal: " ;

cin >> e;

cout << "five decimals are: " << endl;

cout << a << endl;

cout << b << endl;

cout << c << endl;

cout << d << endl;

cout << e << endl;

cout << "nearest integer for first decimal is: " << (int)a <<endl;

cout << "nearest integer for second decimal is: " << (int)b <<endl;

cout << "nearest integer for third decimal is: " << (int)c <<endl;

cout << "nearest integer for fourth decimal is: " << (int)d <<endl;

cout << "nearest integer for fifth decimal is: " << (int)e <<endl;

int sum, average;


sum = a+b+c+d+e ;

average = (a+b+c+d+e)/5;

cout << "sum of all converted integers is: " << sum <<endl;

cout << "average of all converted inetegers is: " << average <<endl;

return 0;

Output:
Enter first decimal: 6.7

Enter second decimal: 4.6

Enter third decimal: 9.1

Enter fourth decimal: 5.7

Enter fifth decimal: 9.3

five decimals are:

6.7

4.6

9.1

5.7

9.3

nearest integer for first decimal is: 6

nearest integer for second decimal is: 4

nearest integer for third decimal is: 9

nearest integer for fourth decimal is: 5

nearest integer for fifth decimal is: 9

sum of all converted integers is: 35

average of all converted inetegers is: 7

You might also like