KnowledgeBoat Logo

Computer Applications

Write a program to create three single dimensional arrays cod[], pric[], qty[] to store product code, unit price and quantity of each product for 10 products. Calculate the total cost of each product and print the result in tabular form including product code, unit price, quantity and total cost of each product. At the end print total of price, total of quantity and the total of costs.

Java

Java Arrays

14 Likes

Answer

import java.util.Scanner;

public class KboatProducts
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int cod[] = new int[10];
        double pric[] = new double[10];
        int qty[] = new int[10];
        
        for (int i = 0; i < 10; i++) {
            System.out.print("Enter code of product " + (i + 1) + ": ");
            cod[i] = in.nextInt();
            System.out.print("Enter unit price of product " + (i + 1) + ": ");
            pric[i] = in.nextDouble();
            System.out.print("Enter quantity of product " + (i + 1) + ": ");
            qty[i] = in.nextInt();
        }
        
        double tp = 0;
        long tq = 0;
        double tc = 0;
        
        System.out.println("Code\tPrice\tQty\tCost");
        for (int i = 0; i < 10; i++) {
            double cost = qty[i] * pric[i];
            tp += pric[i];
            tq += qty[i];
            tc += cost;
            System.out.println(cod[i] + "\t" + pric[i] + "\t" + qty[i] + "\t" + cost);
        }
        
        System.out.println("Total Price = " + tp);
        System.out.println("Total Quantity = " + tq);
        System.out.println("Total Costs = " + tc);
    }
}

Output

BlueJ output of Write a program to create three single dimensional arrays cod[], pric[], qty[] to store product code, unit price and quantity of each product for 10 products. Calculate the total cost of each product and print the result in tabular form including product code, unit price, quantity and total cost of each product. At the end print total of price, total of quantity and the total of costs.

Answered By

7 Likes