Hi All,
I am new to C language and am suppose to write a program that gives me an even integer with cube greater than 40000 and also is divisible by 7.
This is what i have...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
float a,b,c,d,e;
a=pow(40000,1/3);
while(pow(40000,1/3)<=a){
	a=b;

if(b % 2 == 0)
b=c;
if(b % 7 == 0)
c=d;
printf("%f\n",b);
else
{
a=a++;
}
}
}

I dont know what i am doing.
Please help. I dont want a program i just want to know what am i doing wrong and what loops and logics can be used to write it??

Edited 6 Years Ago by WaltP: Added CODE Tags

Why would you need a loop.

Find the cube root of 40,000 pow(40000, 1.0/3.0) call it R
Find the first integer > R, I = (int)R + 1
Find the first integer >= I that is a multiple of 7, I += I % 7

BTW you have written pow(40000, 1/3) , 1/3 is integer arithmetic and equals 0 anything raised to the power 0 is 1 (except may be 0) so pow(40000, 1/3) = 1. That is certainly causing your loop logic to be wrong.

But what if i have to start from 1 and keep going until a number is found?
Then i have to use a loop??
and secondly is this correct for the even number test
if(4 % 2 == o)
{
true
}

Since the number must be both even and a multiple of 7,
it must therefore be a multiple of 14,

I = (I+13)/14; /* integer math rounds down */
  I = I * 14;

I put the statements on separate lines, for convenience in the debugger.

Sorry I missed the even bit, still no loop required. Even means divisible by 2 divisible by 2 and divisible by 7 means divisible by (2 * 7) = 14

Replace the step

Find the first integer >= I that is a multiple of 7, I += I % 7

with

Find the first integer >= I that is a multiple of 14, I += I % 14

I think the equation I += I % 14 is wrong. For example if I == 15, the statement will add 1, which does not make it a multiple of 14.

Comments
Yes good point

I'd suggest using a for loop, and incrementing the number to be tested by 14, with each loop.

Put all your other tests that you need, right inside the for loop.

for(i = 0; i <=R; i+= 14) {
  //etc

}

I think the equation I += I % 14 is wrong. For example if I == 15, the statement will add 1, which does not make it a multiple of 14.

Your completely right it should of course be

I += 14 - (I % 14)

Which is the equivilent of your statement

I don't think that statement is equivalent, take for example if I =14,
the equation will add 14 and subtract 0, giving 28. Is that what you intended? Think about values of 13,14,15,27,28

That'll teach me for quickly bashing out "a correction" when I was supposed to be concentrating on my work :D

Not only are you correct again but now I think about it when I have needed to do a similar think in my actual work I thing I have done it the way you suggest.

Edited 6 Years Ago by Banfa: n/a

I'm thinking of:

First, find your lowN's lowest possible value lowN ^3 > 40000. Which may not be an equal number, or divisible by 7, either one.

then (I thought you might be able to increment this by 14, but that was incorrect. So scrap that idea.) ;)

if(lowN's lowest possible N is odd, then add one to it, so it's even)
then we're ready for a loop

haveIt assigned to zero //prime the for loop

for(i equals lowN; haveIt less than 1; i +=2) {
if(i % 7 is zero )
haveIt assigned value of 1;
}

Should do it. ;)

You have to break out of the for loop, when i % 7 == 0, or the for loop will increment i another 2, and give you a wrong number.

I wonder why they have that feature in a for loop? Not a desirable one, imo.

This article has been dead for over six months. Start a new discussion instead.