Question

Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 +...

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.

Homework Answers

Answer #1

Note:As U didnt mentioned In which language you want me to develop ,I developed in java..Plz tell me if u need this in c++ or c.

Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// numbers.in

153
6
0

_________________________

package org.students;

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

public class CheckArmstrongOrPerfectNumber {

   public static void main(String[] args) {
   int num;
       try {
           Scanner sc=new Scanner(new File("numbers.in"));
           while(sc.hasNext())
           {
               num=sc.nextInt();
               if(num!=0)
               {
                   boolean b1=isArmstrongNum(num);
                   boolean b2=isPerfectNum(num);
                   if(b1)
                   {
                       System.out.print(num+" is an Armstrong number");
                   }
                   else
                   {
                       System.out.print(num+" is not an Armstrong number");
                   }
                  
                   if(b2)
                   {
                       System.out.println(" and it is a perfect number.");
                   }
                   else
                   {
                       System.out.println(" but it is not a perfect number.");
                   }
               }
               else
               {
                   break;
               }
           }
       } catch (FileNotFoundException e) {
   System.out.println("** File Not Found **");
       }
      

   }

   private static boolean isPerfectNum(int num) {
   //Declaring the variable
   int sum=0;
  
  
   for(int i=1;i<num-1;i++)
   {
   if(num%i==0)
   sum+=i;
   }
  
   /* if the user entered number is equal to the
   * sum value the number is perfect number
   */
   if(sum==num)
   return true;
   else
   return false;
   }

   private static boolean isArmstrongNum(int number) {
   // Declaring variables
   int rem = 0, tot = 0;
   int value = number;
   /* This while loop will get executes
   * until the number is greater than zero
   */
   while (number > 0)
   {
   rem = number % 10;
   tot += Math.pow(rem, 3);
   number = number / 10;
   }
   /* If the sum of cubes of individual digits is equal
   * to the number which was passed as parameter
   * then that number is Armstrong number.So it returns true.
   * else it returns false
   */
   if (tot == value)
   {
   return true;
   }
   else
   {
   return false;
   }

   }

}

_______________________________

Output:

153 is an Armstrong number but it is not a perfect number.
6 is not an Armstrong number and it is a perfect number.


_______________Could you plz rate me well.Thank You

Know the answer?
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
Similar Questions
Problem: Our Armstrong number Please write code for C language So far we have worked on...
Problem: Our Armstrong number Please write code for C language So far we have worked on obtaining individual digits from 4 digits or 5 digit numbers. Then added them to find the sum of digits in various examples and assignments. However, the process of extracting individual digits is actually can be solved using a loop as you were doing a repetitive task by using mod operation and division operation. Now, we know how loops work and we can remove the...
4.13 LAB: Interstate highway numbers Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the...
4.13 LAB: Interstate highway numbers Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, I-405 services I-5, and I-290 services I-90. Given a highway number, indicate whether it is a primary or auxiliary highway. If auxiliary, indicate what primary highway it serves. Also indicate if the (primary)...
Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south,...
Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, I-405 services I-5, and I-290 services I-90. Given a highway number, indicate whether it is a primary or auxiliary highway. If auxiliary, indicate what primary highway it serves. Also indicate if the (primary) highway runs north/south or east/west....
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
What would the pseudocode be for this? The code for this has already been answered. I...
What would the pseudocode be for this? The code for this has already been answered. I need the pseudocode. Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375 4657 is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS OR SWITCH STATEMENTS IN THE JAVASCRIPT. Even though there is stylesheet in the HTML file do no create a new one. Project Standards: Students will use click events to capture user input. Students will use variables to store information needed by their application and keep track of their program’s state. Students will use conditionals to control project flow. Project Task You will be building...