Basic Java Programs
Practice basic Java programs for printing, keyboard input, simple maths, unit conversions, and swapping values. Read full code, steps, and output.
20 programs · open one, then the next
Start with Hello World- 01Hello WorldBeginnerPrint Hello, World! in Java with System.out.println inside main. Save Main.java, compile with javac, and run it to check that your Java setup works.Open example →javapublic class Main { public static void main(String[] args) { System.out.println("Hello, World!"); }
- 02Add Two NumbersBeginnerAdd two numbers in Java by reading doubles with Scanner and using +. The sample prints 30.0; decimal input is allowed, but text input is not added.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 03Swap Two NumbersBeginnerExchange two integer values in Java with a temporary variable. Compare the before and after output, and see why assigning a = b first loses a value.Open example →javapublic class Main { public static void main(String[] args) { int a = 5; int b = 10;
- 04Find ASCII Value of a CharacterBeginnerFind a character code in Java with Scanner, charAt(0), and an int cast. A gives 65; values outside 0–127 are Unicode values, not ASCII codes.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 05Even or Odd NumberBeginnerCheck whether an integer is even or odd in Java using Scanner and n % 2. A zero remainder means even, including zero and negative even numbers.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 06Largest of Two NumbersBeginnerChoose the larger of two numbers in Java with Scanner and an if/else chain. Decimal scores are accepted, and equal inputs receive their own message.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 07Largest of Three NumbersBeginnerFind the largest of three numbers in Java with paired >= comparisons and &&. Scanner reads decimal values, and the comparisons include tied scores.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 08Positive or Negative NumberBeginnerClassify a number in Java with > 0 and < 0 after reading a double using Scanner. Zero has its own branch; this example expects ordinary finite input.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 09Area of CircleBeginnerCalculate a circle's area in Java using Scanner and Math.PI * r * r. The result is in square units; enter a non-negative radius in a consistent unit.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 10Area of RectangleBeginnerWork out a rectangle's area in Java by multiplying length and breadth read with Scanner. Decimal lengths are allowed; both must use the same unit.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 11Area of TriangleBeginnerCompute triangle area in Java from base and height with 0.5 * base * height. Scanner reads decimals; height must be perpendicular to the base.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 12Simple InterestBeginnerCalculate simple interest in Java from principal, annual rate, and years using Scanner. Divide the product by 100; the output excludes the principal.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 13Compound InterestIntermediateUse Math.pow in Java to calculate compound amount and interest from Scanner input. Set a positive compounding count; double values may round slightly.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 14Celsius to FahrenheitBeginnerConvert Celsius to Fahrenheit in Java with Scanner and c * 9 / 5 + 32. A double keeps decimal parts; grouping 9 / 5 alone would use integer division.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 15Sum of N Natural NumbersBeginnerTotal 1 through N in Java with n * (n + 1) / 2 instead of a loop. Scanner reads N; use small non-negative inputs to avoid overflowing the int product.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 16Power without Math.pow()BeginnerBuild a power in Java without Math.pow by multiplying a long result in a for loop. Scanner reads the base and exponent; negative powers are not supported.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 17ASCII to CharacterBeginnerTurn an ASCII number into a character in Java using Scanner and a char cast. Enter 0–127; control codes may not print as visible letters on screen.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 18Character to ASCIIBeginnerRead a letter in Java with Scanner.next().charAt(0) and cast it to int. Lowercase a gives 97; a word uses only its first char, and not all chars are ASCII.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 19Convert Seconds to Hours and MinutesBeginnerSplit total seconds into hours, minutes, and seconds in Java using integer division and %. Scanner reads a non-negative duration; 3672 becomes 1:1:12.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {
- 20Gross Salary CalculationBeginnerFind gross salary in Java by reading basic pay and HRA/DA rates with Scanner. Calculate both allowances from basic pay; deductions are not included.Open example →javaimport java.util.Scanner; public class Main { public static void main(String[] args) {