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

Printable View

- 07-02-2008DaniiChrisSimple 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-2008laserlight
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-2008dwks
The % operator is useful for both parts of the problem as laserlight has outlined it . . . .

- 07-02-2008DaniiChris
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("%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-2008MacGyver
Well, that's one way to overly complicate something simple.

- 07-02-2008dwks
Like I said, the % 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 */ }`

- 07-02-2008DaniiChris
Thanks! You helped me in the first part :D: D

- 07-02-2008dwks
Yeah, because I gave you the exact code required. :rolleyes: :)

- 07-02-2008DaniiChris
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 %3==0)

printf("%i/n",x);

}

}

- 07-02-2008Salem
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-2008broli86Code:
`#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-2008laserlight
broli86, read our homework policy.

- 07-03-2008broli86
Ok , I appologize.

- 07-04-2008dwks
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 % 6 == 0) {

printf("%3d", num);

if(++count % 3 == 0) putchar('\n');

}

}

return 0;

}

- 07-04-2008MacGyver
I think we're skipping the most obvious solution. Salem hinted at it.