armstrong number (java program pdf)

Upload: amrendra-singh

Post on 02-Mar-2016

37 views

Category:

Documents


0 download

DESCRIPTION

JAVA PROGRAM TO CHECK IF A NUMBER IS AN ARMSTRONG NUMBER OR NOT ( An armstrong number is a number in which the sum of cubes of every digit of the number is equal to the number itself. eg. 153 = 1^3 + 5^3 +3^ 3 = 1+125+27 = 153) . WITH COMMENTS AND DETAILS TO HELP YOU UNDERSTAND THEM.THIS PROGRAM HAS BEEN RUN ON BlueJ AND IS ERROR FREE.IT WILL SURELY HELP YOU IN STUDYING JAVA PROGRAMS FOR EXAMS OR OTHERWISE.

TRANSCRIPT

  • Class armstrong_number 1/1/* * [email protected] * javaprogramsbyashking13th.blogspot.in * ashkingjava.blogspot.in *

    * QUESTION *

    * Design a program in java to check whether a number * is an armstrong number or not. * An armstrong number is a number in which the sum * ofcubes of every digit of the number is equal to * the number itself. * eg. 153 = 1^3 + 5^3 +3^ 3 = 1+125+27 = 153 */

    import java.io.*;public class armstrong_number{ public void main()throws IOException { DataInputStream d=new DataInputStream(System.in); System.out.println("enter the number"); int n=Integer.parseInt(d.readLine()); int m=n; //creating dummy variable int s=0; int r=0; while(m>0) { r=m%10; s=s+(r*r*r); //finding sum of cubes of digits of the number m=m/10; } if(n==s) /* * checking if sum of cubes of every digit if the number * is equal to the number itself and then displaying messages * accordingly. */ { System.out.println("Number is an armstrong number"); } else { System.out.println("Number is not an armstrong number"); } }}

    Mar 25, 2014 3:28:21 PM