[Computer Science]
Q) WAP to store 15 Fibonacci numbers in an array and then display only prime Fibonacci in descending order and also print how many such numbers found.
_
• Kindly do not spam.
• Also, clear my doubt regarding whether I should find the prime numbers while sorting the array or I should find the primes and store it in second array in sorted manner?
Answers
Solution:
The given code is written in Java.
import java.util.*;
public class Array {
public static void main(String args[]) {
int a[]=new int[15];
int i,j,factors,number_of_primes=0;
a[0]=0;
a[1]=1;
for(i=2;i<a.length;i++)
a[i]=a[i-1]+a[i-2];
System.out.println("Given Array: "+Arrays.toString(a));
System.out.print("Prime Numbers in descending order in the array are: ");
for(i=a.length-1;i>-1;i--){
factors=0;
for(j=1;j<=a[i];j++){
if(a[i]%j==0)
++factors;
}
if(factors==2){
System.out.print(a[i]+" ");
number_of_primes++;
}
}
System.out.println("\nTherefore, there are "+number_of_primes+" prime fibonacci numbers in the array.");
}
}
Logic:
- Store the numbers in the array.
- Initialise counter = 0.
- Access each elements in arrays from last to first.
- Check if the number is prime or not. If prime, display the number.
- Increase the value of counter by 1.
- At last, display the value of counter.
Note:
- Variable 'factor' contains the total number of factors of each numbers of the array and 'number_of_primes' store the total number of prime fibonacci numbers in the array.
- There is no need of sorting the array. It is already sorted. You can access the array from backwards.
- To sort an array, you can use Arrays.sort() function. This function sorts array in ascending order. Example: To sort array 'a', use - Arrays.sort(a). You must import the Array class from java utility package to use this function.
See the attachment for output.
•••♪
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int temp=0,pcount=0;
int a[]=new int[15];
a[0]=0;
a[1]=1;
for(int i=2;i<a.length;i++)
{
a[i]=a[i-1]+a[i-2];//i-1 refers to 1 and i-2 refers to 0 so after each increment value of i increases so prints according to the suk of the two elemenys
}
System.out.println("Given Array:"+Arrays.toString(a));
System.out.print("Arrays in descending order:");
for(int i=a.length-1;i>=0;i--)//length-1 as the index of an array starts with 0 and it will check until index position is 0 so array is sorred in descending order
{
int c=0;//At the beginning of each number c is reversed so as to otherwise c will remain updating
{
for(int j=1;j<=a[i];j++)//To check the factors
{
if(a[i]%j==0)//if factors are 2 i.e 1 and the number itself so it prints the number
c++;
}
if(c==2)
{
System.out.print(a[i]+ " ");
pcount++;//After a prime element is found in the array so pcount is updated
}
}
}
System.out.println();//To print a new line
System.out.println("Number of prime elements:"+pcount);
}
}
- Output is attached
Answer of your doubt:-
- I recommend you to find the prime numbers while sorting, you may also do like that but it will take more time.
- In the same way in place of sorting like in the above you may sort using Arrays.sort() function.
- I also recommend you to not to sort using selection and bubble sort otherwise you may get confused as the program will become complicated so prefer using easier ways.
- Some statements I have explained in the program in the form of comments to understand the logic.
Hope you understood.