#include <stdio.h>
main()
{
int choice, no;
printf("1. Show sum of odd/even number to N term\n");
printf("2. Smallest, largest and average of the supplied numbers\n");
printf("3. Terminate the programs\n\n");
printf("Enter your choice[1|2|3]: ");
scanf("%d", &choice);
if (choice == 1)
{
int i , no , sum = 0, j, sum2 = 0;
printf("\nEnter any number: ");
scanf("%d", &no);
for (i = 2; i <= no; i = i + 2)
{
sum = sum + i;
}
printf("\nSum of all even number between 1 to %d = %d\n", no, sum);
for (j = 1; j <= no; j = j + 2)
{
sum2 = sum2 + j;
}
printf("Sum of all odd number between 1 to %d = %d\n", no, sum2);
}
else if(choice == 2)
{
float max, min, avg, num,counter=0, sum = 1;
printf("\nPlease enter all the number you want![0 to end]: ");
scanf("%f", &num);
max = min = num;
while (num != 0)
{
printf("Please enter all the number you want![0 to end]: ");
scanf("%f", &num);
if (max < num && num > 0)
max = num;
else if (min > num && num > 0)
min = num;
sum = sum + num;
counter++;
}
printf("\nThe smallest and largest of entered numbers are %.2f and %.2f respectively.\n", min, max);
avg = sum/counter;
printf("The sum of entered number is %.2f\n", sum);
printf("The average of entered number is %.2f\n", avg);
}
}
我的问题是,当我选择第2号时,它会显示最小和最大的数字,但总和显示错误时,我输入200像大数!但是,当我输入小值时它工作正常!?我的计算在输入大数字时显示错误?
图片包含
为什么'sum'初始化为1而不是0?为什么在处理第一个数字之前扫描第二个数字? –
改变总和= 0,但我不明白...“为什么在处理第一个数字之前扫描第二个数字?”......我不是电脑学生......但我必须参加c班。 – Shaik
请勿张贴文字图片。 –