8.6k views
4 votes
write a program that prompts the users to enter integers. Find and display the sum of the first 10 positive integers entered from keyboard.

User Tamberg
by
6.7k points

1 Answer

6 votes

Answer:

#include <iostream>

using namespace std;

int main() {

int c=0,in,a[10],sum=0;

cout<<"Enter integers"<<endl;

while(true) //taking input of first ten integers..

{

if(c==10)

{

break;

}

cin>>in;

if(in>0)

{

a[c]=in;

c++;

}

}

for(int i=0;i<10;i++)

{

sum=sum+a[i];//finding the sum.

}

cout<<"The sum of first ten positive integers entered by user is "<<sum<<endl;

return 0;

}

Output:-

Enter integers

-45 6 -67 34 76 34 5 76 4 -45 4 -67 3 -45 -123 6

The sum of first ten positive integers entered by user is 248

Step-by-step explanation:

I have an array of size 10 an integer variable c=0 as an counter sum=0 to hold the sum of first ten positive integers,My approach is to first take the input from the user if the integer entered by user is positive then increase the count by 1 and insert this integer in the array.If the count reaches 10 then break out of the loop.

All the positive integers are stored in the array.Then I have used a for loop to find the sum of the array.In the end printing the sum.

User Dopstar
by
7.2k points