0% found this document useful (0 votes)
13 views3 pages

Filling A Matrix With Prime No

ISC question of computer science paper. the question is Write a program to create m*n array where the value is entered by the user. Fill the array with prime numbers.

Uploaded by

krishna beriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Filling A Matrix With Prime No

ISC question of computer science paper. the question is Write a program to create m*n array where the value is entered by the user. Fill the array with prime numbers.

Uploaded by

krishna beriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a program to create m*n array where the value is entered by the user.

Fill
the array with prime numbers.
For example

import java.util.Scanner;
public class prime_matrix

// declaring class

{
Scanner scan = new Scanner(System.in); // input object
public void main ()// main function
{
System.out.println (" enter the value for m");// input
int m = scan.nextInt();
System.out.println (" enter the value for n");
int n = scan.nextInt();
int a[] []= new int [m][n];
int b=1;int c=0;

for (int i=0;i<m;i++)


{
for (int j=0;j<n;j++)

int d = prime(c);
a[i][j]=d;// inserting prime number to array
c=d;
}
}
System.out.println (" output matrix");
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)

{
System.out.print (a[i][j]+" ");

}
System.out.println ();
}
}

public int prime(int c)// prime function


{
for(int i=c+1;;i++)
{
int e=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{

++e;
}

}
if(e==2)
{
return i; // returning prime number
}
}
}
}// end of class

You might also like