Java: Use BigInteger in for-loop

In my previous post, I mentioned a way to handle large integers by using BigInteger. Now I'm going to provide a very important usage of it. We often use for-loop. So here is the way to use it:

Ordinary integers:
for(int i = 1; i <= n; i++) {
 //Task to do
}

BigInteger:
for (BigInteger bi = BigInteger.valueOf(1);
                bi.compareTo(n) <= 0;
                bi = bi.add(BigInteger.ONE)) {
//Task to do
}
here n is a BigInteger variable.

0 comments:

Post a Comment

Popular Labels

Featured Post (TOP)

Flickr Images

Follow Me on Facebook.com

Follow on Google+

Recent Posts

AD (728x60)

Pages

Powered by Blogger.

Labels

Followers

Popular Posts

Popular Posts

Subscribe Via Email

Sign up for our newsletter, and well send you news and tutorials on web design, coding, business, and more! You'll also receive these great gifts: