Basics (4)
Basics (4)
Write a program that calculates the interest of a Bank Deposit. The interest
is determined by the following formula:
Answer
#include <iostream>
using namespace std;
int main() {
double depositAmount;
int numberOfYears;
double interestRate;
return 0;
}
IF Exercises
1. Write a program to check whether the number is odd or even.
Answer
#include <iostream>
int main() {
int number;
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;
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>
int main() {
int 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
Answer
#include <iostream>
using namespace std;
int main() {
double num1, num2;
char 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.
int main() {
double sum = 0.0;
cout << "The average score is: " << average << endl;
return 0;
}
6
While Loop Answer
#include <iostream>
using namespace std;
int main() {
while (i < 5) {
double score;
cout << "Enter exam score " << i+1 << ": ";
cin >> score;
total += score;
i++;
}
return 0;
}
7
Do..While Loop Answer
#include <iostream>
using namespace std;
int main() {
do {
double score;
cout << "Enter exam score " << i + 1 << ": ";
cin >> score;
totalScore += score;
i++;
} while (i < 5);
return 0;
}
8
2. Write a program to find the sum of series 1+3+…...+n, where n is a
positive odd integer.
int main() {
int n;
int sumSeries = 0;
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;
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 << "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 << "The sum of the series 1 + 3 + ... + " << n << " is:
" << sumSeries << endl;
return 0;
}
12