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
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
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.
The % operator is useful for both parts of the problem as laserlight has outlined it . . . .
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 :) :)
Well, that's one way to overly complicate something simple.
Like I said, the % operator is useful in two cases here. You can use it to check if a number is divisible by six.
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.Code:if(n % 6 == 0) { /* it's divisible by 6 */ }
Thanks! You helped me in the first part :D: D
Yeah, because I gave you the exact code required. :rolleyes: :)
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);
}
}
If x is a number which is divisible by 6, then doing x = x + 6 also results in a number divisible by 6.
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);
}
broli86, read our homework policy.
Ok , I appologize.
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 . . . .
(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.)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;
}
I think we're skipping the most obvious solution. Salem hinted at it.