TheBulgarianWolf

Magic Number - Dates

Mar 26th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.time.LocalDate;
  3. import java.time.format.DateTimeFormatter;
  4. public class Main
  5. {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         System.out.print("Enter the first year: ");
  9.         int startYear = Integer.parseInt(sc.nextLine());
  10.         System.out.print("Enter the final year: ");
  11.         int finalYear = Integer.parseInt(sc.nextLine());
  12.         System.out.print("Enter your magic number: ");
  13.         int magicNumber = Integer.parseInt(sc.nextLine());
  14.         boolean found = true;
  15.         LocalDate currentDate = LocalDate.of(startYear, 1, 1);
  16.  
  17.     //doing the calculations
  18.     while(currentDate.getYear() <= finalYear){
  19.       int d1 = currentDate.getDayOfMonth() /10; // first day digit
  20.       int d2 = currentDate.getDayOfMonth() %10; //second day digit
  21.       int d3 = currentDate.getMonthValue() / 10; // first month digit
  22.       int d4 = currentDate.getMonthValue() % 10; // second month digit
  23.       int d5 = currentDate.getYear() / 1000; // first year digit
  24.       int d6 = (currentDate.getYear() / 100) % 10; // second year digit
  25.       int d7 = ((currentDate.getYear() / 10)%100)%10;
  26.       int d8 = currentDate.getYear() % 10;
  27.      
  28.  
  29.       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;
  30.        if(dateWeight == magicNumber){
  31.       DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
  32.       System.out.println(outputFormatter.format(currentDate));
  33.       found = true;
  34.       }
  35.  
  36.       currentDate = currentDate.plusDays(1);
  37.     }
  38.    
  39.  
  40.     if(!found){
  41.       System.out.println("NO!");
  42.     }
  43.    
  44.  
  45.    
  46.     }
  47. }
Add Comment
Please, Sign In to add comment