< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • Long Int Addition or Subtraction
  • Long Int Multiplication

Was this helpful?

Long Integer Operations

Long Int Addition or Subtraction

#include<iostream>
using namespace std;

int main() {

     int a[] = {9, 8, 5, 2, 8};
     int i = sizeof(a)/sizeof(int);

     int b[] = {0, 1, 7, 4};
     int j = sizeof(b)/sizeof(int);

     int c[((i>j)? (i+1): (j+1))], k = ((i>j)? i: j);

     int carry = 0, remainder, quotient;

     for (i -= 1, j -= 1; i >= 0 && j >= 0; i--, j--) {
          remainder = (a[i] + b[j] + carry) % 10;
          quotient = (a[i] + b[j] + carry) / 10;

          c[k--] = remainder;
          carry = quotient;
     }

     if(j >= 0) {
          for (; j >= 0 ; j--) {
               c[k--] = (b[j] + carry) % 10;   
               carry = (b[j] + carry) / 10;   
          }
     }
     else if(i >= 0) {
          for (; i >= 0 ; i--) {
               c[k--] = (a[i] + carry) % 10;   
               carry = (a[i] + carry) / 10;   
          }
     }
     
     c[0] = carry;

     int size = sizeof(c)/sizeof(int);

     for (i = 0; i < size; i++) {
          cout << c[i] << ' ';
     }

     return 0;
}

Long Int Multiplication

#include<iostream>
using namespace std;

int main() {
     int a[] = {7, 2, 9, 5, 6};
     int b[] = {7, 5, 1, 3};

     int i = sizeof(a)/sizeof(a[0]);
     int j = sizeof(b)/sizeof(b[0]);
     
     const int rows = j;
     const int col = (j * 2) + 1;

     int p = j*2,
     k = 0,
     sum ;

     int twoD[rows][col] = {0};
     for (int i = 0; i < rows; i++) {
          for (int j = 0; j < col; j++) {
               twoD[i][j] = 0 ;
          }
     }
     
     for ( j -= 1; j >= 0; j--) {
          int carry = 0, q = 0;
          for (int r = i-1; r >= 0; r--) {
               sum = ((b[j] * a[r]) + carry);
               twoD[k][p-q] = sum % 10;
               carry = sum / 10;
               q++;
          }
          twoD[k][p-q] = carry;
          k++;
          p--;
     }
     
     for (int i = 0; i < rows; i++) {
          for (int j = 0; j < col; j++) {
               cout << twoD[i][j] << " ";
          }cout << endl;
     }
     
     int carry = 0;
     int res[col]; k = col-1;
     
     for (int i = col-1; i >= 0; i--) {
          sum = 0;
          for (int j = rows-1; j >= 0; j--) {
               sum += twoD[j][i]; 
          } 
          sum += carry;
          res[k] = sum % 10;
          carry = sum / 10;
          k--;
     }
     res[0] = sum;
     cout << "-----------------"<< endl;
     for (int i = 0; i < col; i++) {
          cout << res[i] << " ";
     }
}
PreviousAssignments - DSANextOOP

Last updated 1 year ago

Was this helpful?

Page cover image