
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Handling large numbers in C++?
In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections. It has large domain of applications. For example, using boost, we can use large number like 264 in C++.
Here we will see some examples of boost library. We can use big integer datatype. We can use different datatypes like int128_t, int256_t, int1024_t etc. By using this we can get precision up to 1024 easily.
At first we are multiplying two huge number using boost library.
Example
#include<iostream> #include <boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; using namespace std; int128_t large_product(long long n1, long long n2) { int128_t ans = (int128_t) n1 * n2; return ans; } int main() { long long num1 = 98745636214564698; long long num2 = 7459874565236544789; cout << "Product of "<< num1 << " * "<< num2 << " = " << large_product(num1,num2); }
Output
Product of 98745636214564698 * 7459874565236544789 = 736630060025131838840151335215258722
Another kind of datatype is that Arbitrary Precision Datatype. So we can use any precision using cpp_int datatype. It automatically assigns precision at runtime.
Example
#include<iostream> #include <boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; using namespace std; cpp_int large_fact(int num) { cpp_int fact = 1; for (int i=num; i>1; --i) fact *= i; return fact; } int main() { cout << "Factorial of 50: " << large_fact(50) << endl; }
Output
Factorial of 50: 30414093201713378043612608166064768844377641568960512000000000000
- Related Articles
- Large Fibonacci Numbers in C#
- Sum of two large numbers in C++
- How to handle very large numbers in Python?
- Add grouping specifiers for large numbers in Java
- How to generate large random numbers in Java?
- Multiply Large Numbers represented as Strings in C++
- How to multiply large numbers using Python?
- How to divide large numbers using Python?
- C++ Program to add few large numbers
- Behavior of + operator in JavaScript to store large numbers?
- Divisible by 37 for large numbers in C++ Program
- How to stop default printing of large numbers in R?
- How to add/subtract large numbers using Python?
- C++ Program to Find Factorial of Large Numbers
- Find Last Digit of a^b for Large Numbers in C++

Advertisements