Write a program that ask the user for a positive value and u

Write a program that ask the user for a positive value and use a loop to compute the sum of all integers from 1 to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4..., 50.

Solution

#include<studio.h>

int main()

{

int n;

printf(\"Enter a positive number::\");

scanf(\"%d\", &n);

if(n>0)

{

for(int i = 1; i<=n; i++)

{

sum = sum + i;

}

printf(\"Sum of 1 to %d is %d\", n, sum);

}

else

{

printf (\"Entered number is not positive. Please try again!!\");

}

}

Sample output:

Enter a positive number::

6

Sum of 1 to 6 is 21

 Write a program that ask the user for a positive value and use a loop to compute the sum of all integers from 1 to the number entered. For example, if the user

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site