Exercises on numbers

Here is a list of exercises on numbers that will help you develop and improve your logic.

Five exercises on numbers : Find the middle number from the set of 5 numbers

12,18,5,22,13

Here the requirement is to find the middle number.
Please note I do not mean “Median”. In case of above series 22 is the largest number and 5 is lowest number and 13 is the middle

Five exercises on numbers : Find the largest number from the following series of numbers

In this exercise you have to the find the largest number from the following series. Feel to add more numbers in the series. The code should still work

10,12,15,3,5, 22, 2

Hint:

  • Store the numbers in an array
  • Make use of temp variable to store the greater when comparing 2 of them
  • Write a for loop to compare all the other numbers

Click here to view the solution to this exercise

Five exercises on numbers : Find the mean, median, mode and range from the series of following numbers

23,9,14,2,28,19,3,15,9,25,2,4,9

Hints

  • Mean is the average. Add all numbers and divide it by the count of numbers
  • Median is the middle number, in the above case 19 is the median
  • Mode is the number that is repeated more often than any other number. In this case it is 9
  • Range is the difference between the largest and lowest number. In this case largest is 28 and lowest is 2. So 28-2 is 26

Five exercises on numbers : Form the below Matrix

Write a program that will form a matrix like the below table. This is one of my favorite exercise. Very trickly and worth solving.

0 1 2 3 4
1 0 1 2 3
2 1 0 1 2
3 2 1 0 1
4 3 2 1 0

 

Hint:

  • Use a for loop and nested loop
  • Use array

Five exercises on numbers : Find the numbers unique and which are repeated.

Identify the numbers which are unique and which are repeated in the following series:

3,4,5,6,9,10,3,4,2,9,4,3,9,3

Example output should be as follows:

Unique numbers : 5,6,10,2
Repeat numbers :  3 (4 times), 4 (3 times), 9 (2 times)

Hint:

  • Store the numbers in an array called ‘numbers’
  • Create an empty ‘duplicate’ array
  • Create an array named ‘unique’ which is also empty
  • Use a for loop to iterate through the numbers
  • Store the first number in a temporary variable and compare with the rest of the numbers
  • if the number is found, add the number in ‘duplicate’ array
  • If the number is not found store the number in the “unique” array
  • Slice the first element from the ‘numbers’ array and repeat untill the ‘numbers’ array is empty
  • Printing the numbers in unique array should be pretty straight forward
  • I shall let you build the logic to display the out of the duplicate array