编写C程序计算数值根

编写C程序计算数值根,c,C,x的数值根计算如下: a) 计算所有x(十进制)数字的和y; b) 如果y大于10,则将x设置为y并转至步骤a)。否则,y是x的数值根。 因此,10、202和875的数字根分别为1、4和2 这是我的密码: #include <stdio.h> int main(void) { int x, y, index, found; found = 0; scanf("%d", &x); if (x < 0) { p

x的数值根计算如下: a) 计算所有x(十进制)数字的和y; b) 如果y大于10,则将x设置为y并转至步骤a)。否则,y是x的数值根。 因此,10、202和875的数字根分别为1、4和2

这是我的密码:

#include <stdio.h>

int main(void)
{

    int x, y, index, found;

    found = 0;
    scanf("%d", &x);

    if (x < 0)
    {
        printf("The input number must be nonnegative.\n");
    }
    else
    {
        y = 0;
        index = x % 10;

        while (found != 1)
        {
            while (x > 10)
            {
                y = y + index;
                x = (x - index) / 10;
                index = x % 10;
            }

            y = y + index;

            if (y < 10)
            {
                printf("%d\n", y);
                found = 1;
            }
            else
            {
                x = y;
            }
        }
    }

    return 0;
}
#包括
内部主(空)
{
int x,y,索引,found;
发现=0;
scanf(“%d”和&x);
if(x<0)
{
printf(“输入的数字必须是非负的。\n”);
}
其他的
{
y=0;
指数=x%10;
while(已找到!=1)
{
而(x>10)
{
y=y+指数;
x=(x-指数)/10;
指数=x%10;
}
y=y+指数;
如果(y<10)
{
printf(“%d\n”,y);
发现=1;
}
其他的
{
x=y;
}
}
}
返回0;
}
我的输出总是像“-2147483623”这样的数字。
任何帮助都将不胜感激。

我认为您的问题在于:

if(y < 10){
    printf("%d\n",y);
    found = 1;
  }
  else{
    x = y;
  }
在您的整个程序中,您似乎没有减少或重置
y
的值,这就是为什么一旦输入else块,就无法输入if块,因为
y
将增加,并且在溢出发生之前总是大于10,这就是为什么
y
在最后为负值


PS:始终使用调试器或
printf
语句,它将帮助您检测此类问题

使用一个简单的函数可以大大简化您的解决方案。 使用Ada语言查看以下解决方案:

with Ada.Integer_Text_IO; use Ada.Integer_Text_Io;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   Inpt_Value : Natural;
   Root       : Natural := 0;
   function Find_Root(X : Natural) return Natural is
      Value : Natural := X;
      Root  : Natural := 0;
   begin
      while Value > 0 loop
         Root  := Root + (Value mod 10);
         Value := Value / 10;
      end loop;
      return Root;
   end Find_Root;

begin
   Put("Enter a non-negative integer: ");
   Get(Item => Inpt_Value);
   Root := Find_Root(Inpt_Value);
   while Root > 10 loop
      Root := Find_Root(Root);
   end loop;
   Put_Line(Root'Image);
end Main;

使用调试程序注意
x=(x-index)/10可以安全地替换为
x=x/10使用调试器是一个好主意。另一个选项是使用
printf
在程序的各个点显示
x
y
索引的值。我运行的代码是10->0、202->4和875->-2147483616。如上所述,学习如何使用调试器是学习编程的一部分。即-x=y之后添加y=0还需要在该部分中添加
index=x%10
。因此,
y
index
都需要正确地重新初始化。我想评论者们通常都知道这一点,但尽量避免把答案透露给一个应该学习一些调试的OP。但是,唉……我明白了!谢谢!
with Ada.Integer_Text_IO; use Ada.Integer_Text_Io;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   Inpt_Value : Natural;
   Root       : Natural := 0;
   function Find_Root(X : Natural) return Natural is
      Value : Natural := X;
      Root  : Natural := 0;
   begin
      while Value > 0 loop
         Root  := Root + (Value mod 10);
         Value := Value / 10;
      end loop;
      return Root;
   end Find_Root;

begin
   Put("Enter a non-negative integer: ");
   Get(Item => Inpt_Value);
   Root := Find_Root(Inpt_Value);
   while Root > 10 loop
      Root := Find_Root(Root);
   end loop;
   Put_Line(Root'Image);
end Main;