Java Program to Convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch

In this article we will see how to convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch by using Java programming language.

Java Program to Convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch

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

Both Cubic Inch and Cubic Foot are used as unit of measuring volume of three dimensional object when length, width and height are given

1 Cubic Foot =  1728 Cubic Inch
1 Cubic Inch  = 0.000578704 Cubic Foot

Formula to convert Cubic Inch to Cubic Foot.

Cubic Foot = Cubic Inch / 1728

Formula to convert Cubic Foot  to Cubic Inch.

Cubic Inch  = Cubic Foot * 1728

Let’s see different ways to convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch.

Method-1: Java Program to Convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch By Using Static Input Value

Approach:

  • Declare Cubic Inch and Cubic Foot value.
  • Then convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch 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);
        //value of cubic inch declared
        double cubicInch = 1;
        //value of cubic foot declared  
        double cubicFoot = 1;

        //converting cubic inch to cubic foot
        double cf = cubicInch / 1728 ;
        //converting cubic foot to cubic inch 
        double ci = cubicFoot * 1728;
        //printing result
        System.out.println("Value of "+cubicInch+" cubic inch in cubic foot: "+ cf);   
        System.out.println("Value of "+cubicFoot+" cubic foot in cubic inch: "+ ci);   
   }
}
Output:

Value of 1.0 cubic inch in cubic foot: 5.787037037037037E-4
Value of 1.0 cubic foot in cubic inch: 1728.0

Method-2: Java Program to Convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch By Using User Input Value

Approach:

  • Take user input of Cubic Inch and Cubic Foot value.
  • Then convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch 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 cubicInch
        System.out.println("Enter value of cubic inch: ");  
        double cubicInch = sc.nextDouble();
        //Taking the value input of double variable cubicFoot
        System.out.println("Enter value of cubic foot: ");  
        double cubicFoot = sc.nextDouble();

        //converting cubic inch to cubic foot
        double cf = cubicInch / 1728 ;
        //converting cubic foot to cubic inch 
        double ci = cubicFoot * 1728;
        //printing result
        System.out.println("Value of "+cubicInch+" cubic inch in cubic foot: "+ cf);   
        System.out.println("Value of "+cubicFoot+" cubic foot in cubic inch: "+ ci);   
   }
}
Output:

Enter value of cubic inch: 
100000
Enter value of cubic foot: 
2
Value of 100000.0 cubic inch in cubic foot: 57.870370370370374
Value of 2.0 cubic foot in cubic inch: 3456.0

Method-3: Java Program to Convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch By Using User Defined Method

Approach:

  • Take user input of Cubic Inch and Cubic Foot value.
  • Call a user defined method by passing Cubic Inch and Cubic Foot value as parameter.
  • Inside method convert Cubic Inch to Cubic Foot and Cubic Foot to Cubic Inch 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 cubicInch
        System.out.println("Enter value of cubic inch: ");  
        double cubicInch = sc.nextDouble();
        //Taking the value input of double variable cubicFoot
        System.out.println("Enter value of cubic foot: ");  
        double cubicFoot = sc.nextDouble();
         //calling user defined method convert()
        convert(cubicInch, cubicFoot);
   }
   
   
   //convert() method to convert cubic inch to cubic foot and vice versa
   public static void convert(double cubicInch, double cubicFoot)
   {
        //converting cubic inch to cubic foot
        double cf = cubicInch / 1728 ;
        //converting cubic foot to cubic inch 
        double ci = cubicFoot * 1728;
        //printing result
        System.out.println("Value of "+cubicInch+" cubic inch in cubic foot: "+ cf);   
        System.out.println("Value of "+cubicFoot+" cubic foot in cubic inch: "+ ci);   
   }
}
Output:

Enter value of cubic inch: 
564321
Enter value of cubic foot: 
234
Value of 564321.0 cubic inch in cubic foot: 326.57465277777777
Value of 234.0 cubic foot in cubic inch: 404352.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:

Java Program to Convert Hectare to Square Inch and Square Inch to Hectare

In this article we will see how to convert Hectare to Square Inch and Square Inch to Hectare by using Java programming language.

Java Program to Convert Hectare to Square Inch and Square Inch to Hectare

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

