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: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
Get Answers For Free
Most questions answered within 1 hours.