0
Using while loop to calculate 1+2+...+n
I write programm but only show input Sum is 0
3 odpowiedzi
+ 2
Jo'shqinbek Bekniyozov If u r looking for a program to add n natural numbers then here is the solution:
#include <iostream>
using namespace std;
int main() {
int n;
int sum=0;
cin>>n; //no of natural number
while(n)
{sum+=n;
n--;
}
cout<<sum;
return 0;
}
Sample input:5
Output:15
0
int n = 5;
int sum = 0;
for(int i = 0; i < n; i++){
sum += i;
}
I think this is what you are looking for.
It would be nice if you can show us your code so people can help you find your errors.