In the previous article, we have discussed about Java Program to Convert Day to Year and Year to Day
In this article we will see how to convert Week to Month and Month to Week by using Java programming language.
Java Program to Convert Week to Month and Month to Week
Before jumping into the program let’s know the relationship between Week and Month and how we can convert Week to Month and vice versa.
One week is combination of 7 days starting from Monday to Sunday. Month is a period time used in calendar which is 30 or 31 days long (For February 28 or 29 days(Leap year)). There are total 12 months starting from January to December in a year.
1 Week = 0.230137 Month 1 Month = 4.34524 Week
(For approximate result we have considered 1 month as 4.34524 weeks)
Formula to convert Month to Week.
Week = Month * 4.345
Formula to convert Week to Month.
Month = Week / 4.345
Let’s see different ways to convert Week to Month and Month to Week.
Method-1: Java Program to Convert Week to Month and Month to Week By Using Static Input Value
Approach:
- Declare Week and Month value.
- Then convert Week to Month and Month to Week 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); //week value declared double week = 1; //month value declared double month = 1; //converting week to month double m = week / 4.34524; //converting month to week double wk = month * 4.34524; //printing result System.out.println("Value of "+week+" week in month: "+ m); System.out.println("Value of "+month+" month in week: "+ wk); } }
Output: Value of 1.0 week in month: 0.23013688541944746 Value of 1.0 month in week: 4.34524
Method-2: Java Program to Convert Week to Month and Month to Week By Using User Input Value
Approach:
- Take user input of Week and Month value.
- Then convert Week to Month and Month to Week 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 week System.out.println("Enter value of week: "); double week = sc.nextDouble(); //Taking the value input of double variable month System.out.println("Enter value of month: "); double month = sc.nextDouble(); //converting week to month double m = week / 4.34524; //converting month to week double wk = month * 4.34524; //printing result System.out.println("Value of "+week+" week in month: "+ m); System.out.println("Value of "+month+" month in week: "+ wk); } }
Output: Enter value of week: 8 Enter value of month: 5 Value of 8.0 week in month: 1.8410950833555797 Value of 5.0 month in week: 21.726200000000002
Method-3: Java Program to Convert Week to Month and Month to Week By Using User Defined Method
Approach:
- Take user input of Week and Month value.
- Call a user defined method by passing Week and Month value as parameter.
- Inside method convert Week to Month and Month to Week 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 week System.out.println("Enter value of week: "); double week = sc.nextDouble(); //Taking the value input of double variable month System.out.println("Enter value of month: "); double month = sc.nextDouble(); //calling user defined method convert() convert(week, month); } //convert() method to convert week to month and vice versa public static void convert(double week, double month) { //converting week to month double m = week / 4.34524; //converting month to week double wk = month * 4.34524; //printing result System.out.println("Value of "+week+" week in month: "+ m); System.out.println("Value of "+month+" month in week: "+ wk); } }
Output: Enter value of week: 24 Enter value of month: 12 Value of 24.0 week in month: 5.523285250066739 Value of 12.0 month in week: 52.142880000000005
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: