Numerical problems are tasks that involve mathematical calculations, such as arithmetic operations, formula evaluations, and number manipulations. An algorithm for a numerical problem provides a precise recipe for performing these calculations in the correct order to achieve a desired result.
The same systematic four-step design process is used to ensure the logic is sound.
1. Understand the Problem
2. Devise a Plan
3. Formulate the Algorithm
4. Test and Refine
1. Basic Example: Simple Interest Calculation 💰
· Problem Statement: Calculate the simple interest given the principal amount, annual interest rate, and time in years.
· Step 1: Understand the Problem
o Input:
Principal amount (P), Rate of interest (R), Time in years (T).
o Output: The
Simple Interest (SI).
o Rule: The formula is SI=(P×R×T)/100.
· Step 2: Devise a Plan This is a direct formula application. The plan is to get the three input values, apply the formula, and display the result.
· Step 3: Formulate the Algorithm (Pseudocode)
START // Get all required inputs READ Principal (P) READ Rate (R) READ Time (T) // Apply the formula SET SI = (P * R * T) / 100 // Display the final output PRINT "Simple Interest is", SIEND· Step 4: Test and Refine
o Given P=1000, R=5, T=2.
o SI = (1000 * 5 * 2) / 100 = 10000 / 100 = 100.
o The output is "Simple Interest is 100". The logic is correct.
2. Moderate Example: Reversing a Number 🔄
·
Problem
Statement: Take an integer as input
and reverse its digits. For example, if the input is 123, the output should be 321.
· Step 1: Understand the Problem
o Input: An
integer Number.
o Output: The
integer with its digits reversed, ReversedNumber.
o Rule: We need to extract the last digit of the number repeatedly and build a new reversed number with it.
·
Step 2: Devise
a Plan We can use a loop that
continues as long as the Number is
greater than 0. In each iteration:
1. Extract the last digit using the modulo operator (Remainder = Number
% 10).
2. Append this digit to our ReversedNumber. We do this by multiplying the current ReversedNumber by 10 and adding the Remainder.
3. Remove the last digit from the original Number using integer division (Number = Number /
10).
· Step 3: Formulate the Algorithm (Pseudocode)
START READ Number SET ReversedNumber = 0 // Loop until all digits are processed WHILE Number > 0 DO SET Remainder = Number % 10 ReversedNumber = (ReversedNumber * 10) + Remainder Number = Number / 10 // Integer division ENDWHILE PRINT "Reversed number is", ReversedNumberEND· Step 4: Test and Refine
o Given Number = 123. ReversedNumber = 0.
o Loop 1: Remainder = 3. ReversedNumber = (0*10)+3=3. Number = 12.
o Loop 2: Remainder = 2. ReversedNumber = (3*10)+2=32. Number = 1.
o Loop 3: Remainder = 1. ReversedNumber = (32*10)+1=321. Number = 0.
o The loop terminates. The output is "Reversed number is 321". The logic is correct.
3. Advanced Example: Armstrong Number Check 🚀
· Problem Statement: Check if a given number is an Armstrong number. An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits. (e.g., 153=13+53+33).
· Step 1: Understand the Problem
o Input: An
integer Number.
o Output: A message: "Is an Armstrong number" or "Is not an Armstrong number".
o Rule: First, count the number of digits. Then, calculate the sum of each digit raised to the power of that count. Finally, compare the sum to the original number.
· Step 2: Devise a Plan This requires a two-pass approach.
1. Pass 1:
Count the number of digits. Loop through the number, dividing by 10 until it
becomes 0, incrementing a DigitCount counter each time.
2. Pass 2:
Calculate the sum. Loop through the number again (using a copy, as the original
will be modified). In each iteration, extract the last digit, raise it to the
power of DigitCount, and add it to a running Sum.
3. Compare:
After the second loop, compare Sum with the original number.
· Step 3: Formulate the Algorithm (Pseudocode)
START READ OriginalNumber // Pass 1: Count digits SET NumberCopy = OriginalNumber SET DigitCount = 0 WHILE NumberCopy > 0 DO DigitCount = DigitCount + 1 NumberCopy = NumberCopy / 10 ENDWHILE // Pass 2: Calculate sum of powers SET NumberCopy = OriginalNumber SET Sum = 0 WHILE NumberCopy > 0 DO SET Remainder = NumberCopy % 10 Sum = Sum + (Remainder ^ DigitCount) // '^' denotes power NumberCopy = NumberCopy / 10 ENDWHILE // Final comparison IF Sum == OriginalNumber THEN PRINT "Is an Armstrong number" ELSE PRINT "Is not an Armstrong number" ENDIFEND· Step 4: Test and Refine
o Given OriginalNumber = 153.
o Pass 1: DigitCount will correctly be calculated as 3.
o Pass 2: Sum = 0.
§ Extract 3: Sum = 0 + (3^3) = 27.
§ Extract 5: Sum = 27 + (5^3) = 27 + 125 = 152.
§ Extract 1: Sum = 152 + (1^3) = 152 + 1 = 153.
o Compare: Sum (153) equals OriginalNumber (153).
o The output is "Is an Armstrong number". The logic is correct.