程序读取4位整数并输出数字之和

时间:2012-09-15 19:10:48

标签: c scanf

我在编程的入门课程中遇到了一项任务。我们正在使用C.作业是:

  

编写一个程序,要求用户输入四位INTEGER,并输出整数中四位数的总和。

我尝试了不同的东西,但是当我测试程序时它不起作用。当我编译它时,我没有得到任何错误,所以当我继续测试它时,它不会给我正确的答案。我尝试了不同的东西,它只是给了我一个不同的答案,但它仍然是错误的。

我得到了这个来测试程序:当被要求输入四位数整数并且答案必须是10时输入1234.

#include <stdio.h>

int
main(void)
{
    int sum,digit1,digit2,digit3,digit4;
    printf("enter four digit integer:\n");
    scanf("%d", &digit1,&digit2,&digit3,&digit4);

    digit1=(digit1%1000)/10;
    digit2=(digit2%1000)/10;
    digit3=(digit3%1000)/10;
    digit4=(digit4%1000)/10;

    /*calculation*/
    sum=digit1+digit2+digit3+digit4;

    printf ("answer is: %d\n",sum);
    return (0);
}

8 个答案:

答案 0 :(得分:3)

一个问题是这个

scanf("%d%d%d%d", &digit1,&digit2,&digit3,&digit4);

否则你只读一位数

请参阅Wikipedia

但是......最好这样做

scanf("%d", &number);
sum  = 0;
do 
{
    //get a digit 
    sum += number % 10;
    //remove the digit  
    number /= 10;
} while (number > 0);
print ("%d", sum);

答案 1 :(得分:3)

当您阅读用户输入时,您将其读作一个数字:

printf("enter four digit integer:\n");
scanf("%d", &digit1,&digit2,&digit3,&digit4);

应该是:

printf("enter four digit integer:\n");
scanf("%d", &number);

接下来,您需要使用%/运算符从读取的数字中提取单个数字,最后添加它们。

答案 2 :(得分:1)

问题在于您的scanf,它要求您传递的每个参数都有格式化程序%d

所以不要这样:

scanf("%d", &digit1,&digit2,&digit3,&digit4);

你应该有这个(如果分别输入数字,它们之间有空格):

scanf("%d", &digit1);
scanf("%d", &digit2);
scanf("%d", &digit3);
scanf("%d", &digit4);

这将在四个不同的int中读取。如果你想读入一个整数,然后提取各个数字,那么这就足够了:

scanf("%d", &digit1);

int scanf ( const char * format, ... );

  

返回成功转换和分配的字段数

From MSDN

答案 3 :(得分:0)

#include <stdio.h>

main()
{
    int n, rem;
    int sum=0;

    printf("enter a no.");
    scanf("%d",&n);

    while(n!=0)
    {
        rem = n%10;
        sum += rem;
        n=n/10;
    }

    printf("sum is=%d\n",sum);
}

答案 4 :(得分:0)

首先,不需要将四个整数变量输入到find sum of digits of a number

需要以这种方式完成

publish_actions

答案 5 :(得分:0)

以下是基于我的技术。它通过数字的数学分解完成,然后打印出单个结果。这里:

#include<stdio.h>
#include<stdlib.h>

int main ()

{   //Code Begins
//Variable Initialization
int digit, dig1, dig2, dig3, dig4,digi, sum;
//Acquiring input
printf("Enter 4-Digit Number:\n");
scanf("%d", &digit);
//Number resolution technique
//Digit at 1000th place
dig1 = (digit/1000);
printf ("Digit at 1000th place is: %d \n", dig1);
//Digit at 100th place
dig2 = (digit-(dig1*1000))/100;
printf ("Digit at 100th place is: %d \n", dig2);
//Digit at 10th place
digi = ((digit/100)*100);
dig3 = (digit-digi)/10;
printf ("Digit at 10th place is: %d \n", dig3);
//Digit at unit place
dig4 = ((digit-((digit/10)*10)));
printf ("Digit at unit place is: %d \n", dig4 );
//Sum of Digits
sum = dig1+dig2+dig3+dig4;
printf ("Sum of digits in the number is: %d \n\n", sum);
//Self Applaud
printf ("Isn't it Awesome ! A big hand of applaud for my technique !!! \n\n\n");
system ("Pause");
return 0;
}

答案 6 :(得分:-1)

当用户仅输入4个数字时,

输出正确。 当用户添加超过4个时,则输出不同......

#include<stdio.h>

void main() {

int num,d1,d2,d3,d4,sum;

printf("\nEnter the four digits you wish to add: ");

scanf("%d",&num);

d1=num%10;
num=num/10;
d2=num%10;
num=num/10;
d3=num%10;
d4=num/10;

sum=d1+d2+d3+d4;

printf("\n*The sum of four digits is: %d*",sum);

}

答案 7 :(得分:-1)

#include <stdio.h>
#include <stdlib.h>
//writing program to read four digit integer and print the sum of its digits
//by nirav adatiya
main()
{
    int num,a,b,c,d,result;
    printf("enter four digit number for sum ");
    scanf("%d",&num);
    a=num%10;
    b=num%100/10;
    c=num%1000/100;
    d=num%10000/1000;
    result=a+b+c+d;
    printf("so\na=%d\nb=%d\nc=%d\nd=%d",a,b,c,d);
    printf("\nresult is%d ",result);
}