Print the Armstrong numbers between the two intervals | faceprep

Program to print all the Armstrong numbers between the two intervals is discussed here. A number is said to be an Armstrong number when the sum of nth power of digit of the number is equal to the number itself.

armstrong number between two intervals

Algorithm to print Armstrong numbers between two intervals

  1. Input the start and end values.
  2. Repeat from i = start_value to end_value.
  3. Repeat until (temp != 0)
  4. remainder = temp % 10
  5. result = resut + pow(remainder,n)
  6. temp = temp/10
  7. if (result == number)
  8. Print the number
  9. Repeat steps from 2 to 8 until the end_value is encountered.

Program to print the Armstrong numbers between the two intervals

// C program to print the Armstrong numbers between the two intervals

#include <stdio.h>
#include <math.h>

int main()
{
int start, end, i, temp1, temp2, remainder, n = 0, result = 0;

printf(“Enter start value and end value : “);
scanf(“%d %d”, &start, &end);
printf(“\nArmstrong numbers between %d an %d are: “, start, end);

for(i = start + 1; i < end; ++i)
{
temp2 = i;
temp1 = i;

while (temp1 != 0)
{
temp1 /= 10;
++n;
}

while (temp2 != 0)
{
remainder = temp2 % 10;
result += pow(remainder, n);
temp2 /= 10;
}

if (result == i) {
printf(“%d “, i);
}

n = 0;
result = 0;

}
printf(“\n”);
return 0;
}

// C++ program to print the Armstrong numbers between the two intervals

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int start, end, i, temp1, temp2, remainder, n = 0, result = 0;

cout << “Enter start value and end value : “;
cin >> start >> end;
cout << “\nArmstrong numbers between ” << start << ” and ” << end << ” are : “;

for(i = start + 1; i < end; ++i)
{
temp2 = i;
temp1 = i;

while (temp1 != 0)
{
temp1 /= 10;
++n;
}

while (temp2 != 0)
{
remainder = temp2 % 10;
result += pow(remainder, n);
temp2 /= 10;
}

if (result == i) {
cout << i << ” “;
}

n = 0;
result = 0;

}
cout << endl;
return 0;
}

//Java program to print the Armstrong numbers between the two intervals

import java.util.*;
public class sum_of_primes {

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter start and end values: “);
int start = sc.nextInt();
int end = sc.nextInt();
int i, temp1, temp2, remainder, n = 0, result = 0;


for(i = start + 1; i < end; ++i)
{
temp2 = i;
temp1 = i;

while (temp1 != 0)
{
temp1 /= 10;
++n;
}

while (temp2 != 0)
{
remainder = temp2 % 10;
result += Math.pow(remainder, n);
temp2 /= 10;
}

if (result == i) {
System.out.print(i + ” “);
}

n = 0;
result = 0;

}

}

}

# Python program to print the Armstrong numbers between the two intervals

import math
start = int(input(“Enter start value : “))
end = int(input(“Enter end value : “))
result = 0
n = 0
for i in range(start+1 ,end,1):
temp2 = i
temp1 = i

while (temp1 != 0):
temp1 = int(temp1/10)
n = n + 1

while (temp2 != 0):
remainder = temp2 % 10
result = result + math.pow(remainder, n)
temp2 = int(temp2/10)

if (result == i):
print(i,” “)

n = 0
result = 0

Output:

armstrong numbers between the two intervals

Time complexity: O(n)