You do not need to use any functions beyond the main function in this problem.
Initialize an array of int with the values: 4, 6, 9, and 12.
Write a for loop to add the values in the array and find their sum.
Use a loop to print the values in the array.
Print the values in the array and the sum in the following format:
4 + 6 + 9 + 12 = 31
#include <iostream>
using namespace std;
int main() {
const int N=4;
int arr[N] = {4, 6, 9, 12};
int sum=0;
for (int i=0; i<N; i++) {
sum += arr[i];
}
cout << arr[0];
for (int i=1; i<N; i++) {
cout << " + " << arr[i];
}
cout << " = " << sum << endl;
return 0;
}
Comments
Leave a comment