Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

JAVA PROGRAM


  • Please log in to reply
3 replies to this topic

#1 msrrahul

msrrahul

  • Members
  • 32 posts
  • OFFLINE
  •  
  • Local time:04:11 PM

Posted 29 August 2011 - 05:24 AM

import java.util.Scanner;
public class PrimeNumberFinding
{
	static int  rem;
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		int j,num=Integer.parseInt(sc.next());
		j=++num;
		printPrimeNo(j);		
	}
	static void printPrimeNo(int j)
	{
		for(int i=2;i<j;i++)
		{
			rem=j%i;
			if( rem==0)
			{
				++j;
			    printPrimeNo(j);
			}
		}
		//if(rem!=0)
			System.out.println(j);
	}
}

the above java program asks one integer number, and prints prime number next to entered input number..

after running this above program, when i give input as 7, then 11 is printed 8 times, whereas when i provide input as 8, then 11 is printed 4 times, can anyone please give me the explanation and details of the flow when 7, 8 are provided as input respectively?? I mean why is 11 printed 8 times when input is 7 and why is 11 printed 4 times when input is 8.

Thanks a lot for every help. !!

Edited by groovicus, 29 August 2011 - 07:09 AM.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:11 PM

Posted 29 August 2011 - 07:10 AM

Write out the flow of the program on a piece of paper, and I think it will become obvious.

#3 msrrahul

msrrahul
  • Topic Starter

  • Members
  • 32 posts
  • OFFLINE
  •  
  • Local time:04:11 PM

Posted 29 August 2011 - 11:37 PM

the problem is itself that i am unable to track down how stack is storing recursive calls? thats why i asked why is 11 printed "8 times" when input is 7 and why is same output i.e. 11 printed "4 times" when input is 8.

if iam not clear then plz let me know.. i cant solve my this doubt.

thanks

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:11 PM

Posted 30 August 2011 - 11:36 AM

If your input is 7, then j=8 when the printPrimeNo function is called.

If j=8, how many times will printPrimeNo be called in your for loop?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users