Display count of positive, negative and zeroes
In this example, we will understand to display count of positive, negative and zeroes which is entered by user using while loop and know how to write, compile and debug it in C language and also learn how to implement it in c.
In order to do this you will need
- Basic knowledge of c programming language.
- As well as know how to operate codeblocks.
The while statement:
The basic format of the while statement is:
while(test condition)
{
body of loop
}
- The while is an entry–controlled loop statement.
- The test-condition is evaluated and if the condition is true, then the body of the loop is executed.
So let's begin with our program
1.Open a codeblocks software.
2.Open new empty file.
3.Copy and paste the code from below.
Input code-
// Program to enter numbers till the user wants. At end, it should display count of positive, negative and zero entered.
#include<stdio.h>
int main()
{
int limit, num, positive = 0, negative = 0, zero = 0;
printf("Enter the limit\n");
scanf("%d", &limit);
printf("Enter %d numbers\n", limit);
while(limit)
{
scanf("%d", &num);
if(num > 0)
{
positive++;
}
else if(num < 0)
{
negative++;
}
else
{
zero++;
}
limit--;
}
printf("\nPositive Numbers: %d\n", positive);
printf("Negative Numbers: %d\n", negative);
printf("Number of zero: %d\n", zero);
return 0;
}
Now simply build the code. Look for any error. Now run the code.
For output enter the limit then enter the positive, negative and zero numbers and press the enter key.
Click the following button to download a program of display count of positive, negative and zeroes entered in c programming language
Thats it.Thank you for scrolling.
Tags:
Loop for do-while while