Prime Numbers using a for loop

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 81647 Views - Last Post: 21 February 2010 - 05:32 PM Rate Topic: -----

#1 Sergiu  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 01-February 05

Prime Numbers using a for loop

Posted 11 June 2006 - 05:27 PM

Hi, guys, I have this assignment that I'm working on and I really need some help. Ok the only instructions that I received was to display all the prime numbers between 1 and 100 using a for loop. The teacher said that I have to use the modulo (%) operator and two for loops, one that goes throught and counts from 1 to 100 and another that takes all of those numbers and "modulo" them against 1 thorough 100 and sees which one has only 2 divisors. I don't know if any of this makes sense, but I only learned the basics so far and those are the only two things that I can use. And no, I can't just display the prime numbers without the loop, I already tried that and it didn't work sadly. If anyone could kindly help me I would greatly appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Prime Numbers using a for loop

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Prime Numbers using a for loop

Posted 11 June 2006 - 05:37 PM

Hi,

The site has a policy by which we prefer to see some good faith effort on the part of the user before providing source code for academic assignments. If you could post the code you have written thus far in an attempt to complete the assignment, our members would be more than happy to provide some guidance.

Your instructions do make sense...the modulus operator used in a loop will indeed allow you to identify prime numbers.
Was This Post Helpful? 0
  • +
  • -

#3 Sergiu  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 01-February 05

Re: Prime Numbers using a for loop

Posted 12 June 2006 - 09:50 AM

Ok, to be honest I'm not even really sure how the logic of this whole program is supposed to be. I don't know how to put that if statement in there to decide whether or not there are 2 divisors only. This is what I molded up so far. It's giving me an infinte loop for some reason. Again thanks for your help. You are helping a student pass a Comp.Sci. course.

class Prime
{
 public static void main(String[] args)
 {
 int num1 = 0;
 int num2 = 0;
 int num3;

  for (num1 = 1; num1 < 101; num1++)
	System.out.println(num1);

  for (num2 = 1; num2 < 101; num1++)
	System.out.println(num2);

  num3 = num2 % num1;

  if (num3 == 0)
	System.out.println("The prime numbers are " + num1);
  else
	System.out.println("The prime numbers are " + (num1 += 1));
  }
} 



Was This Post Helpful? 1

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Prime Numbers using a for loop

Posted 12 June 2006 - 09:58 AM

I can't check all the code right now, but you are getting an infinite loop becasue your second for loop uses num2 as a control variable, but you increment num1...the break condition is not met.
Was This Post Helpful? 0
  • +
  • -

#5 Sergiu  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 01-February 05

Re: Prime Numbers using a for loop

Posted 12 June 2006 - 05:14 PM

Ok, that makes sense, but what do you think I should do. Is the "if" statement needed there or do I need to get rid of the "num1 +=1". I just don't seem to understand what to do with the numbers. How do I figure out that only those specific "prime" numbers are the ones that have only 2 divisors?
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Prime Numbers using a for loop

Posted 12 June 2006 - 06:38 PM

You are getting close...you need to run a loop through number 1-100, which you are doing. For each of those numbers, you need to see if they have any factors besides 1 and the number itself. If they do not, they are prime.
for (num1 = 1; num1 <= 100; num1++)


There is your first loop, although you need to use braces around the code blocks to properly group things for the compiler.

Now you need to check the factors of each number in that loop:
for (num2 = 1; num2 <= num1; num2++)


That will run through each number from one to the number in question. I'd suggest having a temporary variable that you set to 0 each time you completely factor a number. You need to check to see
  • If the number is a factor
  • If the number is the actual specified number
  • If the number is one.
If a number is a factor (num1%num2==0), the number is NOT the number in question (num2!=num1) and the number is not 1 (num2!=1), then you do not have a prime number. You can set a variable flag to indicate that you've reached that condition.

Give that a try, there's not much more to say without actually writing the code. :)

Let us know how you make out, and post what you've done...we can provide some more guidance.
Was This Post Helpful? 0
  • +
  • -

#7 Sergiu  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 01-February 05

Re: Prime Numbers using a for loop

Posted 12 June 2006 - 07:14 PM

Amadeus thanks so much. Ok, I did everything as you said, but sadly it still doesn't work. Is there supposed to be something after the for statements? I just put declared and initialized num3 from the beginning to 0 so it will do something. Is that what I'm doing wrong.
Also the if statement, I'm thinking I might have messed up there somewhere because the only thing that I get when I run it is 102. For the part when the number isn't a prime number, do I have to display something, again the only thing that I'm displaying is the variable with 0. Can you just point out what I'm doing wrong? Again thank you.This is the corrected code.
 public static void main(String[] args)
 {
 int num1 = 0;
 int num2 = 0;
 int num3 = 0;

  for (num1 = 1; num1 <= 100; num1++){
  num3 = 0;
  }

  for (num2 = 1; num2 <= num1; num2++){
  num3 = 0;
  }


  if ((num1%num2) == 0 & num2!=num1 & num2!=1)
	System.out.println(num3);
  else
	System.out.println("The prime numbers are " + num2);
  }


Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Prime Numbers using a for loop

