Thread: check for y condition

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    check for y condition

    let say i have two loops and i want to check if the varibles from one loop can evenly divided like this
    Code:
    for (y=1;y<5;y++){
         for (x=0;x<1000;x++){
                if (x%y==0){ printf("this one: %d \n",x)}}}
    how can i make the(x%y==0) condition to be checked for every y condtion ranging from
    1 to 5
    i mean how can it make it find every number that can be eveny divided to all number ranging from 1 to 5
    thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Presumably by putting a semicolon ";" after your printf statement. Once you have that syntactically correct, your if statement will check 4000 different cases (y 1,2,3,4; x from 0 to 999).

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    for (y=1;y<6;y++){
      for (x=0;x<1000;x++){
        if (x%y==0){ 
          printf("this one: %d \n",x);
       }
      }
    }
    If i understand you question correctly this should do it

    edit:
    as stated above, this will not check x=1000
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Maybe you want something along these lines:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x,y;
    	for (x=0;x<1000;x++)
    	{
    		int flag = 0;
    		for (y=1;y<=5;y++)
    		{
    			if (x%y)
    			{
    				flag = 1; /*found y that is not dividable by */
    				break;
    			}
    		}
    		if(flag == 0)
    		{
    			/* x is dividable by all y */
    			printf("this one: %d \n",x);
    		}
    	}
    	return 0;
    }
    Last edited by vart; 03-28-2009 at 02:06 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. pLease check my programming~~
    By iedchan in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2007, 10:23 AM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. get and check input as while condition
    By Marksman in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2006, 09:22 PM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM