In this program a two dimensional array will be filled up with only prime numbers and the numbers will be generated using a function.
public class Main
{
int arr[][]=new int[4][3];
int i,j,x;
void take()
{
x=0;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
x=prime(x) ;
arr[i][j]=x;
}
}
System.out.print("\nThe matrix with all prime numbers \n ");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
}
int prime(int x)
{
int i;
x++;
while(true)
{
for(i=2;i<x;i++)
{
if(x%i==0)
break;
}
if(i<x)
x++;
else
break;
}
return x;
}
public static void main(String[] args)
{
int arr[][]=new int[4][3];
int i,j,x;
void take()
{
x=0;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
x=prime(x) ;
arr[i][j]=x;
}
}
System.out.print("\nThe matrix with all prime numbers \n ");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
System.out.print(arr[i][j]+ " ");
}
System.out.println();
}
}
int prime(int x)
{
int i;
x++;
while(true)
{
for(i=2;i<x;i++)
{
if(x%i==0)
break;
}
if(i<x)
x++;
else
break;
}
return x;
}
public static void main(String[] args)
{
Main ob=new Main();
ob.take();
}
}
Main ob=new Main();
ob.take();
}
}
No comments:
Post a Comment