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

Basics (4)

The document contains a series of programming exercises in C++ that cover various topics such as calculating bank deposit interest, checking if a number is odd or even, creating a calculator, and calculating averages and sums using different types of loops. Each exercise is accompanied by a sample code solution demonstrating the implementation of the specified functionality. The exercises are designed to help learners practice and understand fundamental programming concepts.

Uploaded by

omar ahmed
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)
9 views

Basics (4)

The document contains a series of programming exercises in C++ that cover various topics such as calculating bank deposit interest, checking if a number is odd or even, creating a calculator, and calculating averages and sums using different types of loops. Each exercise is accompanied by a sample code solution demonstrating the implementation of the specified functionality. The exercises are designed to help learners practice and understand fundamental programming concepts.

Uploaded by

omar ahmed
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/ 12

Basic Exercise

Write a program that calculates the interest of a Bank Deposit. The interest
is determined by the following formula:

Interest = DepositAmount * (InterestRate / 100) * NumberOfYears

Answer
#include <iostream>
using namespace std;

int main() {
double depositAmount;
int numberOfYears;
double interestRate;

// Prompt the user to enter the deposit amount


cout << "Enter the deposit amount: ";
cin >> depositAmount;

// Prompt the user to enter the number of years


cout << "Enter the number of years: ";
cin >> numberOfYears;

// Prompt the user to enter the interest rate


cout << "Enter the interest rate in percentage: ";
cin >> interestRate;

// Calculate the interest earned


double interestEarned = depositAmount * (interestRate / 100)
* numberOfYears;

// Print the result


cout << "Interest earned: " << interestEarned << endl;

return 0;
}
IF Exercises
1. Write a program to check whether the number is odd or even.

Answer
#include <iostream>

using namespace std;

int main() {
int number;

// Input number from user


cout << "Enter a number: ";
cin >> number;

// Check if the number is divisible by 2


if (number % 2 == 0)
cout << number << " is even." << endl;
else
cout << number << " is odd." << endl;

return 0;
}
2. Write a program to check the number is
- Negative and Odd
- Negative and Even
- Positive and Odd
- Positive and Even

Answer
#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a number: ";
cin >> number;

if (number < 0 && number % 2 != 0)


cout << "The number is negative and odd." << endl;
else if (number < 0 && number % 2 == 0)
cout << "The number is negative and even." << endl;
else if (number > 0 && number % 2 != 0)
cout << "The number is positive and odd." << endl;
else if (number > 0 && number % 2 == 0)
cout << "The number is positive and even." << endl;
else
cout << "The number is zero." << endl;

return 0;

}
3. Write a program by using nested if to check the number is
- Negative and Odd
- Negative and Even
- Positive and Odd
- Positive and Even

Answer
#include <iostream>

using namespace std;

int main() {
int number;

// Input number from user


cout << "Enter a number: ";
cin >> number;

if (number < 0) {
if (number % 2 == 0) {
cout << "Negative and Even" << endl;
} else {
cout << "Negative and Odd" << endl;
}
} else {
if (number % 2 == 0) {
cout << "Positive and Even" << endl;
} else {
cout << "Positive and Odd" << endl;
}
}

return 0;
}
Switch Exercise

Write a program to create a calculator.

Answer

#include <iostream>
using namespace std;

int main() {
double num1, num2;
char operation;

cout << "Enter the first number: ";


cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the operation (+, -, *): ";
cin >> operation;

switch (operation) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;

default:
cout << "Error: Invalid operation!" << endl;
}

return 0;
}

5
Loop Exercises
1. Write a program for the calculation of the average from 5 exam scores.

For Loop Answer


#include <iostream>
using namespace std;

int main() {
double sum = 0.0;

cout << "Enter 5 exam scores:" << endl;

for (int i = 0; i < 5; i++) {


int score;
cin >> score;
sum += score;
}

double average = sum / 5;

cout << "The average score is: " << average << endl;

return 0;
}

6
While Loop Answer

#include <iostream>
using namespace std;

int main() {

double total = 0.0;


int i = 0;

while (i < 5) {
double score;
cout << "Enter exam score " << i+1 << ": ";
cin >> score;
total += score;
i++;
}

double average = total / 5;


cout << "The average of the 5 exam scores is: " << average
<< endl;

return 0;
}

7
Do..While Loop Answer
#include <iostream>
using namespace std;

int main() {

double totalScore = 0.0;


int i = 0;

do {
double score;
cout << "Enter exam score " << i + 1 << ": ";
cin >> score;
totalScore += score;
i++;
} while (i < 5);

double average = totalScore / 5;


cout << "The average of the 5"<< " exam scores is: " <<
average << endl;

return 0;
}

8
2. Write a program to find the sum of series 1+3+…...+n, where n is a
positive odd integer.

For Loop Answer


#include <iostream>
using namespace std;

int main() {
int n;
int sumSeries = 0;

cout << "Enter a positive odd integer (n): ";


cin >> n;

if (n > 0 && n % 2 == 1) {
for (int i = 1; i <= n; i += 2) {
sumSeries += i;
}
cout << "The sum of the series 1 + 3 + ... + " << n << "
is: " << sumSeries << endl;
}

else
cout << "The number is not a positive odd integer." <<
endl;

return 0;
}

9
While Loop Answer
#include <iostream>
using namespace std;

int main() {
int n;
int sumSeries = 0;
int i = 1;

cout << "Enter a positive odd integer (n): ";


cin >> n;

if (n > 0 && n % 2 == 1) {

while (i <= n) {
sumSeries += i;
i += 2;
}
cout << "The sum of the series 1 + 3 + ... + " << n << "
is: " << sumSeries << endl;
}

else
cout << "Invalid number." << endl;

return 0;
}

10
Do..While Loop Answer
#include <iostream>
using namespace std;

int main() {
int n;
int sum = 0;
int i = 1;

cout << "Enter the value of n (a positive odd integer): ";


cin >> n;

// Ensure that n is a positive odd integer


if (n > 0 && n % 2 == 1) {
do {
sum += i;
i += 2;

} while (i <= n);

cout << "The sum of the series 1 + 3 + ... + " << n << "
is: " << sum << endl;
}

else
cout << "Invalid number." << endl;

return 0;
}

11
3. Write a program to find the sum of series 1+3+…...+n, where n is a
positive odd integer. N.B. uses the loop to make the user enter n again if
it is negative or even.

Answer
#include <iostream>
using namespace std;

int main() {
int n;
int sumSeries = 0;

cout << "Enter a positive odd integer (n): ";


cin >> n;

// Ensure that n is a positive odd integer


while (!(n % 2 == 1 && n > 0)) {
cout << "The number is not a positive odd integer.
Please enter a positive odd integer: ";
cin >> n;
}

// Calculate the sum of the series


for (int i = 1; i <= n; i += 2) {
sumSeries += i;
}

cout << "The sum of the series 1 + 3 + ... + " << n << " is:
" << sumSeries << endl;

return 0;
}

12

You might also like