C语言 计算偶数和奇数的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26951288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:33:55  来源:igfitidea点击:

Programs counting even and odd numbers

calgorithm

提问by user62029

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

我正在自学 C 并且我正在尝试制作 2 个锻炼程序:

  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 nnumbers 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.

  1. 第一个取一个数字并检查它是偶数还是奇数;这是我想出的第一个:

    #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. 第二个应该以n个数字作为输入,并计算输入的数字中偶数、奇数和零的数量。输出应该是偶数、奇数和零的个数。

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?

我想问一下在这种情况下如何实现循环:如果每个整数都可以接受,我如何设置 EOF 值(所以我不能说,把 0 放在结尾)?你能告诉我如何有效地构建这个短代码吗?

回答by bigtlb

#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;
}

enter image description here

在此处输入图片说明

回答by chux - Reinstate Monica

Check the return value of scanf()

检查返回值 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).

1, 1 个字段已填充 (n)。
0, 0 个字段已填充,可能是为某个数字输入了诸如“abc”之类的东西。
EOF,遇到文件结束(或很少发生 IO 错误)。

#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;
}

回答by Mohit Saud

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();

}

回答by smartdan

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.

该程序要求输入一个数字,评估该数字,然后要求继续输入另一个数字或退出。