Thread: loop problem

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    loop problem

    The while loop in the program dosnt seem to execute ?

    Code:
    #include	"stdio.h";
    
    int main () 
    {
    int num , remainder , quo, cnt , sum; 
    cnt = num = remainder = quo = sum = 0;
    
    
    printf( "Please Enter number(only positive numbers): " );
    scanf ( "%d", &num );
    
    
    quo = num;
    
    
    if ( num < 0 )
    	printf ( "Error end of programm" );
    else 
    	{
    		while ( (cnt != 1 ) && (remainder != 0) )
    		{ 
    			quo = quo/10;
    			remainder = quo%10;
    			
    			if ( remainder == 0 ) 
    			{
    				sum = sum + quo;
    				printf ("%d\n", sum );
    				quo = sum;
    				remainder = 1;
    				cnt = 0;
    				sum = 0;
    			}
    			else 
    			{
    				sum = sum + remainder;
    			}
    		
    			cnt++;
    		}
    	}
    	
    return 0;
    }
    Thanks .. I cant find a reason ..why it wont execute ..

    I want the loop to keep executing when thill this happens ot the vairables

    cnt = 1 and remainder = 0
    Last edited by datainjector; 12-21-2009 at 11:04 AM.
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Since remainder==0 so the condition of while will become false and thus doesn't get executed.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    int num , remainder , quo, cnt , sum; 
    cnt = num = remainder = quo = sum = 0;
    
    //...
    		while ( (cnt != 1 ) && (remainder != 0) )
    Your assigning cnt and remainder both the value of 0, which isnt changed before the while loop. So cnt != 1 is true, but the second condition remainder != 0 is false, so the while loop isnt executed. I dont know what your doing so cant suggest what to do.

  4. #4
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    I want the loop to keep executing till

    cnt = 1 and remainder = 0

    I am trying to do this .. suppose the user enteres a number 765

    I will add up .. each digit .. 7 + 6 + 5 = 18
    then again .. 8 + 1 = 9

    till i reach a single digit .

    thanks
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    A do-while loop means that control enters the loop at least once. A while loop means that the loop isnt guaranteed to be executed at all. If you want it to execute at least once, then use a do-while loop, where the condition is checked at the end of the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  2. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  3. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  4. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM