Sum of the input numbers

Ask user to enter numbers and script will print sum of all user entered numbers. User can exit entering numbers by entering 0.

We can ask user to enter numbers by using a WHILE loop. Each time the loop will check the user entered number if it is equal to 0. While loop will exit if the user entered number is 0.
#include <stdio.h>

int main(void) {
    int sum = 0, x = 1;  // Initialize sum and x.

    while (x != 0) {
        printf("\n Enter a number (0 to stop):  ");
        scanf("%d", &x);
        sum = sum + x;
    }

    printf("Sum of the entered numbers: %d ", sum);
    return 0;
}
Here is one sample output. You can enter different numbers and check the output.
 Enter a number :  4

 Enter a number :  5

 Enter a number :  7

 Enter a number :  0
Sum of the entered  numbers : 16

Allowing 0

Sometime we have to allow entry of 0. To exit the while loop we will use negative number. Any number less than 0 is entered then we will come out of the loop but same number should not be added to value of sum.
#include <stdio.h>

int main(void) {
    int sum = 0, x = 1;  // Initialize sum and x.

    while (x >= 0) {
        printf("\n Enter a number :  ");
        scanf("%d", &x);

        if (x >= 0)  
            sum = sum + x;  
            // Negative number is not added.
    }

    printf("Sum of the entered numbers : %d ", sum);
    return 0;
}

With highest Number entered

Out of all the entered numbers , the highest number can be displayed at the end.
#include <stdio.h>

int main(void) {
    int sum = 0, x = 1;  // Initialize sum and x.
    int max_no = 0;

    while (x >= 0) {
        printf("\n Enter a number :  ");
        scanf("%d", &x);

        if (x >= 0) {
            sum = sum + x;  
            // Negative number is not added.

            if (x > max_no) {
                max_no = x;
            }
        }
    }

    printf("Sum of the entered numbers : %d ", sum);
    printf("\n Highest number : %d ", max_no);

    return 0;
}

Example 2: Calculating the Sum of Multiple Numbers

#include <stdio.h>

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

    printf("Enter how many numbers you want to sum: ");
    scanf("%d", &n);

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

    printf("The sum is: %d\n", sum);
    return 0;
}

Example 3: Summing Numbers in an Array

#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int sum = 0;

    for (int i = 0; i < 5; i++) {
        sum += arr[i];
    }

    printf("The sum of the array elements is: %d\n", sum);
    return 0;
}
Output
The sum of the array elements is: 150

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    30-04-2022

    #include <stdio.h>
    #include <conio.h>
    int main()
    {
    int grade=74;

    if (grade>74) {
    printf(“Passed”);
    }
    else if (grade<75 && grade>69) {
    printf(“Conditional Pass”);
    }
    else (grade<70) {
    printf(“Failed!”);
    }
    getch();
    return 0;
    }



    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
    ©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer