What is Armstrong number?
From any number, the digits are to be separated and sum of the digit to the power number of digits of each digit is to be calculated. If the sum and number matches then the number is an Armstrong number.For example if we enter 134 as the number then the number of digit in the number is 3. The sum would be 1^3+3^3+4^3 which is not equal to 134, so 134 is not an Armstrong number. Again if the number is 1234 then the sum would be 1^4+2^4+3^4+4^4. 153 is an Armstrong number as 1^3+5^3+3^3 equals to 153.
It is found that students always use 3 as the power but it is not correct way of checking Armstrong number although 3 digit numbers are Armstrong number only.
Codes of the Armstrong number checking program using 3 as power value
import java.io.*;class Armstrong
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int p,sum=0,c,spy=0,i;
public void takeData() throws IOException
{
System.out.println("Enter the number:-");
i=Integer.parseInt(br.readLine());
p=i;
while(p>9)
{
spy=1;
c=p%10;
sum=sum+(int)Math.pow(c,3);
p=p/10;
}
if(spy==1)
{
sum=sum+(int)Math.pow(p,3);
if(sum==i)
System.out.println(i+ " is amstrong number");
else
System.out.println("Not amstrong number.");
}
else
System.out.println("Not possible.");
}
public static void main(String args[])throws IOException
{
Armstrong obj=new Armstrong();
obj.takeData();
}
}
Now I will do the same program using number of digits of the number as power value.
Variable description of the Armstrong number checking program
BufferedReader br is used to take the number from user.int p is a variable is used as loop control variable in this program.
int digit counts the number of digits in the entered number
int sum stores the sum of the digit to the power number of digits value.
int c holds the individual digits of the number.
int spy is used for checking purpose, whether the number is single digit or not.
int i holds the original number entered by user.
How to proceed in the program
Firstly the number is stored in variable ‘i’ and using do while loop the number of digits is counted and stored in variable ‘digit’.The second do while loop is used to extract the digits of the number one by one and the power value is stored in the variable ‘sum’. At the end of the loop, values of ‘sum’ and the number is compared to reach the conclusion.
Modified codes of the same program on Armstrong number checking
import java.io.*;
class Armstrong
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int p,digit=0,sum=0,c,spy=0,i;
public void takeNumber() throws IOException
{
System.out.println("Enter the number:-");
i=Integer.parseInt(br.readLine());
p=i;
do
{
digit++;
p=p/10;
}while(p!=0);
p=i;
do
{
spy=1;
c=p%10;
sum=sum+(int)Math.pow(c,digit);
p=p/10;
}while(p!=0);
if(spy==1)
{
if(sum==i)
System.out.println(i+ " is amstrong number");
else
System.out.println("Not amstrong number.");
}
else
System.out.println("Not possible.");
}
public static void main(String args[])throws IOException
{
Armstrong obj=new Armstrong();
obj.takeNumber();
}
}
Related Post: BlueJ Programs on Number
thank you!!!
ReplyDeletesame to you. If my programs help anybody its a great pleasure for me.
ReplyDeleteCan u give me a program that would give me the sum of digits of a user specified integer.
ReplyDeleteclass S
ReplyDelete{
int sum,i;
public void show(int n)
{
sum=0;
for( i=n; i > 0; i=i/10)
{
sum=sum+i%10;
}
System.out.println(" Sum of the digits=" + sum);
}
}
can u please give the code for binary recursive search and find the position of the number found in the array....
ReplyDeletePlease check my post on 19 August 2011
ReplyDeletecan any one give me the prog to check for an armstrong no using for ..... in blue java
ReplyDeleteProgram on armstrong number is already posted in my site. Please search it.
ReplyDeleteCan u give me program such that if user enters 5
ReplyDeletethen output should be=12345 and if example 7 output=1234567
import java.io.*;
ReplyDeleteclass Series
{
int i,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void show() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
for(i=1;i< =n;i++)
System.out.print(i);
}
public static void main(String args[])throws Exception
{
Series ob=new Series();
ob.show();
}
}
thank u
ReplyDeletecan u give me a program for the patteren below
a
a a
a a a
a a a a
a a a
a a
a
class Arr
Delete{
int i,j,x=1;
public void show()
{
for (i=0;i<7;i++)
{
for(j=0;j=7/2)
x--;
else
x++;
System.out.println();
}
}
}
thankyoi sir. ur site gives me a lot more confidence :)
ReplyDeletecan you give me a program to take the input size of array and then take the input for those many numbers and print the Amstrong numbers?
ReplyDeleteYou will get the program very soon.
Deletecan u pls give a program to check a no.which is Armstrong no. or not using bufferedReader & using while loop
ReplyDeleteThe program will be posted very soon.
DeleteCan you give me a program to extract the first digit of a number..
ReplyDeleteEg: 23456
Output: 2
Posted today.
Delete