Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: KEITH NUMBER JAVA CODE

Tuesday, August 5, 2014

KEITH NUMBER JAVA CODE

Post by Maths.

import java.util.*;
public class Keith {
    public static void main(String[] args){
        int numb,temp;
        int a,b,c;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number:");
        numb = in.nextInt();
        TestKeith tk = new TestKeith(numb);
        tk.initdigitNumb();
        if(tk.isKeith() == true)
            System.out.println(numb+"is a Keith number");
        else
            System.out.println(numb+" is not a Keith number");   
        }
    }
class TestKeith {       
    TestKeith(int numb) {
        numbString = "";
        Numb = numb;
        numbString +=numb;
        digitNumb = new int[numbString.length()];
        }
    public void initdigitNumb() {
        for(int i = 0; i < numbString.length(); i++)
            digitNumb[i] = numbString.charAt(i)-48;
        }   
    public boolean isKeith() {
        int temp=0,j,stop=0;
        flag = false;
        while ( stop <= Numb ) {
            for (int i =0; i < numbString.length(); i++)
                temp +=digitNumb[i];
            if (temp == Numb) {
                flag = true;
                break;
                }
            for (j =0; j < numbString.length()-1; j++)
                digitNumb[j] = digitNumb[j+1];
            digitNumb[j] = temp;   
            stop = temp;
            temp = 0;
            }
        return (flag == false? false : true);   
        }   
    private String numbString;
    private int Numb;
    private int[] digitNumb;   
    private boolean flag;
    }

No comments:

Post a Comment