AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: How to find lucky number?

Write a java program to calculate the lucky number, It takes date of birth as input in format dd/MM/yyyy. and it´ll give the one digit number by doing the sum of all the digits in DOB until we get the only one digit. It´s the similar concept which we use at the time when we talk about the numerology.

corejava x 353
programming x 169
Posted On : 2016-03-11 00:36:55.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188249150
up-rate
5
down-rate

Answers


import java.util.Scanner;

public class LuckyNumber {
public static void main(String[] args) {
int luckyNumber=0;
System.out.println("Enter the DOB in dd/MM/yyyy format:");
Scanner input=new Scanner(System.in);
String dob=input.nextLine();
for(int i=0;i<dob.length();i++){
if(dob.charAt(i)!=´/´){
/*Sum of all the digits in the DOB*/
luckyNumber=luckyNumber+Integer.parseInt(String.valueOf(dob.charAt(i)));
}
}

System.out.println("Lucky Number: "+getOneDigit(luckyNumber));

}

public static int getOneDigit(Integer number){
int result=number;
while(number.toString().length()>1){
result=0;
for(int i=0;i<number.toString().length();i++){
result=result+Integer.parseInt(String.valueOf(number.toString().charAt(i)));
}
number=result;
}
return result;
}

}


Output:
----------------------------
Enter the DOB in dd/MM/yyyy format:
06/09/1999
Lucky Number: 7

Posted On : 2016-03-11 00:52:08
Satisfied : 10 Yes  12 No
profile Garima Gupta - anyforum.in Garima Gupta
596129558962
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in