According to entered number (b), please find the “a” numbers corresponds
equation b = a3 * a2.

My codes. Whats the mistake ? thanks for helping.

#include"stdio.h"
#include"conio.h"
#include"math.h"
int main(void)
{
int a,b = 0;
printf("Please enter a number");
scanf("%d",&b);
for(int a = 0;a<1000;a++)
{
     if(b==(a*a*a)-(a*a))
     {
      printf("%d",a);
     }
} 
getche();    
}

Hello madmark,
you have declared 'a' twice,one in the begining and other in the for loop. So the compiler will throw an error("multiple declaration error").
Try this...

for(a=0;a<1000;a++)
{  c=(a*a*a)-(a*a);
   if(b==c)
   { 
      printf("%d",a);
     }
}

Hi arbus,
Thanks for helping but it doesnt work. i tried your codes.

#include"stdio.h"
#include"conio.h"
#include"math.h"
int main(void)
{
int a,b = 0;
int c=0;
printf("Please enter a number : ");
scanf("%d",&b);

for(a=0;a<1000;a++)
{  
   c=(a*a*a)-(a*a);
   if(b==c)
   { 
      printf("%d",a);
     }
}
getch();
}

Hello,
whats your test data?
because it works for 4 , 222 etc,.

ty

#include"stdio.h"
#include"conio.h"
#include"math.h"
int main(void)
{
int a,b = 0;
int c=0;
printf("Please enter a number : ");
scanf("%d",&b);

for(a=0;a<1000;a++)
{  
   c=(a*a*a)-(a*a);
   if(b==c)
   { 
      printf("%d",a);
     }
}
getch();
}

For starters, get rid of that conio.h and getch() nonsense, and use angle brackets to surround standard header files, like this:

#include <stdio.h>

For another thing, if the user inputs "4", do you really need to check values of a all the way up to 1000?

And furthermore, if the user inputs "four", how do you tell the difference between that and zero?

Finally, you do realize that only a few numbers meet this requirement, right? [4, 18, 48, 100, 180, 294, 448, 648, 900] are the only positive integers below 1000 that can be expressed as the difference between the cube of a whole number and its square.

How does it not work?

3.According to entered number (b), please find the “a” numbers corresponds
equation b = a3 * a2.
“a” is number that looping between 0-999 and b is entered number by
user.

full version of question.

Will the code really throw a 'multiple declaration error'? It has been a long time since I programmed in C, but I thought that there would just be two declarations for 'a', one global and one local to the for loop?

Basically the global declararion would be ignored in favor of the local declaration. Given the simplicity of the program, and that 'a' is really only used inside the for loop, it should still work.

The problem is really in the logic. The program will never solve the equation b = a3 * a2.

Since this seems like a homework problem and I can only give you a hint to why the code is not creating the correct answer.

Review the equation you are solving for and the look at the code again. I am sure that the answer will come to you.

In C99 (and GNU C, I think) the second declaration of a masks the first only for the scope of the for loop, as you said.

C89 syntax doesn't allow declarations to appear in the initializer of a for loop, so it's still invalid even if the initial declaration is removed.

I think the problem is that c=(a*a*a)-(a*a) crosses the limit of int values. U should instead use unsigned long.

Assuming you want to find (a3)-(a2), and not a3*a2 (which is same as a5), try following code

#include"stdio.h"
#include"conio.h"
#include"math.h"
int main(void)
{
unsigned long a,b = 0;
unsigned long c=0;
printf("Please enter a number : ");
scanf("%ld",&b);

for(a=0;a<1000;a++)
{
c=(a*a*a)-(a*a);
if(b==c)
{
printf("a=%ld",a);
getch();
return
}
}
printf("No a exists, such that %ld=(a*a*a)-(a*a)",b);
getch();
}

I think the problem is that c=(a*a*a)-(a*a) crosses the limit of int values. U should instead use unsigned long.

Assuming 16-bit ints, that should only happen when a > 31, and the algorithm as given should work fine for c up to and including 28830. (With 32-bit ints, it will never overflow because a never exceeds 1000.)

The real problem I'm seeing, though, is that we've been told it "doesn't work" but haven't been given any further information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.