Total of the Numbers Using For Loop


Explanation

The for 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 entered number is added to the variable s. After the loop finishes, the program displays the total of all the entered numbers.

How It Works?

  • Step 1: Declare the variables k, no, and initialize s with 0.
  • Step 2: Initialize k with 1 in the for loop.
  • Step 3: The loop checks whether k <= 10.
  • Step 4: Accept a number from the user.
  • Step 5: Add the entered number to the variable s.
  • Step 6: Increase the value of k by 1.
  • Step 7: Repeat the process until all 10 numbers are entered.
  • Step 8: Display the final total.

Example Program


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

main()
{

    int k, no, s = 0;

    clrscr();

    for(k = 1; k <= 10; k = k + 1)
    {

        printf("\n Enter any number: ");

        scanf("%d", &no);

        s = s + no;

    }

    printf("\n Total: %d", s);

    getch();

}

Output

Enter any number: 5
Enter any number: 10
Enter any number: 15
Enter any number: 20
Enter any number: 25
Enter any number: 30
Enter any number: 35
Enter any number: 40
Enter any number: 45
Enter any number: 50

Total: 275

Practice Exercises

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

Goal: Learn how to use the for loop to accept multiple inputs and calculate the total of all entered numbers.

💡 Hint: Initialize s with 0. Use the for loop with the condition k <= 15. Read a number during each iteration, add it to s, and display the final total after the loop ends.
💡 Show Solution

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

main()
{

    int k, no, s = 0;

    clrscr();

    for(k = 1; k <= 15; k = k + 1)
    {

        printf("\n Enter any number: ");

        scanf("%d", &no);

        s = s + no;

    }

    printf("\n Total: %d", s);

    getch();

}

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

Total: 600
Explanation: The program uses a for loop to read 15 numbers from the user. Each entered number is added to the variable s. After the loop completes, the program displays the total of all entered numbers.