Advertisement
janac

Sum 5 numbers using a C array

Nov 8th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. // Enter 5 numbers and
  5. // display the numbers and the sum.
  6. int main()
  7. {
  8.     int input[5] = { 0, 0, 0, 0, 0 }; // The values entered.
  9.     int sum = 0; // The sum of the values entered
  10.  
  11.     cout << "Enter 5 numbers, one at a time." << endl;
  12.     for (int i = 0; i < 5; i++) // Five times,
  13.     {
  14.         cout << "Enter a number: ";
  15.         cin >> input[i]; // Store the number.
  16.         sum += input[i]; // Add the number to the total.
  17.     }
  18.     cout << endl; // Blank line.
  19.     cout << "The sum of\n";
  20.     for (int j = 0; j < 5; j++) // Five times,
  21.     {
  22.         cout << input[j];
  23.         if (j < 4)
  24.         {
  25.             cout << " + ";
  26.         }
  27.     }
  28.     cout << " is " << sum << endl; // Display the sum.
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement