Java Programming | Volume Conversion

Introduction

This is a tutorial for conversion of volume from one unit to other. The program is given below that converts volume given by user from one unit to other and prints it. The program is extendable. Go enjoy the program. Lets begin……..

Program for conversion of volume from meter cube to centimeter cube in java.

//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class VolumeConversion {
	//void main
		public static void main (String[] args)
		{
			//declare float
			float to,from;
			//print message
			System.out.println("Enter Volume in meter cube:");
			//Take input
			Scanner input = new Scanner(System.in);
			//calculate
			 from = input.nextFloat();
		     to = from*1000000;
			//print the average
			System.out.println("Volume in centimeter cube = "+to);
		}
}

Output

Enter Volume in meter cube:
4.56
Volume in centimeter cube = 4560000.0

Program to convert volume from meter cube to litre in java.

//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class VolumeConversion {
	//void main
		public static void main (String[] args)
		{
			//declare float
			float to,from;
			//print message
			System.out.println("Enter Volume in meter cube:");
			//Take input
			Scanner input = new Scanner(System.in);
			//calculate
			 from = input.nextFloat();
		     to = from*1000;
			//print the average
			System.out.println("Volume in litre = "+to);
		}
}

Output

Enter Volume in meter cube:
1.254
Volume in litre = 1254.0

How it works

  1. The program prints the message to enter volume.
  2. The user enters volume.
  3. Volume is converted.
  4. The converted volume is printed.

Extending it

The Program can be extended by adding more unit to conversion and by adding their Conversion formula for other unit and more variables can be added to program to convert multiple volumes.

Explanation.

  1. Import the Scanner.
  2. Declare the class as public
  3. Add the void main function
  4. Add system.out.println() function with the message to enter volume.
  5. Declare input as Scanner.
  6. Take the volume and save it in variable.
  7. Convert volume and save it in other variable.
  8. Add system.out.println() function to print the converted volume.

At the end.

You learnt creating the Java program for Volume conversion. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s