- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Getting century from year in JavaScript
In the given problem statement we are required to get the century from the given year with the help of Javascript functionalities. So we will use the basic functions of Javascript to solve this problem.
Understanding the Problem
The problem at hand is to find the century number of the given input year. So basically we have to create an algorithm for finding the century number corresponding to the given year. So first understand what the century number is!
A century represents a period of 100 years, beginning from the year 1. For example, the 20th century refers to the years 1901 to 2000. This is how the output should be given by the function. So we will discuss the logic, algorithm and code to get the century from the year given in Javascript.
Logic for the given Problem
To solve the given problem to determine the century from a year, we will first create a function with a simple logic. First we will divide the given year by 100 and round it up to the nearest integer. So if the remainder is greater than 0, we will add 1 to the quotient to get the century. Otherwise the quotient itself will represent the century of the given year.
Algorithm
Step 1: As we have to find out the century of the given year. So first we will create a function and give it a name as getCentury. In this function we will pass a parameter of year. For this year's input we will find out the century.
Step 2: After defining the function, inside this function we will divide the year by 100 and after that round it up to the nearest integer.
Step 3: Now we will check the condition, that the remainder of the division is greater than 0 so add 1 to the quotient.
Step 4: If the above condition is not true then we will add the quotient itself as the century.
Step 5: At the end we will return the value of the calculated century as a result.
Example
//Function to get the year's century function getCentury(year) { var century = Math.ceil(year / 100); return century; } // Example usage var year = 2020; var year1 = 1995 var century = getCentury(year); var century1 = getCentury(year1); console.log("The century corresponding to " + year + " is " + century); console.log("The century corresponding to " + year1 + " is " + century1);
Output
The century corresponding to 2020 is 21 The century corresponding to 1995 is 20
Complexity
The time complexity for finding the century of the given year is O(1). Because the code performs a fixed number of operations besides the input given. The division and rounding operations take a constant amount of time which makes the algorithm efficient for any given year.
Conclusion
In Javascript we have determined the century for the given year. This is a straightforward process. As we have divided the year by 100 and round it up the quotient. After doing these operations we can obtain the century. The function allows us to calculate the century for any given year.