C Program to Find Sum of 10 Numbers until user enters positive number

Write a c program to find sum of 10 numbers until user enters positive number using for loop. In this c example, for loop iterate from 1 to 10, the if statement checks the number is less than zero. If true, the break statement will exit the compiler from the loop. So, it will find the sum of positive numbers.

#include <stdio.h>

int main()
{   
    int num, sum = 0;
    
    printf("Please Enter the 10 Numbers\n");
    for(int i = 1; i <= 10; i++)
    {
        printf("Number %d = ", i);
        scanf("%d", &num);

        if(num < 0)
        {
            break;
        }
        sum = sum + num;
    }

    printf("\nSum of Positive Numbers Only = %d\n", sum); 
}
C Program to Find Sum of 10 Numbers until user enters positive number

This c program reads ten numbers from user input and calculates the sum of 10 numbers until the user enters positive numbers using a while loop.

#include <stdio.h>

int main()
{   
    int i, num, sum = 0;
    
    printf("Please Enter the 10 Numbers\n");
    i = 1; 
    while(i <= 10)
    {
        printf("Number %d = ", i);
        scanf("%d", &num);

        if(num < 0)
        {
            break;
        }
        sum = sum + num;
        i++;
    }

    printf("\nSum of Positive Numbers Only = %d\n", sum); 
}
Please Enter the 10 Numbers
Number 1 = 44
Number 2 = 123
Number 3 = 87
Number 4 = 99
Number 5 = 25
Number 6 = 129
Number 7 = 7655
Number 8 = -9

Sum of Positive Numbers Only = 8162

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.