Add student example inputs
This commit is contained in:
45
examples/palindrome/palindrome.mc
Normal file
45
examples/palindrome/palindrome.mc
Normal file
@@ -0,0 +1,45 @@
|
||||
|
||||
int modulo(int n, int k) {
|
||||
return (n - k * (n / k));
|
||||
}
|
||||
|
||||
bool is_palindrome(int original) {
|
||||
int reversed;
|
||||
int remainder;
|
||||
int to_check;
|
||||
|
||||
reversed = 0;
|
||||
to_check = original;
|
||||
|
||||
while (to_check != 0) {
|
||||
remainder = modulo(to_check, 10);
|
||||
reversed = reversed * 10 + remainder;
|
||||
to_check = to_check / 10;
|
||||
}
|
||||
|
||||
if (original == reversed) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int input;
|
||||
|
||||
print("Please enter a number:");
|
||||
input = read_int();
|
||||
print_nl();
|
||||
|
||||
bool result = is_palindrome(input);
|
||||
|
||||
print_int(input);
|
||||
if (result) {
|
||||
print(" is a palindrome.");
|
||||
} else {
|
||||
print(" is not a palindrome.");
|
||||
}
|
||||
print_nl();
|
||||
|
||||
return 0;
|
||||
}
|
1
examples/palindrome/palindrome.stdin.txt
Normal file
1
examples/palindrome/palindrome.stdin.txt
Normal file
@@ -0,0 +1 @@
|
||||
4321234
|
2
examples/palindrome/palindrome.stdout.txt
Normal file
2
examples/palindrome/palindrome.stdout.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Please enter a number:
|
||||
4321234 is a palindrome.
|
Reference in New Issue
Block a user