### Prime, Fibonacci and Factorial number with example in java

Prime number, Fibonacci series and Factorial number programs are most commonly asked questions in interview. Read this article to know what is and how to write programs for prime number, fibonacci series and factorial number.

The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false.

Here is the another example to find the given number is prime or not. But, the logic and returning output will differ from above example.

The below program will help you to identify and print the primitive numbers from 1 to 100 range. It also print total number of primitive numbers exists between 1 to 100.

The below example will produces the similar output of the previous example. But, implementation logic will differ from previous program.

The below example will find and print all Fibonacci numbers which value is lessthan 100. Please have a look at the below source code to understand more.

In the above formula, n represents the which you are passing the natural number for finding the factorial number. There are two different ways of calculating factorial number of a given number. Those are with recursion and without recursion. The below program illustrates, finding factorial number without using recursion.

The program will help you to find the factorial number of a given number by using the recursion.

**Prime Number:**prime number is natural number greater than 1 that has no positive divisor other than 1 and itself. A natural number greater than 1 is not a prime number, is called**Composite number**. For example, 7 is a prime number. Because it can divide with 1 and 7 only. Where as 8 is composite number. Since it has the divisor 2 and 4 in addition to the 1 and 8.The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false.

package com.javatbrains.practice; public class PrimeNumber { public boolean isPrimeNumber(int number) { if(number <= 1) return false; // There's only one even prime: that is two if((number % 2) == 0) return(number == 2); int from = (int) (Math.sqrt(number) + 1); // You have to check possible divisors from 3 to sqrt(value) for(int i = 3; i <= from; i += 2) if((number % i) == 0) return false; return true; } public static void main(String args[]) { PrimeNumber prime = new PrimeNumber(); System.out.println("11 is prime or not? "+ prime.isPrimeNumber(11)); System.out.println("13 is prime or not? "+ prime.isPrimeNumber(13)); System.out.println("15 is prime or not? "+ prime.isPrimeNumber(15)); } }

OutPut: 11 is prime or not? true 13 is prime or not? true 15 is prime or not? false

Here is the another example to find the given number is prime or not. But, the logic and returning output will differ from above example.

package com.javatbrains.practice; public class PrimeNumber { public String isPrimeNumber(int number) { String isPrime = "Prime"; for(int j=2;j<number;j++){ if(number%j==0){ isPrime = "Not Prime"; break; } } returnisPrime; } publicstaticvoidmain(String args[]) { PrimeNumber prime = new PrimeNumber(); System.out.println("11 is prime or not? "+ prime.isPrimeNumber(11)); System.out.println("13 is prime or not? "+ prime.isPrimeNumber(13)); System.out.println("15 is prime or not? "+ prime.isPrimeNumber(15)); } }

OutPut: 11 is prime or not? Prime 13 is prime or not? Prime 15 is prime or not? Not Prime

The below program will help you to identify and print the primitive numbers from 1 to 100 range. It also print total number of primitive numbers exists between 1 to 100.

packagecom.javatbrains.practice; public class FindAllPrimeNumbers { static int limit= 100; static int count= 0; public static void main(String[] args) { for(int i=1;i<limit;i++){ boolean isPrime = true; for(int j=2;j<i;j++){ if(i%j==0){ isPrime = false; break; } } if(isPrime){ count++; System.out.print(i+", "); } } System.out.println(); System.out.println("Total Prime Numbers between 1 to "+limit+": "+count); } }

```
```

OutPut: 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

53, 59, 61, 67, 71, 73, 79, 83, 89, 97,

Total Prime Numbers between 1 to 100: 26

**Fibonacci Number:**Sum of the previous two numbers is called Fibonacci number. It has multiple synonyms like Fibonacci Series and Fibonacci sequence. The first two numbers in the sequence is 0 and 1. The following integer sequence represents the Fibonacci sequence as 0,1,1,2,3,5,8,13,21,34...etc. The below program will produces output as first 10 Fibonacci numbers starting from 0.package com.javatbrains.practice; public class Fibonacci { public static void main(String[] args) { int prev = 0, next = 1, sum, n; for(n = 1; n <= 10; n++) { System.out.print(prev+", "); sum = prev + next; prev = next; next = sum; } } } OutPut: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

The below example will produces the similar output of the previous example. But, implementation logic will differ from previous program.

package com.javatbrains.practice; public class Fibonacci { public static void main(String[] args) { int febCount = 10; int feb[] = new int[febCount]; feb[0] = 0; feb[1] = 1; for(int i = 2; i<febCount; i++){ feb[i] = feb[i-1]+feb[i-2]; } for(int i=0; i<febCount; i++){ System.out.print(feb[i]+", "); } } } OutPut: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

The below example will find and print all Fibonacci numbers which value is lessthan 100. Please have a look at the below source code to understand more.

package com.javatbrains.practice; public class Fibonacci { public static void main(String[] args) { int feb[] = new int[15]; feb[0] = 0; feb[1] = 1; for (int i = 2; i < 100; i++) { if (feb[i - 1] + feb[i - 2] < 100) { feb[i] = feb[i - 1] + feb[i - 2]; } else { break; } } for (int i = 0; i < feb.length; i++) { if (i == 0 || feb[i] > 0) { System.out.print(feb[i] + ", "); } } } }

Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

**Factorial Number:**The product of the natural numbers from 1 to the given number is called the factorial number. There is a small Math formula to understand before going into program. Factorial formula is,n!=1*2*3*.....*(n-1)*n

In the above formula, n represents the which you are passing the natural number for finding the factorial number. There are two different ways of calculating factorial number of a given number. Those are with recursion and without recursion. The below program illustrates, finding factorial number without using recursion.

package com.javatbrains.practice; public class Factorial { public static void main(String[] args) { int n = 6; int fact=1; for(int i=1;i<=n;i++){ //Factorial formula fact = fact*i; } System.out.println("Factorial of 6 is: "+fact); } } OutPut: Factorial of 6 is: 720

**Factorial using Recursion:**This is the another way of finding factorial of a given number using recursion. What is recursion? a function calls itself is called recursion. Below is the formula of finding factorial using recursion.fact(n) = n * fact(n-1);

The program will help you to find the factorial number of a given number by using the recursion.

package com.javatbrains.practice; public class RecursionFactorial { public static void main(String[] args) { int n = 6; intfact = recursionFactorial(n); System.out.println("Factorial number of "+n+"is: "+fact); } public static int recursionFactorial(int n){ if(n==0){ return1; }else{ returnn * recursionFactorial(n-1); } } } OutPut: Factorial number of 6 is: 720

## Comments

## Post a Comment