
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Display Armstrong Numbers Between Intervals Using Function
In this article, we will understand how to display Armstrong numbers between intervals using function. An Armstrong number is a number that is equal to the sum of the cubes of its own digits.
An integer is called an Armstrong number of order n if it's every digit separate out and cubed and summed up then the sum will be same as the number i.e. abcd... = a3 + b3 + c3 + d3 + ...
In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example: 153 is an Armstrong number.
153 = 13 + 53 + 33
For example: 371 is an Armstrong number.
371 = 27 + 343 + 1
Below is a demonstration of the same −
Input
Suppose our input is −
The two inputs : 1 and 500
Output
The desired output would be −
The Armstrong numbers are: 153 370 371 407
Algorithm
Step 1 - START Step 2 - Declare three integer values namely my_low, my_high and i Step 3 - Read the required values from the user/ define the values Step 4 - Define a function IsArmstrong which takes an integer value returns Boolean value. Step 5- In this function, Divide the input variable by 10 and get remainder for ‘check’ . Step 6 - Then Multiply ‘my_rem thrice, and add to ‘my_sum’, and make that the current ‘my_sum. Step 7 - Later ‘check’ by 10, and make that the current ‘check’. Compare the ‘my_sum’ with the function input ‘I’ and return true or false. Step 8 - Using a for loop, iterate from my_low to my_high, for each number, call the function IsArmstrong. If true is returned , it is an Armstrong number, store the number Step 9 - Display the result Step 10 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.Scanner; public class ArmstrongNumbers { public static void main(String[] args) { int my_low, my_high, i; Scanner my_scanner = new Scanner(System.in); System.out.println("Required packages have been imported"); System.out.println("A scanner object has been defined "); System.out.println("Enter the first number :"); my_low = my_scanner.nextInt(); System.out.println("Enter the limit :"); my_high = my_scanner.nextInt(); System.out.println("The Armstrong numbers are :"); for(i = my_low + 1; i < my_high; ++i) { if (IsArmstrong (i)) System.out.print(i + " "); } } public static boolean IsArmstrong(int i) { int check, my_rem, my_sum; my_sum = 0; check = i; while(check != 0) { my_rem = check % 10; my_sum = my_sum + (my_rem * my_rem * my_rem); check = check / 10; } if(my_sum == i){ return true; } return false; } }
Output
Required packages have been imported A scanner object has been defined Enter the first number : 1 Enter the limit : 500 The Armstrong numbers are : 153 370 371 407
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class ArmstrongNumbers { public static void main(String[] args) { int my_low, my_high, i; my_low = 1; my_high = 500; System.out.println("The starting and ending numbers are defined as " + my_low + " and " + my_high); System.out.println("The Armstrong numbers are :"); for(i = my_low + 1; i < my_high; ++i) { if (IsArmstrong (i)) System.out.print(i + " "); } } public static boolean IsArmstrong (int i) { int check, my_rem, my_sum; my_sum = 0; check = i; while(check != 0) { my_rem = check % 10; my_sum = my_sum + (my_rem * my_rem * my_rem); check = check / 10; } if(my_sum == i){ return true; } return false; } }
Output
The starting and ending numbers are defined as 1 and 500 The Armstrong numbers are : 153 0 371 407
- Related Articles
- Swift Program to display Armstrong Numbers Between Intervals Using Function
- Java Program to Display Prime Numbers Between Intervals Using Function
- Java Program to Display Armstrong Number Between Two Intervals
- Swift program to display prime numbers between intervals using function
- C++ Program to Display Armstrong Number Between Two Intervals
- Swift Program to Display Armstrong Number Between Two Intervals
- Golang Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Prime Numbers Between Two Intervals
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- Golang Program to Display Prime Numbers Between Two Intervals using library functions.
- C++ Program to Display Prime Numbers Between Two Intervals
- Swift program to display prime numbers between two intervals
- Java program to print the Armstrong numbers between two numbers
- C program to display the prime numbers in between two intervals
- Java Program to Check Armstrong Number between Two Integers
