• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Add Sub Multiply very large number stored as string

August 10, 2014 by Dhaval Dave

Very large number like 2994320121 and 125346232 is given in character string.
You need to add, subtract and multiply two numbers and store number in character array only.

Logic
1) First way to create a “long long”number from String and add/multiply/subtract.
And convert this result in to character array.2) keep numbers in Character array only and try to add/sub/multiply index wise.
See code for Add+Multiply
You can create for Subtract.

Thanks to Dhaval for suggesting this approach and Article

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000

char * addition(char a[],char b[]){

    static char add[MAX];
    char c[MAX];
    int temp;
    int la,lb;
    int i,j,k=0,x=0,y;
    long int car=0;
    long sum = 0;
    la=strlen(a)-1;
    lb=strlen(b)-1;
    //printf(“n la=%d lb=%d “,la,lb);
    for(i=0;i<=la;i++){ a[i] = a[i] – 48; }
    for(i=0;i<=lb;i++){ b[i] = b[i] – 48; }

   if(la>lb){
       y=0;
       k=la;

   }
   else{
       k=lb;
       y=1;
   }
    //printf(“nAddition called “);
    for(i=la,j=lb;i>=0,j>=0;i–,j–){
        //printf(“Addition called “);
        c[k–]=(car+a[i]+b[j])%10;
        car=(car+a[i]+b[j])/10;
    }//for
    printf(“nWithout carry %s”,c);

    if(y=0){
        j=0;
        for(i=0;i<la+1;i++){
        add[j++]=c[i] + 48;
        }
        add[j]=”;
    }
    else{
        j=0;
        for(i=0;i<lb+1;i++){
        add[j++]=c[i] + 48;
        }
        add[j]=”;
    }
    return add;

}

char * multiply(char a[],char b[]){
    static char mul[MAX];
    char c[MAX];
    char temp[MAX];
    int la,lb;
    int i,j,k=0,x=0,y;
    long int r=0;
    long sum = 0;
    la=strlen(a)-1;
    lb=strlen(b)-1;

        for(i=0;i<=la;i++){
                a[i] = a[i] – 48;
        }

        for(i=0;i<=lb;i++){
                b[i] = b[i] – 48;
        }

    for(i=lb;i>=0;i–){
         r=0;
         for(j=la;j>=0;j–){
             temp[k++] = (b[i]*a[j] + r)%10;
             r = (b[i]*a[j]+r)/10;
         }
         temp[k++] = r;
         x++;
         for(y = 0;y<x;y++){
             temp[k++] = 0;
         }
    }

    k=0;
    r=0;
    for(i=0;i<la+lb+2;i++){
         sum =0;
         y=0;
         for(j=1;j<=lb+1;j++){
             if(i <= la+j){
                 sum = sum + temp[y+i];
             }
             y += j + la + 1;
         }
         c[k++] = (sum+r) %10;
         r = (sum+r)/10;
    }
    c[k] = r;
    j=0;
    for(i=k-1;i>=0;i–){
         mul[j++]=c[i] + 48;
    }
    mul[j]=”;
    return mul;
}

int main(){
    char a[MAX]={‘9′,’5′,’5′,’5′,’5’ };
    char b[MAX]={‘3′,’3′,’3′,’3′,’3′,’3′,’3′,’3′,’3′,’3’};
    char *c;
    int la,lb;
    //int i = 0;
    int i = 1;
    //int i = 2;

    printf(“Answer of two numbers : “);
    switch(i){
        case 0:  c = multiply(a,b); break;
        case 1:  c = addition(a,b); break;
        //case 2:  c = sub(a,b); break;
        default: break;
    }
    //c = multiply(a,b);
    printf(“%s”,c);
    return 0;
}

You can view code at http://ideone.com/Kd2Rv1

Similar Articles

Filed Under: Amazon Interview Question, Interview Questions, Microsoft Interview Questions Tagged With: Mathematical, string

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Reversal of LinkedList

Check Binary Tree is Binary Search Tree or not

Sequence Finder Dynamic Programming

Convert number to words java

Serialise Deserialise N-ary Tree

Maximum size of square sub matrix with all 1’s in a binary matrix

Max Sum in circularly situated Values

CodeChef Code SGARDEN

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

How Radix sort works

Find if two rectangles overlap

Edit Distance ( Dynamic Programming )

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

TicTacToe Game As Asked in Flipkart

Flipkart SDET Interview Experience

SAP Off Campus Hiring_ March 2015 Verbal Skills

Python List

Binary Tree Isomorphic to each other

Diagonal Traversal of Binary Tree

Python String and numbers

Hackerearth : Counting Subarrays

Find loop in Singly linked list

Check if an array has duplicate numbers in O(n) time and O(1) space

Find min element in Sorted Rotated Array (Without Duplicates)

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

The Magic HackerEarth Nirvana solutions Hiring Challenge

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Wrong Directions given find minimum moves so that he can reach to the destination

Generate largest number arranging a no. of given non negative integer numbers

Copyright © 2025 · Genesis Framework · WordPress · Log in