Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- public class Main
- {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter the first year: ");
- int startYear = Integer.parseInt(sc.nextLine());
- System.out.print("Enter the final year: ");
- int finalYear = Integer.parseInt(sc.nextLine());
- System.out.print("Enter your magic number: ");
- int magicNumber = Integer.parseInt(sc.nextLine());
- boolean found = true;
- LocalDate currentDate = LocalDate.of(startYear, 1, 1);
- //doing the calculations
- while(currentDate.getYear() <= finalYear){
- int d1 = currentDate.getDayOfMonth() /10; // first day digit
- int d2 = currentDate.getDayOfMonth() %10; //second day digit
- int d3 = currentDate.getMonthValue() / 10; // first month digit
- int d4 = currentDate.getMonthValue() % 10; // second month digit
- int d5 = currentDate.getYear() / 1000; // first year digit
- int d6 = (currentDate.getYear() / 100) % 10; // second year digit
- int d7 = ((currentDate.getYear() / 10)%100)%10;
- int d8 = currentDate.getYear() % 10;
- int dateWeight = d1*(d2+d3+d4+d5+d6+d7+d8) + d2*(d3+d4+d5+d6+d7+d8) + d3*(d4+d5+d6+d7+d8) + d4*(d5+d6+d7+d8) + d5*(d6+d7+d8) + d6*(d6+d7+d8) + d7*d8;
- if(dateWeight == magicNumber){
- DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
- System.out.println(outputFormatter.format(currentDate));
- found = true;
- }
- currentDate = currentDate.plusDays(1);
- }
- if(!found){
- System.out.println("NO!");
- }
- }
- }
Add Comment
Please, Sign In to add comment