can long long store 12 digit numbers.
Its showing in my comp that limit of long long is 9223372036854775807, but then it's showing too large when i declare it like this:
long long num;
i don't want to store in array.
My seniors told that long long will do but i am having trouble.
please help.

Recommended Answers

All 13 Replies

a) post what you actually tried, not a description.
b) post your actual error messages
c) tell us what your OS/Compiler is.

From this thread I would suggest you to use: [B]unsigned long long[/B] :)

>can long long store 12 digit numbers.
Depends on what your compiler is...

this is my code. Problem as described above.
We have to find largest prime factor of a number.

#include<iostream>
using namespace std;
int main()
{
	
	long long num=600851475143,large,k,i;
	
	for(i=2;;++i)
	{  k=i;
		if(num%i==0)
		{	num/=i;
			i=k-1;
			large=k;
		}
	   if(num==1)
		   break;
	}
cout<<large;
return 0;
}

the error i get is:
integer constant is too large for ‘long’ type
i don't want to use array.

unsigned long long gives same error.

>i don't want to use array.
In that case there's probably no other choice left than choosing for a Big Number library like GNU MP, it can handle much bigger numbers than a standard C++ datatype can...

And take a look at Salem's post as well:

  • What is your compiler?
  • What is your OS?

Try to append LL suffix to long long constants.

commented: ArkM blazes in with both barreLLs for the win ;) +33
commented: Sometimes it's so simple that you even don't think about it :P +8

Try

long long num=600851475143LL,large,k,i;

to tell the compiler that the constant is a long long.

ubuntu 8.10
compiler g++ 4:4.3.1

thanks man, a million thanks. It works

>thanks man, a million thanks. It works
So, what is stopping you to mark the thread as Solved.

frankly, i didn't know that we can marl thread as solved.
But wait, i need to know something more.
While using cin to take a long long int, can simple method,
long long num;
cin>>num;
will do or not ??
and suppose we use it in a code like this,

#include<iostream>
using namespace std;
int main()
{
	long long i;
	for (i=999999999;i<1234567890123;++i)
	if (i%100000000==0)
		cout<<i;
}

it's again showing
integer constant is too large for ‘long’ type

so how to correctly implement it???

> integer constant is too large for ‘long’ type
Oh come on, we've told you how to fix "long long" constants, can't you at least make some intuitive guess as to how to fix "long" ?

And "mark as solved" is a link at one end of the thread (top or bottom, I forget, you can find it).

The integers are too big. You will have to explicitly flush the stream as it takes a hell lot of time to generate the next number which makes your condition true.
Also, postfix LL after the long long constants as already told by others.

#include<iostream>
using namespace std;
int main()
{
	unsigned long long i;
	for (i=999999999LL;i<1234567890123LL;++i)
        if (i%100000000==0)
            cout<<i<<flush; //check here
}

Read this post for a possible explanation: http://www.daniweb.com/forums/post873624.html#post873624

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.