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

Simple Java Programs

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)
4 views7 pages

Simple Java Programs

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

Simple Java Programs

1. Hello World Program

class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

2. Addition of Two Numbers (Input)

import java.util.Scanner;

class AddTwoNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
int sum = a + b;
System.out.println("Sum = " + sum);
}
}
3. Area of a Circle (Input Radius)

import java.util.Scanner;

class CircleArea {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius: ");
double r = sc.nextDouble();
double area = 3.14159 * r * r;
System.out.println("Area = " + area);
}
}

4. Swap Two Numbers (Using Third Variable)

import java.util.Scanner;

class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();

int temp = a;
a = b;
b = temp;

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


}
}

5. Check Even or Odd

import java.util.Scanner;

class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();

if(n % 2 == 0)
System.out.println(n + " is Even");
else
System.out.println(n + " is Odd");
}
}

6. Find Largest of Two Numbers


import java.util.Scanner;

class LargestOfTwo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();

if(a > b)
System.out.println(a + " is larger");
else
System.out.println(b + " is larger");
}
}

7. Factorial of a Number

import java.util.Scanner;

class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
long fact = 1;
for(int i = 1; i <= n; i++) {
fact *= i;
}

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


}
}

8. Check Prime Number

import java.util.Scanner;

class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();

boolean isPrime = true;


if(n <= 1) isPrime = false;
else {
for(int i = 2; i <= n/2; i++) {
if(n % i == 0) {
isPrime = false;
break;
}
}
}

if(isPrime)
System.out.println(n + " is Prime");
else
System.out.println(n + " is Not Prime");
}
}

9. Fibonacci Series (n terms)

import java.util.Scanner;

class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of terms: ");
int n = sc.nextInt();

int a = 0, b = 1;
System.out.print("Fibonacci: " + a + " " + b);

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


int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}
}
}

10. Reverse a Number

import java.util.Scanner;

class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();

int rev = 0;
while(n != 0) {
int digit = n % 10;
rev = rev * 10 + digit;
n /= 10;
}

System.out.println("Reversed Number = " + rev);


}
}

You might also like