Wednesday, July 29, 2015

Java Code to Print age of user from B.Date

import java.util.*;
class Demo{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);       // to fetch user input
System.out.println("Enter B.date (dd-mm-yyyy):");
String bdate = sc.nextLine();
String year = bdate.substring(6); // to extract yyyy
int y = Integer.parseInt(year);  // convert into numberic value
int age = 2015 - y;                   // find age
System.out.println("Your age is "+ age+" hopefully");

// ans may not be correct as we need to take month into account as well.
}
}

No comments:

Post a Comment