# 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? Jul 23 '18 at 15:25
• @LuisfelipeDejesusMunoz Probably because it is trivial. Jul 23 '18 at 19:26
• Since when we are downvoting trivial challenges? Jul 24 '18 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. Jul 24 '18 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
Jul 25 '18 at 15:34

# Pepe, 31 bytes

rEeEEeeEeeREeEREEEEeeREeEEEReEE


Try it online!

Basically ceil(n/100).

Explanation:

rEeEEeeEeeREeEREEEEeeREeEEEReEE - full program

rEeEEeeEee                      - push 100 to stack B
REeE                  - input as number to stack A. float gets rounded.
REEEEee           - A / B, or A / 100. push in stack A.
REeEEE     - ceil(A)
ReEE - output stack A as number


# MATL, 7 bytes

q100/kQ


Try it online!

• Wouldn’t 100/Xk do? Jul 23 '18 at 17:10

# Jelly, 4 bytes

÷ȷ2Ċ


Try it online!

# Gaia, 4 bytes

(ℍ/)


Pretty straightforward...

Thanks to Mr. Xcoder and this post for getting me interested in this cool but relatively unused language!

Try it online!

• @JonathanAllan True, edited. Jul 23 '18 at 16:41

# Labyrinth, 10 bytes

?(_100/)!@


Same method as my Gaia answer.

Try it online!

# Kotlin, 19 bytes

{y:Int->(y+99)/100}


Try it online!

# Scala, 24 bytes

def f(a:Int)=(a-1)/100+1


Try it online!

# Scala, 33 bytes, with floats

def f(a:Float)=(Int)((a-1)/100)+1


Try it online!

# TeaScript, 8 bytes

Mc(x / h


Try it online!

# CJam, 6 bytes

(100/)


Try it online!

# K (oK), 7 bytes

Solution:

-_-.01*


Try it online!

Explanation:

Pretty trivial... almost looks like an emoji (-_-):

-_-.01* / the solution
* / multiply input by
.01  / 0.01
-     / negate  \
_      / floor    - ceiling the result
-       / negate  /


# TI-Basic, 6 bytes

When given only one argument, sub( divides it by 100.

sub(Ans+99


# Octave/MATLAB, 15 14 bytes

@(x)(x+49)/100


Try it online!

Managed to save a byte by requiring the input to be provided as an integer not a double.

This anonymous function takes an integer as an input, and returns the required value.

MATLAB/Octave when dividing integers performs rounding rather than truncation (Why Mathworks? Why?!). To convert rounding to ceil, we need to add on 49 to the number prior to division.

Original, perhaps uninteresting answer:

@(x)ceil(x/100)


Try it online!

Anonymous function to find the result. Just the standard ceil() of dividing by 100.

# Excel VBA, 13 bytes

An anonymous function that takes input from cell [A1] and outputs to the console.

?[A1-1]\100+1

• Hahaha. I went all way in creating a function Jul 25 '18 at 12:04

## Python 2, 18 bytes

lambda x:-(x/-100)


Taking advantage of the fact / divides towards 0, we make a impromptu ceil division, assuming all inputs are positive which is the case according to the spec. Would work in Python 3 with // but it's golfier in Py2.

Uses ceil(year/100)

# Whitespace, 51 50 bytes

[S S S T    N
_Push_1][S N
S _Duplicate_1][S N
S _Duplicate_1][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][S S S T    N
_Push_1][T  S S T   _Subtract][S S S T  T   S S T   S S N
_Push_100][T    S T S _Integer_divide][T    S S S _Add][T   N
S T _Print_as_integer]


Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

-1 byte thanks to @aschepler.

Explanation in pseudo-code:

Integer n = STDIN as integer
n = n - 1
n = n integer-divided by 100
n = n + 1
Print n as integer to STDOUT

• You can take off one byte by removing one Dup (-3) and replacing the Swap (-3) with a Push 1 (+5). Jul 25 '18 at 21:42
• @aschepler Ah, of course. Thanks! Jul 26 '18 at 6:52

# Ruby, 24 bytes

def f(n);(n-1)/100+1;end


Try it online!

# Bash + coreutils, 18 bytes

bc<<<"(\$1+99)/100"


Try it online!

• When shell submissions use external programs we typically mention that in the language field to distinguish them from pure shell solutions. So in this case consider specifying the language as "Bash + coreutils" or "Bash + bc". Jul 25 '18 at 7:09

# Common Lisp, 2919 18 bytes

(ceiling(/ x 100))


Or, if variable definitions aren't allowed, 21 bytes

(ceiling(/(read)100))


Pretty self-explanatory, can be tested with something along these lines:

(setf x 1900)
(print (ceiling (/ x 100)))

• a snippet is not allowed (i.e. you can't assume x is defined) Jul 26 '18 at 10:51
• @ASCII-only I assumed it was allowed, considering most of the other answers rely on a variable/state being already defined. For example, both Excel answers, TI-Basic, Octave/Matlab Jul 26 '18 at 12:49
• Well, Excel is kinda a special case (it's not even a programming language), Ans in TI-BASIC can be considered STDIN (well, at least I think so), and Octave/Matlab defines a lambda Jul 27 '18 at 7:36
• I’d argue that Ans in TI basic is definitely a variable. It performs like one in all TI Basic programs. However, when I get home, I’ll amend my answer to better accommodate those rules. Jul 28 '18 at 15:20
• Hmm, thinking about it, you're right. Then in that case I'd say that answer is invalid as well Jul 30 '18 at 5:43

# C (gcc), 19 bytes

f(x){x=x/100.+.99;}


Try it online!

# Vim (5 bytes)

99^AXx


This code looks for number at or on the right of the cursor, adds 99 (^A99) and divides it by 100 (Xx).

Note: ^A is single byte (0x1 – Start Of Heading).

# MAWP, 11 bytes

@1A554WWPM:


Try it!

Saved 3 bytes thanks to @Razetime

• 9 bytes Sep 13 '20 at 15:32
• @Razetime your variant doesn't handle edge cases like 1900. However, still saved some bytes by adding 1A
– Dion
Sep 14 '20 at 5:52
• oh, silly mistake in that one. Sep 14 '20 at 5:53

# befunge 11 bytes

&1-aa*/1+.@@ 

• & read input,
• 1- subtract one (2000 is 20th century)
• aa*/ divide by 100
• 1+ add one
• . print
• @ end

# APL (Dyalog), 6 bytes

⌈⎕÷100


My first APL answer!

Thanks to @dzaima for saving me a load of bytes!

Try it online!

• @dzaima That's a stupid rule... Jul 23 '18 at 15:43
• Well blame the people who didn't believe in year 0 :p Jul 23 '18 at 15:47

# Charcoal, 7 bytes

Ｉ⌈∕Ｎ¹⁰⁰


Try it online! Link is to verbose version of code. Explanation:

    ¹⁰⁰ Literal 100
Ｎ    Input number
∕     Floating-point division
⌈      Ceiling
Ｉ       Cast to string
Implicitly print


# Noether, 7 bytes

I100/UP


Try it online

# Python 3, 30 bytes

print((int(input())-1)//100+1)


Try it online!

# Math++, 12 bytes

_(.99+?/100)


# Lua, 21 bytes

print((...-1)//100+1)


Try it online!

I'm not sure if I can get the year as an argument, but that's how I did it.

EXCEL, 17 bytes

A1 as input

=ROUNDUP(A1/100,)

# Java, 19 bytes

y->Math.ceil(y/100)


Try it online.

In java, the ceil function helps. Input and output are both double.

• Hello and welcome to PPCG! This question is code golf, so you want to make your submission as short as possible (I believe you can remove the spaces.) Also please provide a header with the name of the language you submitted and your answer's bytecount. Jul 23 '18 at 18:38
• Welcome to PPCG! Your code currently seems to give a few incorrect answers. Jul 24 '18 at 6:58
• @KevinCruijssen it does happen to work if you take input as a double instead of an int... Jul 24 '18 at 17:29
• @ETHproductions Oh, didn't notice that but you're right. I've edited the answer to our standards. Please take a look at it so you can see how to do it yourself next time akhil7886. And once again welcome to PPCG. Jul 25 '18 at 7:09
• @KevinCruijssen BTW you don't need the ds after the year: tio.run/… Jul 25 '18 at 10:27