Thread: Even / Odd Counting with Loops

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Angry Even / Odd Counting with Loops

    I can easily figure out how to determine how whether or not a number is even/odd and display that using if-else statements. No problem!

    But as I attempt to complete the same task using a sentinal loop; let alone count them up in and print out a message stating how many of each were entered; I am screwed! I was wondering if anyone could provide some pointers in the right direction. This is what I have now:

    Code:
    #include< stdio.h>
    
    int main()
    {
    int num;
    int oddcount, int evencount;
    
    printf("\nThis program will ask you to enter numbers and determine whether they are even or odd. It will also count how many of each you entered.");
    
    	oddcount=0;
    	evencount=0;
    
    while (num!=999)
    {
    printf("Enter a number: (to stop entering numbers enter 999) ");
    scanf("%d",&num);
    
    if (num%2 != 0)
    
    	printf("\nThe number %d Is Odd.\n",num);
    	oddcount++;
    
    else if (num%2 == 0)
    	
    printf("\nThe number %d Is Even.\n",num);
    	evencount++;
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    You are missing a few braces. And notice how more careful indenting makes it easier to read.
    Code:
    #include< stdio.h>
    
    int main()
    {
        int num;
        int oddcount, evencount;
    
        printf("This program will ask you to enter numbers and determine"
               " whether they are even or odd. It will also count how many"
               " of each you entered.\n");
    
        oddcount = 0;
        evencount = 0;
    
        while (num != 999)
        {
            printf("Enter a number: (to stop entering numbers enter 999) ");
            scanf("%d", &num);
    
            if (num%2 != 0)
            {
                printf("\nThe number %d Is Odd.\n", num);
                oddcount++;
            }
            else
            {
                printf("\nThe number %d Is Even.\n", num);
                evencount++;
            }
        }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    One last thing?

    I appreciate the help, those braces are killing me! The only thing is that I can't get it to complile. I was using my flowchart to create the counting statments requried at the end, and I am getting like 10 compiler errors. Does anyone know how to help me this one time? Please!

    Code:
    #include< stdio.h>
    
    int main()
    {
    	int num;
    	int oddcount;
    	int evencount;
    	
    
    	printf("\nThis program will ask you to enter numbers and determine  whether they are even or odd. It will also count how many of each you entered.\n");
    
    	oddcount=0;
    	evencount=0;
    
    	while (num!=999)
    	{
    	printf("Enter a number: (to stop entering numbers enter 999) ");
    	scanf("%d",&num);
    
    		if (num%2 != 0)
    		{
    			printf("\nThe number %d Is Odd.\n",num);
    			oddcount++;
    		}
    		else
    		{
    			printf("\nThe number %d Is Even.\n",num);
    			evencount++;
    		}
    	}
    	printf("\nThe number of odd numbers entered was %d, oddcount);
    	printf("\nThe number of even numbers entered was %d, evencount);
    		
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Hi, I think you forgot to put " in your last printf statements

    Code:
    printf("\nThe number of odd numbers entered was &#37;d",oddcount);
    printf("\nThe number of even numbers entered was %d" , evencount);

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    Not only that, you've put a space before stdio.h.

    Instead of
    Code:
    #include< stdio.h>
    try
    Code:
    #include <stdio.h>
    That should remove all errors except the printf() ones, which nynicue has already explained.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Stupid Compiler

    Thanks for the feedback, stupid errors! It was so early in the morning. I am trying to compile and everything is good. But when I execute it, this is what I get:

    C:\Documents and Settings\Christopher\Desktop\C Programming\Midterm\Midterm two.c(12): error #2092: Missing identifier.
    C:\Documents and Settings\Christopher\Desktop\C Programming\Midterm\Midterm two.c(12): error #2001: Syntax error: expected ';' but found 'int'.
    C:\Documents and Settings\Christopher\Desktop\C Programming\Midterm\Midterm two.c(29): error #2157: Unrecognized statement.
    C:\Documents and Settings\Christopher\Desktop\C Programming\Midterm\Midterm two.c(29): error #2001: Syntax error: expected ';' but found 'if'.
    C:\Documents and Settings\Christopher\Desktop\C Programming\Midterm\Midterm two.c(35): warning #2096: Missing return value.
    C:\Documents and Settings\Christopher\Desktop\C Programming\Midterm\Midterm two.c(35): error #2001: Syntax error: expected '}' but found 'end of input'.
    *** Error code: 1 ***

    This is screwing me big time, so much for the career as a programmer! Help once again please!

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If this is what you get, you didn't compile correctly. You're missing braces again.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Thanks

    Thank you for all the help! I had another file in my compiler that I needed to exclude. You guys really know your stuff!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum all odd array elements?
    By Marth_01 in forum C Programming
    Replies: 9
    Last Post: 11-04-2008, 10:47 PM
  2. Counting with loops up, down, up, ... ets
    By at0m1sk in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2007, 08:01 PM
  3. Counting in loops?
    By chuy in forum C Programming
    Replies: 4
    Last Post: 10-17-2005, 02:56 AM
  4. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM