Home » C program to calculate sum until User enter positive numbers

C program to calculate sum until User enter positive numbers

Learn about C program to calculate sum until User enter positive numbers in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C program to calculate sum (maximum 10 numbers) until User enter positive

numbers

Program:

#include<stdio.h>
int main()
{
  int sum=0, number, i=1;

  while(1)
  {

    printf("Enter the number:");
    scanf("%d",&number);


    if(number < 0)
     break;

    sum += number; 
    i++;

    if( i>10 )
    break;

  }

  printf("Sum= %d", sum);

  return 0;
}

Output:

Enter the number:10
Enter the number:9
Enter the number:8
Enter the number:7
Enter the number:6
Enter the number:5
Enter the number:4
Enter the number:3
Enter the number:2
Enter the number:1
Sum= 55

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Sum of first N terms of Fibonacci series in C
Sum of Fibonacci Series in C