Reputation: 19
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
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
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
Reputation: 6173
I have found few problems in your code.
%d
. That code section must be look like this( 1 == scanf("%d",&number) ) && ( 0 != number )
^
wrong
int loop = ( 1 == scanf("%di",&number) ) && ( 0 != number );
while( loop )
{
//code
}
Right
while(( 1 == scanf("%d",&number) ) && ( 0 != number ))
{
//code
}
while(( 1 == scanf("%d",&number) ) && ( 0 != number ))
{
if (number < 0)
countneg++;
else
countpos++;
if (number % 2 == 0)
counteven++;
else
countodd++;
}
int number, counteven = 0, countodd = 0, countneg = 0, countpos = 0;
Upvotes: 2