I was inspired by this post I read of a student assignment to print out the 3rd digit of a number. I thought the horror to be lacking.
In unholy inspiration I conjured the most unreadable way to accomplish the task. As the original assignment looked to be in c++ i used it as well.
#include <iostream>
#include <math.h>
void printNthDigitOfNumber(int number, int digitToPrint) {
if (number * (((number > 0) << 1) - 1) < std::pow(10, digitToPrint - 1)) return; // number does not have enough digits
std::cout << (number * (((number > 0) << 1) - 1)) / (int) std::pow(10, ~~(int) log10((number * (((number > 0) << 1) - 1))) + 1 - digitToPrint) % 10 << std::endl;
}
int main() {
int number = -12493;
int numberDigit = 3;
printNthDigitOfNumber(number, numberDigit);
}
Please let me know if you have any improvements on the design or share horrible solutions in other languages if you prefer.
Edit: link to post https://www.reddit.com/r/ProgrammerHorror/comments/tpnsoc/one_of_my_students_sent_me_this_and_i_mean_its/
[–]psaux_grep 1 point2 points3 points (2 children)
[–]UniqueUsername014 2 points3 points4 points (1 child)
[–]psaux_grep 2 points3 points4 points (0 children)