In the previous article, we have discussed about Java Program to Convert Month to Decade and Decade to Month
In this article we will see how to convert Month to Century and Century to Month by using Java programming language.
Java Program to Convert Month to Century and Century to Month
Before jumping into the program let’s know the relationship between Month and Century and how we can convert Month to Century and vice versa.
Month is a period time used in calendar which is 30 or 31 days long.(For February 28 or 29 days(if Leap year)). While Century is a period of 100 years.
1 Month = 0.000833334 Century 1 Century = 1200 Month
Formula to convert Century to Month.
Month = Century * 1200
Formula to convert Month to Century.
Century = Month / 1200
Let’s see different ways to convert Month to Century and Century to Month.
Method-1: Java Program to Convert Month to Century and Century to Month By Using Static Input Value
Approach:
- Declare Month and Century value.
- Then convert Month to Century and Century to Month 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 month double month = 1; //initialized value of century double century = 1; //converting month to century double c = month / 1200; //converting century to month double m = century * 1200; //printing result System.out.println("Value of "+month+" month in century: "+ c); System.out.println("Value of "+century+" century in month: "+ m); } }
Output: Value of 1.0 month in century: 8.333333333333334E-4 Value of 1.0 century in month: 1200.0
Method-2: Java Program to Convert Month to Century and Century to Month By Using User Input Value
Approach:
- Take user input of Month and Century value.
- Then convert Month to Century and Century to Month 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 month System.out.println("Enter value of month: "); double month = sc.nextDouble(); //Taking the value input of double variable century System.out.println("Enter value of century: "); double century = sc.nextDouble(); //converting month to century double c = month / 1200; //converting century to month double m = century * 1200; //printing result System.out.println("Value of "+month+" month in century: "+ c); System.out.println("Value of "+century+" century in month: "+ m); } }
Output: Enter value of month: 1600 Enter value of century: 2 Value of 1600.0 month in century: 1.3333333333333333 Value of 2.0 century in month: 2400.0
Method-3: Java Program to Convert Month to Century and Century to Month By Using User Defined Method
Approach:
- Take user input of Month and Century value.
- Call a user defined method by passing Month and Century value as parameter.
- Inside method convert Month to Century and Century to Month 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 month System.out.println("Enter value of month: "); double month = 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(month, century); } //convert() method to convert month to century and vice versa public static void convert(double month, double century) { //converting month to century double c = month / 1200; //converting century to month double m = century * 1200; //printing result System.out.println("Value of "+month+" month in century: "+ c); System.out.println("Value of "+century+" century in month: "+ m); } }
Output: Enter value of month: 2000 Enter value of century: 1.5 Value of 2000.0 month in century: 1.6666666666666667 Value of 1.5 century in month: 1800.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: