In the previous article, we have discussed about Java Program to Convert Hour to Week and Week to Hour
In this article we will see how to convert Day to Week and Week to Day by using Java programming language.
Java Program to Convert Day to Week and Week to Day
Before jumping into the program let’s know the relationship between Day and Week and how we can convert Day to Week and vice versa.
Day is a period of 24 hours starting from midnight 12 AM to next 12AM which is nothing but 24 hours and Week is a combination of 7 days starting from Monday to Sunday.
1 Day = 0.142857 Week 1 Week = 7 Day
Formula to convert Week to Day.
Day = Week * 7
Formula to convert Day to Week.
Week = Day / 7
Let’s see different ways to convert Day to Week and Week to Day.
Method-1: Java Program to Convert Day to Week and Week to Day By Using Static Input Value
Approach:
- Declare Day and Week value.
- Then convert Day to Week and Week to Day 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); //day value declared double day = 1; //week value declared double week = 1; //converting day to week double wk = day / 7; //converting week to day double d = week * 7; //printing result System.out.println("Value of "+day+" day in week: "+ wk); System.out.println("Value of "+week+" week in day: "+ d); } }
Output: Value of 1.0 day in week: 0.14285714285714285 Value of 1.0 week in day: 7.0
Method-2: Java Program to Convert Day to Week and Week to Day By Using User Input Value
Approach:
- Take user input of Day and Week value.
- Then convert Day to Week and Week to Day 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 day System.out.println("Enter value of day: "); double day = sc.nextDouble(); //Taking the value input of double variable week System.out.println("Enter value of week: "); double week = sc.nextDouble(); //converting day to week double wk = day / 7; //converting week to day double d = week * 7; //printing result System.out.println("Value of "+day+" day in week: "+ wk); System.out.println("Value of "+week+" week in day: "+ d); } }
Output: Enter value of day: 8 Enter value of week: 3 Value of 8.0 day in week: 1.1428571428571428 Value of 3.0 week in day: 21.0
Method-3: Java Program to Convert Day to Week and Week to Day By Using User Defined Method
Approach:
- Take user input of Day and Week value.
- Call a user defined method by passing Day and Week value as parameter.
- Inside method convert Day to Week and Week to Day 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 day System.out.println("Enter value of day: "); double day = sc.nextDouble(); //Taking the value input of double variable week System.out.println("Enter value of week: "); double week = sc.nextDouble(); //calling user defined method convert() convert(day, week); } //convert() method to convert day to week and vice versa public static void convert(double day, double week) { //converting day to week double wk = day / 7; //converting week to day double d = week * 7; //printing result System.out.println("Value of "+day+" day in week: "+ wk); System.out.println("Value of "+week+" week in day: "+ d); } }
Output: Enter value of day: 28 Enter value of week: 2 Value of 28.0 day in week: 4.0 Value of 2.0 week in day: 14.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: