Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Fill 2-D Array with All Prime Numbers

Tags: number main array
 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     {
       for(j=0;j        {
          
          x=prime(x) ;
       arr[i][j]=x;
        }
    }
        System.out.print("\nThe matrix with all prime numbers \n ");
       
        for(i=0;i     {
       for(j=0;j        {
          
         System.out.print(arr[i][j]+ " ");
      
        }
        System.out.println();
    }
}
int prime(int x)
{
   
    int i;
    x++;
    while(true)
    {
        for(i=2;i         {
             if(x%i==0)
             break;
        }
        if(i         x++;
        else
        break;
    }
    return x;
}
       
        
        public static void main(String[] args)
 {
Main ob=new Main();
ob.take();
}
}


This post first appeared on Tutorial Site On Computer Programming Languages F, please read the originial post: here

Share the post

Fill 2-D Array with All Prime Numbers

×

Subscribe to Tutorial Site On Computer Programming Languages F

Get updates delivered right to your inbox!

Thank you for your subscription

×