# Peculiar Problem with Printf

Printable View

• 07-01-2002
Unregistered
Peculiar Problem with Printf
I am trying to find numbers between 1 and 100000000 which are both perfect squares and perfect cubes.

I have written the code successfully and the results are good. The problem is coming with printf.
When I print the results seperately, they're right.. when I print them in the same line, it is showing 0....

please check the code below:

#include <stdio.h>

void sqroot(long long int number, long long int *prevsqroot, long long int *sqexists)
{
long long int i;
*sqexists = 0;

for(i= *prevsqroot; i<=number; i++)
{
long long int var = i*i;

if (var == number)
{
*prevsqroot = i;
*sqexists = 1;
break;
}

if (var > number)
{
break;
}

}
}

void curoot(long long int number, long long int prevsqroot, long long int *prevcuroot, long long int *cuexists)
{
long long int i;
*cuexists = 0;

for(i=*prevcuroot; i<=prevsqroot; i++)
{
long long int var = i*i*i;

if (var == number)
{
*prevcuroot = i;
*cuexists = 1;
break;
}

if (var > number)
{
break;
}

}
}

main()
{
long long int i;
long long int prevsqroot = 1;
long long int prevcuroot = 1;
long long int sqexists=0, cuexists=0;

for (i=1; i<=1000000000; i++)
{
sqroot(i, &prevsqroot, &sqexists);

if (sqexists ==1)
{
curoot(i, prevsqroot, &prevcuroot, &cuexists);

if (cuexists ==1)
{
printf("Squareroot is %6d\n", prevsqroot);
printf("Cuberoot is %6d\n", prevcuroot);

printf("Number is %9d Sqroot is %6d Curoot is %6d\n", i, prevsqroot, prevcuroot);
}
}

}

printf("Successful!\n");

getchar();
}

notice that there are three printfs under the if above... the first
two printfs are working fine... but when i do the same thing in the thrid printf, it is not working..

another problem is that when i run this code in TC++, I get one extra problem, it is showing i as a negative number ... however, the squareroots and cuberoots calculated are right ...

Can anyone tell me what's gone wrong with this ?
• 07-01-2002
quzah
Quote:

another problem is that when i run this code in TC++, I get one extra problem, it is showing i as a negative number ... however, the squareroots and cuberoots calculated are right ..
Um... TurboC++ does not support 'long long', IIRC. TurboC++ has 16bit integers. I'm surprised your code even compiles. In the same vein, I doubt that implementation of printf knows what to do with them either.

Quzah.
• 07-01-2002
Unregistered
Turbo C++ DOES support long long int
TURBO C++ DOES support long long int (64 bit)
I wrote many programs before using that.
There's also long long float, FYI

If it doesn't, it wouldn't have printed the values
in the first two printfs.
• 07-01-2002
shaik786
Code:

```printf("Squareroot is %ld\n", prevsqroot); printf("Cuberoot is %ld\n", prevcuroot);```
%d is for integers, %ld is for long. Try the above, may help.
• 07-01-2002
quzah
Re: Turbo C++ DOES support long long int
Quote:

Originally posted by Unregistered
TURBO C++ DOES support long long int (64 bit)
I wrote many programs before using that.
There's also long long float, FYI

If it doesn't, it wouldn't have printed the values
in the first two printfs.

Really? What version of TC++ are you running? IIRC, the last version that was called "Turbo C++" was version 3.1, which was a Windows version, and did not support long long.

Assuming we're talking about the same product here. Turbo C++ by Borland? What version?

Quzah.
• 07-02-2002
Unregistered
What version ?
Borland Turbo C++ 3.0 (DOS)

It also works in Dev C++

I think you should include a library in the later
versions of Borland C++ (Turbo or whatever)
to use long long float ... instead, they
say something like __long64 , i am not sure..

And about the other post....

your %ld looks correct, but if you put them in the printf below,
it still doesn't work...

printf("Number is %9ld Sqroot is %6ld Curoot is %6ld\n", i, prevsqroot, prevcuroot);