# Get the century of a year

Your program has to have an year like 1957 to be the input and then output the century of that year.

For example:

In: 1946
Out: 20
In: 1705
Out: 18
In: 1900
Out: 19
In: 100
Out: 1
In: 2001
Out 21


because 1946 is in the 20th century.

Keep in mind that 2000 should be 20th century or 1900 should be in 19th century.

Therefore, the first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.

Any programming language is allowed and keep your code short and sweet. :)

Additional Challenge: Try to also include float values

• "Try to also include float values" Huh? What do you mean? Commented Jul 23, 2018 at 15:25
• @LuisfelipeDejesusMunoz Probably because it is trivial. Commented Jul 23, 2018 at 19:26
• Since when we are downvoting trivial challenges? Commented Jul 24, 2018 at 12:43
• @DeadPossum I downvoted because this challenge is neither interesting nor golf-able. There is no algorithmic complexity in finding the century: nearly all answers simply implement the expression floor((year - 1)/100) + 1, and there aren't other clever optimizations that can be done to shorten the overall program; there aren't any "alternative approaches" to the challenge. Since most answers implement the exact same expression, this challenge looks no more interesting than a list of "floor," "decrement," "divide," and "increment" functions in various languages. Commented Jul 24, 2018 at 14:51
• How far in the future must our answers be correct until? Is it acceptable to only produce correct input up to the present year? (It matters in the R answer, we can save 2 bytes by only being correct up to the year 9998, possibly 9999)
– JDL
Commented Jul 25, 2018 at 15:34

# ArnoldC, 308 298 bytes

IT'S SHOWTIME
HEY CHRISTMAS TREE x
YOU SET US UP 0
GET YOUR ASS TO MARS x
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
GET TO THE CHOPPER x
HERE IS MY INVITATION x
GET UP 99
ENOUGH TALK
TALK TO THE HAND x
YOU HAVE BEEN TERMINATED


Try it online!

This seemed a challenge easy enough to try to write my first answer in ArnoldC...

• Upvote cause it's ArnoldC Commented Jul 24, 2018 at 8:02
• Please vote on the solution, not the language. Commented Jul 27, 2018 at 20:33

# JavaScript, 14 bytes

y=>1+--y/100|0


Try it online

# dc, 6

Assuming input and output on the top-of-stack is OK:

99+A0/


Try it online!

# Java 8, 12 bytes

y->~-y/100+1


Try it online.

Explanation:

y->     // Method with integer as both parameter and return-type
~-y   //  Decrease the input by 1
/100  //  Then integer-divide it by 100
+1    //  And then add 1

• nah, I guess youll have to provide wrapping stuff, too. Commented Jul 24, 2018 at 12:52
• @LMD This has been discussed many times in the meta ever since functions were allowed as alternative for full programs, but Java and C# doesn't need to add the definitions, parameter-types, nor trailing semi-colon to the code for lambdas (recursive methods and imports are a different story of course). Commented Jul 24, 2018 at 13:09
• If I knew those rules... sniff Commented Jul 24, 2018 at 13:10
• +1, though I prefer y->--y/100+1 for the sake of readability. Commented Jul 24, 2018 at 22:33
• @KevinCruijssen No worries, it's only a personal preference. Your answer is as clear as the suggestion, plus you explain the code, so what more can we ask for? ;) Commented Jul 25, 2018 at 15:25

# C (gcc), 23 22 bytes

f(i){return--i/100+1;}


Try it online!

-1 byte saved thanks to @luser droog

# 20 bytes

R;f(i){R=--i/100+1;}


Try it online!

-2 bytes saved thanks to @Digital Trauma

# 18 bytes

f(i){i=--i/100+1;}


Try it online!

Another -2 bytes saved thanks to @ErikF

• Any tips on improving the score? I feel like a couple bytes could be saved Commented Jul 23, 2018 at 18:58
• If you're OK with a bit of undefined behaviour, you can do this for 20. Commented Jul 23, 2018 at 20:57
• Drop the space after return. Commented Jul 24, 2018 at 3:42
• You can reuse the i as its own return (18 bytes): Try it online! Commented Jul 24, 2018 at 7:09
• @ErikF that's absolutely amazing Commented Jul 24, 2018 at 8:33

# Stax, 5 bytes (packed)

vAJ/^


Run and debug it

Explanation:

vAJ/^ Full program, implicit input
v     Decrement
AJ   Push 100 (102)
/  Integer division
^ Increment

# Perl 6, 13 bytes

(*+99)/100+|0


Test it

## Expanded:

# WhateverCode lambda
(
*      # this is the parameter

) / 100  # divide by 100

+| 0     # Integer bitwise-or with 0 (coerce to Int)


# GolfScript, 5 7 bytes

~(100/)


Try it online!

~(100/)
AAA  AA
\\\   \\
\\\   \Increments the stack
\\\   Divides the stack
\\\
\Decrements the stack
Turn the input string into an int

• Note that e.g. 2001 is in the 21st century. This one works: ~(100/) Commented Jul 24, 2018 at 11:44

# Python 2, 18 bytes

lambda x:~-x/100+1


Try it online!

• Putting 100 in seems to return 1.99 Commented Jul 23, 2018 at 23:57
• @Acccumulation no?
– Rod
Commented Jul 24, 2018 at 0:47
• Sorry, I missed that it was Python 2 rather than 3. Commented Jul 24, 2018 at 15:04
• @Acccumulation Duplicate the / and it´s both Python 2 and 3. Commented Jul 25, 2018 at 9:37

(div100).(+99)


Try it online!

# 16 bytes

f x=div(x+99)100


Try it online!

# PHP, 22 bytes

<?=1+--$argv[1]/100|0;  Try it online <?=ceil($argv[1]/100);


Try it online

• It's a bit awkward, but you can save 1 byte using -R: Try it online! Commented Jul 23, 2018 at 20:21

# PHP, 19 bytes

<?=ceil($argn/100);  or <?=--$argn/100+1|0;


Save to file, run as pipe with -nF.

Excel Vba, 89 bytes

Function GetCentury(Year As Long)
GetCentury = Mid((Year - 1) / 100, 1, 2) + 1
End Function


And a version with 65 bytes

Function C(Y As Long)
C = Mid((Y - 1) / 100, 1, 2) + 1
End Function


Honestly I dont even know if this is a valid entry :/

• Welcome to PPCG! Usually you would state your golfed version first and add a longer version as explanation only. Thus your score 65 bytes. I'm not familiar with VBA, but can you remove some of the spaces in C = Mid((Y - 1) / 100, 1, 2) + 1? Finally you may want to check out our Tips for Golfing in VBA. Have fun! Commented Jul 25, 2018 at 13:18

# Shakespeare Programming Language, 195 bytes

,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:Listen toheart!You is the sum ofa cat the quotient betweenthe sum ofyou a pig the square ofthe sum ofa big big big cat a big cat!Open heart


Try it online!

### Explanation:

,.Ajax,.Page,.Act I:.Scene I:.[Enter Ajax and Page]Ajax:   #Header

Listen toheart!   # Set Page to the input

# Set Page to (n-1)/(10)**2+1
You is the sum ofa cat the quotient betweenthe sum ofyou a pig the square ofthe sum ofa big big big cat a big cat!

Open heart  # Print Page's value as a number


# Elixir, 89 78 bytes

Not perfect but I'm just learning Elixir so code-golf it is!

IO.gets("")|>String.to_integer|>(&(Integer.floor_div(100,&1-1)+1)).()|>IO.puts


Try it online!

In a better format:

IO.gets("")                # Get input from terminal
|>String.to_integer        # Make it an int
|>(&(                      # Declare an anonymous function
Integer.floor_div(100,   # Do a floored division with 100 on the bottom
&1-1)                  # ... and the integer input minus 1 on top
+1))                    # Add 1 to the result
.()                      # Gotta invoke that anonymous for pipes
|>IO.puts                  # Output


# Pyth - 6 7 bytes

.EcQ100


Explanation:

.EcQ100  Implicit print
Q     Input
/      Divided by
100  100
.E       Rounded up


Fixed edge case

• I believe this fails the test case for input 1900. Should return 19, instead of 20 Commented Jul 25, 2018 at 18:21