114k views
3 votes
Write c++ code to find the sum of all natural no from n to 1 using do while loop​

User Coincoin
by
5.0k points

1 Answer

6 votes

Answer:

#include

using namespace std;

int main(){

int n, sum=0;

cout<<"Enter the value of n(should be a positive integer): ";

//Storing the input integer value into the variable n

cin>>n;

/* If user enters value of n as negative then display an

* error message to user that the value of n is invalid

*/

if(n<=0){

cout<<"Invalid value of n";

}

else{

// We are using while loop to calculate the sum

int i=1;

while(i<=n){

sum=sum+i;

i++;

}

cout<<"Sum of first n natural numbers is: "<<sum;

}

return 0;

}

Step-by-step explanation:

User Chris Frederick
by
4.4k points