Problem on loops. Where have I gone wrong ?

This is a discussion on Problem on loops. Where have I gone wrong ? within the C Programming forums, part of the General Programming Boards category; Hi... I'm new to both C language and this forum. I have a problem with the following program. Code: #include<stdio.h> ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    15

    Problem on loops. Where have I gone wrong ?

    Hi...
    I'm new to both C language and this forum.
    I have a problem with the following program.

    Code:
    #include<stdio.h>
    main()
    {
    	int con=0,con_limit,beta,alpha,num,reva,i;
    	printf("\nEnter a number : ");
    	scanf("%d",&con_limit);
    	printf("\nThe required number(s) are :\n");
    	for(num=2;num<=1000000;num++)
    	{
    	reva=num;	
    		for(alpha=2;alpha<=(num-1);alpha++)
    		{
    			num=reva;
    			for(i=1;i=1;)
    			{
    				if(num%alpha==0)
    				{
    				num=num/alpha;
    				continue;
    				}
    				if(num==1)
    				{
    				printf(" %d ",reva);
    				con=con+1;
    				break;
    				}
    				if((num%alpha)!=0)
    				break;
    			}
    		}
    		num=reva;
    		if(con==con_limit)
    		break;
    	}
    }
    Now if I enter the value of con_limit as 8 I get the input as 4 8 9 16 25 27 32 49...
    Clearly all the numbers printed are good and are what I need. But what happened to the number 36 ???Why isn't it printed even though it satisfies the conditions ?

    I'd be happy if anybody could help me with this.

    Thanks in advance !!

    P.S. Great forum !!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    For 36, as far as I can tell, you get
    36->18->9 (for alpha=2)
    36->12->4 (for alpha=3)
    loop stop (since we are checking against num, and num is now 4, not 36)

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Now that I see it I feel like shooting myself..such a silly yet fatal mistake..

    thanks a lot tabstop..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with loops and Stringstreams
    By mintsmike in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2009, 11:08 AM
  2. C: confusing problem with 'for' loops.
    By thealmightyone in forum C Programming
    Replies: 13
    Last Post: 02-05-2009, 07:23 AM
  3. for loop problem What's Wrong???
    By benjamin923 in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2007, 12:36 AM
  4. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  5. strange problem, not sure what is wrong
    By Shadow12345 in forum C++ Programming
    Replies: 7
    Last Post: 07-23-2002, 11:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21