Addition of Positive and Negative Numbers Using While Loop


Explanation

The while 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 number is checked using an if-else statement. If the number is less than 0, it is added to the negative total. Otherwise, it is added to the positive total.

How It Works?

  • Step 1: Initialize k with 1, n with 0 for negative total, and p with 0 for positive total.
  • Step 2: The while loop checks whether k <= 10.
  • Step 3: Accept a number from the user.
  • Step 4: Check whether the entered number is less than 0.
  • Step 5: If the condition is true, add the number to the negative total.
  • Step 6: Otherwise, add the number to the positive total.
  • Step 7: Increase the value of k by 1.
  • Step 8: Repeat the process until all 10 numbers are entered.
  • Step 9: Display the totals of negative and positive numbers separately.

Example Program


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

main()
{

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

    clrscr();

    while(k <= 10)
    {

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

        scanf("%d", &no);

        if(no < 0)
        {

            n = n + no;

        }

        else
        {

            p = p + no;

        }

        k = k + 1;

    }

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

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

    getch();

}

Output

Enter any number: -10
Enter any number: 20
Enter any number: -30
Enter any number: 40
Enter any number: -50
Enter any number: 60
Enter any number: -70
Enter any number: 80
Enter any number: -90
Enter any number: 100

Negative number total: -250
Positive number total: 300

Practice Exercises

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

Goal: Learn how to use the while loop with the if-else statement to identify positive and negative numbers and calculate their totals separately.

💡 Hint: Initialize k with 1, n with 0 for negative total, and p with 0 for positive total. Use the while loop condition k <= 15. Check whether the entered number is less than 0. If true, add it to n; otherwise, add it to p. Increase k by 1 after each iteration.
💡 Show Solution

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

main()
{

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

    clrscr();

    while(k <= 15)
    {

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

        scanf("%d", &no);

        if(no < 0)
        {

            n = n + no;

        }

        else
        {

            p = p + no;

        }

        k = k + 1;

    }

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

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

    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

Negative number total: -320
Positive number total: 280
Explanation: The program accepts 15 numbers from the user using a while loop. Each entered number is checked using an if-else statement. If the number is less than 0, it is added to the negative total; otherwise, it is added to the positive total. After all 15 numbers are entered, the program displays the total of negative numbers and the total of positive numbers separately.