Java Program to Calculate Price of a Product in Kilogram

In the previous article, we have discussed about Java Program to Convert Meter to Yard and Yard to Meter

In this article we will see how to calculate price of product in Kilogram by using Java programming language.

Java Program to Calculate Price of a Product in Kilogram

We have to calculate the price of a product and the unit is given in Kilogram. We have the price of product per kilogram which will be used to calculate exact price of the product based on given unit.

For example:

If the price of product for 1kg = 110 rupees

Then price of product for 1.5kg = 165 rupees

Let’s see different ways to calculate price of product in Kilogram.

Method-1: Java Program to Calculate Price of a Product in Kilogram By Using Static Input Value

Approach:

  • Declare the price of the product per kilogram.
  • Declare the unit that you bought in kilogram and gram.
  • First find the actual price of product per 1 gram from the price of 1 kilogram.
  • Convert bought unit in gram and find the price based on price per 1 gram.

Program:

public class Main 
{
   public static void main(String args[])
   {
        //price per kilogramm declared
        double price = 170; 
        
        //bought unit in kilogram and gram
        //how much kilogram declared
        double kg=1;
        //how much gram declared
        double g=750;
        
        double totalUnit=g+(kg*1000);
        System.out.println("Your bought unit: "+totalUnit+" gram"); 
        
        double pricePerGram=price/100;
        System.out.println("Price per 1 gram: "+pricePerGram); 
        double totalPrice= (totalUnit*pricePerGram)/10;
        System.out.print("Price of bought unit: "+totalPrice);
   }
}
Output:

Your bought unit: 1750.0 gram
Price per 1 gram: 1.7
Price of bought unit: 297.5

Method-2: Java Program to Calculate Price of a Product in Kilogram By Using User Input Value

Approach:

  • Take input of price of the product per kilogram.
  • Take input of unit that you bought in kilogram and gram.
  • First find the actual price of product per 1 gram from the price of 1 kilogram.
  • Convert bought unit in gram and find the price based on price per 1 gram.

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the price per kilogram "); 
        double price = sc.nextInt(); 
        System.out.println("Enter the unit you bought-"); 
        System.out.print("Enter how much kilogram: "); 
        double kg=sc.nextInt();
        System.out.print("Enter how much gram: "); 
        double g=sc.nextInt();
        
        double totalUnit=g+(kg*1000);
        System.out.println("Your bought unit: "+totalUnit); 
        
        double pricePerGram=price/100;
        System.out.println("Price per 1 gram: "+pricePerGram); 
        double totalPrice= (totalUnit*pricePerGram)/10;
        System.out.print("Price of bought unit: "+totalPrice);
   }
}
Output:

Enter the price per kilogram 102
Enter the unit you bought-
Enter how much kilogram: 3
Enter how much gram: 750
Your bought unit: 3750.0
Price per 1 gram: 1.02
Price of bought unit: 382.5

Method-3: Java Program to Calculate Price of a Product in Kilogram By Using User Defined Method

Approach:

  • Take input of price of the product per kilogram.
  • Take input of unit that you bought in kilogram and gram.
  • Call a user defined method by passing per kg, bought kg and gram as parameter.
  • First find the actual price of product per 1 gram from the price of 1 kilogram.
  • Convert bought unit in gram and find the price based on price per 1 gram.

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the price per kilogram "); 
        double price = sc.nextInt(); 
        System.out.println("Enter the unit you bought-"); 
        System.out.print("Enter how much kilogram: "); 
        double kg=sc.nextInt();
        System.out.print("Enter how much gram: "); 
        double g=sc.nextInt();
        //calling calculatePrice() method
        calculatePrice(price, kg, g);
   }
   
    public static void calculatePrice(double price, double kg, double g)
    {   
        double totalUnit=g+(kg*1000);
        System.out.println("Your bought unit: "+totalUnit); 
        
        double pricePerGram=price/100;
        System.out.println("Price per 1 gram: "+pricePerGram); 
        double totalPrice= (totalUnit*pricePerGram)/10;
        System.out.print("Price of bought unit: "+totalPrice);
   }
}
Output:

Enter the price per kilogram 150
Enter the unit you bought-
Enter how much kilogram: 1
Enter how much gram: 600
Your bought unit: 1600.0
Price per 1 gram: 1.5
Price of bought unit: 240.0

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: