如何计算数字 [英] How to count numbers

查看:89
本文介绍了如何计算数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个用于计算从1到X整数的奇数和偶数的代码,我的程序显示了结果。但是我需要在我的代码中添加一个如何计算列表中奇数/偶数的数量。



例如:示例输出



1 2 3 4 5 6 7 8 9 10

偶数是:2

偶数是:4

偶数是:6

偶数是:8

偶数是:10

有5个偶数< - 需要在我的代码中添加它



奇数是:1

奇数是: 3

奇数是:5

奇数是:7

奇数是:9

有5个奇数



我尝试过:



I've made a code for calculating odd and even number from 1 to X integer and my program showing the result. But I need to add one thing in my code that how to calculate how many odd/even numbers are in the list.

As Example:Sample Output

1 2 3 4 5 6 7 8 9 10
The Even number are: 2
The Even number are: 4
The Even number are: 6
The Even number are: 8
The Even number are: 10
There are 5 even numbers <- need to add it in my code

The odd number are: 1
The odd number are: 3
The odd number are: 5
The odd number are: 7
The odd number are: 9
There are 5 odd numbers

What I have tried:

/*My trying code is below:*/

#include <stdio.h>

int main() 
{
  int i, j; 
  int X=10;

  for(i=1;i<=X;i++)
  {
  printf("%d\t", i);
  }
  printf("\n");
 for(i=1;i<=X;i++)
 {
    if((i%2)==0)
 {
  printf(" The Even number are: %d\n", i);
 }
    
}

 printf("\n");
 
 for(i=1;i<=X;i++)
  {
  if((i%2)!=0)
  {
  printf(" The odd number are: %d\n", i);
    }
     }
  return 0;
}

推荐答案

Quote:

但是我需要在我的代码中添加一个如何计算列表中奇数/偶数的数量。

But I need to add one thing in my code that how to calculate how many odd/even numbers are in the list.



如何编写正确的代码来列出奇数和偶数并且无法计算它们?



学会正确地缩进你的代码,它显示它的结构,它有助于阅读和理解。它还有助于发现结构错误。


How can you write correct code to list odd and even numbers and be unable to count them?

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

/*My trying code is below:*/

#include <stdio.h>

int main()
{
  int i, j;
  int X=10;

  for(i=1;i<=X;i++)
  {
    printf("%d\t", i);
  }
  printf("\n");
  for(i=1;i<=X;i++)
  {
    if((i%2)==0)
    {
      printf(" The Even number are: %d\n", i);
    }

  }

  printf("\n");

  for(i=1;i<=X;i++)
  {
    if((i%2)!=0)
    {
      printf(" The odd number are: %d\n", i);
    }
  }
  return 0;
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]


这篇关于如何计算数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