Oddly Even
2 minutes to read
We are given the following website to write a coding solution:
Problem
We need to write a program that is able to take a number as input and print either "odd"
or "even"
if the number is odd or even. For instance:
1
->"odd"
2
->"even"
Solution
Since we are allowed to use C, C++, Python and Rust, let’s write a solution on every language, just because.
C
#include <stdio.h>
int main() {
// take in the number
int n;
scanf("%d", &n);
// calculate answer
char* answer = n % 2 ? "odd" : "even";
// print answer
puts(answer);
return 0;
}
C++
#include <iostream>
int main() {
// take in the number
int n;
std::cin >> n;
// calculate answer
std::string answer = n % 2 ? "odd" : "even";
// print answer
std::cout << answer << std::endl;
return 0;
}
Python
# take in the number
n = int(input())
# calculate answer
answer = 'odd' if n % 2 else 'even'
# print answer
print(answer)
Rust
use std::io;
fn main() {
// take in the number
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
let n: i32 = input.trim().parse().expect("Please enter a valid number");
// calculate answer
let answer = if n % 2 == 1 { "odd" } else { "even" };
// print answer
println!("{}", answer);
}
Flag
Using any of these programs, we are able to solve the chalenge and capture the flag:
HTB{n0t_3v3n_cl0s3!}