Finished first group of assignments
This commit is contained in:
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