Addition of Positive and Negative Numbers Using For Loop


Explanation

The for loop can be used to read multiple numbers from the user and calculate the total of positive and negative numbers separately. In this program, the user enters 10 numbers. Each entered number is checked using the if-else statement. If the number is negative, it is added to the negative total. Otherwise, it is added to the positive total.

How It Works?

  • Step 1: Declare the variables k, no, n, and p. Initialize n and p 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: Check whether the entered number is negative using no < 0.
  • Step 6: If the number is negative, add it to n; otherwise, add it to p.
  • Step 7: Repeat the process until all 10 numbers are entered.
  • Step 8: Display the total of negative numbers and positive numbers separately.

Example Program


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

main()
{

    int k, no, n = 0, p = 0;

    clrscr();

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

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

        scanf("%d", &no);

        if(no < 0)
        {

            n = n + no;

        }

        else
        {

            p = p + no;

        }

    }

    printf("\n Negative total: %d", n);

    printf("\n Positive total: %d", p);

    getch();

}

Output

Enter any number: 10
Enter any number: -5
Enter any number: 20
Enter any number: -8
Enter any number: 15
Enter any number: -12
Enter any number: 30
Enter any number: -25
Enter any number: 5
Enter any number: -7

Negative total: -57
Positive total: 80

Practice Exercises

Task 1: Write a C program using the for loop to read 15 numbers from the user and calculate the total of positive numbers and negative numbers separately.

Goal: Learn how to use the for loop with the if-else statement to calculate the sum of positive and negative numbers separately.

💡 Hint: Initialize n and p with 0. Use the loop condition k <= 15. If no < 0, add the number to n; otherwise, add it to p.
💡 Show Solution

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

main()
{

    int k, no, n = 0, p = 0;

    clrscr();

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

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

        scanf("%d", &no);

        if(no < 0)
        {

            n = n + no;

        }

        else
        {

            p = p + no;

        }

    }

    printf("\n Negative total: %d", n);

    printf("\n Positive total: %d", p);

    getch();

}

Output
Enter any number: 10
Enter any number: -5
Enter any number: 20
Enter any number: -8
Enter any number: 15
Enter any number: -12
Enter any number: 30
Enter any number: -25
Enter any number: 5
Enter any number: -7
Enter any number: 18
Enter any number: -9
Enter any number: 22
Enter any number: -16
Enter any number: 40

Negative total: -82
Positive total: 160
Explanation: The program accepts 15 numbers using a for loop. Each entered number is checked to determine whether it is negative or positive (including zero). Negative numbers are added to the variable n, while positive numbers are added to the variable p. After all numbers are entered, the program displays the totals of negative and positive numbers separately.