了解一些使用循环扭转 [英] Understanding number reversing using loop

查看:124
本文介绍了了解一些使用循环扭转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我钻进在C编程这个简单的程序,该程序是简单地扭转由用户任意输入数字,使用whil​​e循环,我不知道它的好张贴这样的问题,但我没有真的知道如何while循环工程

  INT N,反向= 0;   的printf(请输入一个数字,以扭转\\ n);
   scanf函数(%d个,&安培; N);   而(N!= 0)
   {
      反向=反向* 10;
      反向=反转+ N%10;
      N = N / 10;
   }   的printf(输入数字的反向是=%d个\\ N,反向);

我会如此心存感激,如果任何人都可以解释给我


解决方案

 而(条件为真)
{
    // 做东西。
    //通常情况下,但并非总是如此,在东西我们正在做的是一件修改
    //我们正在检查的条件。
}

所以你的情况只要 N 不等于 0 ,你会继续在内容执行循环。如果 0 scanf函数输入()循环会被一起跳过。

在你每次循环你使用整数除法的属性,以使值 N 由10即较小的因素:

  N = 541
N = N / 10 =一十分之五百四十一= 54
N = N / 10 = 54/10 = 5
N = N / 10 = 5/10 = 0

所以 N 最终将是0,循环将退出。

i got into this simple program in c programing, the program is simply reversing any input number by user, using a while loop, i dont know if its okay to post such a question but i didnt realy understood how the while loop works

int n, reverse = 0;

   printf("Enter a number to reverse\n");
   scanf("%d",&n);

   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }

   printf("Reverse of entered number is = %d\n", reverse);

i would be so thankfull if anyone could explain it to me

解决方案

while(condition is true)
{
    // Do stuff.
    // Normally, but not always, the "stuff" we're doing is something that modifies
    // the condition we're checking.
}

So in your case as long as the content of n is not equal to 0, you will continue to execute the loop. If 0 is entered in the scanf() the loop will be skipped altogether.

At each iteration of your loop you're using the property of integer division to make the value of n smaller by a factor of 10. ie:

n = 541
n = n / 10 = 541/10 = 54
n = n / 10 = 54/10  = 5
n = n / 10 = 5/10   = 0 

So n will eventually be 0 and the loop will exit.

这篇关于了解一些使用循环扭转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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