0% found this document useful (0 votes)
26 views3 pages

The Age Comparison Calculator-1

Uploaded by

التعزي
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

The Age Comparison Calculator-1

Uploaded by

التعزي
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Course name: Solving software problems

Article code: 1311 CS

Section No.: 4410

Participating female students:

Fajr Yahya Asiri

University number: 445913661

Raghad Abdullah Al-Qarni

University number: 445809078


The code:
import java.time.LocalDate;
import java.time.Period;
import java.util.Scanner;
public class AgeComparisonCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter birthdate for Person A (YYYY-MM-DD):");
String birthdateA = scanner.nextLine();
LocalDate dateOfBirthA = LocalDate.parse(birthdateA);
System.out.println("Enter birthdate for Person B (YYYY-MM-DD):");
String birthdateB = scanner.nextLine();
LocalDate dateOfBirthB = LocalDate.parse(birthdateB);
int ageA = calculateAge(dateOfBirthA);
int ageB = calculateAge(dateOfBirthB);
int ageDifference = Math.abs(ageA - ageB);
System.out.println("Person A's Age: " + ageA + " years");
System.out.println("Person B's Age: " + ageB + " years");
System.out.println("Age Difference: " + ageDifference + " years");
scanner.close();
}
private static int calculateAge(LocalDate birthDate) {
LocalDate currentDate = LocalDate.now();
return Period.between(birthDate, currentDate).getYears();
}
}
Output:

You might also like