
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Armstrong Numbers Between Intervals Using Function in Swift
This tutorial will discuss how to write Swift program to display Armstrong numbers between intervals using function
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 ? Create a function
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 ? Declare two variables to store the lower and upper limit.
Step 9 ? Call the function and pass lower and upper limits as an arguments.
Step 10? Print the output.
Example
The following program shows how to display Armstrong number between two intervals using function.
import Foundation import Glibc // Function to find the Armstrong number // in between the given intervals func findArmstrongNumber(interval1: Int, interval2: Int){ 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) } } } var lower = 1 var upper = 1000 print("Lower Limit:", lower) print("Upper Limit:", upper) print("Armstrong numbers are:") // Calling function findArmstrongNumber(interval1: lower, interval2: upper)
Output
Lower Limit: 1 Upper Limit: 1000 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 we create a function to find 3 digit Armstrong numbers. In this function, 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 ?
findArmstrongNumber(interval1: 1, interval2: 1000): 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 999.
Find the n-digit Armstrong Numbers
Example
The following program shows how to display Armstrong number between two intervals using function.
import Foundation import Glibc // Function to find armstring number func FindArmstrong(lower: Int, upper: Int){ for q in lower..<upper{ 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) } } } var interval1 = 1000 var interval2 = 10000 print("Lower Limit:", interval1) print("Upper Limit:", interval2) print("Armstrong numbers are:") FindArmstrong(lower: interval1, upper: interval2)
Output
Lower Limit: 1000 Upper Limit: 10000 Armstrong numbers are: 1634 8208 9474
Here, in the above code, we have two intervals in which lower limit is 1000 and upper limit is 10000. Now we create a function to find n-digit Armstrong numbers. In this function, 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 1000 to 10000 are 1634, 8208, and 9474.