Home » C program to calculate the sum and skip negative numbers

C program to calculate the sum and skip negative numbers

Learn about C program to calculate the sum and skip negative 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 the sum (maximum 10 numbers) and skip negative numbers

Program:

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

  for(int i=1 ; i<=10; i++)
  {
    printf("Enter number: ");
    scanf("%d",&number);

    if( number<0 ) 
     continue;

    sum += number;
  }
  
  // print result
  printf("Sum = %d",sum);

  return 0;
}

Output:

Enter number: 3
Enter number: 4
Enter number: -3
Enter number: 5
Enter number: -2
Enter number: 6
Enter number: 1
Enter number: 2
Enter number: -6
Enter number: 3
Sum = 24

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

Similar Codes :
C program to calculate sum (maximum 10 numbers) until User enter positive numbers
Sum of first N terms of Fibonacci series in C