Java Program to Convert Year to Century and Century to Year

In this article we will see how to convert Year to Century and Century to Year by using Java programming language.

Java Program to Convert Year to Century and Century to Year

Before jumping into the program let’s know the relationship between Year and Century and how we can convert Year to Century and vice versa.

Year is a time period of 12 months starting from January to December. While 100 years called as a Century.

1 Year = 0.01 Century
1 Century = 100 Year

Formula to convert Century to Year.

Year = Century * 100

Formula to convert Year to Century.

Century = Year / 100

Let’s see different ways to convert Year to Century and Century to Year.

Method-1: Java Program to Convert Year to Century and Century to Year By Using Static Input Value

Approach:

  • Declare Year and Century value.
  • Then convert Year to Century and Century to Year by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //initialized value of year
        double year = 1;
        //initialized value of century
        double century = 1;
        
        //converting year to century
        double c = year / 100;
        //converting decade to year
        double y = century * 100;
        //printing result
        System.out.println("Value of "+year+" year in century: "+ c);   
        System.out.println("Value of "+century+" century in year: "+ y);   
   }
}
Output:

Value of 1.0 year in century: 0.01
Value of 1.0 century in year: 100.0

Method-2: Java Program to Convert Year to Century and Century to Year By Using User Input Value

Approach:

  • Take user input of Year and Century value.
  • Then convert Year to Century and Century to Year by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable year
        System.out.println("Enter value of year: ");  
        double year = sc.nextDouble();
        //Taking the value input of double variable century
        System.out.println("Enter value of century: ");  
        double century = sc.nextDouble();
        
        //converting year to century
        double c = year / 100;
        //converting decade to year
        double y = century * 100;
        //printing result
        System.out.println("Value of "+year+" year in century: "+ c);   
        System.out.println("Value of "+century+" century in year: "+ y);   
   }
}
Output:

Enter value of year: 
200
Enter value of century: 
3
Value of 200.0 year in century: 2.0
Value of 3.0 century in year: 300.0

Method-3: Java Program to Convert Year to Century and Century to Year By Using User Defined Method

Approach:

  • Take user input of Year and Century value.
  • Call a user defined method by passing Year and Century value as parameter.
  • Inside method convert Year to Century and Century to Year by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable year
        System.out.println("Enter value of year: ");  
        double year = sc.nextDouble();
        //Taking the value input of double variable century
        System.out.println("Enter value of century: ");  
        double century = sc.nextDouble();
         //calling user defined method convert()
        convert(year, century);
   }
   
   //convert() method to convert year to century and vice versa
   public static void convert(double year, double century)
   {
        //converting year to century
        double c = year / 100;
        //converting decade to year
        double y = century * 100;
        //printing result
        System.out.println("Value of "+year+" year in century: "+ c);   
        System.out.println("Value of "+century+" century in year: "+ y);   
   }
}
Output:

Enter value of year: 
167
Enter value of century: 
4
Value of 167.0 year in century: 1.67
Value of 4.0 century in year: 400.0

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: