C and C++

C program to add all the numbers entered by a user until user enters 0

This program repeatedly asks the user to enter a number. The loop continues adding the numbers until the user enters 0. Once 0 is entered, the program displays the total sum.

#include 

int main() {
    int num, sum = 0;

    printf("Enter numbers to add (Enter 0 to stop):\n");

    while (1) {
        scanf("%d", &num);

        if (num == 0) {
            break; // Exit the loop when 0 is entered
        }

        sum += num; // Add the number to sum
    }

    printf("The total sum is: %d\n", sum);

    return 0;
}
OUTPUT
Enter numbers to add (Enter 0 to stop):
5
10
-3
0
The total sum is: 12

Explanation:

The while(1) creates an infinite loop, allowing continuous input.

If the user enters 0, the loop breaks.

Otherwise, the number is added to the sum.

After breaking the loop, the final result is printed.