# Simple C program displaying all numbers divisible by 6

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-02-2008
DaniiChris
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
• 07-02-2008
laserlight
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.
• 07-02-2008
dwks
The &#37; operator is useful for both parts of the problem as laserlight has outlined it . . . .
• 07-02-2008
DaniiChris
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 :) :)
• 07-02-2008
MacGyver
Well, that's one way to overly complicate something simple.
• 07-02-2008
dwks
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.
• 07-02-2008
DaniiChris
Thanks! You helped me in the first part :D: D
• 07-02-2008
dwks
Yeah, because I gave you the exact code required. :rolleyes: :)
• 07-02-2008
DaniiChris
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);
}

}

• 07-02-2008
Salem
If x is a number which is divisible by 6, then doing x = x + 6 also results in a number divisible by 6.
• 07-03-2008
broli86
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);
}

• 07-03-2008
laserlight
• 07-03-2008
broli86
Ok , I appologize.
• 07-04-2008
dwks
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.)
• 07-04-2008
MacGyver
I think we're skipping the most obvious solution. Salem hinted at it.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last