❶ c语言怎么输入一个数后并输出这个数的最后一位数
第一种取巧 scanf("%1d%1d%1d%1d", &a,&b, &c, &d) ;就是用abcd分别储存它的位数。
❷ c语言怎么输入一个数后并输出这个数的最后一位数
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
printf("%d",n%10);
}
❸ c语言如何取整和取余
c语言取整和取余:
示例
public class Demo_1 {undefined
public static void main(String args) {undefined
Scanner sc = new Scanner(System.in)
System.out.print("请输入要判断的数字(五位数):")
int num = sc.nextInt()
sc.close()
//截取最后一位数
int a = num % 10
//截取第一位数
int b = num / 10000
//截取第四位数
int c = num % 100 / 10
//截取第二位数
int d = num / 1000 % 10
System.out.println(a + "," + b + "," + c + "," + d)
boolean b1 = (a == b)
boolean b2 = (c == d)
if(b1 && b2) {undefined
System.out.println(num + "是回文数")
}else {undefined
System.out.println(num + "不是回文数")
}
}
}
1.直接赋值给整数变量
int i = 3.5;或i = (int) 3.5。
这样的方法采用的是舍去小数部分。
2、整数除法运算符‘/’取整
‘/’本身就有取整功能(int / int),可是整数除法对负数的取整结果和使用的C编译器有关。
❹ C语言怎样提取一个数的十位个位百位千位
设一个数为n,则在C语言中其个位、十位、百位、千位依次这样计算:n/1%10,n/10%10,n/100%10,n/1000%10
代码如下:
#include<stdio.h>
int main(){
int n = 123456;
int unitPlace = n / 1 % 10;
int tenPlace = n / 10 % 10;
int hundredPlace = n / 100 % 10;
int thousandPlace = n / 1000 % 10;
printf("个位:%d 十位:%d 百位:%d 千位:%d ", unitPlace, tenPlace, hundredPlace, thousandPlace);
getchar();
return 0;
}
运行结果如图:
(4)c语言如何取整数后几位扩展阅读
C语言的运算符包含的范围很广泛,共有34种运算符。C语言把括号、赋值、强制类型转换等都作为运算符处理。从而使C语言的运算类型极其丰富,表达式类型多样化。灵活使用各种运算符可以实现在其它高级语言中难以实现的运算。