Question
Please do both parts (in Java); thanks!
CSULB CECS 174 Armstrong Numbers An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself For example, 371 is an Armstrong number since 3A3 7A3 1 371 raw the flowchart and write a Java code to find Au Armstrong number in the range of and 999. You should write your code in two ways: (Y es as if you are solving the problem twice) A- I would like for you to write the whole code as a method that will output all Armstrong numbers, which means in main you have a method call only. public static void Armstrong put your code here You are generating the numbera and checking if every number is an Armstrong one In main you are only calling the method B- This time you are going to write a method that returns back a true is the number is an Armstrong one, false otherwise public static Boolean trong int num) //in this code you are just checking ir the number pasaed to this function (num) is an //Armstrong one and you are returning true if so, otherwise you should return false In main you are generating the numbers within the loop and you are calling IaArmatrong for every number. If the returned value is true you will output the number
0 0
Add a comment Improve this question Transcribed image text
Answer #1

part A:

import java.util.*;

import java.lang.*;

import java.io.*;

class Abc
{
public static void Armstrong()
{
int n,rem,arm; //rem for remender,arm for armstrong
for(int i=0;i<=999;i++) //loop for finding armstrong number b/w 0-999
{
arm=0;
n=i;
while(n>0)
{
rem=n%10;
n=n/10;
arm=arm+(rem*rem*rem);
}
if(i==arm)
System.out.println(i); //printning value if Armstrong
}

}
   public static void main (String[] args) throws java.lang.Exception
   {
       Abc c = new Abc(); //declairing object of class Abc
       c.Armstrong(); //calling method
   }
}

part B:

import java.util.*;
import java.lang.*;
import java.io.*;

class Abc
{
public static Boolean IsArmstrong(int i)
{
int n,rem,arm; //rem for remender,arm for armstrong
arm=0;
n=i;
while(n>0)
{
rem=n%10;
n=n/10;
arm=arm+(rem*rem*rem);
}
  
if(i==arm)
return true; //if number is armstrong return true
else
return false;

}
   public static void main (String[] args) throws java.lang.Exception
   {
       Abc c = new Abc(); //declairing object of class Abc
       for(int i=0;i<=999;i++) //loop for finding armstrong number b/w 0-999
{
   Boolean isTrue=   c.IsArmstrong(i); //calling method IsArmstrong
   if(isTrue==true) //if return true print value
   System.out.println(i);
}
   }
}

Add a comment
Know the answer?
Add Answer to:
Please do both parts (in Java); thanks! An Armstrong number of three digits is an integer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a JAVA program to check whether a given number is an Armstrong number. An Armstrong...

    Write a JAVA program to check whether a given number is an Armstrong number. An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

  • C program--write a program to find whether the given number of three digits is an integer...

    C program--write a program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or * to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or * to quit : 371 3**3 + 7**3 + 1**3 is = 371...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to...

    Using C++ language please not matlab a. Write a function, called SumOfDigits that is able to calculate the summation of a five-digit number. This function receives one integer parameter 'a', then returns the sum of 5 digits of the number. [2.5 marks] b. Write a function, called Armstrong that prints all Armstrong numbers in the range of 0 and 500. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is...

  • A palindrome is a word, phrase, number, or other sequence of characters which reads the same...

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Write a recursive method in java that accepts a integer as its argument and returns true if it is palindrome, false otherwise. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“Is 12321 a palindrome? “+ra.isPalindrome(12321)); //true                 }                 public Boolean isPalindrome(int num){                                 return false;...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • java Write an application that input a number from the user and checks if all digits...

    java Write an application that input a number from the user and checks if all digits are prime numbers using method prime. The number entered can be of any size. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers don’t use an if to check numbers from 1 – 9; you need to find an algorithm to check...

  • Java, please. Work based on the code above. DESCRIPTION: The purpose of this question is offload...

    Java, please. Work based on the code above. DESCRIPTION: The purpose of this question is offload processing from the main method to a static helper method. You will be given the main method. Do not alter this code. Your goal is to write a new method called oddOneOut, which will accept a String, and print it in out every other letter. For example, if the String was "abcdefghijk", the oddOneOut method will return "acegik" METHOD INPUT: • A string METHOD...

  • Write in Java Implement the parse method and test it by calling with three different strings...

    Write in Java Implement the parse method and test it by calling with three different strings and by printing the results. The Scanner method can be used to read values from strings, files, or System.in. We need to invoke the useDelimiter method to define what symbols can separate, or terminate, the digits of a Fraction. public static Fraction parse(String input) t Scanner s new Scanner(input) useDelimitercTVMitln"); int num s.nextlnt() int denom s.nextlnt); s.close): return new Fraction(num, denom) class Codechef static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT