snox
snox

Reputation: 19

Count Positive, Negative, Even, Odd Numbers after user inputs as many values as he wants

i need this for school. I am a beginner in coding. I found a way to loop input values until the user inputs number 0. After that the program should display amount of pos,neg,even and odd numbers. If anyone can help me, I've been stuck googling and trying myself for the past 3 hrs.

int main()
{
    int positive, negative, even, odd, i, number,num;
    int counteven = 0, countodd = 0, countneg = 0, countpos = 0;

    printf("Hello!\n");                  
    printf("I am a programm that will tell you how many\npositive, negative, even and odd numbers you have inputted.\n");
    printf("You can start inputting numbers. When you're finished input the number 0 to let me know you're done.\n");

    int loop = ( 1 == scanf("%di",&number) ) && ( 0 != number );
    while( loop )
    {
        loop = ( 1 == scanf("%di",&number) ) && ( 0 != number );
    }

     if (number < 0)
    {
        countneg++;
    }
    else
    {
        countpos++;
    }

    printf("\nPositive Numbers : %d\nNegative Numbers : %d\nEven Numbers : %d\nOdd Numbers : %d", countpos, countneg, counteven, countodd);

        return 0;
}

Upvotes: 1

Views: 1987

Answers (3)

Blaze
Blaze

Reputation: 16876

I don't know what the "%di" specifier does. Just use %d.

There is an issue with you loop. Your checks to see whether the number is positive or negative isn't part of it. You just get all the numbers and discard the results, the do one check on the last number that you got (which is 0). Put those jobs in the loop, and preferably put the 1 == scanf() into the loop condition:

while ((1 == scanf("%d", &number)) && (0 != number))
{
    if (number < 0)
        countneg++;
    else
        countpos++;
}

You also aren't doing the even/odd check. Add this to the loop:

if (number % 2 == 0)
    counteven++;
else
    countodd++;

Upvotes: 3

Venkatesh Nandigama
Venkatesh Nandigama

Reputation: 518

mod(%) operator is used the check the remainder

#include<stdio.h>

int main() {

    int positive, negative, even, odd, i, number,num;
    int counteven = 0, countodd = 0, countneg = 0, countpos = 0;

    printf("Hello!\n");                  
    printf("I am a programm that will tell you how many\npositive, negative, even and odd numbers you have inputted.\n");
    printf("You can start inputting numbers. When you're finished input the number 0 to let me know you're done.\n");

    i = scanf("%d",&number);

    while(number!=0 && i==1)
    {
       if (number < 0)
          countneg++;
       else
          countpos++;


       if(number%2==0) 
          counteven++;
       else            
          countodd++;

       i = scanf("%d",&number);

    }

    printf("\nPositive Numbers : %d\nNegative Numbers : %d\nEven Numbers : %d\nOdd Numbers : %d", countpos, countneg, counteven, countodd);

    return 0;
}

Upvotes: -1

Kalana
Kalana

Reputation: 6173

I have found few problems in your code.

  1. what is the purpose of putting i into %d. That code section must be look like this
( 1 == scanf("%d",&number) ) && ( 0 != number )
               ^
  1. Don't assign comparisons into variables. It's lead to wrong outputs.

wrong

    int loop = ( 1 == scanf("%di",&number) ) && ( 0 != number );
    while( loop )
    {
        //code
    }

Right

    while(( 1 == scanf("%d",&number) ) && ( 0 != number ))
    {
        //code
    }
  1. If you want to calculate something continuously, put that calculations in a loop. @Blaze have mentioned that.
    while(( 1 == scanf("%d",&number) ) && ( 0 != number ))
    {
        if (number < 0)
            countneg++;
        else
            countpos++;

        if (number % 2 == 0)
            counteven++;
        else
            countodd++;        
    }
  1. Last thing is you have used lot of unused variable. If you don't use it remove those
 int number, counteven = 0, countodd = 0, countneg = 0, countpos = 0;

Upvotes: 2

Related Questions