本文介绍了2位数输入。和奇,产品甚至的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是新手,我们有一个 ssignment来制作一个接受10 int的程序并打印出所有奇数和产品的总和甚至没有。进入。每次输入2位数字和2位数字时,我的问题输出错误都会发生。这是我朋友和我做的代码。 注意:我们使用turbo c #include < stdio.h > ; #include < ; conio.h > void main() { int a [ 50 ],i,甚至[ 50 ],奇数[ 50 ],sum = 0 ,prod = 1 ; clrscr(); printf( 输入10个整数:); for (i = 0 ; i< 10; i ++) { scanf( %d,& a [i]); } for (i = 0 ; i< 10 ; i ++) { if (a [i]%2 == 0 ) { even [i] = a [i]; prod = prod * even [i]; } } for (i = 0 ; i< 10; i ++) { if (a [i]%2!= 0 ) { odd [i] = a [i]; sum = sum + odd [i]; } } printf( \ nSum of odd number is%d \\\,总和); printf( \ n产品偶数是%d \ n,prod ); getch(); } 解决方案 我运行它,它的工作原理。 我用零运行它:它运作 我用两位数运行它 - 它的工作原理。 那么问题是什么?我能看到的唯一麻烦是你的输出有点奇怪:奇数和偶数的数组集合是不必要的,因为你只设置你遇到它们的索引就不好了。 顺便说一下:你不需要三个循环:你可以使用两个,甚至一个。 void main () {i, value ,sum = 0 ,prod = 1 ; clrscr(); printf( 输入10个整数:); for (i = 0 ; i< 10; i ++) { scanf( %d,& value); if ( value % 2 == 0 ) { prod = prod * value ; } else { sum = sum + value; } } printf( \ nSum of odd number is%d \\\,总和); printf( \ n产品偶数是%d \ n,prod ); getch(); } 第一个问题是,为什么要将数字存储在数组中?这是一个要求吗?如果没有,为什么不在输入时简单地判断数字是偶数还是奇数,并根据计算得到的总和或产品的中间结果。 另一件事如果使用数组,则偶数数组的索引器与a的索引器不同。例如,如果a为1,2,3,4,那么a的索引器将为0到3,而even的索引器将为0到1(如果按顺序填充)。 否则看起来不错,除非产品增长太多,因此在格式化 printf时需要一个长整数以及 ld Hi, I'm new and we've got an assignment to make a program that will accept 10 int and prints the sum of all odd nos and product of even nos. entered. My problem output error occurs everytime I enter a 2 digit number and 2 digit number with zero. Here's the code my friend and I did. Note: we use turbo c#include<stdio.h>#include<conio.h>void main(){ int a[50],i,even[50],odd[50],sum=0,prod=1; clrscr(); printf("Enter 10 integers:"); for(i=0;i<10;i++) { scanf("%d",&a[i]); } for(i=0;i<10;i++) { if(a[i]%2==0) { even[i]=a[i]; prod=prod*even[i]; } } for(i=0;i<10;i++) { if(a[i]%2!=0) { odd[i]=a[i]; sum=sum+odd[i]; } } printf("\nSum of odd numbers is %d\n",sum); printf("\nproduct of even numbers is %d\n",prod); getch();} 解决方案 I run it, it works.I run it with zeros: it worksI run it with double digit numbers - it works.So what is the problem? The only hassle I can see is that your output are a bit odd: your array collections of odd and even numbers are unnecessary and not good since you only set the indexes where you meet them.By the way: you don't need three loops: You can use two, or even one.void main(){ i,value,sum=0,prod=1; clrscr(); printf("Enter 10 integers:"); for(i=0;i<10;i++) { scanf("%d",&value); if(value % 2 == 0) { prod=prod*value; } else { sum=sum+value; } } printf("\nSum of odd numbers is %d\n",sum); printf("\nproduct of even numbers is %d\n",prod); getch();}The first question is, why do you store the numbers in an array? Is that a requirement? If not, why not simply decide if the number is even or odd when the input is made and based on that calculate intermediate result for either sum or product.Another thing is that if you use arrays the probability is that the indexer for even array is different from indexer of a. For example if a would be 1,2,3,4 then indexer for a would be 0 to 3 while the indexer for even would be 0 to 1 (if filled in order).Otherwise looks good unless the product grows too much so that a long integer would be required along with ld in formatting of printf. 这篇关于2位数输入。和奇,产品甚至的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 06:59