0% found this document useful (0 votes)
7 views2 pages

Java Program to Print Armstrong Numbers

The document contains a Java program that identifies and prints Armstrong numbers within a specified range. It includes methods to check if a number is an Armstrong number and to print all Armstrong numbers between a given start and end. The program prompts the user for input and displays the results accordingly.

Uploaded by

121pratham2032
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Java Program to Print Armstrong Numbers

The document contains a Java program that identifies and prints Armstrong numbers within a specified range. It includes methods to check if a number is an Armstrong number and to print all Armstrong numbers between a given start and end. The program prompts the user for input and displays the results accordingly.

Uploaded by

121pratham2032
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Write a java Program to print armstrong number.

import java.u [Link];

public class ArmstrongNumber {

public sta c boolean isArmstrong(int number) {

int originalNumber = number;

int sum = 0;

int digits = [Link](number).length();

while (number != 0) {

int digit = number % 10;

sum += [Link](digit, digits);

number /= 10;

return sum == originalNumber;

public sta c void printArmstrongNumbers(int start, int end) {

[Link]("Armstrong numbers between " + start + " and " + end + " are:");

for (int i = start; i <= end; i++) {

if (isArmstrong(i)) {

[Link](i + " ");

public sta c void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the start of the range: ");

int start = [Link]();

[Link]("Enter the end of the range: ");

int end = [Link]();


printArmstrongNumbers(start, end);

[Link]();

O/P :

Enter the start of the range: 1

Enter the end of the range: 1000

Armstrong numbers between 1 and 1000 are:

1 2 3 4 5 6 7 8 9 153 370 371 407

Common questions

Powered by AI

The isArmstrong method determines if a number is an Armstrong number by calculating the sum of its digits each raised to the power of the number of digits in the number. It compares this sum to the original number, returning true if they are equal and false otherwise .

The Math.pow function is used to raise each digit of the number to the power of the total number of digits. This operation is crucial for verifying if the sum matches the original number as part of determining if it is an Armstrong number .

This program offers clear educational value by demonstrating basic loops, conditionals, and mathematical operations to new programmers. It offers practical insights into algorithm construction, debugging, and user input handling, making it an engaging introduction to solving mathematical problems through programming .

The 'while' loop extracts each digit from the number starting from the least significant digit, calculates its contribution to the overall Armstrong sum by raising it to the power of all digits, and accumulates it in the sum variable. This iterative process continues until all digits are processed, enabling the accurate determination of Armstrong numbers .

Alternative approaches in other programming languages might utilize list comprehensions in Python for concise calculations, streaming APIs in functional languages like JavaScript or Scala, or recursion in Haskell, providing diverse syntax and logical structure alternatives to achieve the same outcome .

If the range is between 500 and 550, the program would not output any Armstrong numbers since none exist within this range. An Armstrong number between two given limits is rare and does not occur in every arbitrarily set range. Thus, the output would simply state 'Armstrong numbers between 500 and 550 are:' with no numbers following .

To handle larger ranges efficiently, incorporating parallel processing or distributing tasks could speed up execution. Using hashmaps for previously calculated powers of digits can prevent redundant calculations. Additionally, shifting to a lower-level language like C for high-performance regions of the code might also be beneficial .

The program is simple but can be relatively inefficient due to recalculations and the absence of pre-computation or optimizations. Improvements include using pre-stored Armstrong numbers, limiting repeated power calculations using memoization where applicable, or implementing checks that terminate further calculations early if the sum exceeds the original number during processing .

The Scanner object is used to take input from the user by reading integers from the console. It facilitates interactive input where the program prompts the user for a starting and ending range to identify Armstrong numbers .

The printArmstrongNumbers method iterates through each number in the specified range and calls the isArmstrong method to check if a given number is an Armstrong number. If the method returns true, it prints the number, resulting in all Armstrong numbers within the range being displayed .

You might also like