Generally Hectare is used as unit in case of measuring medium to large field/land while Square inch is used as unit in case of measuring small to medium field/land.

1 Hectare = 1.55e+7 Square Inch
1 Square Inch = 6.4516e-8 Hectare

Formula to convert Hectare to Square Inch.

Square Inch = Hectare * 1.55e+7

Formula to convert Square Inch to Hectare.

Hectare  = Square Inch / 1.55e+7

Let’s see different ways to convert Hectare to Square Inch and Square Inch to Hectare.

Method-1: Java Program to Convert Hectare to Square Inch and Square Inch to Hectare By Using Static Input Value

Approach:

  • Declare Hectare and Square Inch value.
  • Then convert Hectare to Square Inch and Square Inch to Hectare by using the formula.
  • Print result.

Program:

public class Main 
{
   public static void main(String args[])
   {
        //value of square inch declared  
        double squareInch = 1;
        //value of hectare declared
        double hectare = 1;
        
        //converting Hectare to square inch
        double si = hectare * 1.55e+7;
        //converting square inch to Hectare 
        double h = squareInch / 1.55e+7;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square inch: "+ si);   
        System.out.println("Value of "+squareInch+" square inch in hectare: "+ h);   
   }
}
Output:

Value of 1.0 hectare in square inch: 1.55E7
Value of 1.0 square inch in hectare: 6.451612903225807E-8

Method-2: Java Program to Convert Hectare to Square Inch and Square Inch to Hectare By Using User Input Value

Approach:

  • Take user input of Hectare and Square Inch value.
  • Then convert Hectare to Square Inch and Square Inch to Hectare 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 squareInch
        System.out.println("Enter value of square inch: ");  
        double squareInch = sc.nextDouble();
        //Taking the value input of double variable hectare
        System.out.println("Enter value of hectare: ");  
        double hectare = sc.nextDouble();
        
        //converting Hectare to square inch
        double si = hectare * 1.55e+7;
        //converting square inch to Hectare 
        double h = squareInch / 1.55e+7;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square inch: "+ si);   
        System.out.println("Value of "+squareInch+" square inch in hectare: "+ h);   
   }
}
Output:

Enter value of square inch: 
10000000
Enter value of hectare: 
0.5
Value of 0.5 hectare in square inch: 7750000.0
Value of 1.0E7 square inch in hectare: 0.6451612903225806

Method-3: Java Program to Convert Hectare to Square Inch and Square Inch to Hectare By Using User Defined Method

Approach:

  • Take user input of Hectare and Square Inch value.
  • Call a user defined method by passing Hectare and Square Inch value as parameter.
  • Inside method convert Hectare to Square Inch and Square Inch to Hectare 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 squareInch
        System.out.println("Enter value of square inch: ");  
        double squareInch = sc.nextDouble();
        //Taking the value input of double variable hectare
        System.out.println("Enter value of hectare: ");  
        double hectare = sc.nextDouble();
        //calling user defined method convert()
        convert(squareInch, hectare);
   }
   
   //convert() method to convert square inch to hectare and vice versa
   public static void convert(double squareInch, double hectare)
   {
        //converting Hectare to square inch
        double si = hectare * 1.55e+7;
        //converting square inch to Hectare 
        double h = squareInch / 1.55e+7;
        //printing result
        System.out.println("Value of "+hectare+" hectare in square inch: "+ si);   
        System.out.println("Value of "+squareInch+" square inch in hectare: "+ h);   
   }
}
Output:

Enter value of square inch: 
999999
Enter value of hectare: 
2
Value of 2.0 hectare in square inch: 3.1E7
Value of 999999.0 square inch in hectare: 0.06451606451612903

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs:

Java Program to Remove a Node from a Given Position in a Linked List

In this article we are going to see how we can remove a node from a given position in a linked list by using Java programming language.

Java Program to Remove a Node from a Given Position in a Linked List

Each element in Java is treated as a node in Linked List. Each position is like index which starts from 0 and goes on. To delete a specific node from given position, you have to specify the index.

Let’s see a program to understand it more clearly.

