Count Positive and Negative Numbers Using For Loop


Explanation

The for loop can be used to count how many positive and negative numbers are entered by the user. In this program, the user enters 10 numbers. Each entered number is checked using the if-else statement. If the number is positive or zero, the positive counter is increased by 1. Otherwise, the negative counter is increased by 1.

How It Works?

  • Step 1: Declare the variables k, no, p, and n. Initialize p and n 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 positive or zero using no >= 0.
  • Step 6: If the number is positive or zero, increase p by 1; otherwise, increase n by 1.
  • Step 7: Repeat the process until all 10 numbers are entered.
  • Step 8: Display the total count of positive and negative numbers.

Example Program


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

main()
{

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

    clrscr();

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

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

        scanf("%d", &no);

        if(no >= 0)
        {

            p = p + 1;

        }

        else
        {

            n = n + 1;

        }

    }

    printf("\n %d positive numbers are there", p);

    printf("\n %d negative numbers are there", n);

    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

5 positive numbers are there
5 negative numbers are there

Practice Exercises

Task 1: Write a C program using the for loop to read 15 numbers from the user and count how many positive numbers and negative numbers are entered.

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

💡 Hint: Initialize p and n with 0. Use the loop condition k <= 15. If no >= 0, increase p by 1; otherwise, increase n by 1.
💡 Show Solution

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

main()
{

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

    clrscr();

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

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

        scanf("%d", &no);

        if(no >= 0)
        {

            p = p + 1;

        }

        else
        {

            n = n + 1;

        }

    }

    printf("\n %d positive numbers are there", p);

    printf("\n %d negative numbers are there", n);

    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

8 positive numbers are there
7 negative numbers are there
Explanation: The program accepts 15 numbers using a for loop. Each entered number is checked to determine whether it is positive (or zero) or negative. The corresponding counter is increased by one. After all inputs are completed, the program displays the total count of positive and negative numbers separately.