Java Math’s Programs 1
Fibonacci Series:-In Fibonacci series, next number is the sum of previous two numbers. It usually starts
with 0 and 1. The sequence goes like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on.
Fibonacci Series:- simple program with for loop Output
public class Fibonacci_Numbers { 0,1,1,2,3,5,8,13,21,34
public static void main(String[] args) {
int a = 6;
int n1 = 0;
int n2 = 1;
[Link](n1 + "," + n2);
for (int i = 2; i < a; i++) {
int n3 = n1 + n2;
[Link]("," + n3);
n1 = n2;
n2 = n3;
}}}
Fibonacci Series:- Static method is used Output
public class Fibonacci_Numbers { 0,1,1,2,3,5,8,13,21,34
static int n1=0,n2=1,n3=0;
static void fibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
[Link](" "+n3);
fibonacci(count-1);
}
}
public static void main(String[] args) {
int count=10;
[Link](n1+" "+n2);
fibonacci(count-2);
}}
Fibonacci Series:- Recursion is used i.e use of HashMap Output
import [Link]; F(0) = 0
import [Link]; F(1) = 1
public class Fibonacci_Numbers { F(2) = 1
private static int fibonacci(int n, Map<Integer, Integer> map) { F(3) = 2
if (n == 0) F(4) = 3
return 0; // Base case
if (n == 1)
return 1; // Base case
if ([Link](n)) {
return [Link](n);
}
int result = fibonacci(n - 1, map) + fibonacci(n - 2, map);
[Link](n, result);
return result;
}
public static void main(String[] args) {
int count = 5;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < count; i++) {
[Link](i, fibonacci(i, map));
}
for ([Link]<Integer, Integer> entry : [Link]()) {
[Link]("F(" + [Link]() + ") = " + [Link]());
}}}
Java Math’s Programs 2
Prime Number: - Prime no. is a number that is greater than 1 and divided by 1 or itself only. In other
words, Prime no’s can't be divided by other no’s than itself or 1. (e.g 2, 3, 5, 7, 11, 13... are the prime nos.)
Prime Number: - simple Output
public class PrimeNumberCheck { 13 is a prime number
public static void main(String[] args) {
int a = 13, b = a / 2;
for (int i = 2; i <= b; i++) {
if (a % i == 0) {
[Link](a + " is a not prime number");
break;
} else {
[Link](a + " is a prime number");
break;
}}}}
Prime Number: - with array Output
package javaMathemathPrograms; 13 is a prime number
15 is not a prime
public class PrimeNumberCheck { number
public static void main(String[] args) { 2 is a prime number
int a[] = { 13, 15, 2, 16, 18 }; 16 is not a prime
number
for (int number : a) { 18 is not a prime
boolean isPrime = true; number
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= [Link](number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}}
if (isPrime) {
[Link](number + " is a prime number");
} else {
[Link](number + " is not a prime number");
} } }}
Java Math’s Programs 3
Even/Odd Number: -
Even/Odd Number: - with Array Output
public class Fibonacci_Numbers { 18 is a even number
public static void main(String[] args) {
int a = 18;
if (a % 2 == 0) {
[Link](a + " is a even number");
} else {
[Link](a + " is a odd number");
}
}
Even/Odd Number: - with Array Output
package javaMathemathPrograms; 13 is a odd number
15 is a odd number
public class Fibonacci_Numbers { 2 is a even number
16 is a even number
public static void main(String[] args) { 18 is a even number
int a[] = { 13, 15, 2, 16, 18 };
for (int i = 0; i < [Link]; i++) {
if (a[i] % 2 == 0) {
[Link](a[i] + " is a even number");
} else {
[Link](a[i] + " is a odd number");
} } }}
Java Math’s Programs 4
Palindrome Program: - In simple terms, a palindrome is a word, phrase, number, or other sequences of
characters that reads the same forward and backward.
Palindrome Program - with Array Output
package javaMathemathPrograms; 13 is a odd number
15 is a odd number
public class Fibonacci_Numbers { 2 is a even number
16 is a even number
public static void main(String[] args) { 18 is a even number
int a[] = { 13, 15, 2, 16, 18 };
for (int i = 0; i < [Link]; i++) {
if (a[i] % 2 == 0) {
[Link](a[i] + " is a even number");
} else {
[Link](a[i] + " is a odd number");
} } }}