write a code for large nos multilication (upto 200 digits)

Answers were Sorted based on User's Feedback



write a code for large nos multilication (upto 200 digits)..

Answer / atul kabra

#include<stdio.h>
#include <conio.h>
#include<string.h>
#include<alloc.h>
char * mul(char *, char);
char * add(char *, char *);
void main()
{

char *no1=(char * )malloc(100);
char *no2=(char *)malloc(100);
char *q="0";
char *p;
int i=0,j=0,t=0,l=0;
clrscr();
printf("\n Enter the large no ");
gets(no1);
printf("\n Enter the second no ");
gets(no2);

while(l<strlen(no2))
{
p=mul(no1,no2[l]);
for(j=1;j<=strlen(no2)-l-1;j++)
strcat(p,"0");
q=add(q,p);
l++;
}
printf("\n Multiplication is %s",q);
free(no1);
free(no2);
}


char * mul(char *x, char ch)
{
int i=0,j=0,t=0;
char *p=(char *)malloc(300);
char *a =(char *)malloc(300);
strcpy(p,x);
strrev(p);
while(p[i]!='\0')
{
a[j]=(p[i]-48)*(ch-48)+t;
t=a[j]/10;
a[j]=(a[j]%10)+48;
i++;
j++;
}
if(t!=0)
a[j]=t+48;
else
j--;
a[j+1]='\0';
strrev(a);
free(p);
return(a);
}
char * add(char *p, char *q)
{
char *t=(char *)malloc(300);
int i=0,j=0,x=0,a;
strrev(p);
strrev(q);
while(p[i]!='\0' && q[i]!='\0')
{
a=(p[i]-48)+(q[i]-48)+x;
x=a/10;
a=a%10;
t[i]=a+48;
i++;
}
while(i<strlen(p))
{
a=(p[i]-48)+x;
x=a/10;
a=a%10;
t[i]=a+48;
i++;
}
while(i<strlen(q))
{
a=(q[i]-48)+x;
x=a/10;
a=a%10;
t[i]=a+48;
i++;
}

if(x!=0)
t[i++]=x+48;
t[i]='\0';
strrev(t);
return(t);
}



Is This Answer Correct ?    5 Yes 2 No

write a code for large nos multilication (upto 200 digits)..

Answer / ajeet kumar

#include<iostream>
#include<string>
#include<sstream>
#include<vector>

using namespace std;

string toString(int a){
string s="";
stringstream buf;
buf<<a;
s=buf.str();
return s;
}

string ADD( string n1, string n2 ) {
int l1 = n1.size();
int l2 = n2.size();
int d = l1-l2;
d=(d>0)?d:-d;

string s1="";

if( d > 0 ){
for(int i=0;i<d;i++)
s1=s1+'0';
}
if(l1 > l2)
n2=s1+n2;
if(l2>l1)
n1=s1+n1;
int l = n1.size();
int carry = 0;
int x,y,z;
string ans="";

for(int j=l-1;j>=0;j--){
x=(int)n1[j]-48;
y=(int)n2[j]-48;
z=x+y+carry;

if(z>9){
carry=z/10;
string ad = toString(z%10);
ans=ad+ans;
}
else{
carry=0;
string ad = toString(z);
ans=ad+ans;
}
}
if(carry!=0)
ans='1'+ans;
return ans;
}

string remove_leading_zeroes(string tmp) {
string s="0";
for(int i=0;i<tmp.size();i++){
if(tmp[i]=='0')
continue;
tmp=tmp.substr(i,tmp.size()-i+1);
return tmp;
}
return s;
}

vector <string> equate_with_leading_zeros( string a, string
b ) {
int l1=a.size();
int l2=b.size();
int d=(l1-l2>0)?(l1-l2):(l2-l1);
string s="";
vector<string>v;

for(int i=0;i<d;i++)
s=s+'0';
if(l1>l2)
b=s+b;
if(l2>l1)
a=s+a;

v.push_back(a);
v.push_back(b);
return v;
}

string MULTIPLY( string s1, string s2 ) {
vector<string> v = equate_with_leading_zeros(s1,s2);
string a=v[0];
string b=v[1];
string op1,prev="";
int x,y,z;
int l=a.size();
int counter = 0;
int carry=0;

for(int i=l-1;i>=0;i--){
x = (int)a[i]-48;
op1="";
carry = 0;
for(int j=l-1;j>=0;j--){
y = (int)b[j]-48;
z = x*y+carry;
if(z>9){
op1=toString(z%10)+op1;
carry=z/10;
}
else{
op1=toString(z)+op1;
carry=0;
}
}
if(carry != 0){
op1=toString(carry)+op1;
}
carry=0;
string appString="";
for(int k=0;k<counter;k++)
appString=appString+'0';
op1=op1+appString;
prev = ADD(op1,prev);
counter++;
}
if(carry!=0){
prev = toString(carry)+prev;
}
string ans= remove_leading_zeroes(prev);
return ans;
}

int main()
{
string a,b;
cout<<"Enter first no.: ";
cin>>a;
cout<<"Enter second no.: ";
cin>>b;
string ans = MULTIPLY(a,b);
cout<<ans<<"\n";
return 0;
}

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More C Interview Questions

a C prog to swap 2 no.s without using variables just an array?

5 Answers   TCS,


I have an array of 100 elements. Each element contains some text. i want to: append a star character to the end of every fifth element remove every second character from every tenth element, and… add a line feed (ascii 10) after the 30th character of every array element whose length is greater than 30 characters.

1 Answers  


Sir,please help me out with the output of this programme:- #include<stdio.h> #include<conio.h> void main() { int a=18,b=12,i; for(i=a<b?a:b;a%i||b%i;i--); printf("%d %d",i); }

4 Answers  


Write a c program to build a heap method using Pointer to function and pointer to structure ?

0 Answers   Wipro,


If errno contains a nonzero number, is there an error?

0 Answers  






Between macros and functions,which is better to use and why?

0 Answers  


Write a program to generate the Fibinocci Series

0 Answers   TISL,


how to go with this?

1 Answers   Wipro,


what will be the result of the following program ? char *gxxx() { static char xxx[1024]; return xxx; } main() { char *g="string"; strcpy(gxxx(),g); g = gxxx(); strcpy(g,"oldstring"); printf("The string is : %s",gxxx()); } a) The string is : string b) The string is :Oldstring c) Run time error/Core dump d) Syntax error during compilation e) None of these

2 Answers   IBM,


Can a binary search tree be used as an index? If yes, how? Explain

0 Answers   TCS,


write a program that will accept two integers and will implement division without using the division operator if the second value is an odd number and will implement multiplication without using multiplication operator if the second value is an even number.

1 Answers  


Do variables need to be initialized?

0 Answers  


Categories