Approach:

  • Create a linked list and add some elements.
  • Display the elements to the user.
  • Ask the user to enter a position to delete a node.
  • Pass it to the user function. The function removes the node at the position and makes the previous node point to the next node in the list.
  • Display the new linked list.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  

    // Method to delete a node from a specified position from a linked list
    public void deletePos(int pos)
    {
        System.out.println("Removing node from position "+pos+"...");
        // If linked list is empty
        if (head == null)
        {
            System.out.println("The linked list is empty");
            return;
        }
        // Store head node
        Node temp = head;
        // If pos = 0, head shall be removed
        if (pos == 0) 
        {
            // Point head to the next node
            head = temp.nextNode;
            return;
        }
     
        //Iterate to the previous node of the to be deleted node
        for (int i = 0; temp != null && i < pos - 1;i++)
        temp = temp.nextNode;
     
        // If position is more than number of nodes
        if (temp == null || temp.nextNode == null)
        {
            System.out.println("Wrong input");
            return;
        }
     
        // Store the address of the next node
        Node nextNode = temp.nextNode.nextNode;
        // Unlink the node at the position
        temp.nextNode= nextNode;
    }

    // method to add a node to the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        ll.add(60);
        ll.add(70);
        ll.add(80);
        ll.add(90);
        ll.add(100);
        // display the nodes
        ll.show();
        // Taking user input
        System.out.println("Enter a position to delete a node");
        Scanner sc = new Scanner(System.in);
        int pos = sc.nextInt();
        // Deleting the node
        ll.deletePos(pos);
        // display the nodes
        ll.show();
        // Taking user input
        System.out.println("Enter another position to delete a node");
        pos = sc.nextInt();
        // Deleting the node
        ll.deletePos(pos);
        // display the nodes
        ll.show();
    }
}
Output:

The nodes are:
10,20,30,40,50,60,70,80,90,100,
Enter a position to delete a node
1
Removing node from position 1...
The nodes are:
10,30,40,50,60,70,80,90,100,
Enter another position to delete a node
3
Removing node from position 3...
The nodes are:
10,30,40,60,70,80,90,100,

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Java Program to Create and Count Nodes in a Singly Linked List

In this article, we are going to see how we can create and count nodes in a singly linked list by using Java programming language.

Java Program to Create and Count Nodes in a Singly Linked List

Each element in linkedlist is called as a node. Here we need to first create nodes and then count the number of nodes. To create nodes we need to add elements into the linkedlist and to count the number of nodes traverse the linkedlist starting from head to tail and keep a track on its total count.

Let’s see the program to understand it clealry.

Approach:

  • Create a linked list by creating an object of that class.
  • Call the user defined count( ) method to print the number of nodes in the list.
  • Add some elements to the list.
  • Print the linked list by calling user defined method show() in which we will traverse each element one by one and print it.
  • Call the count( ) method again and see the number of nodes after the elements have been added.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and end of the node as NULL  
    public Node head = null;    
    public Node tail = null;  
    // Count the number of nodes in the linked list
    public void count()
    {
        int c = 0;
        Node curr = head;
        //continue it till curr refers to null
        while(curr!=null)
        {
            c++;
            curr = curr.nextNode;
        }
        System.out.println("The number of nodes in the linked list currently are: "+c);
    }
    
    // method to add a node to the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        //if the list is empty then head and tail both will point to newNode
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        Node curr = head;
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }
    
    //Driver method
    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // Counts the number of nodes in the linked list
        ll.count();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        // display the nodes
        ll.show();
        // Again counts the number of nodes
        ll.count();
    }
}
Output:

The number of nodes in the linked list currently are: 0
The nodes are:
10,20,30,40,50,
The number of nodes in the linked list currently are: 5

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Java Program to Create a Singly Linked List, Add Elements to it and Display

In this article, we are going to see how we can create a singly linked list, add elements to it and display it by using Java programming language.

Java Program to Create a Singly Linked List, Add Elements to it and Display

Singly LinkedList:

In Java singly linkedlist is a data structure which contains a series of elements. Each element in the list contains the element and a pointer to next element which is called a node. Where the first node is termed as head and last node is termed as tail of the linkedlist. Last node pointer contains null.

