❶ 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語言的運算類型極其豐富,表達式類型多樣化。靈活使用各種運算符可以實現在其它高級語言中難以實現的運算。