Posted 12 June 2006 - 07:44 PM

You've done quite well...getting very close...at this point, it's really just a matter of logical execution.

Your for loops are not actually doing anything except setting the value of a third variable...the second needs to be nested in the first, and the comparative statement inside as well.

See a base example below:
class Test{

	public static void main(String[] args) {
		int num1,num2;
		int flag;
		for(num1=1;num1<=100;num1++)
		{
		   flag = 0;
		   for(num2=1;num2<=num1;num2++)
		   {
			  if(((num1%num2)==0) & num2!=1 & num2!=num1)
			  {
				 flag=1;
			  }
		   }
		   if(flag==0)
		   {
			  System.out.println(num1 + " is a prime number\n");  
		   }
		 }
	}
}


This is not an optimized version by any means, but you can use it to look at yours...notice the similarities and differences.

One thing that may help is to go through the program, and verbally say what is going on at each line...an old trick, but one that can demonstrate the flow.

Good job on yours! Keep it up. :)
Was This Post Helpful? 0
  • +
  • -

#9 Sergiu  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 01-February 05

Re: Prime Numbers using a for loop

Posted 13 June 2006 - 12:02 PM

Thanks a lot Amadeus, that really helped. :D Everything worked perfectly and I actually understand it all. :)
Was This Post Helpful? 0
  • +
  • -

#10 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 185
  • Joined: 28-December 05

Re: Prime Numbers using a for loop

Posted 17 June 2006 - 12:05 PM

A very slightly improved code to do this would be as below. Notice the changes in the code by reading the comments.

class PrimeNum {
	//Program to generate prime numbers from 1 to 100
	public static void main(String args[]){
		int num1,num2;
		int flag=0; //Checks to see if the number in question is prime or not
		
		for(num1=1;num1<=100;num1++)
			{
				flag=0;
				for(num2=1;num2<num1;num2++) //Remove the num2<=num1 and let it be as shown
					{
						if(((num1%num2)==0) & num2!=1) //Thus you can remove the third condition
						{
							flag=1; //Flag set to indicate that the number is prime
						}
					}
					if(flag==0)
					{
						System.out.println(num1+"\n");
					}
			}
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Prime Numbers using a for loop

Posted 17 June 2006 - 02:10 PM

Good points, rahulbatra, good points indeed. For the record, my intent was to demonstrate the determination of required conditions for a prime number, not an optmized prime number application, as noted.
Was This Post Helpful? 0
  • +
  • -

#12 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 185
  • Joined: 28-December 05

Re: Prime Numbers using a for loop

Posted 17 June 2006 - 03:02 PM

Yes Amadeus, I could see your intent on making the code work using the same conditions that Sergiu had put, and its a great way to show how the logic of the program is built. My intent was to show a slightly more optimised way of doing it (though still not the best optimisation), while stressing upon code comments which are very useful.

Well either way, I'm myself a beginner and I learnt a lot from reading this whole topic. Just my 2 cents added to the kitty. :)
Was This Post Helpful? 0
  • +
  • -

#13 felixculpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-September 06

Re: Prime Numbers using a for loop

Posted 21 September 2006 - 04:07 AM

I don't entirely understand this code. I get all of it apart from the flag variable, which is changed to 1 numerous times within the nest for loop:

for ( ... ) {
...

flag = 1; //Flag set to indicate that the number is prime

...

}



Because the flag variable is set to 1 at least once (pretty much) between num1 and num2, and then the if statement outside the nested for loop, and within the first for loop (immediately after above code):

if (flag == 0) {
System.out.println(num1 + "\n");
}



The bit I don't understand is, how does it "know" that the flag variable is for each number, and not, instead just never print the number, since flag will be set to 1 at some point between num1 and num2?

I hope I made myself clear and that someone can answer my question.
Was This Post Helpful? 0
  • +
  • -

#14 bettyboo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-March 08

Re: Prime Numbers using a for loop

Posted 11 March 2008 - 06:42 AM

just want to thanks, this thead has helped with my assignment, cheers
Was This Post Helpful? 0
  • +
  • -

#15 jrooneo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-February 10

Re: Prime Numbers using a for loop

Posted 04 February 2010 - 03:45 PM

This too helped with my assignment but I will say that num1 should be 2 to begin with
for(num1=2;num1<=100;num1++)

This is to stop it claiming that one is a prime. In case you are curious as to why I have a good link.
Why one is not a prime.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2