Question

In: Computer Science

Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number...

Problem 1:

More than 2500 years ago, mathematicians got interested in numbers.

Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 + 125 + 27 = 153. Namely, 153 is equal to the sum of the cubes of its own digits.

Perfect Numbers: A number is said to be perfect if it is the sum of its own divisors (excluding itself). For example, 6 is perfect since 1, 2, and 3 divide evenly into 6 and 1+ 2 + 3 = 6.

Input File

The input is taken from a file named number.in and which a sequence of numbers, one per line, terminated by a line containing the number 0.

Output File

All the numbers read from the input file, are printed one per line followed by a sentence indicating whether the number is or is not Armstrong number and whether it is or is not a perfect number. Sample Input 153 6 0 Sample Output 153 is an Armstrong number but it is not a perfect number. 6 is not an Armstrong number but it is a perfect number.

Note: the program must be write in java

Solutions

Expert Solution

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class NumbersPro {
   public static void main(String[] args) throws Exception {
       Scanner sc = new Scanner(new File("numbers.in"));
       PrintWriter pw = new PrintWriter(new File("output.txt"));
       int num;
       String out = "";
       boolean isAmstrong, isPerfect;
       while (sc.hasNext()) {
           out = "";
           num = sc.nextInt();
           if (num == 0)
               break;
           isAmstrong = isAmstrong(num);
           if (isAmstrong)
               out = num + " is an Armstrong number";
           else
               out = num + " is not an Armstrong number";

           isPerfect = isPerfect(num);
           if (isPerfect)
               out += " and is an Perfect number";
           else
               out += " but not an perfect number";
           pw.println(out);

       }
       pw.close();
   }

   private static boolean isPerfect(int number) {
       int sum = 0, i;
       for (i = 1; i < number; i++) {
           if (number % i == 0) {
               sum = sum + i;
           }
       }
       return sum == number;
   }

   private static boolean isAmstrong(int number) {
       int originalNumber = number;
       int result = 0;
       while (originalNumber != 0) {
           int remainder = originalNumber % 10;
           result += Math.pow(remainder, 3);
           originalNumber /= 10;
       }

       return result == number;
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number...
Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 + 125 + 27 = 153. Namely, 153 is equal to the sum of the cubes of its own digits. Perfect Numbers: A number is said to be perfect if it is the sum of its own divisors (excluding itself). For example, 6 is perfect since 1, 2, and 3 divide evenly into...
1. Thirty years ago, the mean number of rides that were disabled (broken) for more than...
1. Thirty years ago, the mean number of rides that were disabled (broken) for more than two hours at Disneyland was 10.2 per month. The current CEO, Bob Iger, believes that number has gone down and randomly selects 11 months from the past three years and checks on the number of disabled rides. If the mean has decreased, he will give the members of the maintenance staff a $50K bonus this year. If not, they will all be immediately fired....
Assignment 1 Instructions A. You have 2500 words to utilize B. No more than 10% above...
Assignment 1 Instructions A. You have 2500 words to utilize B. No more than 10% above the maximum nor 10% below the maximum word count C. Use the Harvard referencing system D. The deadline in the 20/09/2018 NO EXTENSIONS AVAILABLE E. Submissions are to be made via the portal and no email submissions will be accepted The Federal Reserve System is the central bank of the United States. It performs five general functions to promote the effective operation of the...
1. A financial manager should be more interested / concerned with the book balance than the...
1. A financial manager should be more interested / concerned with the book balance than the net float and bank cash. True&False 2. ..........refers to the time required to clear a cheque through the banking system. a. Mail float b. Availability float c. Disbursement float 3. Short term marketable securities are usually referred and considered as cash equivalents? True&False 4. The differnce between bank cash and book cash is called float. True&False 5. .................are incurred with a firm runs out...
(1 point) A researcher is interested in whether the number of years of formal education is...
(1 point) A researcher is interested in whether the number of years of formal education is related to a person's decision to never smoke, continue to smoke, or quit smoking cigarettes. The data below represent the smoking status by level of education for residents of the United States 18 years or older from a random sample of 475 residents. Round all numeric answers to four decimal places. Smoking Status Education Level Current Former Never Less than high school 25 21...
(1 point) A researcher is interested in whether the number of years of formal education is...
(1 point) A researcher is interested in whether the number of years of formal education is related to a person's decision to never smoke, continue to smoke, or quit smoking cigarettes. The data below represent the smoking status by level of education for residents of the United States 18 years or older from a random sample of 475 residents. Round all numeric answers to four decimal places. Smoking Status Education Level Current Former Never Less than high school 40 21...
(1 point) A researcher is interested in whether the number of years of formal education is...
(1 point) A researcher is interested in whether the number of years of formal education is related to a person's decision to never smoke, continue to smoke, or quit smoking cigarettes. The data below represent the smoking status by level of education for residents of the United States 18 years or older from a random sample of 350 residents. Round all numeric answers to four decimal places. Smoking Status Education Level Current Former Never Less than high school 14 19...
Management Accounting: It’s more than just crunching numbers. Discuss. (more than 500 words)
Management Accounting: It’s more than just crunching numbers. Discuss. (more than 500 words)
Explain how Brazil was able to reduce the rate of inflation from from more than 2500%to...
Explain how Brazil was able to reduce the rate of inflation from from more than 2500%to less than 10% between 1994 and 1997.
A few years ago, you got married and bought a house with an adjustable rate mortgage...
A few years ago, you got married and bought a house with an adjustable rate mortgage with the following terms: Loan: $240,000 Term: 20 years Initial Rate: 4% Margin: 2% over the Index Rate Lifetime Max: 4.5% The index rate was 2% in year 1, 1.5% in year 2, 4% in year 3, 1% in year 4, and 1% in year 5. a) What is your loan balance at year 5? (5pts) b) What is the effective interest rate is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT