0% found this document useful (0 votes)
2 views

Lab-05 ME-A Solution

This document is a lab manual for a Computer Systems & Programming course at NUST, detailing various C++ programming tasks. It includes instructions for writing programs that demonstrate loops, input validation, and basic account management functionalities. The manual also contains example solutions for each task, emphasizing practical coding skills.

Uploaded by

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

Lab-05 ME-A Solution

This document is a lab manual for a Computer Systems & Programming course at NUST, detailing various C++ programming tasks. It includes instructions for writing programs that demonstrate loops, input validation, and basic account management functionalities. The manual also contains example solutions for each task, emphasizing practical coding skills.

Uploaded by

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

DEPARTMENT OF COMPUTER & SOFTWARE ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

EC-237 Computer Systems & Programming

LAB MANUAL – 05

Course Instructor: Dr. Sagheer Khan

Lab Instructor: Engr. Eman Fatima

Student Name: _______________________________________________

Degree/ Syndicate: ____________________________________________

1
Lab Tasks:

1. (a) Write a C++ program to display the first 10 multiples of 5 using a for loop. The
program should start from 5 and display up to 50.
Solution:

#include <iostream>
using namespace std;
int main() {
cout << "The first 10 multiples of 5 are:"<<endl;
for (int i = 1; i <= 10; i++) {
cout << 5 * i << " ";
}
cout << endl;
return 0;
}
(b) Write a C++ program that takes a 4-digit integer as input and uses a for loop to
reverse its digits without using arrays or strings. For Example, if the user enters 1234, the
output should be 4321.
Solution:
#include <iostream>
using namespace std;

int main() {
int num, reversedNum = 0;

cout << "Enter a 4-digit integer: ";


cin >> num;
for (int i = 0; i < 4; i++) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
cout << "Reversed number: " << reversedNum << endl;

return 0;
}

2
2. Write a C++ program that calculates the factorial of prime numbers from 1 to N,
where N is input by the user. Use a for loop to print the factorial of only prime
numbers. Ensure input validation (N should be ≥ 1).
Solution:
#include <iostream>
using namespace std;

int main() {
int N;
cout << "Enter a number: ";
cin >> N;
if (N < 1) {
cout << "Invalid input! N should be ≥ 1." << endl;
return 1;
}
for (int num = 2; num <= N; num++) {
// Check if the number is prime
int count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) count++;
}
if (count == 2) { // Prime numbers have exactly 2 factors (1 and itself)
long long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
cout << "Factorial of " << num << " is " << fact << endl;
}
}

return 0;
}

3. Write a C++ program that asks the user to enter a message and a number N. The program
should then display the message N times using a do-while loop. Each message should be
printed on a new line. Hint: Since the message may contain spaces, use getline(); to read
the complete line properly.
Solution:

#include <iostream>
#include <string>
using namespace std;

int main() {
string message;

3
int N, count = 0;
cout << "Enter a message: ";
getline(cin, message);
cout << "Enter a number: ";
cin >> N;
do {
cout << message << endl;
count++;
} while (count < N);
return 0;
}

4. Write a C++ program that takes a positive integer and uses a do-while loop to extract and
display its first digit without using arrays.
Solution:
#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;
do {
num /= 10;
} while (num >= 10);
cout << "First digit: " << num << endl;
return 0;
}

5. Write a C++ program that asks the user for a number N and displays its multiplication
table up to 12 using a for loop. Make sure that the program correctly formats the output
to show calculations.
Solution:
#include <iostream>
using namespace std;

int main() {
int N;
cout << "Enter a number: ";
cin >> N;
for (int i = 1; i <= 12; i++) {
cout << N << " x " << i << " = " << N * i << endl;
}
return 0;
}

4
Home Task:

1. Write a C++ program that manages multiple bank accounts. The program should allow
users to create an account, deposit and withdraw money, and display transaction
details.
 Create Bank Accounts (Using a For Loop):

Ask the user how many accounts they want to create (at least 3). Use a for loop
to input account details such as Account Number, Account Holder Name and
Initial Balance (should be at least Rs. 50,000 /-).

 Perform Transactions (Using a While Loop):

Allow users to deposit and withdraw money from their accounts. Use a while
loop to ensure valid transactions such as Deposit should be greater than Rs.
1000/-, Withdrawal should not exceed the available balance and update the
account balance after each transaction.

 Transaction History & Exit Option (Using a Do-While Loop);

After each transaction, display the updated balance and ask if the user wants to
perform another transaction (deposit/withdraw), to check another account or to
exit the program.

Use a do-while loop to repeat the process until the user decides to exit.

Solution:

#include <iostream>
using namespace std;
int main() {
int accountNumber;
string holderName;
double balance, amount;
int choice;
cout << "Enter Account Number: ";
cin >> accountNumber;
cout << "Enter Account Holder Name: ";

5
getline(cin, holderName);
do {
cout << "Enter Initial Balance (minimum Rs. 50,000): ";
cin >> balance;
} while (balance < 50000);
do {
cout << "\n--- Bank Account Menu ---\n";
cout << "1. Deposit Money\n";
cout << "2. Withdraw Money\n";
cout << "3. Check Balance\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
do {
cout << "Enter deposit amount (minimum Rs. 1000): ";
cin >> amount;
} while (amount < 1000);
balance += amount;
cout << "Deposit successful! New Balance: Rs. " << balance << endl;
} else if (choice == 2) {
do {
cout << "Enter withdrawal amount: ";
cin >> amount;
if (amount > balance)
cout << "Insufficient balance! Try again.\n";
} while (amount > balance);

6
balance -= amount;
cout << "Withdrawal successful! New Balance: Rs. " << balance << endl;
} else if (choice == 3) {
cout << "Account Holder: " << holderName << endl;
cout << "Current Balance: Rs. " << balance << endl;
}
} while (choice != 4);

cout << "Exiting program. Thank you!\n";


return 0;
}

You might also like