Summation of prime Fibonacci numbers under 100

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static int prime(int num)
   {
	   for(int i=2;i<=(num/2);i++)
	   {
		   if(num%i==0)
			   return 0;
	   }
	   return 1;
   }
	
	public static void main(String[] args) {
		int n1=1, n2=2;
		System.out.println(n1);
		System.out.println(n2);
		int x,total=2;
		x=n2;
		for(;;) 
		{   
		       x=n1+n2;
			   if(x<100)
				{
					System.out.println(x);
					
					if(prime(x)==1)
					{
						total+=x;
					}
					
				}
				else
					break;
			
			
			n1=n2;
			n2=x;
			
		}
		System.out.println("The Result is="+total);
	}
}

Output:

1
2
3
5
8
13
21
34
55
89
The Result is=112

Leave a Comment

Your email address will not be published. Required fields are marked *