10.Important Java Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

10.Imp Java Programs By Mr.

Vishnu

Important Java Programs


Program to print alphabets both in small and capital.

packagecom.sst;

public class Alphabets {


public static void main(String args[]) {
char ch;
System.out.println("Small Alphabets: ");
for (ch = 'a'; ch<= 'z'; ch++) {
System.out.println(ch);
}

System.out.println("Capital Alphabets: ");

for (ch = 'A'; ch<= 'Z'; ch++) {


System.out.println(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 S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp 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.

packagecom.sst;

2
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp Java Programs By Mr. Vishnu

public class ArithmeticOperations {


static void addition(int num1, int num2) {
System.out.print("Addition of given numbers = ");
System.out.println(num1 + num2);
}

static void subtraction(int num1, int num2) {


System.out.print("Subtraction of given numbers = ");
System.out.println(num1 - num2);
}

static void multiplication(int num1, int num2) {


System.out.print("Multiplication of given no.s = ");
System.out.println(num1 * num2);
}

static void division(int num1, int num2) {


System.out.print("Division of given given no.s = ");
System.out.println(num1 / num2);
}

staticvoid moduloDivision(int num1, int num2) {


System.out.print("ModuloDivision of given no.s = ");
System.out.println(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 S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp Java Programs By Mr. Vishnu

ModuloDivision of given no.s = 2

Program to find that given number is Armstrong or not.

packagecom.sst;

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) {
System.out.println(num + " is armstrong.");
} else {
System.out.println(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.

packagecom.sst;

4
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp Java Programs By Mr. Vishnu

public class EvenOrOdd {


static void evenOdd(int num) {
if (num % 2 == 0) {
System.out.println("Given number is even.");
} else {
System.out.println("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.

packagecom.sst;

public class Factorial {


static void factorialNumber(int num) {
int fact = 1;
// Factorial of 0 and 1 is 1.
if (num == 0 || num == 1) {
System.out.println("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;
}
System.out.println("factorial of " + num + " is " + fact);
} else {
System.out.println("no. should be non negative.");
}
}

public static void main(String args[]) {


5
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp Java Programs By Mr. Vishnu

// method call
factorialNumber(5);
}

Output:

factorial of 5 is 120

Program to find factorial of given number by recursion.

packagecom.sst;

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);
System.out.println("Factorial of " + num + " is " + factorial);
} else {
System.out.println("Number should be non negative.");
}
}

Output:

Factorial of 5 is 120

6
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp Java Programs By Mr. Vishnu

Program to print fibonacci series.

packagecom.sst;

public class FibonacciSeries {


static void fibonacci(intnum) {
int f1, f2 = 0, f3 = 1;
for (inti = 1; i<= num; i++) {
System.out.println(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 {
System.out.println("No. should be greater than zero.");
}
}

Output:

1
1
2
3
5
8

Program to find that given number is Palindrome or not.

packagecom.sst;

public class PalindromeNumber {


7
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp 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) {
System.out.println(num + " is palindrome.");
} else {
System.out.println(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.

packagecom.sst;

public class PrimeNumber {


static void primeNumber(intnum) {
intcount = 0;
// 0 and 1 are not prime numbers.
if (num == 0 || num == 1) {
System.out.println(num + " is not prime.");
} else {
for (inti = 1; i<= num / 2; i++) {
8
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp Java Programs By Mr. Vishnu

if (num % i == 0) {
count++;
}
}
if (count> 1) {
System.out.println(num + " is not prime.");
} else {
System.out.println(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.

packagecom.sst;

publicclass SwapNumbers {
staticvoid swapNumbers(intnum1, intnum2) {
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;

System.out.println("After swapping: " + num1 + " and " + num2);


}

publicstaticvoid main(String args[]) {


intnum1 = 20;
intnum2 = 30;
System.out.println("Before swapping:" + num1 + " and " + num2);
// method call
9
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677
10.Imp 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 S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677

You might also like