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
    {
        SimpleReader in = new SimpleReader();

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

        int num = in.readInt(); // number is read in.

                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 3 Years Ago by mike_2000_17: Fixed formatting

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

int primenumber[] = new int[max capacity of your array];

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

Read about ArrayList

best regards,
sukatoa

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 5 Years Ago by NormR1: n/a

@TheDumbestOne: Don't hijack old threads to ask vague questions. If you have something to inquire about, start a new thread and state your question more clearly.

Closing this old thread.

This article has been dead for over six months. Start a new discussion instead.