//write a program whih takes input until user enters a negative number. print the sum of given inputs.
#include <iostream>
using namespace std;
int main ()
{ int a[10];
cout << " enter numbers " << endl ;
int i = 0 ;
while ( i < 10)
{while (a[i] > 0 ) {cin>>a[i];}
i++;}
int sum = 0;
i = 0 ;
while ( i < 10 )
{sum=sum+a[i];
i++;}
cout<< " sum is " <<sum;
system ("pause");
return 0 ;
}
maiza.razzaq
0
Newbie Poster
Recommended Answers
Jump to PostWith your current approach, you are actually making this far more difficult than it is.
There is no reason to use an array, all you need are two integer variables, one to hold the current input and the second to hold the sum.
Then you continue to take integer input …
All 2 Replies
Reply to this topic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.