Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:39:34+00:00 2026-05-30T14:39:34+00:00

I’m pretty new to C# and got this assignment. I have a class called

  • 0

I’m pretty new to C# and got this assignment. I have a class called person with some details, of which one is the social security number (CPR number). From this number I need to calculate the age of this person. It needs to fit in this, according to assignment, if i understand it right:

public class Person
{
    int _Cpr;

    public int Age
    {
        get
        {
            return (age calculation from cpr number somehow)
        }
    }
}

I however have no idea how to do so – I’m guessing I need to compare the social security number integer having this form: DDMMYYXXXX to the current date, and return the difference in rounded down years – but I have no idea how to do so.


I’m starting to get the picture as how to calculate the age – What I’m still lost on, is how to fit this into the code i have. The description I have says that the Age should be a “calculated property” – But I can’t create variables to use inside a class definition as far as i know, so i can’t really do all the steps required to make Cpr into Age – Can I?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:39:35+00:00Added an answer on May 30, 2026 at 2:39 pm

    I assume this is homework so let me try to push you in the right direction instead of simply providing the answer.

    You have to extract the birth date from the social security number. By doing integer divisions by powers of 10 you can “chop” of digits from the right. By doing modulo operations by powers of 10 you can keep the number of digits you need:

    123456/100 = 1234
    1234%100 = 34
    

    In C# you can use the DateTime class to represent a point in time (e.g. the birth date). The field DateTime.Now contains the current time.

    You want to get the age in years (rounded down) so you will have substract the Year property of the two DateTime values. You also have to take into account if the person already had his birthday this year.


    Spoiler ahead

    The largest CPR is 3112999999 and that number is actually larger than Int32.MaxValue. This means that you have to change the type of _Cpr to uint (or long). The code below also takes into account the rule described by TheKaneda to get the four digit year from the two digit year.

    public class Person       
    {       
      uint _Cpr;       
    
      public int Age       
      {       
        get       
        {       
          int century = (int) ((cpr/1000)%10);
    
          int shortBirthYear = (int) ((cpr/10000)%100);
          int birthYear = 1900 + shortBirthYear;
          if ((century == 4 || century == 9) && shortBirthYear <= 36)
            birthYear += 100;
          else if (5 <= century && century <= 8 && shortBirthYear <= 57)
            birthYear += 100;
    
          int birthMonth = (int) ((cpr/1000000)%100);
    
          int birthDayOfMonth = (int) ((cpr/100000000));
    
          DateTime birthDate = new DateTime(birthYear, birthMonth, birthDayOfMonth);
          DateTime today = DateTime.Now.Date;
          DateTime birthDay = new DateTime(today.Year, birthMonth, birthDayOfMonth);
    
          int birthdayAdjustment = today < birthDay ? -1 : 0;
          int age = today.Year - birthDate.Year + birthdayAdjustment;
        }
      }
    }
    

    Even though the code is surprisingly complex it doesn’t handle the situation when a person is born on February 29. To improve the code it should be refactored into separate parts that deals with extracting the birth date and calculating the age. The last part should deal with the Feburary 29 issue and depending on culture you may discover that in non-leap year people born on February 29 celebrate their birthday February 28.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have this code to decode numeric html entities to the UTF8 equivalent character.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.