Number based brainteasers

In this collection of challenges you are going to manipulate numbers and perform basic arithmetic operations with numbers as well as use string manipulation techniques to format numbers.

Challenge #1: Square Number


ChallengeSolutionLearning Outcome?
Write a program that prompts the user to enter a number and returns (displays on screen) the square of this number (e.g. by multiplying this number by itself or by raising this number to the power of 2).

Notice the need to be able to convert a user input from a string to an integer using the int() function on line 1.

Converting a user input to an integer is required to do some maths based calculations such as the calculation used on line 2.

Vice versa, we can convert an integer to a string using the str() function on line 3.

Challenge #2: Total 100


ChallengeSolutionLearning Outcome?
Write a program that prompts the user to enter a number between 0 and 100. The program should return the number that needs to be added to reach 100. For instance if the user enters 36, the program should return 64 because 36 + 64 = 100.

Once again notice the use of the int() function on line 1 to convert a user input to an integer and the str() function on line 3 to convert an integer to a string.

Challenge #3: Odd or Even?


ChallengeSolutionLearning Outcome?
Write a program that prompts the user to enter a number and returns whether this number is an odd number or an even number. (tip: A number is even if the remainder of dividing this number by two is null). So, in Python if number % 2 = 0 then this number is even otherwise it is odd.

In Python the % operator is used to calculate the remainder of a division. This is very useful here to find out if a number can be divided by 2 (even number) or not (odd number).

Challenge #4: Telephone Number


ChallengeSolutionLearning Outcome?
Ask the user to enter their telephone number using 11 digits. Format this number as follows (xxxx) xxx xxx. For instance if the user types 01473123456, the program output should be (01473) 123 456

In Python it is easy to slice a string to extract certain characters. This is done by using the following approach: using the square brackets on a string: e.g. string[startingPosition:endPosition] will retrieve all the characters of the string between the startingPostion and the endPosition.

Notice that in this challenge we are treating the user input as a string not a number. (We did not convert the user input on line 1 to an integer as we did in previous challenges using the int() function.) Each digit of our number/string is in fact a character from the string.

Challenge #5: Palindrome


ChallengeSolutionLearning Outcome?
Ask the user to enter any large number (e.g. at least 3 digits long). The program should return the palindrome of this number. For instance if the user enters 123 the program should return 123321.

In Python it is easy to iterate (loop through) through each character of a string one at a time using a for loop.

Notice that in this challenge, once again we are treating the user input as a string not a number. (We did not convert the user input on line 1 to an integer as we did in previous challenge using the int() function.) Each digit of our number/string is in fact a character from the string.

Challenge #6: Number Upgrade


ChallengeSolutionLearning Outcome?
Ask the user to enter any large number (e.g. at least 3 digits long). The program should return a number where each digit is being incremented by 1 except if the digit is equal to 9 then it becomes 0. For instance if the user enters 2489740 the program should return 3590851.

This is the same approach as for challenge number #5 where we iterate (loop through) each character of a string one at a time using a for loop.

We retrieve each digit (character) one at a time and have to convert these digits to integers using the int() on line 5 and 8 to be able to perform maths calculation with them (adding them up).

Challenge #7: Spaced Out Number


ChallengeSolutionLearning Outcome?
Ask the user to enter any large number (e.g. at least 3 digits long). The program should return this same number adding a “-“ between each digit. For instance if the user enters 3658 the program should return 3-6-5-8.

Once again we are iterating (looping through) each character of a string one at a time using a for loop.

Notice the slicing approach used to truncate the string from its first digit on line 7 using the string[1:] which returns all the characters from a string from position 1. (So slicing out the first character which is at position 0).

Challenge #8: Total Number


ChallengeSolutionLearning Outcome?
Ask the user to enter any large number (e.g. at least 3 digits long). The program should return the result of adding each digit together. For instance if the user enters 7324 the program should return 7+3+2+4 = 16.
Nothing new here… Once again we are iterating (looping through) each character of a string one at a time using a for loop.

Challenge #9: Plus Minus


ChallengeSolutionLearning Outcome?
Ask the user to enter any large number (e.g. at least 3 digits long). The program should return the result of alternatively adding and subtracting each digit together. For instance if the user enters 7324512 the program should return 7-3+2-4+5-1+2 = 8.
Nothing new here… Once again we are iterating (looping through) each character of a string one at a time using a for loop.

Challenge #10: Digit Grouping


ChallengeSolutionLearning Outcome?
When displaying numbers it is good practice to group digits together and use the comma to separate groups of three digits. For instance 100000000 is easier to read when it is displayed as 100,000,000.
Ask the user to enter any large number (e.g. at least 3 digits long). The program should display the result of formatting this number using the comma. For instance if the user enters 65738924 the program should return 65,738,924.
You will notice on this code that we are iterating through each character of a string starting from the last character and going backwards up to the first character. To do so we used a negative step in our for loop using the [::-1] notation on line 5.
Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges

Our Latest Book