Write a program that inputs ten values in an array and calculates the sum of these values.
So let’s Write Code:
Let’s build logic First:
Algorithm of Code:
- Step 1: Start
- Step 2: Declare and intialize an array of 10 elements.
- Step 3: Declare and initialize a variable s=0 for store the values of sum.
- Step 4: Take Inputs from user.
- Step 5: Repeat Step 6, 7, 8 until i<=9
- Step 6: Read the curent index value
- Step 7: Add array elements in s as s = s + ar[ i]
- Step 8: i++
- Step 9: Print s
Write a program that inputs ten values in an array and calculates the sum of these values.
#include<iostream>
using namespace std;
main()
{
int ar[10], s = 0;
for(int i=0; i<=9; i++)
{
cout<<"Enter value in element "<<i<<" ? ";
cin>>ar[i];
s = s + ar[i];
}
cout<<"Sum of array is : "<<s<<endl;
}
Hope You will learn Something. Happy Coding.
Visit my YouTube Channel as Well😇 https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Click on the below Links for more Programming Exercises:
Conditional Structure Exercises: 👉https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/
Array Exercises: 👉https://myustaadg.com/category/programming-exercises/arrays-exercises/