0% found this document useful (0 votes)
4 views5 pages

Java Programs

The document contains multiple Java programs demonstrating basic programming concepts. These include printing 'Hello World', adding two numbers, checking even or odd, calculating factorial, reversing a string, checking for palindrome, printing Fibonacci series, swapping numbers, checking for prime numbers, and printing a multiplication table. Each program is structured with a main method and utilizes user input where necessary.

Uploaded by

lord91249
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)
4 views5 pages

Java Programs

The document contains multiple Java programs demonstrating basic programming concepts. These include printing 'Hello World', adding two numbers, checking even or odd, calculating factorial, reversing a string, checking for palindrome, printing Fibonacci series, swapping numbers, checking for prime numbers, and printing a multiplication table. Each program is structured with a main method and utilizes user input where necessary.

Uploaded by

lord91249
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
You are on page 1/ 5

Program to Print Hello World

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello, World!");

Program to Add Two Numbers

public class AddNumbers {

public static void main(String[] args) {

int num1 = 10, num2 = 20;

int sum = num1 + num2;

System.out.println("Sum: " + sum);

Program to Check Even or Odd

import java.util.Scanner;

public class EvenOdd {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

if(num % 2 == 0)

System.out.println("Even");

else

System.out.println("Odd");

}
Program to Find Factorial

import java.util.Scanner;

public class Factorial {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

long fact = 1;

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

fact *= i;

System.out.println("Factorial: " + fact);

Program to Reverse a String

import java.util.Scanner;

public class ReverseString {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str = sc.nextLine();

String reversed = "";

for(int i = str.length() - 1; i >= 0; i--) {

reversed += str.charAt(i);

System.out.println("Reversed: " + reversed);

}
Program to Check Palindrome

import java.util.Scanner;

public class Palindrome {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str = sc.nextLine();

String reversed = new StringBuilder(str).reverse().toString();

if(str.equals(reversed))

System.out.println("Palindrome");

else

System.out.println("Not a Palindrome");

Program to Print Fibonacci Series

import java.util.Scanner;

public class Fibonacci {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int a = 0, b = 1;

System.out.print(a + " " + b);

for(int i = 2; i < n; i++) {

int c = a + b;

System.out.print(" " + c);

a = b;

b = c;
}

Program to Swap Two Numbers

public class SwapNumbers {

public static void main(String[] args) {

int a = 5, b = 10;

int temp = a;

a = b;

b = temp;

System.out.println("a = " + a + ", b = " + b);

Program to Find Prime Number

import java.util.Scanner;

public class PrimeCheck {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

boolean isPrime = true;

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

if(num % i == 0) {

isPrime = false;

break;

}
if(isPrime)

System.out.println("Prime");

else

System.out.println("Not Prime");

Program to Print Multiplication Table

import java.util.Scanner;

public class MultiplicationTable {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

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

System.out.println(num + " x " + i + " = " + (num * i));

You might also like