Create a package named Series having three different classes to print series:
a. Prime numbers b. Fibonacci series c. Squares of numbers
Write a program to generate ‘n’ terms of the above series.
Prime.java
package Series;
public class Prime
{
static private boolean prime(int no)
{
for(int i=2;i<=no/2;i++)
{
if(no%i==0)
return false;
}
return true;
}
static public void display(int n)
{
int i=0;
System.out.println("Prime numbers are:");
for(int no=2;i<n;no++)
{
if(prime(no))
{
System.out.println(no+" ");
i++;
}
}
}
}
fib.java
package Series;
public class fib
{
public static void display(int n)
{
int a=0,b=1,c;
System.out.println("Fibonacci Series :");
System.out.println(a);
System.out.println(b);
for(int i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
System.out.println(c);
}
}
}
Square.java
package Series;
public class Square
{
public static void display(int n)
{ System.out.println("Squares Of Numbers:");
for(int i=1;i<=n;i++)
{
int x=i*i;
System.out.println(" "+x);
}
}
}
series.java
import Series.Prime;
import Series.Square;
import Series.fib;
import java.util.Scanner;
class series
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("\n Enter the number\n");
int n=s.nextInt();
Prime.display(n);
Square.display(n);
fib.display(n);
}
}
0 Comments
If anyone has Doubts or suggestions please let me know