0% found this document useful (0 votes)
9 views4 pages

Java Basic Codes Explained

The document provides basic Java code examples along with explanations for each program. It covers fundamental concepts such as printing to the console, arithmetic operations, loops, conditional statements, arrays, and user input. Each example is accompanied by a brief description of its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Java Basic Codes Explained

The document provides basic Java code examples along with explanations for each program. It covers fundamental concepts such as printing to the console, arithmetic operations, loops, conditional statements, arrays, and user input. Each example is accompanied by a brief description of its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Basic Codes with Explanations

Hello World
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}

Explanation: This is the simplest Java program. It prints 'Hello, World!' to the screen.

Addition
public class Addition {
public static void main(String[] args) {
int a = 5, b = 3;
int sum = a + b;
[Link]("Sum: " + sum);
}
}

Explanation: This program adds two integers (5 and 3) and prints their sum.

Subtraction
public class Subtraction {
public static void main(String[] args) {
int a = 5, b = 3;
int diff = a - b;
[Link]("Difference: " + diff);
}
}

Explanation: This program subtracts b from a (5 - 3) and prints the result.

Multiplication
public class Multiplication {
public static void main(String[] args) {
int a = 5, b = 3;
int prod = a * b;
[Link]("Product: " + prod);
}
}

Explanation: This program multiplies two numbers (5 * 3) and displays the product.
Division
public class Division {
public static void main(String[] args) {
int a = 10, b = 2;
int quotient = a / b;
[Link]("Quotient: " + quotient);
}
}

Explanation: This program divides a by b (10 / 2) and prints the quotient.

For Loop Example


public class ForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link]("i = " + i);
}
}
}

Explanation: This program uses a for loop to print the values of i from 1 to 5.

While Loop Example


public class WhileLoop {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link]("i = " + i);
i++;
}
}
}

Explanation: This program uses a while loop to print numbers 1 through 5.

Factorial Loop
public class Factorial {
public static void main(String[] args) {
int n = 5;
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
[Link]("Factorial of " + n + " is " + fact);
}
}

Explanation: This program calculates factorial of 5 (5! = 1*2*3*4*5 = 120).

Double Factorial Loop


public class DoubleFactorial {
public static void main(String[] args) {
int n = 6;
int doubleFact = 1;
for (int i = n; i > 0; i -= 2) {
doubleFact *= i;
}
[Link]("Double Factorial of " + n + " is " + doubleFact);
}
}

Explanation: This program calculates double factorial of 6 (6!! = 6*4*2 = 48).

If-Else Example
public class IfElse {
public static void main(String[] args) {
int num = 10;
if (num % 2 == 0) {
[Link](num + " is even");
} else {
[Link](num + " is odd");
}
}
}

Explanation: This program checks if a number is even or odd using if-else.

Array Example
public class ArrayExample {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < [Link]; i++) {
[Link]("Element at index " + i + ": " + arr[i]);
}
}
}

Explanation: This program declares an array of integers and prints each element with its
index.
Scanner Input Example
import [Link];
public class ScannerExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello, " + name + "!");
[Link]();
}
}

Explanation: This program demonstrates user input in Java using Scanner. It asks for a
name and then greets the user.

You might also like