[Link] Java Programs By Mr.
Vishnu
Important Java Programs
Program to print alphabets both in small and capital.
[Link];
public class Alphabets {
public static void main(String args[]) {
char ch;
[Link]("Small Alphabets: ");
for (ch = 'a'; ch<= 'z'; ch++) {
[Link](ch);
}
[Link]("Capital Alphabets: ");
for (ch = 'A'; ch<= 'Z'; ch++) {
[Link](ch);
}
}
Output:
Small Alphabets:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
1
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
q
r
s
t
u
v
w
x
y
z
Capital Alphabets:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Program to perform arithmetic operations.
[Link];
2
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
public class ArithmeticOperations {
static void addition(int num1, int num2) {
[Link]("Addition of given numbers = ");
[Link](num1 + num2);
}
static void subtraction(int num1, int num2) {
[Link]("Subtraction of given numbers = ");
[Link](num1 - num2);
}
static void multiplication(int num1, int num2) {
[Link]("Multiplication of given no.s = ");
[Link](num1 * num2);
}
static void division(int num1, int num2) {
[Link]("Division of given given no.s = ");
[Link](num1 / num2);
}
staticvoid moduloDivision(int num1, int num2) {
[Link]("ModuloDivision of given no.s = ");
[Link](num1 % num2);
}
public static void main(String args[]) {
// method call from main method
addition(20, 10);
subtraction(40, 30);
multiplication(20, 30);
division(20, 4);
moduloDivision(20, 3);
}
Output:
Addition of given numbers = 30
Subtraction of given numbers = 10
Multiplication of given no.s = 600
Division of given given no.s = 5
3
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
ModuloDivision of given no.s = 2
Program to find that given number is Armstrong or not.
[Link];
public class ArmstrongNumber {
static void armstrong(int num) {
int newNum = 0, reminder, temp;
temp = num;
// find sum of all digit's cube of the number.
while (temp != 0) {
reminder = temp % 10;
newNum = newNum + reminder * reminder * reminder;
temp = temp / 10;
}
// Check if sum of all digit's cube of the number is
// equal to the given number or not.
if (newNum == num) {
[Link](num + " is armstrong.");
} else {
[Link](num + " is not armstrong.");
}
}
public static void main(String args[]) {
// method call
armstrong(407);
}
Output:
407 is armstrong.
Program to check that given number is even or odd.
[Link];
4
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
public class EvenOrOdd {
static void evenOdd(int num) {
if (num % 2 == 0) {
[Link]("Given number is even.");
} else {
[Link]("Given number is odd.");
}
}
public static void main(String args[]) {
// method call, no need of object.
evenOdd(123);
}
}
Output:
Given number is odd.
Program to calculate Factorial of given number.
[Link];
public class Factorial {
static void factorialNumber(int num) {
int fact = 1;
// Factorial of 0 and 1 is 1.
if (num == 0 || num == 1) {
[Link]("factorial of " + num + " is 1.");
}
// for calculating factorial, number should be non negative.
if (num> 0) {
// calculate factorial.
for (inti = 2; i<= num; i++) {
fact = fact * i;
}
[Link]("factorial of " + num + " is " + fact);
} else {
[Link]("no. should be non negative.");
}
}
public static void main(String args[]) {
5
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
// method call
factorialNumber(5);
}
Output:
factorial of 5 is 120
Program to find factorial of given number by recursion.
[Link];
public class FactorialByRecursion {
static int fact(intnum) {
// Factorial of 0 is 1.
if (num == 0)
return 1;
else
return num * fact(num - 1);
}
public static void main(String args[]) {
int num = 5, factorial;
// method call
if (num> 0) {
factorial = fact(num);
[Link]("Factorial of " + num + " is " + factorial);
} else {
[Link]("Number should be non negative.");
}
}
Output:
Factorial of 5 is 120
6
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
Program to print fibonacci series.
[Link];
public class FibonacciSeries {
static void fibonacci(intnum) {
int f1, f2 = 0, f3 = 1;
for (inti = 1; i<= num; i++) {
[Link](f3);
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
public static void main(String args[]) {
int num = 6;
// method call
if (num> 0) {
fibonacci(num);
} else {
[Link]("No. should be greater than zero.");
}
}
Output:
1
1
2
3
5
8
Program to find that given number is Palindrome or not.
[Link];
public class PalindromeNumber {
7
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
static void palindrome(intnum) {
int newNum = 0, reminder, temp;
temp = num;
// find sum of all digit's of the number.
while (temp != 0) {
reminder = temp % 10;
newNum = newNum * 10 + reminder;
temp = temp / 10;
}
// Check if sum of all digit's of the number
// is equal to the given number or not.
if (newNum == num) {
[Link](num + " is palindrome.");
} else {
[Link](num + " is not palindrome.");
}
}
public static void main(String args[]) {
// method call
palindrome(12321);
}
Output:
12321 is palindrome.
Program to find that given number is prime or not.
[Link];
public class PrimeNumber {
static void primeNumber(intnum) {
intcount = 0;
// 0 and 1 are not prime numbers.
if (num == 0 || num == 1) {
[Link](num + " is not prime.");
} else {
for (inti = 1; i<= num / 2; i++) {
8
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
if (num % i == 0) {
count++;
}
}
if (count> 1) {
[Link](num + " is not prime.");
} else {
[Link](num + " is prime.");
}
}
}
public static void main(String args[]) {
// method call
primeNumber(37);
}
Output:
37 is prime.
Program to swap two numbers without using third or temp variable.
[Link];
publicclass SwapNumbers {
staticvoid swapNumbers(intnum1, intnum2) {
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
[Link]("After swapping: " + num1 + " and " + num2);
}
publicstaticvoid main(String args[]) {
intnum1 = 20;
intnum2 = 30;
[Link]("Before swapping:" + num1 + " and " + num2);
// method call
9
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677
[Link] Java Programs By Mr. Vishnu
swapNumbers(num1, num2);
}
Output:
Before swapping:20 and 30
After swapping: 30 and 20
10
Sri Sureka Technologies, Opposite Geethanjali High School, Near [Link] Signal, [Link],
Hyderabad-500038, Ph: 040-66616677