[Go to site: main page, start]

0% found this document useful (0 votes)
3 views15 pages

Java Programs for Basic Calculations

The document contains a series of Java programs that perform various calculations and checks, including arithmetic operations, area and perimeter calculations, divisibility checks, and triangle validations. Each program is structured with a main method and demonstrates basic programming concepts such as conditionals and loops. The document serves as a practical guide for beginners to understand Java programming through hands-on examples.

Uploaded by

utsha.purkait221
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)
3 views15 pages

Java Programs for Basic Calculations

The document contains a series of Java programs that perform various calculations and checks, including arithmetic operations, area and perimeter calculations, divisibility checks, and triangle validations. Each program is structured with a main method and demonstrates basic programming concepts such as conditionals and loops. The document serves as a practical guide for beginners to understand Java programming through hands-on examples.

Uploaded by

utsha.purkait221
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

Java Programs

Calculations

1.​ WAP to find out the sum of two numbers.


public class Add
{
​ public static void main(String args[ ])
​ {
​ ​ int a=10;
int b=27;
​ ​ int sum;
sum=a+b;
[Link](“Sum is “+sum);

​ }
}

2.​ WAP to find out the Difference between two numbers.


public class Sub
{
​ public static void main(in t a, int b)
​ {
​ ​ int diff;
diff=a-b;
[Link](“Difference is “+diff);
​ }
}

3.​ WAP to find out the average of four numbers.


public class Avg
{
​ public static void main(float a, float b, float c, float d)
​ {
​ ​ float avg;
avg=(a+b+c+d)/4;
[Link](“The average is “+Avg);
​ }
}

4.​ WAP to calculate the area and perimeter of a Square.

public class Square


{
​ public static void main(int s)
​ {​ ​
​ ​ int area, peri;
area=s*s;
peri=4*s;
[Link](“The area of the square is “+area+” and the perimeter
of the square is “+peri);
​ }
}
5.​ WAP to calculate the area and perimeter of a Rectangle.

public class Rectangle


{
​ public static void main(float l, float b)
​ {​ ​
​ ​ double area, peri;
area=l*b;
peri=2*(l+b);
[Link](“The area of the rectangle is “+area);
[Link](“The perimeter of the rectangle is “+peri);
​ }
}

6.​ WAP to calculate the area and perimeter of a circle.

public class Circle


{
​ public static void main(float r)
​ {
​ ​ double area, peri;
area=3.14*r*r;
peri=2*3.14*r;
[Link](“The area of the circle is “+area);
[Link](“The perimeter of the circle is “+peri);
​ }
}

7.​ WAP to calculate the area of a right angled triangle.


public class Triangle
{
​ public static void main(int h, int b)
​ {
​ ​ double area;
area=0.5*h*b;
[Link](“The area of the Triangle is “+area);
​ }
}

8.​ Write a program to take time and speed of a car as input and find the distance
covered.
public class Distance
{
​ public static void main(int s, int t)
​ {
​ ​ int dist;
dist=s*t;
[Link](“The distance covered by a car is “+dist);
​ }
}

9.​ WAP find out the quotient and remainder when a number is divided by 57.
public class Quotient_remainder
{
​ public static void main(int a)
​ {
​ ​ float q;
int r;
q=a/57;
r=a%57;
[Link](“quotient=”+q);
[Link](“remainder=”+r);
​ }
}

10.​Write a program to take an input of hour and convert it into minutes and
seconds.
public class Convert_hour
{
​ public static void main(float hours)
​ {
int minutes = hours * 60;
int seconds = hours * 3600;
[Link](hours + " hours is equal to:");
[Link](minutes + " minutes");
[Link](seconds + " seconds");
​ }
}

11.​WAP to convert a temperature from fahrenheit to celsius.


public class FahrenheitToCelsius
{
public static void main(double fahrenheit)
{
double celsius = (fahrenheit - 32) * 5 / 9;
[Link](fahrenheit + "°F is equal to " + celsius + "°C");
}
}
IF-ELSE
12.​WAP to take a number and check whether the number is divisible by 3 or not.
public class DivisibleBy3
{
public static void main(int num)
{
if (num % 3 == 0)
{
[Link](num + " is divisible by 3.");
}
else
{
[Link](num + " is not divisible by 3.");
}
}
}

13.​WAP to take a number and check whether the number is odd or even.
public class OddEven
{
public static void main(int num)
{
if (num % 2 == 0)
{
[Link](num + " is even.");
}
else
{
[Link](num + " is odd.");
}
}
}
14.​ WAP to accept the length of two different line segments and check whether they
are equal or unequal.

public class LineEquality


{
public static void main(double l1, double l2)
{
if (l1 == l2)
{
[Link]("The two line segments are equal.");
}
else
{
[Link]("The two line segments are unequal.");
}
}
}

15.​WAP to accept the age of a person and check whether he/she is eligible to vote.
A person is eligible to vote only when he/she is 18 years or more.
public class VotingEligibility
{
public static void main(int age)
{
if (age >= 18)
{
[Link]("You are eligible to vote.");
}
else
{
[Link]("You are not eligible to vote.");
}
}
16.​WAP to take a number and check whether the number is positive or negative.
public class NumberSign
{
public static void main(double num)
{
if (num > 0)
{
[Link]("The number is positive.");
}
else if (num < 0)
{
[Link]("The number is negative.");
}
else
{
[Link]("The number is zero.");
}
}
}

17.​Write a program to input 2 numbers and check which is the smallest between
two numbers.
public class SmallestOfTwo {
public static void findSmallest(int a, int b) {
if (a < b) {
[Link]("The smallest number is: " + a);
}
else if (b < a) {
[Link]("The smallest number is: " + b);
}
else {
[Link]("Both numbers are equal.");
}
}
}
18.​ WAP to accept 3 numbers and find out the maximum number.
public class MaxOfThree {
public static void findMaximum(int num1, int num2, int num3)
{
if(num1 >= num2 && num1 >= num3) {
[Link]("The maximum number is: " +num1);
}
else if(num2 >= num1 && num2 >= num3) {
[Link]("The maximum number is: " +num2);
}
else {
[Link]("The maximum number is: " +num3);
}
}
}

19.​WAP to enter the cost price(CP) and selling price(SP) of an article. If the selling
price(SP) is more than the cost price(CP), then calculate profit and profit percent.
Otherwise, calculate the loss and the loss percent
public class ProfitOrLoss {
public static void calculateProfitLoss(double cp, double sp) {
if (sp > cp) {
double profit = sp - cp;
double profitPercent = (profit / cp) * 100;
[Link]("Profit is ₹" + profit);
[Link]("Profit Percentage is: " + profitPercent + "%");
} else if (cp > sp) {
double loss = cp - sp;
double lossPercent = (loss / cp) * 100;
[Link]("Loss is ₹" + loss);
[Link]("Loss Percentage is: " + lossPercent + "%");
} else {
[Link]("No Profit, No Loss.");
}
}
}
20.​ WAP to take 3 angles and check whether a triangle is possible or not.
[ Given : Triangle is possible if sum of all the angles is 180]
public class TriangleCheck
{
public static void isTrianglePossible(double a1, double a2, double a3)
{
double sum = a1 + a2 + a3;
if (sum == 180 && a1 > 0 && a2 > 0 && a3 > 0)
{
[Link]("Triangle is possible");
}
else
{
[Link]("Triangle is not possible");
}
}
}

21.​WAP to accept three numbers and check whether they are the 'Pythagorean
Triplets' or not. Display the message accordingly.
(Hint: Use Pythagoras Formula for a Right-angled Triangle: h² = p² + b²)
public class PythagoreanTriplets
{
public static void main(int a, int b, int c)
{
if (a * a == b * b + c * c || b * b == a * a + c * c || c * c == a * a + b * b)
{
[Link]("Pythagorean Triplets");
}
else
{
[Link]("Not Pythagorean Triplets");
}
}
}

22.​Accept a number and check:


(a) whether the number is divisible by 2 and 5.
(b) whether the number is divisible by 2 but not by 5.
(c) whether the number is divisible by 5 but not by 2.
Display the message accordingly.
public class DivisibilityCheck
{
public static void main(int num)
{
if (num % 2 == 0 && num % 5 == 0)
{
[Link]("The number is divisible by both 2 and 5.");
}
else if (num % 2 == 0)
{
[Link]("The number is divisible by 2 but not by 5.");
}
else if (num % 5 == 0)
{
[Link]("The number is divisible by 5 but not by 2.");
}
else
{
[Link]("The number is not divisible by 2 or 5.");
}
}
}

23.​ Input three angles of a triangle and check whether a triangle is possible or not. If
possible, then check whether it is an 'Acute-angled Triangle', 'Obtuse-angled
Triangle' or a 'Right-angled Triangle'.
public class TriangleChecker {

// Method with parameters to check triangle type


public static void main(int angle1, int angle2, int angle3) {
int sum = angle1 + angle2 + angle3;

// Check if triangle is possible


if (sum == 180 && angle1 > 0 && angle2 > 0 && angle3 > 0) {
[Link]("Triangle is possible.");

// Determine triangle type


if (angle1 < 90 && angle2 < 90 && angle3 < 90) {
[Link]("It is an Acute-angled Triangle.");
}
else if (angle1 == 90 || angle2 == 90 || angle3 == 90) {
[Link]("It is a Right-angled Triangle.");
}
else {
[Link]("It is an Obtuse-angled Triangle.");
}
}
else {
[Link]("Triangle is not possible.");
}
}
}

24.​Write a program to input the runs made by a batsman and display the message
accordingly. The various conditions are mentioned as under:
(a) If the batsman makes no score, then display "Made no score".
(b) If the batsman makes less than 50 then display "Made less than
half-century".
(c)If the batsman makes more than 50 but less than 100 then display "Made
half-century" else display "Brilliant Century!!!".
public class BatsmanRuns {
public static void displayMessage(int runs) {
if (runs == 0) {
[Link]("Made no score");
}
else if (runs < 50) {
[Link]("Made less than half-century");
}
else if (runs < 100) {
[Link]("Made half-century");
}
else {
[Link]("Brilliant Century!!!");
}
}
}

25.​Write a program to accept the name and marks obtained by a student in the
Computer Project. Display the grades as per the table given below:
Marks obtained​ ​ ​ ​ ​ ​ Grade
80% or more​ ​ ​ A
60% or more but less than 80% ​ ​ ​ ​ B
40% or more but less than 60% ​​ ​ ​ C
Less than 40%​ ​ ​ ​ No Grade
public class StudentGrade {
public static void calculateGrade(String name, double marks) {
[Link]("Student Name: " + name);
[Link]("Marks Obtained: " + marks + "%");
if (marks >= 80) {
[Link]("Grade: A");
}
else if (marks >= 60) {
[Link]("Grade: B");
}
else if (marks >= 40) {
[Link]("Grade: C");
}
else {
[Link]("No Grade");
}
}
}

26.​Write a program to input the quantity purchased and the rate. Calculate the total
purchase price and display it along with the gift to be presented. The gifts for the
customers are given as under:
Amount of Purchase (in ₹) Gift
100 and above but less than 500 A key ring
500 and above but less than 1000 A leather purse
1000 and above A pocket calculator
Note:
The flowchart will end with a ‘Thank you’ message"
public class PurchaseGift {
public static void calculateGift(int quantity, double rate) {
double total = quantity * rate;
[Link]("Total Purchase: ₹" + total);

if (total >= 100 && total < 500) {


[Link]("Gift: A key ring");
}
else if (total >= 500 && total < 1000) {
[Link]("Gift: A leather purse");
}
else if (total >= 1000) {
[Link]("Gift: A pocket calculator");
}
else {
[Link]("No gift");
}
[Link]("Thank you!");
}
}

27.​A librarian charges a fine for books, if returned late. The tariff for the fine is given
below:

No. of days​ ​ ​ ​ ​ ​ ​ Fine


For the first ten days ​ ​ ​ ​ 40 paise per day
Eleven to twenty days ​ ​ ​ ​ 60 paise per day
More than twenty days​ ​ ​ ​ 80 paise per day

Write a program to calculate the fine, assuming that a book is returned ‘N’ days
late.
public class LibraryFine {
public static void calculateFine(int daysLate) {
double fine;
if (daysLate <= 10) {
fine = daysLate * 0.40;
}
else if (daysLate <= 20) {
fine = (10 * 0.40) + ((daysLate - 10) * 0.60);
}
else {
fine = (10 * 0.40) + (10 * 0.60) + ((daysLate - 20) * 0.80);
}
[Link]("Days Late: " + daysLate);
[Link]("Total Fine: ₹" + fine));
}
}

28.​Write a program to accept a two digit number and extract the digits in units and
tens place.
public class DigitExtractor
{
public static void main(int number)
{
// Validate if the number is indeed a two-digit number
if (number > 10 || number < 99)
{
​ // Calculate the units digit
int unitsDigit = number % 10;

// Calculate the tens digit


int tensDigit = number / 10;

[Link]("Units digit: " + unitsDigit);


[Link]("Tens digit: " + tensDigit);
}
else
{
​​ ​ [Link]("Invalid input. Please enter a two-digit number.");
}
}
}

You might also like