028 Momina BSCS PF Assignment2
028 Momina BSCS PF Assignment2
028 Momina BSCS PF Assignment2
Assignment No. 2
(A Deep Dive into C++Loops And Control Structure)
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;
{
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;
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;
{
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>
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;
2|Page
}
Else
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;
else
{
HRA = 500;
DA = basic S * 0.98;
}
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:
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];
}
if (per>= 60) {
division = "First";
}
{
division = "Second";
}
4|Page
else if (percentage >= 40)
{
division = "Third";
} else
{
division = "Fail";
}
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;
}
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;
{
sum += 1.0 / i;
}
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