Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Primes (JAVA PROGRAM PDF

JAVA PROGRAM TO FILL A 2-D ARRAY OF GIVEN ROWS AND COLUMNS BY PRIME NUMBERS COLUMN WISE.

Uploaded by

AMRENDRA SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Primes (JAVA PROGRAM PDF

JAVA PROGRAM TO FILL A 2-D ARRAY OF GIVEN ROWS AND COLUMNS BY PRIME NUMBERS COLUMN WISE.

Uploaded by

AMRENDRA SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Class Primes

/**
*
*
*
*
*
*
*
*
*/

1/2

QUESTION
Design a class Primes to fill an array of order [r*c]
where the maximum value of both r and c is 20,with the
first [r*c] prime number column wise.
The details of class are as follows :

package Practice_Question.class_object_constructor;
import java.io.*;
public class Primes
{
int arr[][]=new int[100][100];
int r;
int c;
DataInputStream d=new DataInputStream(System.in);
//Default constructor
public Primes(int x,int y)
{
r=x;
c=y;
}
//method to check for a prime number
int isprime(int q)
{
int c=0;//counter
for(int i=2;i<q;i++)
{
if(q%i==0)
{
return 0;
//returning 0 if factor found
}
}
return 1;
//returning 1 if number is prime i.e. no factor is found
}
//method to fill the array with prime numbers
void fill()
{
int n=2;
for(int i=0;i<r;i++)
Jan 10, 2015 10:01:36 PM

Class Primes (continued)

2/2

{
for(int j=0;j<c;j++)
{
do
{
n++;
}
while((isprime(n))!=1);
/**
* Finding the next prime number and storing
* it in the array column-wise
*/
arr[j][i]=n;
}
}
}
//method to display the matrix
void display()
{
for(int i=0;i<c;i++)
{
for(int j=0;j<r;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
void main()throws IOException
{
int x;
int y;
System.out.println("ENTER NUMBER OF ROWS");
x=Integer.parseInt(d.readLine());
System.out.println("ENTER NUMBER OF COLUMNS");
y=Integer.parseInt(d.readLine());
Primes ob1=new Primes(x,y);//creating object
/**
* calling methods in main() to execute program
*/
ob1.fill();
ob1.display();
}//end of main
}//end of class

Jan 10, 2015 10:01:36 PM

primes1.main();
ENTER NUMBER OF ROWS
5
ENTER NUMBER OF COLUMNS
6
3

19

43

71

101

23

47

73

103

29

53

79

107

11

31

59

83

109

13

37

61

89

113

17

41

67

97

127

You might also like