Field Manual for Hackers

java - Write a program to generate fibonncci series upto given limit and count the number of prime number in the series and find the sum of prime number in the series.

1. import java.util.Scanner;

class Test{

public static void main(String ar[])
{

Scanner sc=new Scanner(System.in);
int x,n1=1,n2=1,n3=0,loop=1,pcount=0,psum=0,count,i;
System.out.println("Enter Number to print Fibonacci Series");
x=sc.nextInt();
while (loop <= x)
{
n1=n2;
n2=n3;
n3=n1+n2;
System.out.printf(n3+",");
count=0;
i=1;

while(i<=n3)
{
if(n3%i==0){count++;}
i++;
}

if(count==2){pcount++;psum+=n3;}

loop++;

}

System.out.println();
System.out.println("No of Prime Numbers = "+pcount);
System.out.println("Sum of Prime Numbers = "+psum);
}
}

2. import java.util.Scanner;

class Test
{

public static void main(String ar[])
{
int num3,num1,num2,terms,i=0,sum=0,j,flag=0,tnum1,tnum2,count=0;
System.out.print("Enter number of terms: ");
Scanner o= new Scanner(System.in);
terms=o.nextInt();
System.out.print("Enter the first number: ");
num1=o.nextInt();
System.out.print("Enter the second number: ");
num2=o.nextInt();
System.out.print("The series is: "+num1+","+num2);
tnum1=num1;
tnum2=num2;
while(i<terms-2)
{
num3=tnum1+tnum2;
System.out.print(","+num3);
tnum1=tnum2;
tnum2=num3;
i++;
}
i=0;
System.out.print("\n The prime numbers are:");
j=2;
while(j<num1)
{
if(num1%j==0)
flag=1;
j++;
}
if(flag!=1)
{
count++;
sum+=num1;
System.out.print(num1+",");
}
flag=0;
j=2;
while(j<num2)
{
if(num2%j==0)
flag=1;
j++;
}
if(flag!=1)
{
count++;
sum+=num2;
System.out.print(num2+",");
}
flag=0;
while(i<terms-2)
{
num3=num1+num2;
num1=num2;
num2=num3;
j=2;
while(j<num3)
{
if(num3%j==0)
flag=1;
j++;
}
if(flag!=1)
{
sum+=num3;
count++;
System.out.print(num3+",");
}
i++;
}
System.out.println("\nThe count of prime numbers is: "+count);
System.out.print("\nThe sum of prime numbers are: "+sum);
}
}

3. //Fibonacci
import java.util.Scanner;
class FibonacciSeries
{
public static void main(String ar[])
{
int a=1,b=1,i=1,c=1,Number,Number1,Number2=0,Total=0,t;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the Number");
Number=scan.nextInt();
while(Number>=i){
a=b;
b=c;
c=a+b;
System.out.print(a+" ");
i++;
t=0;
Number1=1;
while(Number1<=a)
{
if(a%Number1==0)
{
t++;
}
Number1++;

}
if(t==2)
{
Total=Total+a;
Number2++;
}
}
System.out.println("Number of prime number in fibonacci series="+Number2);
System.out.println("Sum of prime number in fibonacci series="+Total);
}
}