Problem of the Day
A new programming or logic puzzle every Mon-Fri

Y2K Age Finder

Your goal for today is to write a program that asks a user for their birth year encoded as two digits (eg "90") and for the current year, also encoded as two digits (eg "14"). The program should then write out the user's age in years. Example input/output:

Year of Birth: 90
Current year: 14
Your age: 24

Permalink: http://problemotd.com/problem/y2k-age-finder/

Comments:

  • keemz - 10 years, 8 months ago

    
    print 'year of birth:'
    byear = raw_input()
    print 'current year:'
    curyear = raw_input()
    
    
    if curyear < byear:
            curyear = (int) ('2' + curyear)
    else:
            curyear = (int) ('1' + curyear)
    
    byear = (int) ('1' + byear)
    
    age = curyear - byear
    print 'your age: ' + str(age)
    
    

    reply permalink

  • keemz - 10 years, 8 months ago

    
    if curyear < byear:
        curyear = 200 + int(curyear)
    else:
        curyear = 100 + int(curyear) 
    byear = 100 + int(byear)
    
    

    reply permalink

  • lash - 10 years, 8 months ago

    Here is my solution in Java:

    public class Main {
        public static void main(String[] args) {
            int age;
            Scanner s = new Scanner(System.in);
            System.out.println("When were you born?");
            int birth = s.nextInt();
            System.out.println("What is the current year?");
            int current = s.nextInt();
            s.close();
            if(birth < current){
                //born post 2k
                age = current - birth;
            }else{
                int pre2k = 100 - birth;
                int post2k = current;
                age = pre2k + post2k;
            }
            System.out.println("You are " + age + " years old.");
        }
    }
    

    reply permalink

  • lashW - 10 years, 8 months ago

    Oh and obviously it should have:

    import java.util.Scanner;
    

    at the top :)

    reply permalink

  • Anonymous - 10 years, 8 months ago

    c# ``` var birth = 0; var current = 0;

        Console.WriteLine("2 digit birth year?");
        while( !int.TryParse(Console.ReadLine(), out birth) || Math.Floor(Math.Log10(birth) + 1) != 2)
            continue;
    
        Console.WriteLine("2 digit current year?");
        while(!int.TryParse(Console.ReadLine(), out current) || Math.Floor(Math.Log10(current) + 1) != 2)
            continue;
    
        Console.WriteLine(current - birth < 0 ? current-birth+100 : current - birth);
    

    reply permalink

  • Emre Ozdemir - 10 years, 8 months ago

    #include <iostream>
    using namespace std;
    int main (void){
        int born, current;
        cout<<"Year of birth: ";
        cin>>born;
        cout<<"Current year: ";
        cin>>current;
        cout<<"Your age: "<<current-born+100;
        getchar();
        getchar();
    }
    

    reply permalink

  • Will - 10 years, 8 months ago

    Easy-peasy lemon squeezy in LISP!

    (defun find-age-helper (birth-year this-year)
      (+ (- 100 birth-year) this-year))
    
    (defun find-age ()
      (let ((birth-year 0)
        (current-year 0))
        (format t "Year of Birth: ")
        (setf birth-year (read))
        (format t "Current year: ")
        (setf current-year (read))
        (format t "Your age: ~D" (find-age-helper birth-year current-year))))
    

    reply permalink

  • Pearce Keesling - 10 years, 7 months ago

    As far as I can tell this is probably the most efficient way to do this (there might be little tweaks, but I mean the general idea). Wrote this up in C++.

    #include <iostream>
    
    using namespace std;
    int main()
    {
        int birth;
        cout << "Year of birth: ";
        cin >> birth;
        int curr;
        cout << "Current Year: ";
        cin >> curr;
        int res = curr - birth;
        cout << "Your age: " << res+(res < 0?100:0)<<endl;
        return 0;
    }
    

    reply permalink

  • brocksrockgym - 10 years, 7 months ago

    import java.util.Scanner;
    public class Y2K
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            int yearBorn = 0;
            int currentYear = 0;
    
            System.out.print("Year born: ");
            yearBorn = in.nextInt();
            System.out.print("Current year: ");
            currentYear = in.nextInt();
            if(yearBorn <= currentYear)
                yearBorn += 100;
            System.out.println("Current age: " + (((currentYear+100)-yearBorn)));
        }
    }
    

    reply permalink

  • rossthebossperot - 10 years, 2 months ago

    Some Python

    def agefinder(birth,current):
        age = 0
        age = (100 - int(birth)) + int(current)
        return(age)
    
    yourage = agefinder(input('Last 2 digits of your birth year:'),input('Last 2 digits of current year:'))
    
    print('You are',yourage,'Years old.')
    

    reply permalink

  • rossthebossperot - 10 years, 2 months ago

    And a one liner for good measure

    print('You are',(100-(int(input('Birth year:')))) + (int(input('Current year:'))))
    

    reply permalink

Content curated by @MaxBurstein