Categories
Class 10 Class 11 Class 12

Generate Password using Name and Age in Java

Write a program that accepts a student’s first name, middle name, last name and age in years. It then generates a password as per the following specifications:

<first and last letter of last name> <first digit of age> <first and last letter of first name> <second digit of age> <first and last letter of middle name>

Example:
INPUT:
Full Name: Pichai Sundara Rajan
Age in years: 45
OUTPUT: rn4pi5sa

import java.io.*;
class Password{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Full name: ");
        String name = br.readLine();
        System.out.print("Age in years: ");
        int age = Integer.parseInt(br.readLine());
        String first = name.substring(0, name.indexOf(' '));
        String middle = name.substring(name.indexOf(' ') + 1, name.lastIndexOf(' '));
        String last = name.substring(name.lastIndexOf(' ') + 1);
        age = age % 100;
        int firstDigit = age / 10;
        int lastDigit = age % 10;
        String p = last.charAt(0) + "" + last.charAt(last.length() - 1);
        p += firstDigit;
        p += first.charAt(0) + "" + first.charAt(first.length() - 1);
        p += lastDigit;
        p += middle.charAt(0) + "" + middle.charAt(middle.length() - 1);
        p = p.toLowerCase();
        System.out.println(p);
    }
}