Approach:

  • Create a class node to contain the data and the link of all nodes.
  • Initialize the head and tail as null as the list is initially empty.
  • Create a method add() which adds the element to the node.
  • Create another method that checks if the linked list is empty else displays the elements.
  • Now from the main function create an object of the class which creates an empty linked list for us.
  • Now add some elements to the list using the add() method.
  • Finally, call the show() method to display the linked list.

Program:

import java.util.*;
// Main class
public class Main
{
    // Class Node that defines the two linked list variables
    class Node
    {
        int data;
        Node nextNode;
        // constructor to create a node
        public Node(int data) 
        {    
            this.data = data;    
            this.nextNode = null;    
        }    
    }

    // Setting the head and tail of the linkedlist as NULL  
    public Node head = null;    
    public Node tail = null;  
    // method to add a node to the linked list
    public void add(int data)
    {
        Node newNode = new Node(data);
        // Checks if there was any previous node
        //if the list is empty then head and tail both will point to newNode
        if(head==null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.nextNode = newNode;
            tail = newNode;
        }
        System.out.println(data+" has been added to the list.");
    }
    
    // Method to display all the nodes of the linked list
    public void show()
    {
        //Node curr refers to the head element
        Node curr = head;
        System.out.println("Trying to display the linked list...");
        // If the head is pointing to no node then the linked list is empty
        if(head==null)
            System.out.println("Linked List is empty");
        else
        {
            System.out.println("The nodes are:");
            while(curr!=null)
            {
                System.out.print(curr.data+",");
                curr = curr.nextNode;
            }
            System.out.println();
        }
    }

    public static void main(String[] args) 
    {
        // create an object of the main class
        Main ll = new Main();
        // add elements to the linked list
        ll.add(10);
        ll.add(20);
        ll.add(30);
        ll.add(40);
        ll.add(50);
        // display the nodes
        ll.show();
    }
}
Output:

10 has been added to the list.
20 has been added to the list.
30 has been added to the list.
40 has been added to the list.
50 has been added to the list.
Trying to display the linked list...
The nodes are:
10,20,30,40,50,

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

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:

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:

Operators in Java

Operators in Java (Prerequisite Topic)

In this article we will discuss about what is operators and what are the operators supported in Java. So, let’s start exploring the concept.

Multiple types operators are supported by Java programming language. Operators are one of integral part of programming language. As with out operator we can not perform any single logical or arithmetical calculation/operation.

Operators:

Operators are the symbols which instructs the compiler to perform some calculation/operation. An operator perform it’s operations on operands, in more simple words on variables and values.

Types of Operators:

Java supports many types of operators and based on their unique functionality they are categorized into a set of operators. They are

  1. Arithmetic Operator
  2. Assignment Operator
  3. Unary Operator
  4. Relational Operator
  5. Logical Operator
  6. Bitwise Operator
  7. Shift Operator
  8. Ternary Operator
  9. Instanceof Operator

Let’s know little more about these operators.

1. Arithmetic Operator:

Arithmetic operators are used arithmetic/mathematical operations.

  • + : Addition operator : Adds two values.
  • - : Subtraction operator : Subtracts two values.
  • * : Multiplication operator : Multiply two values.
  • / : Division operator : Divides one value by another.
  • % : Modulo operator/Reminder operator : Returns reminder of division.

2. Assignment Operator:

Assignment operator is used to simply assign a value to a variable.

  • = : Assignment operator : Assign value to a variable.

3. Unary Operator:

Unary operator uses only one operand to perform operation. That’s why name is like Unary(means one) operator.

  • var++ and var-- : Postfix operator : First Assign value then increment or decrement.
  • ++var and --var : Prefix operator : First Increment or decrement  value hen assign.

4. Relational Operator:

Relational operators are used to check relation between two operands (variables/values). It is also called as comparison operator.

  • == : Equal to operator : Checks two values are equal.
  • != : Not equal to : Checks two values are not equal.
  • > : Greater than operator : Checks left operand is greater than right operand.
  • < : Less than operator : Checks left operand is less than right operand.
  • >= : Greater than equal to operator : Checks left operand is greater than or equal to right operand.
  • <= : Less than equal to operator : Checks left operand is less than or equal to right operand.

5. Logical Operator:

Logical operators are used to determine logic between two variables/values. Checks condition is true or false.

  • && : Logical AND : Returns True if both side conditions are true else false.
  • || : Logical OR : Returns True if any one side condition is true else false.
  • ! : Logical NOT : Returns the reversed result means true becomes false and vice versa.

