Q7 Write a program to enter numbers till the user wants. At the end it should display the count of positive, negative and zeros entered.

Write a program to enter numbers till the user wants. At the end it should display the count of positive, negative and zeros entered.

Program: 100

Write a c program to enter numbers till the user wants. At the end it should display the count of positive, negative and zeros entered.

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, num, count_p=0, count_n=0, count_z=0;
    int arr[100];
    //size of array
    printf("Enter Numbers: ");
    scanf("%d", &num);

    //take input from user for "num" numbers

    for(i=0;i<num;i++)
    {
        scanf("%d", &arr[i]);
    }

    //count the numbers
    for(i=0;i<num;i++)
    {
        //check for positive numbers
        if(arr[i]>0)
        {
            count_p++;
        }
        else if(arr[i]<0)
        {
            count_n++;
        }
        else if(arr[i]==0)
        {
            count_z++;
        }
        else
        {
            printf("Wrong Entry");
            break;
        }
    }
    printf("Positive Numbers: %d\n", count_p);
    printf("Negative Numbers: %d\n", count_n);
    printf("Zero Numbers: %d\n", count_z);
}

Output:

Enter Numbers: 5-1 2 -4 -5 0
Positive Numbers: 1
Negative Numbers: 3
Zero Numbers: 1

Second Method:
#include<stdio.h>
#include<conio.h>

int main()
{
    int number, positive = 0, negative = 0, zero = 0;
    char choice='Y';

    do
    {
        printf("Enter a number: ");
        scanf("%d", &number);

        if (number > 0)
        {
            positive++;
        }
        else if (number < 0)
        {
            negative++;
        }
        else
        {
            zero++;
        }

        printf("Do you want to Continue(y/n)? ");
        scanf(" %c", &choice); //note the space before " %c" otherwise your while loop would not work

    }while(choice == 'y' || choice == 'Y');


    printf("\nPositive Numbers :%d\nNegative Numbers :%d\nZero Numbers :%d", positive, negative, zero);
}

Output:

Enter a number: -4
Do you want to Continue(y/n)? y
Enter a number: 0
Do you want to Continue(y/n)? y
Enter a number: 7
Do you want to Continue(y/n)? y
Enter a number: -45
Do you want to Continue(y/n)? y
Enter a number: 12
Do you want to Continue(y/n)? n

Positive Numbers :2
Negative Numbers :2
Zero Numbers :1
Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.

View Comments (1)

Related Post
Leave a Comment

This website uses cookies.