Thread: Simple C program displaying all numbers divisible by 6

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, indeed. I didn't even comprehend his post, I was so indignantly "fixing" broli86's solution.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    I am now confused :S

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am now confused
    Take a look at Salem's point of information:
    If x is a number which is divisible by 6, then doing x = x + 6 also results in a number divisible by 6.
    Use this idea to display all the numbers divisible by 6 in the range of 1 to 40.

    When you are done, tackle the rows of 3 problem. If you still cannot solve that part, then come back here with your solution for the first part and we'll give you suggestions on how to solve the second part.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Thank You!

  5. #20
    Registered User
    Join Date
    Jul 2008
    Posts
    48

    Question

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x,j=0;
    
    	for (x = 1; x <= 40; x++)
    	{
    		if (j < 3)
    		{
    			if (x % 3 == 0)
    			{
    				printf("\t%i\t", x);
    				j++;
    
    			}
    		}
    		else
    		{       if ( j == 3)
    			{
    				j = 0;
    				printf("\n");
    			}
    		}
    	}
    
    	return (0);
    }

    it works perfectly well..but,what if i need the user to enter the upper limit and the lower limit how can I do this witha do while loop....or a for loop..is it hard?? I mean instead of 1 to 40 it would be the user's chouice!!
    Any suggestions???

    thanks in advance guys!!

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    it works perfectly well..but,what if i need the user to enter the upper limit and the lower limit how can I do this witha do while loop....or a for loop..is it hard?? I mean instead of 1 to 40 it would be the user's chouice!!
    Not too difficult. Read the input from the user into variables and substitute the variables for the numbers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Emmm..That what I have done..I am onkly confused in the for loop condition..
    OI dunno what to r write between ()

    for(???????????)

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If x is a number which is divisible by 6, then doing x = x + 6 also results in a number divisible by 6.
    If you go through with this plan, one isn't divisible by six, so it would be a bad idea to start there. It's a bad idea to start there anyway, but you'll be off by one for the duration of the program.

    >> I am onkly confused in the for loop condition.

    Don't be confused, just make an effort. You'll be counting (possibly by six) starting from lowerbound to upperbound.

  9. #24
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    emm..Makes sense but,still doesn't put me on the way
    thanks for trying anyways

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why not start at 6? That is, by definition, the lowest number divisible by 6.

    [edit] I'm telling you, that code that broli86 had is no good! If I change the condition if(x &#37; 3 == 0) to if(1) -- presumably printing every number -- this is the output I get:
    Code:
            1               2               3
            5               6               7
            9               10              11
            13              14              15
            17              18              19
            21              22              23
            25              26              27
            29              30              31
            33              34              35
            37              38              39
    In other words, the loop is completely ignoring every fourth number. It's skipping them over and not even considering them. If one of those numbers happened to be divisible by 6, it wouldn't be printed.

    What's so bad about my code? This
    Code:
    if(++count % 3 == 0) putchar('\n');
    is the same as this, BTW.
    Code:
    count ++;
    if(count % 3 == 0) {
        printf("\n");
    }
    Even better, what's so hard about Salem's suggestion? . . . . [/edit]

    [edit=2] Hint:
    Code:
    for(...; ...; x += 6)
    [/edit]
    Last edited by dwks; 07-05-2008 at 04:09 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #26
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Thanks!! its not too hard..but,I am still a beginner

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Builder C++ large files support for simple C program
    By Murfury in forum C Programming
    Replies: 3
    Last Post: 11-23-2007, 03:47 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM