Write a program to print Armstrong numbers between two intervals

19 July 2022

Write a program to print Armstrong numbers between two intervals


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get the lower and upper bound from the user and then print all the Armstrong numbers within that range.

Input

10

1000

Output

153  370  371  407

 

 

C Program

#include <stdio.h>

#include<math.h>

int order(int x)

{

    int len = 0;

    while (x)

    {

        len++;

        x = x/10;

    }

    return len;

}

void Armstrong(int lower, int upper){

   

    for(int num = lower; num <= upper; num++){

       

        int sum = 0, temp, rem, len;

        temp = num;

        len = order(num);

        while(temp != 0)

        {

            rem = temp % 10;

            sum = sum + pow(rem,len);;

            temp = temp/10;

        }

        if(sum == num)

            printf("%d ",num);

    }

}

int main ()

{

    int lower,upper;

    printf("Enter a lower & upper bounds: ");

    scanf("%d %d",&lower,&upper);

    Armstrong(lower,upper);

}

 

 

C++ Program

#include <iostream>

#include<math.h>

using namespace std;

int order(int x)

{

    int len = 0;

    while (x)

    {

        len++;

        x = x/10;

    }

    return len;

}

void Armstrong(int lower, int upper){

   

    for(int num = lower; num <= upper; num++){

       

        int sum = 0, temp, rem, len;

        temp = num;

        len = order(num);

        while(temp != 0)

        {

            rem = temp % 10;

            sum = sum + pow(rem,len);;

            temp = temp/10;

        }

        if(sum == num)

            cout<<num<<" ";

    }

}

int main ()

{

    int lower,upper;

    cout<<"Enter a lower & upper bounds: ";

    cin>>lower>>upper;

    Armstrong(lower,upper);

}

 

Java Program

import java.math.*;

import java.util.Scanner;

 class Main {

    static void Armstrong(int lower, int upper)

    {

        for (int i = lower; i < upper; ++i) {

            int x = i;

            int n = 0;

            while (x != 0) {

                x /= 10;

                ++n;

            }

            int pow_sum = 0;

            x = i;

            while (x != 0) {

                int digit = x % 10;

                pow_sum += Math.pow(digit, n);

                x /= 10;

            }

            if (pow_sum == i)

                System.out.print(i + " ");   

        }

    }

 

    public static void main(String args[])

    {

       

        Scanner sc = new Scanner(System.in);

                                System.out.print("Enter lower bound : ");

                                int num1 = sc.nextInt();

                                System.out.print("Enter upper bound : ");

                                int num2 = sc.nextInt();

        Armstrong(num1, num2);

        System.out.println();

    }

}

 

Python Program

lower = int(input("Enter lower range: ")) 

upper = int(input("Enter upper range: ")) 

for num in range(lower,upper + 1): 

   sum = 0 

   temp = num

   count=0

   while temp!=0:

       count=count+1

       temp=temp//10

   temp=num

   while temp > 0:

       digit = temp % 10 

       sum += pow(digit,count) 

       temp //= 10 

       if num == sum: 

            print(num,end=" ")


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !