Finished first group of assignments
This commit is contained in:
8
programs/rev3dig/info.json
Normal file
8
programs/rev3dig/info.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "課題4",
|
||||
"description": "入力した三桁の正の整数の数字の並びを逆順させた数字を表示するプログラム。",
|
||||
"output": {
|
||||
"type": "screenshot",
|
||||
"content": "./assets/rev3dig.png"
|
||||
}
|
||||
}
|
||||
BIN
programs/rev3dig/main
Executable file
BIN
programs/rev3dig/main
Executable file
Binary file not shown.
26
programs/rev3dig/main.c
Normal file
26
programs/rev3dig/main.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int n;
|
||||
|
||||
printf("Integer n: ");
|
||||
scanf("%d", &n);
|
||||
|
||||
// Throw error when n is not in range of 100 <= n <= 999
|
||||
if (n < 100 || n > 999) {
|
||||
printf("Integer n must be 3 digit number");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int a, b, c, m; // a * 10^2 + b * 10^1 + c * 10^0 = n
|
||||
|
||||
a = n / 100;
|
||||
b = n / 10 % 10;
|
||||
c = n % 10;
|
||||
|
||||
m = c * 100 + b * 10 + a; // m is reversed n
|
||||
|
||||
printf("Reversed integer is %d.", m);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user