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; Hey!! Was just wondering if its possible to do a simple C program displays all the numbers divisible by 6 ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    48

    Smile Simple C program displaying all numbers divisible by 6

    Hey!!
    Was just wondering if its possible to do a simple C program displays all the numbers divisible by 6 in the range of 1 to 40 in rows of 3 columns...its kinda complicated to me..can anybody help me with a hint?? or explian??

    thankies

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    First, write a program that finds and "displays all the numbers divisible by 6 in the range of 1 to 40". Then think about the displaying in rows of 3 columns problem.
    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

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The % operator is useful for both parts of the problem as laserlight has outlined it . . . .
    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.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Thanks for who tried to help..
    I did this so far...

    Code:
    #include <stdio.h>
    void main()
    {
    int x=0;
    for(x=3;x<10; x+=3)
    printf("&#37;i\t" ,x);
    printf("\n") ;
    for(x=12;x<19; x+=3)
    printf("%i\t" ,x);
    printf("\n") ;
    for(x=21;x<28; x+=3)
    printf("%i\t" ,x);
    printf("\n") ;
    for(x=30;x<37; x+=3)
    printf("%i\t" ,x);
    printf("\n") ;
    for(x=39;x<40; x+=3)
    printf("%i", x);
    }

    But,I need the program to calculate it..can any body figure my mistake which I on't get....
    thankies again

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well, that's one way to overly complicate something simple.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Like I said, the &#37; operator is useful in two cases here. You can use it to check if a number is divisible by six.
    Code:
    if(n % 6 == 0) { /* it's divisible by 6 */ }
    You can also use it to space out your columns. Think of it this way: when the number of numbers you have printed is a multiple of 3, print a newline.
    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.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Thanks! You helped me in the first part : D

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yeah, because I gave you the exact code required.
    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.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    I think I have done the code...but,with some goofs lol :P

    Code:
    #include <stdio.h>
    int main()
    {
    int x,count=0;
    for(count>=1 && count>=40;)
    {
    if(x &#37;3==0)
    printf("%i/n",x);
    }
    
    }

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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 dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;
    	int j;
    
    	j = 0;
    	for (i = 1; i <= 40; i++)
    	{
    		if (j < 3)
    		{
    			if (i % 6 == 0)
    			{
    				printf("\t%d\t", i);
    				j++;
    			}
    		}
    		else
    		{       if ( j == 3)
    			{
    				j = 0;
    				printf("\n");
    			}
    		}
    	}
    
    	return (0);
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    broli86, read our homework policy.
    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

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Ok , I appologize.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Your solution skips over numbers where j happens to be 3.

    And anyway, you're just overcomplicating as well. You can do it much more simply. Since we're giving away solutions . . . .
    Code:
    #include <stdio.h>
    
    int main() {
        int num, count = 0;
    
        for(num = 1; num <= 40; num ++) {
            if(num &#37; 6 == 0) {
                printf("%3d", num);
    
                if(++count % 3 == 0) putchar('\n');
            }
    
        }
    
        return 0;
    }
    (Most of the time I wouldn't give away something like this, but the other program works too and has been on the internet for a long time already -- I'm sure the OP has seen it.)
    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.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I think we're skipping the most obvious solution. Salem hinted at it.

Page 1 of 2 12 LastLast
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