Print the Positive and Negative Numbers using While Loop in C


This is a C program that prompts the user to enter two integers, 'i' and 'n', and then uses two while loops to find and print all the positive and negative numbers between 'i' and 'n' (inclusive).

  • The #include<stdio.h> is a preprocessor directive that includes the contents of the standard input-output library in the program. The int main() function is the starting point of the program execution. Inside the main function, several integers are declared. printf() is used to display the prompts for the user to enter the starting and ending values, and scanf() is used to get the input and store it in the variable 'i' and 'n' respectively.
  • The first while loop starts with the initial value of 'i' as entered by the user and execute the code inside the loop as long as the condition i<=n is true. The first time the code is executed, the current value of 'i' is checked for negativity using if(i<0). If the condition is true, the current value of 'i' is printed using printf("\n%d",i) and the variable 'neg' is incremented by 1. Then the value of 'i' is incremented by 1.
  • Then the condition i<=n is checked, if it's true the control jumps back to the start of the loop and the same process is repeated until the condition is false. Then, the same process is repeated in the second while loop, this time checking for positivity using if(i>0) instead of negativity and incrementing the variable 'pos' instead of 'neg' if the condition is true.
  • Finally, the program prints the total number of positive and negative numbers using printf("\nTotal number of Positive :%d",pos); and printf("\nTotal number of Negative :%d",neg); respectively and returns 0 to indicate the successful execution of the program.

While loops are a control structure that allows you to repeat a block of code as long as a certain condition is true. They are a more structured and readable alternative to using goto statement.

Source Code

#include<stdio.h>
int main()
{
  int i,n,pos,neg;
  printf("\nEnter the starting value:");
  scanf("%d",&i);
  printf("\nEnter the Ending value:");
  scanf("%d",&n);
  printf("\nNegative numbers:");
  while(i<=n)
  {
    if(i<0)
    {
       printf("\n%d",i);
       neg++;
    }
    i++;
  }
  printf("\nPositive numbers:");
  i=0;
  while(i<=n)
  {
    if(i>0)
    {
       printf("\n%d",i);
       pos++;
    }
    i++;
  }
  printf("\nTotal number of Positive :%d",pos);
  printf("\nTotal number of Negative :%d",neg);
  return 0;
}
To download raw file Click Here

Output

Enter the starting value: -6
Enter the Ending value: 6
Negative numbers:
-6
-5
-4
-3
-2
-1
Positive numbers:
1
2
3
4
5
6
Total number of Positive:6
Total number of Negative:6


List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C