In: Computer Science
Write a program that does the following in order:
1. Ask user to enter a name
2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5”
3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5”
4. If the sum is greater than 0, print out the sum
5. If the sum is equal to zero, print out “Your account balance is zero”
6. If the sum is less than 0, print out “Your account is overdrawn” and the “negative amount”
7. Round all print values to 1 decimal place
Round your sum output to 2 decimal places
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
string name;
cout << "Enter your name : ";
cin >> name;
cout << name << endl;
float arr[5];
cout << "Enter 5 amounts : ";
for(int i=0; i<5; i++) {
cin >> arr[i];
cout << arr[i] << " ";
}
cout << endl;
float sum = 0;
for(int i=0; i<5; i++) {
sum += arr[i];
}
if(sum > 0) {
printf("%.2f\n", sum);
}
else if(sum == 0) {
printf("Your account balance is zero\n");
}
else {
printf("Your account is overdrawn %.1f\n", -sum);
}
}
C++ code
ScreenShot of the code and output