[Go to site: main page, start]

0% found this document useful (0 votes)
1 views2 pages

Java Basics Code

The document contains basic Java programs demonstrating fundamental concepts such as printing output, user input, arithmetic operations, control flow, and algorithms. Key examples include a 'Hello World' program, simple addition, checking even or odd numbers, calculating factorials, and checking for prime numbers. Each program is presented with its code and a brief description of its functionality.

Uploaded by

bharrathi.mca
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)
1 views2 pages

Java Basics Code

The document contains basic Java programs demonstrating fundamental concepts such as printing output, user input, arithmetic operations, control flow, and algorithms. Key examples include a 'Hello World' program, simple addition, checking even or odd numbers, calculating factorials, and checking for prime numbers. Each program is presented with its code and a brief description of its functionality.

Uploaded by

bharrathi.mca
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

Basic Java Programs

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

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

3. Take Input from User (Using Scanner)


import [Link];

public class UserInput {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello " + name + "!");
}
}

4. Even or Odd
import [Link];

public class EvenOdd {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

if (num % 2 == 0)
[Link]("Even");
else
[Link]("Odd");
}
}

5. Find Factorial using Loop


public class Factorial {
Basic Java Programs

public static void main(String[] args) {


int num = 5;
long fact = 1;

for (int i = 1; i <= num; i++) {


fact *= i;
}

[Link]("Factorial of " + num + " is " + fact);


}
}

6. Check Prime Number


public class PrimeCheck {
public static void main(String[] args) {
int num = 7;
boolean isPrime = true;

if (num <= 1) isPrime = false;

for (int i = 2; i <= num / 2; i++) {


if (num % i == 0) {
isPrime = false;
break;
}
}

if (isPrime)
[Link](num + " is Prime");
else
[Link](num + " is Not Prime");
}
}

You might also like