028 Momina BSCS PF Assignment2

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

PROGRAMMING FUNDAMENTALS

Assignment No. 2
(A Deep Dive into C++Loops And Control Structure)

Name: Momina Saleem


Roll No.: 028
Program: BS
Section: Blue
Submitted To: Prof. jahanzaib hafeez

JUNE 17, 2024


Question 1:
Create a program that calculate the total expenses based on user-inputted quantity and price
per item, applying a 10% discount if the total expense exceeds 5000.

Solution:
#include <iostream>
using namespace std;

int main()

{
int quantity;
float price, total, discount;
cout << "Enter quantity";
cin >> quantity;
cout << "Enter price per item";
cin >> price;

Total = quantity * price;

if (total > 5000)

{
discount = total * 0.10;

}
total -= discount;
cout << "Total expense after discount" << total << endl;
return 0;
}
Question 2:
Create a program to calculate the profit or lose of a seller, and display the profit or lose made,
given the user-inputted cost price and selling price of an item.

Solution:
#include <iostream>
using namespace std;

int main() {
float Profit, Lose, cost P, selling P;

cout << "Enter cost price: ";


cin >> cost P;

1|Page
cout << "Enter selling price: ";
cin >> selling P;
profit or Loss = selling p – cost p;
if (profit or Loss > 0)

{
cout << "Profit: " << profit or Loss << endl;

else if (profit or Loss < 0) {


cout << "Loss: " << -profit or Loss << endl;
} else

{
cout << "No profit, No loss" << endl;
}
return 0;
}
Question 3:
Create a program that prompts the user to enter a year, then determines and displays the
whether the entered year is a leap year or a common year.

Solution:
#include<iostream>

Using namespace std:

int main()

int year;

cout<<”Enter a year”;

cin>>year;

if

input_year % 4 == 0;

if input_year % 100 == 0;

if input_year % 400 == 0;

print(input_year, "is a leap year")

2|Page
}

Else

(input_year, "is a common year")

else(input_year, "is a leap year")

else(input_year, "is a common year")

return 0;

Question 4:
Develop a Program that calculate an employee’s gross salary based on their basic salary input
by user, following the company’s rules: 10% HRA and 90% DA for basic salary less then Rs. 1500
and Rs. 500 HRA and 98% DA for basic salary less then Rs. 1500 or more.

Solution:
#include <iostream>
using namespace std;

int main() {
float basic S, gross S, HRA, DA;

cout << "Enter basic salary: ";


cin >> basic S;

if (basic S < 1500)


{
HRA = basic S* 0.10;
da = basic S* 0.90;
}

else

{
HRA = 500;
DA = basic S * 0.98;
}

gross salary = basic salary + HRA + DA;

3|Page
cout << "Gross salary: " << gross S<< endl;

return 0;
}

Question 5:
The marks obtained by a student in 5 different subjects and input by the user. The student gets
a division as per the following rules:

 Percentage above or equal to 60 – First division.


 Percentage between 50 and 59 – second division.
 Percentage between 40 and 49 – Third division.
 Percentage less than 40 – Fail.
Write a program to calculate the division obtained by the student.

Solution:
#include <iostream>
using namespace std;
int main()

Int i;
float marks[5], total[0], per;
string division;
for (int i = 0; i < 5; i++) {
cout << "Enter marks for subject " << i + 1 << ": ";
cin >> marks[i];
total += marks[i];
}

per = (total / 500) * 100;

if (per>= 60) {
division = "First";
}

else if (per>= 50)

{
division = "Second";
}

4|Page
else if (percentage >= 40)

{
division = "Third";
} else

{
division = "Fail";
}

cout << "Division " << division << endl;

return 0;
}

Question 6:
Write a program that user to enter two numbers, then computes and displays the value of the
first number raised to the power of the second number.

Solution:
#include <iostream>
using namespace std;
int main()

{
int base, exp;
long result;
cout << "Enter base number ";
cin >> base;
cout << "Enter exponent";
cin >> exp;
result = power(base, exponent);
cout << base << "^" << exponent << " = " << result << endl;
return 0;
}
Question 7:
Create a program that take an integer as input the reverse of that integer, i.e. the digits of the
original number is reverse order. For example, if the input integer is 123, the program should
output 321.

Solution:
#include <iostream>
using namespace std;

5|Page
int main()

{
int num, rev num, remainder;
cout << "Enter an integer: ";
cin >> num;

while (num != 0) {
remainder = num % 10;
reversed num = reversed num* 10 + remainder;
num /= 10;
}

cout << "reversed num " << rev num<< endl;

return 0;
}
Question 8:
Write a program that calculate the sum of the series 1 + ½ + 1/3 +….. + 1/n, where the value of
n is provided by the user.

Solution:
#include <iostream>
using namespace std;
int main()

{
int n;
float sum;

cout << "Enter the value of n ";


cin >> n;
for (int i = 1; i <= n; i++)

{
sum += 1.0 / i;
}

cout << "Sum of series: " << sum << endl;

return 0;
}

6|Page
Question 9:
Write a program that incorporates multiple decision-making structure, including is statement, if….else
statement. Evaluate the advantages and disadvantages of each structure in different scenarios and
justify your choice.

Solution:
#include <iostream>
using namespace std;
int main()

{
int num;
bool Prime;
cout << "Enter an integer: ";
cin >> num;

if (num <= 1)

{
Prime = false;
}

else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
Prime = false;
break;
}
}
}

if (Prime)

{
cout << num << " is a prime number." << endl;
}

Else

{
cout << num << " is not a prime number." << endl;
}

return 0;

7|Page
Question 10 :
Develop a C++ program that incoprates multiple decision-making structures, including if
statement, if….else statement, and switch statement, evaluate the advantages and
disadvantages of each structure in different scenarios and justify your choice.

Solution:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
// Using if statement
if (num > 0) {
cout << num << " is positive." << endl;
}
if (num % 2 == 0)

{
cout << num << "is even" << endl;
}

Else

{
cout << num << "is odd" << endl;
}
switch (num)

{
case 0:
cout << "The number is zero" << endl;
break;
case 1:
cout << "The number is one" << endl;
break;
default:
cout << "The number is neither zero nor one" << endl;
}

return 0;
}

8|Page
9|Page

You might also like