简体   繁体   中英

Programs counting even and odd numbers

I'm self-studying C and I'm trying to make 2 programs for exercise:

  1. the first one takes a number and check if it is even or odd; This is what I came up with for the first one:

     #include <stdio.h> int main(){ int n; printf("Enter a number that you want to check: "); scanf("%d",&n); if((n%2)==0) printf("%d is even.",n); else printf("%d is odd.",n); return 0; } 
  2. the second one should take n numbers as input and count the number of even numbers, odd numbers, and zeros among the numbers that were entered. The output should be the number of even numbers, odd numbers, and zeros.

I would like to ask how to implement the loop in this case: how can I set an EOF value if every integer is acceptable (and so I cannot, say, put 0 to end)? Can you show me how to efficiently build this short code?

#include <stdio.h>
int main(void) {
    int n, nEven=0, nOdd=0, nZero=0;

    for (;;) {
        printf("\nEnter a number that you want to check: ");
        //Pressing any non-numeric character will break;
        if (scanf("%d", &n) != 1) break;

        if (n == 0) {
            nZero++;
        }
        else {
            if (n % 2) {
                nEven++;
            }
            else {
                nOdd++;
            }
        }
    }

    printf("There were %d even, %d odd, and %d zero values.", nEven, nOdd, nZero);
    return 0;
}

在此处输入图片说明

Check the return value of scanf()

1, 1 field was filled (n).
0, 0 fields filled, likely somehtlig like "abc" was entered for a number.
EOF, End-of-file encountered (or rarely IO error).

#include <stdio.h>
int main(void) {
  int n;

  for (;;) {  
    printf("Enter a number that you want to check: ");
    if (scanf("%d",&n) != 1) break;
    if((n%2)==0)      
      printf("%d is even.",n);
    else
      printf("%d is odd.",n);
  }
  return 0;
}

Or read the count of numbers to subsequently read:

int main(void) {
  int n;

  printf("Enter the count of numbers that you want to check: ");
  if (scanf("%d",&n) != 1) Handle_Error();

  while (n > 0) {  
    n--;
    printf("Enter a number that you want to check: ");
    int i;
    if (scanf("%d",&i) != 1) break;
    if((i%2)==0) {
      if (i == 0) printf("%d is zero.\n",i);
      else printf("%d is even and not 0.\n",i);
    }
    else
      printf("%d is odd.\n",i);
  }
  return 0;
}

hey look at this

#include<stdio.h>
#include<conio.h>

void main()

{    

       int nodd,neven,num,digit ; 
       clrscr();



       printf("Count number of odd and even digits in a given integer number ");

       scanf("%d",&num);

                nodd = neven =0; /* count of odd and even digits */

                while (num> 0)

                    {

                        digit = num % 10; /* separate LS digit from number */

                        if (digit % 2 == 1)

                           nodd++;

                        else neven++;

                               num /= 10; /* remove LS digit from num */

                    }

                        printf("Odd digits : %d Even digits: %d\n", nodd, neven);

                        getch();

}

You can do something like this:

#include <stdio.h>
int main(){
    int n,evenN=0,oddN=0,zeros=0;
    char key;
    do{
        clrscr();
        printf("Enter a number that you want to check: ");
        scanf("%d",&n);
        if(n==0){
            printf("%d is zero.",n);
            zeros++;
        }
        else if((n%2)==0){      
            printf("%d is even.",n);
            evenN++; 
        }
        else{
            printf("%d is odd.",n);
            oddN++;
        }
        puts("Press ENTER to enter another number. ESC to exit");
        do{
        key = getch();
        }while(key!=13 || key!=27) //13 is the ascii code fore enter key, and 27 is for escape key

    }while(key!=27) 
    clrscr();
    printf("Total even numbers: %d",evenN);
    printf("Total odd numbers: %d",oddN);
    printf("Total odd numbers: %d",zeros);
    return 0;
}

This program ask for a number, evaluate the number and then ask to continue for another number or exit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM