Reading Numbers & Finding Total Of Given Numbers


Explanation

The while loop can be used to read multiple numbers from the user and calculate their total. In this program, the user enters 10 numbers one by one. Each number is added to the total variable. After all numbers are entered, the final total of all numbers is displayed.

How It Works?

  • Step 1: Initialize the variable n with 1 and total with 0.
  • Step 2: The while loop checks whether n <= 10.
  • Step 3: Accept a number from the user.
  • Step 4: Add the entered number to the total variable.
  • Step 5: Increase the value of n by 1.
  • Step 6: Repeat the process until 10 numbers are entered.
  • Step 7: Display the final total of all numbers.

Example Program



#include <stdio.h>
#include <conio.h>


main()

{


    int n = 1, total = 0, no;


    clrscr();


    while(n <= 10)

    {


        printf(
        "\n Enter the Number : "
        );


        scanf(
        "%d", &no);


        total = total + no;


        n = n + 1;


    }


    printf(
    "\n Total = %d", total);


    getch();


}


Output

Enter the Number : 10
Enter the Number : 20
Enter the Number : 30
Enter the Number : 40
Enter the Number : 50
Enter the Number : 60
Enter the Number : 70
Enter the Number : 80
Enter the Number : 90
Enter the Number : 100

Total = 550

Practice Exercises

Task 1: Write a C program using the while loop to read 15 numbers from the user and find their total.

Goal: Learn how to use the while loop to accept multiple inputs and perform addition operations to calculate the final total.

💡 Hint: Initialize n with 1 and total with 0. Use the while loop condition n <= 15. Accept a number from the user, add it to total, and increase the value of n by 1 after every iteration.
💡 Show Solution


#include <stdio.h>
#include <conio.h>


main()

{


    int n = 1, total = 0, no;


    clrscr();


    while(n <= 15)

    {


        printf(
        "\n Enter the Number : "
        );


        scanf(
        "%d", &no);


        total = total + no;


        n = n + 1;


    }


    printf(
    "\n Total = %d", total);


    getch();


}


Output
Enter the Number : 5
Enter the Number : 10
Enter the Number : 15
Enter the Number : 20
Enter the Number : 25
Enter the Number : 30
Enter the Number : 35
Enter the Number : 40
Enter the Number : 45
Enter the Number : 50
Enter the Number : 55
Enter the Number : 60
Enter the Number : 65
Enter the Number : 70
Enter the Number : 75

Total = 600
Explanation: The program accepts 15 numbers from the user using a while loop. Each entered number is added to the total variable. The value of n increases by 1 after every input. When all 15 numbers are entered, the final total is displayed.