Print the Armstrong numbers between two intervals

Select Articles

Print the Armstrong numbers between two intervals

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.

 

Algorithm to print Armstrong numbers between two intervals

Input the start and end values.

Repeat from i = start_value to end_value.

Repeat until (temp != 0)

remainder = temp

result = result + pow(remainder,n)

temp = temp/10

if (result == number)

Print the number

Repeat steps from 2 to 8 until the end_value is encountered.

 

Program to print the Armstrong numbers between the two intervals

C

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

#include

#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 ?n ?re: “, start, end);

for(i = start + 1; i < end>

{

temp2 = i;

temp1 = i;

while (temp1 != 0)

{

temp1 /= 10;

++n;

}

while (temp2 != 0)

{

remainder = temp2 ;

result += pow(remainder, n);

temp2 /= 10;

}

if (result == i) {

printf(“%d “, i);

}

n = 0;

result = 0;

}

printf(“\n”);

return 0;

}

 

C++

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

#include

#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>

for(i = start + 1; i < end>

{

temp2 = i;

temp1 = i;

while (temp1 != 0)

{

temp1 /= 10;

++n;

}

while (temp2 != 0)

{

remainder = temp2 ;

result += pow(remainder, n);

temp2 /= 10;

}

if (result == i) {

cout << i>

}

n = 0;

result = 0;

}

cout << endl>

return 0;

}

 

JAVA

//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>

{

temp2 = i;

temp1 = i;

while (temp1 != 0)

{

temp1 /= 10;

++n;

}

while (temp2 != 0)

{

remainder = temp2 ;

result += Math.pow(remainder, n);

temp2 /= 10;

}

if (result == i) {

System.out.print(i + ” “);

}

n = 0;

result = 0;

}

}

}

 

PYTHON 3

# 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

result = result + math.pow(remainder, n)

temp2 = int(temp2/10)

if (result == i):

print(i,” “)

n = 0

result = 0

 

Output

Input- Enter start value and end value: 100 500 Output- Armstrong numbers between 100 and 500 are:370 371 407

 

Time complexity: O(n)