为什么输出不是coreect而不是超过2位? [英] Why output not coreect for no of having more than 2 digits?

查看:149
本文介绍了为什么输出不是coreect而不是超过2位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序反转数字没有给出正确的输出只有两位数但是大于两位数没有输出不正确为什么?



我是什么尝试过:



program to reverse digits of no its giving correct output for two digits only but greater than two digit no output is not correct why?

What I have tried:

#include<stdio.h>
#include<math.h>

int main()
{
    int n,i,r,x=0;

    printf("no of greater than or = two digits/n");
    scanf("%d",&n);

    for(i=1;;)
    {
        if((n/pow(10,i))>=1)
        {
            i++;
        }
        else
        {
            break;
        }
    }

    printf("%d",i);

    for(r=1;r<=i;r++)
    {
        x += ((n % (int)(pow(10, r))) / pow(10, r - 1)) * pow(10, i - r);
    }

    printf("reverse of your number=%d",x);
}

推荐答案

这是一些奇怪的代码。如果你想一想,有一个更简单的解决方案。

但这是你的功课,所以我不会给你任何代码!



想一想:当你反转一个数字时,先打印最低位数,然后打印下一个最低位数,依此类推:1234变为4321.并且获得整数的最低位数很容易:数字模数10将给出你那样: 1234%10 - > 4

所以不要使用 pow 只需设置一个do ... while循环提取底部数字,打印它,并将原始数字除以10。然后你用这个数字回到循环周围。

That's some odd code. There is a much, much simpler solution if you think about it.
But this is your homework, so I'll give you no code!

Think about it: when you reverse a number, you print the lowest digit first, then the next lowest, and so on: 1234 becomes 4321. And getting the lowest digit of an integer is easy: the number modulus 10 will give you that: 1234 % 10 --> 4
So instead of using pow just set up a do...while loop extract the bottom digit, print it, and divide the original number by ten. You then use that number to go back around the loop.
1234 % 10 --> 4
1234 / 10 --> 123

你什么时候退出循环?剩下的数字是零。



简单!

When do you exit the loop? When the number remaining is zero.

Simples!


当你不明白你的代码在做什么或为什么它它做了什么,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于为什么输出不是coreect而不是超过2位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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