Create a program that reads the users name and year of birth from input(keyboard), then print out their name and age in decades(find out the meaning). Sample run 1: Sample run 2: Enter your name: Hausiku Enter your name: Johanna Enter your year of birth:1999 Enter your year of birth:2005 Output: Hello Hausiku, you have lived for 2 Output: Hello Johanna, you have lived for 1 decade(s) decade(s)
package decades;
import java.util.Scanner;
public class Decades {
public static void main(String[] args) {
System.out.println("Enter your name: \n");
Scanner scan = new Scanner(System.in);
String name = scan.next();
System.out.println("Enter your year of birth: \n");
int year = scan.nextInt();
int decades = (2021-year) / 10;
System.out.printf("Hello %s, you have lived for %d decades", name,decades);
}
}
Comments
Leave a comment