Odd/Even counter.

This is a discussion on Odd/Even counter. within the C++ Programming forums, part of the General Programming Boards category; I have written a program to count the number of even and odd numbers entered from a list; but currently ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    6

    Odd/Even counter.

    I have written a program to count the number of even and odd numbers entered from a list; but currently it wont work correctly, think its my modulus operators in the wrong place, but not 100%. Pretty knew to this.


    /* Program to count the no of even and odd numbers*/

    #include <stdio.h>
    void main( )

    {
    int a[100], n, count_odd=0, count_evn=0,I;

    printf("Enter numbers in array");
    scanf("%d",&n);
    printf("Enter the integars, seperated by a space");
    for (I=0;I < n;I++);
    scanf("%d",&a[I]);
    for(I=0;I < n;I++)

    {
    if(a[I] % 2 == 0)
    count_evn++;
    else
    count_odd++;

    }
    printf("There are %d negative numbers in the arrayn\n",count_odd);
    printf("There are %d positive numbers in the arrayn",count_evn);
    }

    Any suggestions would be appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Few things first:
    Read the sticky posts, you will learn to use code tags.
    Read the FAQ, void main should only be used in very few very specific cases, use int main.

    As to your code, your problem is probably this line:
    for (I=0;I < n;I++);
    Get rid of that last semi-colon.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,442
    That program will not compile, and that is even excluding the fact that void main should be int main.

    Furthermore, is this supposed to be C or C++?

    Please remember to indent your code properly and post it within [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Ah missed that compiler error...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-01-2009, 08:54 AM
  2. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  3. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 02:17 AM
  4. Counter Heap Sort
    By Achillles in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 12:17 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21