0% found this document useful (0 votes)
24 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)
24 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) {
[Link]("Hello, World!");
}
}

2. Addition of Two Numbers (Input)

import [Link];

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

import [Link];

class CircleArea {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter radius: ");
double r = [Link]();
double area = 3.14159 * r * r;
[Link]("Area = " + area);
}
}

4. Swap Two Numbers (Using Third Variable)

import [Link];

class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();

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

[Link]("After swap: a = " + a + ", b = " + b);


}
}

5. Check Even or Odd

import [Link];

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

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

6. Find Largest of Two Numbers


import [Link];

class LargestOfTwo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();

if(a > b)
[Link](a + " is larger");
else
[Link](b + " is larger");
}
}

7. Factorial of a Number

import [Link];

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

[Link]("Factorial = " + fact);


}
}

8. Check Prime Number

import [Link];

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

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)
[Link](n + " is Prime");
else
[Link](n + " is Not Prime");
}
}

9. Fibonacci Series (n terms)

import [Link];

class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
int n = [Link]();

int a = 0, b = 1;
[Link]("Fibonacci: " + a + " " + b);

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


int c = a + b;
[Link](" " + c);
a = b;
b = c;
}
}
}

10. Reverse a Number

import [Link];

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

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

[Link]("Reversed Number = " + rev);


}
}

You might also like