6. Bitwise Operator:

Bitwise operators are used to perform any operation on individual bits.

  • ~ : Bitwise Complement : Returns 1 value as 0 and 0 value as 1.
  • | :  Bitwise OR : Returns 1 if at least one operand is 1
  • & : Bitwise AND : Returns 1 if both the value are 1
  • ^ : Bitwise XOR : Returns 1 if one of operand is 1, If both operands are 0 or 1 then returns 0

7. Shift Operator:

Shift operators are used to shift bits to left or right of a number.

  • << : Left Shift : Shifts all bits to left based on specified bits.
  • >> : Signed Right Shift : Shifts all bits to right based on specified bits.
  • >>> : Unsigned Right Shift : Performs right shift and vacant left bits filled with 0 instead of sign bit.

8. Ternary Operator:

It is another version of if-else operation. It is also called as conditional operator. As it uses 3 operands so it is called as ternary operator.

  • ? : Ternary Operator : (expression) ? value if true : value if false If condition is true then statement after ? and if condition is false then executes statement after :

9. Instanceof Operator:

Instanceof operator compares an object to a specified type.

  • instanceof : Checks if a reference variable is of a given type of object reference.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs:

Java Program to Print Zig Zag Number Pattern

Print Zig Zag Number Pattern

In the previous article, we have discussed Java Program to Print Window Number Pattern

In this article we will see how to print zig-zag number pattern.

Example:

Enter the no of characters in a line = 3
Enter the no of zig zag line = 4

1  
  2   
    3 
    1 
  2 
3 
1     
  2   
    3 
    1 
  2 
3 
1     
  2   
    3 
    1 
  2 
3 
1     
  2   
    3 
    1 
  2 
3

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach :

  1. Print one backward slash first then one forward slash and continue.
  2. Enter total characters in a row and store it an integer variable say row.
  3. Enter total no. of zig zag lines and store in an integer variable say count.
  4. To print Backward slash :
  5. Take first for loop to print all the rows.
  6. Take a inner loop to print the column values.
  7. Then go on printing the numbers according to the iteration.
  8. To print forward slash :
  9. Take first for loop to print all the rows.
  10. Take inner loop to print the column values.
  11. Then go on printing the numbers according to the iteration.

Java Code to Print Zig Zag Number Pattern

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        // Take user input for no fo rows 
        System.out.print("Enter the no of characters in a line = ");
        Scanner sc= new Scanner(System.in);
        int r,c,row;
        //starting ASCII value taken 64
        int ascii=64;
        // store the input value in row
        row=sc.nextInt();
         System.out.print("Enter the no of zig zag line = ");
        int count=sc.nextInt();
        
        for (int i=1;i<=count;i++)
        {
        
            // backward
           for(r=1; r<=row; r++)
           {   
               // inner loop to print number
              for(c=1; c<=row; c++)
              {   
                  // if row and column have same value print symbol     
                 if(r==c)      
                    System.out.print(r+" ");      
                 else          
                    System.out.print("  ");      
              } 
              System.out.println("");
           } 
           
           // forward
            for(r=1;r<=row;r++)
            {
              // loop for printing number 
              for(c=1;c<=row;c++)
              {
                    // if c<= row+1-r print symbol else spaces
                    if(c <= (row+1-r))
                    {
                       if( c == (row+1-r) )
                          System.out.print(r+" ");
                       else
                          System.out.print("  ");
                    }
              }
              System.out.println("");
           }
        }
    }
}

 

Output:

Enter the no of characters in a line = 3
Enter the no of zig zag line = 4

1     
  2   
    3 
    1 
  2 
3 
1     
  2   
    3 
    1 
  2 
3 
1     
  2   
    3 
    1 
  2 
3 
1     
  2   
    3 
    1 
  2 
3

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs:

JSP Implicit Objects: 9 Implicit Objects in JSP | Complete Tutorial on Implicit Objects in JSP

JSP Implicit Objects- 9 Implicit Objects in JSP

In the earlier tutorial, we have seen JSP Scripting Elements with Example. In this tutorial, we will discuss & understand JSP implicit objects with examples. Primarily, there are 9 implicit objects in JSP. Let’s learn all these implicit objects in detail. But first, learn what is JSP implicit objects?

