C Program to Find Sum of 10 Numbers Until User Enters Positive Number

In this post, we will learn how to find the sum until the user enters a positive number using C Programming language.

This program will take multiple inputs and find their sum, until the user enters a negative number. When the user enters a negative number, then this program will terminate the loop using break statement and resume the next statement after the loop.

We will be using the following approaches to write this program.

  1. Using For Loop
  2. Using While Loop

So, without further ado, let’s begin this tutorial.

C Program to Find Sum of 10 Numbers Until User Enters Positive Number

// C Program to Find Sum of 10 Numbers Until User Enters Positive Number
#include <stdio.h>
int main(){
    int num, sum = 0;
    
    // Asking for input
    printf("Enter 10 numbers: \n");
    for (int i = 1; i <= 10; i++){
        printf("Number %d = ", i);
        scanf("%d", &num);
        
        if (num < 0){
            break;
        }
        sum = num + sum;
    }
    
    // Display output
    printf("Sum of positive numbers: %d", sum);
    return 0;
}

Output

Enter 10 numbers: 
Number 1 = 5
Number 2 = 12
Number 3 = 8
Number 4 = -4
Sum of positive numbers: 25

How Does This Program Work ?

    int num, sum = 0;

In this program, we have declared two int data type variables named num and sum.

    // Asking for input
    printf("Enter 10 numbers: \n");
    for (int i = 1; i <= 10; i++){
        printf("Number %d = ", i);
        scanf("%d", &num);

Then, the user is asked to enter the numbers. This program will take a maximum of 10 input.

        if (num < 0){
            break;
        }

If the entered number is less than 0, then this loop will terminate.

        sum = num + sum;

And, it will store the sum of all numbers entered before the negative number.

    // Display output
    printf("Sum of positive numbers: %d", sum);

Finally, the sum of all positive numbers is displayed on the screen using printf() function.

C Program to Find Sum Until User Enters a Positive Number Using While Loop

// C Program to Find the Sum Until User Enter a Positive Number Using While Loop
#include <stdio.h>
int main(){
    int num, i = 1, sum = 0;
    
    // Asking for input
    printf("Enter the number: \n");
    while (i <= 10){
        printf("Number %d = ", i);
        scanf("%d", &num);
        
        if (num < 0){
            break;
        }
        sum += num;
        i++;
    }
    
    // Display output
    printf("Sum of positive numbers: %d", sum);
    return 0;
}

Output

Enter the number: 
Number 1 = 18
Number 2 = 11
Number 3 = 7
Number 4 = 15
Number 5 = -8
Sum of positive numbers: 51

Conclusion

I hope after going through this post, you understand how to find the sum (of max 10 numbers) until the user enters a positive number using C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *