Java Program to Check if Your Birthday Occurs in Pi

Write a Java program to check whether your birthday (in DDMMYY format) occurs in the first one million digits of Pi. Assume that there exists a text file “pi.txt” that contains first one million digits of Pi. If required, you may download this text file for free, from the following link:
First One Million Digits of Pi

import java.io.*;
import java.util.Scanner;
class Birthday{
    public static void main(String[] args)throws IOException{
        Scanner in = new Scanner(System.in);
        System.out.print("Your birthday in DDMMYY format: ");
        String date = in.nextLine();
        if(!valid(date)){
            System.out.println("INVALID DATE FORMAT");
            return;
        }
        FileReader fr = new FileReader("pi.txt");
        BufferedReader br = new BufferedReader(fr);
        String line = "";
        boolean found = false;
        while((line = br.readLine()) != null){
            if(line.contains(date)){
                System.out.println("Your birthday occurs in pi!");
                found = true;
                break;
            }
        }
        if(!found)
            System.out.println("Your birthday doesn't occur in pi.");
    }
    public static boolean valid(String d){
        if(d.length() != 6)
            return false;
        for(int i = 0; i < d.length(); i++){
            char ch = d.charAt(i);
            if(!Character.isDigit(ch))
                return false;
        }
        return true;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *