Swift Program to Display Armstrong Number Between Two Intervals


This tutorial will discuss how to write swift program to display Armstrong number between two intervals.

The sum of nth power of individual digit of a number is equal to the number itself, then such type of number is known as Armstrong number.

Suppose we have a number 407 so here n = 3

407 = 43 + 03 + 73

407 = 64 + 0 +343

407 =407

Hence 407 is Armstrong number

Suppose we have another number 2346, so here n = 4

2346 = 24 + 34 + 44 + 64

2346 = 16 + 81 + 256 + 1296

2346 = 1649

Hence 2346 is not an Armstrong number.

Below is a demonstration of the same −

Input

Suppose our given input is −

LowerLimit = 1
UpperLimit = 200

Output

The desired output would be −

Armstrong Numbers are: 1, 153

Algorithm

Following is the algorithm −

  • Step 1 − Declare two variables to store the lower and upper limit.

  • Step 2 − Run a for loop from lower limit to upper limit to iterate through each element.

  • Step 3 − Declare another variable to store the sum.

  • Step 4 − Declare on more variable to store the count of the total number of digits present in the given number.

  • Step 5 − Count the total number of digits present in the given number.

while(armNum != 0){
   armNum = armNum/10
   count = count + 1
}
  • Step 6 − Calculate the sum of power of individual digit the given number −

let rem = armNum % 10
sum = sum + (rem * rem * rem)
armNum = armNum/10
  • Step 7 − Compare the sum with the number itself. If both sum and number are equal, then the number is the Armstrong number. Otherwise not.

  • Step 8 − Print the output.

Find the 3-digit Armstrong Numbers

Example

The following program shows how to display Armstrong number between two intervals.

import Foundation import Glibc var interval1 = 1 var interval2 = 600 print("Lower Limit:", interval1) print("Upper Limit:", interval2) print("Armstrong numbers are:") for q in interval1..<interval2{ var sum = 0 var armNum = q // Calculate the sum of 3rd power // of individual digit the given number while (armNum != 0){ let rem = armNum % 10 sum = sum + (rem * rem * rem) armNum = armNum/10 } // If the sum is equal to the given number // Then the number is Armstrong number if (sum == q){ print(q) } }

Output

Lower Limit: 1
Upper Limit: 600
Armstrong numbers are:
1
153
370
371
407

Here, in the above code, we have two intervals in which lower limit is 1 and upper limit is 600. Now using for loop we iterate through each number in between the given range and find the sum of the individual digit of the number. After calculating the sum now we check the sum is equal to the given number or not by comparing each other. If the sum is equal to the number, then the number is Armstrong number and print the output. So the working of the above code is −

Lower limit = 1
Upper limit = 600
1st iteration:
Sum = 0
armNum = 1
while (1 != 0){
   let rem = 1 % 10 = 1
   sum = 0 + (1 * 1 * 1) = 1
   armNum = armNum/10 = 1/10 = 0
}
Sum = 1
if (1 == 1) // Condition true{
   print(1)
}
2nd iteration:
Sum = 1
armNum = 2
while (2 != 0){
   let rem = 2 % 10 = 2
   sum = 0 + (2 * 2 * 2) = 8
   armNum = armNum/10 = 2/10 = 0
}
Sum = 8
if (8 == 2) // Condition False{
   print() // Print nothing
}
….. iterate till 599.

Find the n-digit Armstrong Numbers

Example

The following program shows how to display Armstrong number between two intervals.

import Foundation import Glibc var interval1 = 406 var interval2 = 5000 print("Lower Limit:", interval1) print("Upper Limit:", interval2) print("Armstrong numbers are:") for q in interval1..<interval2{ var sum = 0 var armNum = q var count = 0 // Count the total number of digits // present in the given number while(armNum != 0){ armNum = armNum/10 count = count + 1 } armNum = q // Calculate the sum of nth power // of individual digits of the given number while (armNum != 0){ let rem = armNum % 10 sum = sum + Int(pow(Double(rem), Double(count))) armNum = armNum/10 } // If the sum is equal to the given number // Then the number is Armstrong number if (sum == q){ print(q) } }

Output

Lower Limit: 406
Upper Limit: 5000
Armstrong numbers are:
407
1634

Here, in the above code, we have two intervals in which lower limit is 406 and upper limit is 5000. Now using for loop we iterate through each number in between the given range to check for Armstrong Number. So, to find Armstrong number, we first calculate the total number digits present in the number. After that we find the sum of nth power of the individual digit of the number. Now after calculating the sum we check the sum is equal to the given number or not by comparing each other. If the sum is equal to the number, then the number is Armstrong number and print the output. So the Armstrong numbers between 406 to 5000 are 407 and 1634.

Updated on: 20-Oct-2022

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements