Find the average of two numbers:
public class Practice { public static void main(String[] args) { int num1= 5 , num2 = 3; int avg = (num1+num2)/2; System.out.println(“Average : ” + avg); } } |
Find the Sum of Square and Cube of a Number:
public class Practice { public static void main(String[] args) { int a = 5; int sq = a*a; int cu = a*a*a; int sum = sq + cu; System.out.println(“Square of : ” + a + ” is : ” + sq); System.out.println(“Cube of : ” + a + ” is : ” + cu); System.out.println(“Sum is : ” + sum); } } |
Perform Arithmetic Operations:
public class Practice { public static void main(String[] args) { int a=5, b=2; System.out.println(a + ” + ” + b + ” = ” + (a+b)); System.out.println(a + ” – ” + b + ” = ” + (a-b)); System.out.println(a + ” * ” + b + ” = ” + (a*b)); System.out.println(a + ” / ” + b + ” = ” + (a/b)); System.out.println(a + ” % ” + b + ” = ” + (a%b)); } } |
Print Last Digit of given Number:
public class Practice { public static void main(String[] args) { int n = 365; int d = n%10; System.out.println(“Last digit of ” + n + ” is ” + d); } } |
Remove Last Digit of Given Number
public class Practice { public static void main(String[] args) { int n=365; n = n/10; System.out.println(“After remove last digit : ” + n); } } |
Find the Sum of First N Natural Numbers:
Formula: n(n+1)/2
public class Practice { public static void main(String[] args) { int n = 5; int sum = n*(n+1)/2; System.out.println(“Sum of First ” + n + ” numbers : ” + sum); } } |
Calculate Total Salary of Employee:
public class Practice { public static void main(String[] args) { double basic = 25000; double hra = 0.25*basic; double ta = 0.2*basic; double da = 0.15*basic; double total = basic + hra + ta + da ; System.out.println(“Total Salary : ” + total); } } |
Find the Third Angle in Triangle:
- Sum of angles of a triangle is 180 degrees
- If two angles given, then third angle become -> c = 180 – (a+b)
public class Practice { public static void main(String[] args) { int a=70, b=60, c; c = 180 – (a + b); System.out.println(“Third angle is : ” + c); } } |
Swap two Numbers:
public class Practice { public static void main(String[] args) { int a=5, b=3; System.out.println(“Before Swap : ” + a + “, ” + b); int temp=a; a=b; b=temp; System.out.println(“After Swap : ” + a + “, ” + b); } } |
Swap two numbers without third variable:
public class Practice { public static void main(String[] args) { int a=5, b=3; System.out.println(“Before Swap : ” + a + “, ” + b); a = a+b; b = a-b; a = a-b; System.out.println(“After Swap : ” + a + “, ” + b); } } |
Display Amount based on Quantity: Accept the rate for a dozen bananas and the quantity required to determine the cost:
import java.util.Scanner; public class Practice { public static void main(String[] args) { double cost, quantity, amount; cost = 55; quantity = 38; amount = quantity/12 * cost ; System.out.println(“Amount is : ” + amount); } } |