This Implicit Objects in JSP Tutorial Contains: 

What are JSP Implicit Objects?

Implicit objects are the set of predefined objects readily available for use. These objects are created by the JSP container, while a JSP page translates into Servlet. Implicit Objects are being created inside the service() method so we can use implicit objects directly within scriptlets without initializing and declaring them. Total 9 Implicit objects are available in JSP.

Implicit Objects and their corresponding classes

out javax.servlet.jsp.JspWriter
request javax.servlet.http.HttpServletRequest
response javax.servlet.http.HttpServletResponse
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
exception javax.servlet.jsp.JspException
page java.lang.Object
pageContext javax.servlet.jsp.PageContext
config javax.servlet.ServletConfig

Also Check: 

How many Implicit Objects are available in JSP?

There are 9 types of implicit objects available in the container:

  1. out
  2. request
  3. response
  4. page
  5. pageContext
  6. config
  7. application
  8. session
  9. exception

Let’s check out one by one from the below sections:

1. out:

It is used for writing content to the client browser. It is an object of the JSP Writer. In servlet you need to write PrintWriter out = response.getWriter() for writing content to the browser, but in JSP you don’t need to write this.

2. request:

It is an object of HttpServletRequest.This object is created for each JSP request by the web container. The main purpose of this object is to get the data on a JSP page which is entered by the user. This object is used to get request information like a parameter, header information, content type, server name, etc.

JSP Implicit Objects 9 Implicit Objects in JSP 1

JSP Implicit Objects 9 Implicit Objects in JSP 2

JSP Implicit Objects 9 Implicit Objects in JSP 3

JSP Implicit Objects 9 Implicit Objects in JSP 4

3. response:

It is an object of HttpServletResponse. This object will be created by the container for each request. We can use the response object to set content type, adding cookies, and redirecting the request to another resource.

JSP Implicit Objects 9 Implicit Objects in JSP 5

JSP Implicit Objects 9 Implicit Objects in JSP 6

4. page:

JSP page implicit object is an object of java.lang.Object class. This object represents the current JSP page. It is rarely used. It provides a reference to the generated servlet class.

5. pageContext:

JSP pageContext object is an object of javax.servlet.jsp.PageContext class. It is used for accessing page, application, request and session scope.

JSP Implicit Objects 9 Implicit Objects in JSP 7

JSP Implicit Objects 9 Implicit Objects in JSP 8

JSP Implicit Objects 9 Implicit Objects in JSP 9

6. config:

JSP config implicit object is an instance of java.servlet.ServletConfig class. This object is created by the container for each JSP page. It is used to get the initialization parameter in the deployment descriptor(web.xml) file.

JSP Implicit Objects 9 Implicit Objects in JSP 10

JSP Implicit Objects 9 Implicit Objects in JSP 11

JSP Implicit Objects 9 Implicit Objects in JSP 12

7. application:

JSP application implicit object is an instance of java.servlet.servletContext class.It is used to obtain the context information and attributes in JSP.The servlet context obtained from the servlet configuration object by using the getServletConfig().getServletContext() method.One application object is created by the container for one JSP application when the application is deployed.

JSP Implicit Objects 9 Implicit Objects in JSP 13

JSP Implicit Objects 9 Implicit Objects in JSP 14

JSP Implicit Objects 9 Implicit Objects in JSP 15

8. session:

JSP Session implicit object is an instance of javax.servlet.http.HttpSession class.The session object is created for the requesting clients(if any). This variable is only valid for HTTP-based protocol. we can use this object to get, set, and remove an attribute from the session scope.

JSP Implicit Objects 9 Implicit Objects in JSP 16

JSP Implicit Objects 9 Implicit Objects in JSP 17

JSP Implicit Objects 9 Implicit Objects in JSP 18

JSP Implicit Objects 9 Implicit Objects in JSP 19

JSP Implicit Objects 9 Implicit Objects in JSP 20

JSP Implicit Objects 9 Implicit Objects in JSP 21

9. Exception:

JSP exception implicit object is an instance of java.lang.Throwable class. It is used for exception handling in JSP. It can be only used for the JSP error page.