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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

while loop contains parts a) initialisation, evalution of an expression,increment /decrement b) initialisation, increment/decrement c) condition evalution d) none of the above

732


Are comments included during the compilation stage and placed in the EXE file as well?

668


What does the error message "DGROUP exceeds 64K" mean?

723


What is the translation phases used in c language?

628


Why clrscr is used after variable declaration?

1035






What type of function is main ()?

581


What is the difference between variable declaration and variable definition in c?

562


What is a c token and types of c tokens?

585


difference between native and cross compilers

1669


How main function is called in c?

622


What are different types of operators?

592


What is a #include preprocessor?

612


What is an example of structure?

585


Explain what is a 'locale'?

581


Explain what is wrong with this statement? Myname = ?robin?;

992