Hi,
Cosidering the following code ....i want to store the output values into an array..
can anyone pliz help.........

``````import java.io.*;
import abr.srw.*;

public class Prime1
{
// This method tests whether a given number is prime or not.

public static boolean isPrime ( int num )
{
boolean prime = true;

int limit = (int) Math.sqrt ( num ); // limit is set to the sqrt of the number

for ( int i = 2; i <= limit; i++ ) // range is set from 2 to the limit
{
if ( num % i == 0 ) // num is checked if it is divisible by numbers equal to
{                   // or lower than its sqrt
prime = false;
break;
}
}

return prime;
}

//  ************** Main Program ***************************** //

public static void main ( String[] args ) throws Exception
{

System.out.print("Enter a number ");

for ( int i = 2; i <= num; i++ )
{

if ( isPrime ( i ) ) // all numbers from i=2<=num is tested with Prime tester
{
System.out.print(i);// prints out all prime numbers <= num :Question
//How can I store this values into an array?
}
``````

Edited by mike_2000_17: Fixed formatting

5
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by Ezzaral

You may initialize an array inside your main class together with your methods.

You can directly access every element of your array inside your main class.
(It would be nice you implement it with set method)

After printing a prime number, assign that value into your array then increment.

Try to have some experiments about it, then

best regards,
sukatoa

what if you don't know the maximum value of the array?

What do you mean by "maximum value"?
Do you mean the number of possible elements to be added to the array?
If you don't know how many elements are to be added, then use a class like ArrayList which has no limit.

Edited by NormR1: n/a