Reputation: 1034
I have a simple program that finds automorphic numbers up to given number. The problem is it just finds the number which square parameter is bound of int.
for example : 90625 x 90625 = 8212890625 but program could't find it.
Question : What is the problem with long type here? It act like int. Is there a cast to int for the square parameter anywhere of the code?
#include <stdio.h>
int main(void)
{
int n;
long i;
printf ("Enter a value:\n") ;
scanf ("%d",&n) ;
printf ("Automorphic numbers:\n") ;
for (i=1L ; i<=n ; i = i+1L)
{
long square = i*i ;
if (square % 10L == i || square % 100L == i || square % 1000L == i || square % 10000L == i || square % 100000L == i || square % 1000000L == i || square % 10000000L == i)
printf ("%li\n", i) ;
}
}
Upvotes: 0
Views: 1565
Reputation: 334
For 64 bits you shall use long long
.
EDIT: long corresponds to long int which can have the same size as int. You can use sizeof
to check the size taken.
Upvotes: 1
Reputation: 6994
C guarantees the following limits (n869 C99 draft):
-- minimum value for an object of type int
INT_MIN -32767 // -(215-1)
-- maximum value for an object of type int
INT_MAX +32767 // 215-1
-- maximum value for an object of type unsigned int
UINT_MAX 65535 // 216-1
-- minimum value for an object of type long int
LONG_MIN -2147483647 // -(231-1)
-- maximum value for an object of type long int
LONG_MAX +2147483647 // 231-1
-- maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 232-1
-- minimum value for an object of type long long int
LLONG_MIN -9223372036854775807 // -(263-1)
-- maximum value for an object of type long long int
LLONG_MAX +9223372036854775807 // 263-1
[5.2.4.2.1 Sizes of integer types <limits.h>]
When you need defined sizes, use uint64_t
and similar datatypes.
Upvotes: 5