happy (java program pdf)

Upload: amrendra-singh

Post on 14-Oct-2015

57 views

Category:

Documents


0 download

DESCRIPTION

JAVA PROGRAM TO CHECK IF A NUMBER IS A HAPPY NUMBER OR NOT (A Happy number is a number for which sum of squares of digits finally comes out o be 1. For Example: 139 * 1. 139 : 1 + 9 +81 = 91 * 2. 91 : 81 + 1 = 82 * 3. 82 : 64 + 4 = 68 * 4. 68 : 36 + 64 = 100 * 5. 100 : 1 + 0 +0 = 1) . 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 happy 1/3/* * A Happy number is a number for which sum of squares of * digits finally comes out o be 1. * For Example: 139 * 1. 139 : 1 + 9 +81 = 91 * 2. 91 : 81 + 1 = 82 * 3. 82 : 64 + 4 = 68 * 4. 68 : 36 + 64 = 100 * 5. 100 : 1 + 0 +0 = 1 *

    * Number of steps =5 *

    * CLASS NAME : happy * DATA MEMBERS : n TO STORE THE NUMBER * FUNCIONS : * happy() : constructor to assign 0 to n * void getnum(int nn) : to assign parameter value to n * int power(int nn) :to return square of a number * int sumOfSquares(int nn) :returns sum of square of digits of a number * void ishappy() :to check if the given number is happy or not *

    * Also find the number of steps required in the process for each number. * Also define the main() function to exeute the program by creating object. */

    import java.io.*;public class happy { int n; public happy() //default constuctor { n=0;//initialing data member }

    DataInputStream d=new DataInputStream(System.in);

    public void getnum(int nn)//method to assign inputed value to n { n=nn; }

    public int power(int nn)//to find and return square of a number { int p=nn*nn; return p; }

    public int sumOfSquares(int nn)//to calculate sum of square { int m=nn;//creating dummy variable int r=0;

    May 27, 2014 9:58:23 PM

  • Class happy (continued) 2/3 int ps=0;//to store sum of squares int t=0; System.out.print(nn+"\t="); //to print the process(only for understaning) while(m>0) { r=m%10; t=power(r); ps=ps+t;//calculating sum m=m/10;//updating loop variable System.out.print("\t"+t+( m>0 ? "\t+":" " )); //to print the process(only for understaning) } System.out.println(); return ps;//returning sum of squares of the digits }

    public void ishappy() { int mm=n; int c=0;//counter to cont no of steps taken do { mm=sumOfSquares(mm); c++; }while(mm>9); //checking if sum of square is single digit

    if(1==mm)//checking if final sum is equal to 1 { System.out.println("It is a happy number"); System.out.println("No of steps = "+c); //printing number of steps System.out.println(); } else { System.out.println("It is not a happy number"); } } /* * PROCESS AND LOGIC * 1) We find the sum of squares of the digits of the number. * 2) If the sum is not single digit then we repeat step 1. * For this we use the do while loop(we don't use only while * because the number entered at the first instance may itself * be sigle digit).We repeat this using the loop till the * sum(mm) is reduced to a single digit. * 3) Then we check if the sum is one or not and then display the * appropriate message. *

    * We do not use the member variable but instead create a dummy * variable mm to not to alter n.

    May 27, 2014 9:58:24 PM

  • Class happy (continued) 3/3 */

    public void main()throws IOException { System.out.println("Enter a number"); int nn=Integer.parseInt(d.readLine());//inputing number System.out.println(); happy ob1=new happy();//creating object ob1.getnum(nn);//calling function via object ob1 ob1.ishappy();//calling function via object ob1 }//end of main

    }//end of class

    May 27, 2014 9:58:24 PM