C 如何从3位数的整数中提取一个位数?

C 如何从3位数的整数中提取一个位数?,c,int,C,Int,这不是家庭作业问题,我只是好奇。如果我有一个计算3位数的程序,比如说123,我怎么能只得到“1”?我试着在最后打印一条消息,上面写着“(第一个数字)告诉你……和(最后两个数字)告诉你……”,但我不知道如何保存或获取这一个数字。有什么想法吗?除了使用数组之外,这是一种更简单的方法吗?谢谢。您可以使用整数除以100: #include <stdio.h> int main() { printf( "%d\n", 123/100 ) ; return 0 ; } 如果可以从最

这不是家庭作业问题,我只是好奇。如果我有一个计算3位数的程序,比如说123,我怎么能只得到“1”?我试着在最后打印一条消息,上面写着“(第一个数字)告诉你……和(最后两个数字)告诉你……”,但我不知道如何保存或获取这一个数字。有什么想法吗?除了使用数组之外,这是一种更简单的方法吗?谢谢。

您可以使用整数除以
100

#include <stdio.h>

int main()
{
  printf( "%d\n", 123/100 ) ;

  return 0 ;
}
如果可以从最后一位到第一位按相反顺序显示数字,则此方法不需要任何额外存储,如果不需要,则可以将结果存储在数组中。

  • 获取数字的长度
  • 迭代并获取每个数字
以下是一个示例:

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

int main ()
{
  int n = 123, i; char buffer [33];

  int len = n==0 ? 1 : floor(log10l(abs(n)))+1;

  for(i=n;len--; i=(int)(i/10)) buffer[len] = (i%10);

  printf("%d", buffer[0]);   // First Digit
  printf("%d", buffer[1]);   // Second Digit
  printf("%d", buffer[2]);   // Third Digit... so on

  return 0;
}
#包括
#包括
int main()
{
int n=123,i;字符缓冲区[33];
内部长度=n==0?1:地板(log10l(abs(n))+1;
对于(i=n;len--;i=(int)(i/10))缓冲区[len]=(i%10);
printf(“%d”,缓冲区[0]);//第一位数字
printf(“%d”,缓冲区[1]);//第二位数字
printf(“%d”,缓冲区[2]);//第三位数字…依此类推
返回0;
}

如果你想用一种简单的方法来做,我的意思是,如果你只想用一个数字来编程,例如123,那么Shafik的第一个例子就足够了

如果您想从末尾取出数字,那么第二个Shafik示例就足够了

欢迎提出建议,如果有人看到改进,谢谢:)

从一开始就把数字去掉怎么样,这是我对你问题的不同看法,我从一开始就把数字去掉如下:

#include<stdio.h>
int main()
{
  int di , i , num , pow = 1;
  setbuf ( stdout , NULL);
  printf ("enter the number of digits of the number\n");// specify at run time what will be the number of digits of the array.
  scanf ( "%d" , &di);
  int a[di];

  printf ("enter the %d digit number:\n",di);// 
  scanf ( "%d" , &num);//One thing is to be noted here that user can enter more digits than specified above , It is up to user to enter the specified digits , you can improve this program by making a check whether user enters the specified digits or not , better do it as exercise.
  while( di > 1 )
    {
    pow = pow * 10;
    di--;
    }

  i = 0;
  while ( num > 0)
    {
      a[i]=num/pow;
      num=num%pow;
      pow=pow/10;
      i++;
    }
  printf("the digits from the beginning are :\n"); 
  for(int j = 0 ; j < i ; j++)
    printf("%d\n",a[j]);
  return 0;
}
#包括
int main()
{
int di,i,num,pow=1;
setbuf(标准输出,空);
printf(“输入数字的位数”);//在运行时指定数组的位数。
scanf(“%d”&di);
int a[di];
printf(“输入%d位数字:\n”,di);//
scanf(“%d”,&num);//这里需要注意的一点是,用户可以输入比上面指定的更多的数字,由用户输入指定的数字,您可以通过检查用户是否输入指定的数字来改进此程序,最好作为练习来做。
而(di>1)
{
功率=功率*10;
迪--;
}
i=0;
while(num>0)
{
a[i]=num/pow;
num=num%功率;
功率=功率/10;
i++;
}
printf(“开头的数字是:\n”);
对于(int j=0;j
重要信息——当使用数组存储数字时,如果用户输入的数字多于指定的数字,则会打印额外的数字作为数组的第一个元素,正如我所说的,如果需要,您可以进一步改进此程序,并检查用户输入的数字,祝您好运:)


注意——这只是看待问题的不同方式,两种解决方案最终会产生相同的结果。我只是想这么说。

还需要注意一点,VLA可以通过修订C标准即C99来实现。谢谢您的帮助。
#include<stdio.h>
int main()
{
  int di , i , num , pow = 1;
  setbuf ( stdout , NULL);
  printf ("enter the number of digits of the number\n");// specify at run time what will be the number of digits of the array.
  scanf ( "%d" , &di);
  int a[di];

  printf ("enter the %d digit number:\n",di);// 
  scanf ( "%d" , &num);//One thing is to be noted here that user can enter more digits than specified above , It is up to user to enter the specified digits , you can improve this program by making a check whether user enters the specified digits or not , better do it as exercise.
  while( di > 1 )
    {
    pow = pow * 10;
    di--;
    }

  i = 0;
  while ( num > 0)
    {
      a[i]=num/pow;
      num=num%pow;
      pow=pow/10;
      i++;
    }
  printf("the digits from the beginning are :\n"); 
  for(int j = 0 ; j < i ; j++)
    printf("%d\n",a[j]);
  return 0;
}