0% found this document useful (0 votes)
7 views

Java Maths Programs

Uploaded by

rasalshitals
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Maths Programs

Uploaded by

rasalshitals
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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;
System.out.print(n1 + "," + n2);
for (int i = 2; i < a; i++) {
int n3 = n1 + n2;
System.out.print("," + 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;
System.out.print(" "+n3);
fibonacci(count-1);
}
}
public static void main(String[] args) {
int count=10;
System.out.print(n1+" "+n2);
fibonacci(count-2);
}}

Fibonacci Series:- Recursion is used i.e use of HashMap Output


import java.util.HashMap; F(0) = 0
import java.util.Map; 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 (map.containsKey(n)) {
return map.get(n);
}
int result = fibonacci(n - 1, map) + fibonacci(n - 2, map);
map.put(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++) {
map.put(i, fibonacci(i, map));
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("F(" + entry.getKey() + ") = " + entry.getValue());
}}}
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) {
System.out.println(a + " is a not prime number");
break;
} else {
System.out.println(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 <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}}
if (isPrime) {
System.out.println(number + " is a prime number");
} else {
System.out.println(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) {
System.out.println(a + " is a even number");
} else {
System.out.println(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 < a.length; i++) {
if (a[i] % 2 == 0) {
System.out.println(a[i] + " is a even number");
} else {
System.out.println(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 < a.length; i++) {
if (a[i] % 2 == 0) {
System.out.println(a[i] + " is a even number");
} else {
System.out.println(a[i] + " is a odd number");
} } }}

You might also like