Tuesday, March 31, 2020

Armstrong Number With Example

If the sum of the cubes of the digit is equal to the number then the number is called as Armstrong number . Armstrong number is also known as narcissistic number or a plus perfect number
take a number 371
we need to check whether it is an armstrong number is or not .
Now
Digits are 3,7,1
cubes of digit are
3*3*3=27
7*7*7=343
1*1*1=1
Sum of cubes of digit =27+343+1 =371
which is equal to the number itself ,  hence  371 is a armstrong number
List of armstrong number between  1 to 10000
0,1,153,370,371,407,
in more generalize way , Let k be the number of digits in a number, n, and d1,d2,d3,d4... be the digits of n.

Say n=370. Then k=3 and d1=3, d2=7 and d3=0.
Now 33+73+03=370. So 370 is an Armstrong number.
Armstrong iff n=d1k+d2k+d3k+d4k+...
Armstrong Number With Example

Here code:
import java.awt.*;
import java.awt.event.*;
public class Armstrong  implements KeyListener,ActionListener
{
 
    Frame f;
    Button five;
    Label one,two;
    TextField four,three;
    String s1="";
    int i=0,t=0,k=0,l=0,m=0;
    public Armstrong()
    {
        f=new Frame("Armstrong");
        five=new Button("Click here to see Result");
        one=new Label("Enter the number to be checked  :");
        two=new Label(" Result  ");
        three=new TextField(5);
        four=new TextField(10);
        f.setLayout(null);
        f.add(one);
        f.add(two);
        f.add(three);
        f.add(four);
        f.add(five);
        f.setVisible(true);
        f.setSize(400,400);
        one.setBounds(20,20,220,40);
        two.setBounds(150,230,120,40);
        three.setBounds(250,35,80,40);
        four.setBounds(150,280,80,40);
        five.setBounds(100,100,200,60);
        three.addKeyListener(this);
        five.addActionListener(this);
    }
    public void keyPressed(KeyEvent k)
    {
        System.out.print("");
    }
    public void keyTyped(KeyEvent k)
    {
        s1=s1+k.getKeyChar();
    }
    public void keyReleased(KeyEvent k)
    {
        System.out.print("");
    }
    public void actionPerformed(ActionEvent ae)
    {
        k=Integer.parseInt(s1);
        t=k;
        while(k!=0)
        {
            l=k%10;
            k=k/10;
            m=m+(l*l*l);
        }
        System.out.print(t+" "+m+"      ");
        if(t == m)
        four.setText("true");
        else
        four.setText("false");
    }
 
 
    public static void main(String args[])
    {
        new Armstrong();
    }
 
}
 Tags: Armstrong Number With Example,how to Armstrong Number With Example,Armstrong Number in java,Program for Armstrong Numbers,What is an Armstrong number

No comments:

Post a Comment