Simple C program displaying all numbers divisible by 6

This is a discussion on Simple C program displaying all numbers divisible by 6 within the C Programming forums, part of the General Programming Boards category; Yes, indeed. I didn't even comprehend his post, I was so indignantly "fixing" broli86's solution....

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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
    21,941
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,941
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    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,048
    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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Replies: 1
    Last Post: 12-30-2007, 09: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, 02:47 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  5. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM

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