[Go to site: main page, start]

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

Java Basic Codes

The document contains basic Java code examples demonstrating fundamental programming concepts such as printing 'Hello, World!', performing arithmetic operations (addition, subtraction, multiplication, division), using loops (for and while), calculating factorials, implementing if-else statements, working with arrays, and taking user input with a scanner. Each example is presented as a separate public class with a main method. These examples serve as a foundational guide for beginners learning Java programming.
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 views3 pages

Java Basic Codes

The document contains basic Java code examples demonstrating fundamental programming concepts such as printing 'Hello, World!', performing arithmetic operations (addition, subtraction, multiplication, division), using loops (for and while), calculating factorials, implementing if-else statements, working with arrays, and taking user input with a scanner. Each example is presented as a separate public class with a main method. These examples serve as a foundational guide for beginners learning Java programming.
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

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

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

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

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

Division
public class Division {
public static void main(String[] args) {
int a = 10, b = 2;
int quotient = a / b;
[Link]("Quotient: " + quotient);
}
}

For Loop Example


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

While Loop Example


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

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);
}
}

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);
}
}

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");
}
}
}

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]);
}
}
}

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]();
}
}

You might also like