Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:29:11+00:00 2026-05-30T03:29:11+00:00

i want to print first 100 prime numbers. so, i created an array of

  • 0

i want to print first 100 prime numbers. so, i created an array of int 100. I added first prime, bag[0] = 2, then for the following numbers, I tried to write an algorithm. It will start from 3 and goes on until array is full. Every number is decided to be if it is prime by whether it is divisible by the previous elements in array and if it is prime then it will be added to array.

here is my code:

public class Trial02 
{   
    public static void main( String[] args) 
    {       
        int[] bag = new int[100];
        bag[0] = 2; //first element of prime array
        int valid = 1;
        int i;
        boolean result = true;
        String str = "";

        //starting from 3 it checks if a number is prime until array is full
        for( i=3; valid<bag.length; i++)
        {
            //it checks if previous primes in array are divisible by current number until coming to current number
            for(int k=0; k<valid; k++)
            {
                if( i % bag[k] == 0)
                    result = false;
            }

            if( result == true) //if it is prime it is added to array
            {
                bag[valid] = i;
                valid ++;
            }
        }

        //printing results
        for(int m=0; m < bag.length; m++)
            str = str + bag[m] + " ";
        System.out.println("zaa xd");
        System.out.println(str);
    }

}

but it don’t give any output, just a blank. I couldn’t find where my mistake is.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:29:12+00:00Added an answer on May 30, 2026 at 3:29 am

    Your logic for determining the first 100 prime numbers is incorrect. And number of logical errors are present as indicated by others. I have re-written your code but not tested. I guess it will work:

    public class Trial02 
    {   
        public static void main( String[] args) 
        {       
        int[] bag = new int[100];
        bag[0] = 2; //first element of prime array
        int valid = 1;
        int i;
        boolean isPrime = true;
        String str = "";
    
        //starting from 3 it checks if a number is prime until array is full
        for( i=3; valid<bag.length; i++)
        {
            isPrime = true;
            for (int k = 2; k < i; k++)
            {
                if (i % k == 0)
                {
                    isPrime = false;
                    break;
                }
            }
    
            if (isPrime == true)
            {
                bag[valid++] = i;
            }
        }
    
        //printing results
        for(i=0; i < bag.length; i++)
            str = str + bag[i] + " ";
        System.out.println("zaa xd");
        System.out.println(str);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to print the first 10000 prime numbers. Can anyone give me the
I want to print all the prime numbers between two numbers. This is my
Let's say I have this array int[] anArray = { 100, 200, 300}; Now,
I want to basically make a simple picture upload, then change the first picture
I need to print out a second array, that takes the numbers from the
First, in order to provide full disclosure, I want to point out that this
I Want Print DataBase Schema in Microsoft Sql Server Compact Edition. Maybe Exist Some
I want to print the full length of a C-string in GDB. By default
I want to print styled html pages with their images from a script. Can
I want to print from a servlet. I believe I have the correct syntax.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.