0% found this document useful (0 votes)
12 views11 pages

Java (IMP)

The document contains Java programs for various tasks including calculating profit or loss based on cost and selling prices, finding the greatest of three numbers, checking leap years, calculating taxi fares based on distance, and computing simple and compound interest. Each program includes user input, conditional statements, and output of results. The code snippets demonstrate fundamental programming concepts in Java such as classes, methods, and control flow.

Uploaded by

Devbrat Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Java (IMP)

The document contains Java programs for various tasks including calculating profit or loss based on cost and selling prices, finding the greatest of three numbers, checking leap years, calculating taxi fares based on distance, and computing simple and compound interest. Each program includes user input, conditional statements, and output of results. The code snippets demonstrate fundamental programming concepts in Java such as classes, methods, and control flow.

Uploaded by

Devbrat Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Statements in Java

[Link].1

Write a program to input the cost price and the selling price of an
article. If the selling price is more than the cost price then calculate
and display actual profit and profit per cent otherwise, calculate and
display actual loss and loss per cent. If the cost price and the selling
price are equal, the program displays the message 'Neither profit nor
loss'.

import [Link];

class ProfitLoss

void main()

Scanner sc = new Scanner([Link]);

[Link]("Enter the cost price: ");

double costPrice = [Link]();

[Link]("Enter the selling price: ");

double sellingPrice = [Link]();

if (sellingPrice > costPrice)

{
Statements in Java

double profit = sellingPrice - costPrice;

double profitPercent = (profit / costPrice) * 100;

[Link]("Profit: " + profit);

[Link]("Profit percent: " + profitPercent + "%");

else if (sellingPrice < costPrice)

double loss = costPrice - sellingPrice;

double lossPercent = (loss / costPrice) * 100;

[Link]("Loss: " + loss);

[Link]("Loss percent: " + lossPercent + "%");

} else

[Link]("Neither profit nor loss.");

[Link].2

Write a program to input three numbers and check whether they are
equal or not. If they are unequal numbers then display the greatest
among them otherwise, display the message 'All the numbers are
equal'.
Sample Input: 34, 87, 61
Statements in Java

Sample Output: Greatest number: 87


Sample Input: 81, 81, 81
Sample Output: All the numbers are equal.

import [Link];

class NumberChecker

void main()

Scanner scanner = new Scanner([Link]);

[Link]("Enter the first number:");

int num1 = [Link]();

[Link]("Enter the second number:");

int num2 = [Link]();

[Link]("Enter the third number:");

int num3 = [Link]();

if (num1 == num2 && num2 == num3)

{
Statements in Java

[Link]("All numbers are equal.");

else

int greatest = num1;

if (num2 > greatest)

greatest = num2;

if (num3 > greatest)

greatest = num3;

[Link]("The greatest among them is: " + greatest);

}
Statements in Java

[Link].3

Write a program to input year and check whether it is:


(a) a Leap year (b) a Century Leap year (c) a Century year but not a
Leap year
Sample Input: 2000
Sample Output: It is a Century Leap Year.

import [Link];

class LeapYear

void main()

Scanner scanner = new Scanner([Link]);

[Link]("Enter a year: ");

int year = [Link]();

if (year % 400 == 0)

[Link](year + " is a Century Leap Year.");

else if (year % 100 == 0)


Statements in Java

[Link](year + " is a Century Year but not a Leap


Year.");

else if (year % 4 == 0)

[Link](year + " is a Leap Year.");

else

[Link](year + " is not a Leap Year.");

}
Statements in Java

[Link].4

import [Link];

class TaxiFareCalculator

void main()

Scanner scanner = new Scanner([Link]);

[Link]("Enter Taxi No.: ");

String taxiNo = [Link]();

[Link]("Enter the distance covered (in km): ");

double distance = [Link]();

double fare = 0.0;


Statements in Java

if (distance <= 5)

fare = 100;

else if (distance > 5 && distance <= 15)

fare = (100) + ((distance - 5) * 10.0);

else if (distance > 10 && distance <= 25)

fare = (100) + (100) + ((distance - 15) * 8.0);

else if (distance > 25)

fare = (100) + (100) + (80) + ((distance - 25) * 5.0);

[Link]("\nTaxi Bill");

[Link]("----------");

[Link]("Taxi No. : " + taxiNo);


Statements in Java

[Link]("Distance Covered : " + distance + " km");

[Link]("Total Amount : Rs. " + fare);

[Link].5

A Java program that calculates Simple Interest (SI) and Compound


Interest (CI) based on a given principal amount, rate, and time.

import [Link];

class InterestCalculator

void main()

Scanner scanner = new Scanner([Link]);

[Link]("Enter Principal Amount (P): ");

double principal = [Link]();

[Link]("Enter Rate of Interest (R): ");

double rate = [Link]();

[Link]("Enter Time (T) in years: ");


Statements in Java

double time = [Link]();

[Link]("\nSelect the type of interest to calculate:");

[Link]("S. Simple Interest (SI)");

[Link]("C. Compound Interest (CI)");

[Link]("Enter your choice (S or C): ");

char choice = [Link]().charAt(0);

double interest = 0.0;

String interestType = "";

switch (choice)

case 'S':

interest = (principal * rate * time) / 100;

interestType = "Simple Interest";

break;

case 'C':

interest = principal * ([Link]((1 + rate / 100), time) - 1);

interestType = "Compound Interest";


Statements in Java

break;

default:

[Link]("Invalid choice. Please enter S or C.");

return;

double sum = principal + interest;

[Link]("--- Calculation Summary ---");

[Link]("Principal Amount : " + principal);

[Link]("Rate of Interest : " + rate + "%");

[Link]("Time : " + time + " years");

[Link](interestType + ": " + interest);

[Link]("Total Amount : " + sum);

You might also like