Java Program to Convert Year to Decade and Decade to Year

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

Java Program to Convert Year to Decade and Decade to Year

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

Year is a time period of 12 months starting from January to December. While Decade is a period of 10 years.

1 Year = 0.1 Decade
1 Decade = 10 Year

Formula to convert Decade to Year.

Year = Decade * 10

Formula to convert Year to Decade.

Decade = Year / 10

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

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

Approach:

  • Declare Year and Decade value.
  • Then convert Year to Decade and Decade 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 decade
        double decade = 1;
        
        //converting year to decade
        double d = year / 10;
        //converting decade to year
        double y = decade * 10;
        //printing result
        System.out.println("Value of "+year+" year in decade: "+ d);   
        System.out.println("Value of "+decade+" decade in year: "+ y);   
   }
}
Output:

Value of 1.0 year in decade: 0.1
Value of 1.0 decade in year: 10.0

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

Approach:

  • Take user input of Year and Decade value.
  • Then convert Year to Decade and Decade 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 decade
        System.out.println("Enter value of decade: ");  
        double decade = sc.nextDouble();
        
        //converting year to decade
        double d = year / 10;
        //converting decade to year
        double y = decade * 10;
        //printing result
        System.out.println("Value of "+year+" year in decade: "+ d);   
        System.out.println("Value of "+decade+" decade in year: "+ y);   
   }
}
Output:

Enter value of year: 
12
Enter value of decade: 
2
Value of 12.0 year in decade: 1.2
Value of 2.0 decade in year: 20.0

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

Approach:

  • Take user input of Year and Decade value.
  • Call a user defined method by passing Year and Decade value as parameter.
  • Inside method convert Year to Decade and Decade 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 decade
        System.out.println("Enter value of decade: ");  
        double decade = sc.nextDouble();
         //calling user defined method convert()
        convert(year, decade);
   }
   
   //convert() method to convert year to decade and vice versa
   public static void convert(double year, double decade)
   {
        //converting year to decade
        double d = year / 10;
        //converting decade to year
        double y = decade * 10;
        //printing result
        System.out.println("Value of "+year+" year in decade: "+ d);   
        System.out.println("Value of "+decade+" decade in year: "+ y);   
   }
}
Output:
Enter value of year: 
24
Enter value of decade: 
1.5
Value of 24.0 year in decade: 2.4
Value of 1.5 decade in year: 15.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: