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

What is I ++ in c programming?

629


By using C language input a date into it and if it is right?

575


How many levels of indirection in pointers can you have in a single declaration?

596


Why can arithmetic operations not be performed on void pointers?

591


Why is event driven programming or procedural programming, better within specific scenario?

1955






What is calloc()?

630


How is a macro different from a function?

657


How to write a program for machine which is connected with server for that server automatically wants to catch the time for user of that machine?

1591


What is the explanation for cyclic nature of data types in c?

648


what will be the output for the following main() { printf("hi" "hello"); }

9336


What is null pointer constant?

596


Explain what is a 'locale'?

585


What is function prototype in c with example?

580


Badboy is defined who has ALL the following properties: Does not have a girlfriend and is not married. He is not more than 23 years old. The middle name should be "Singh" The last name should have more than 4 characters. The character 'a' should appear in the last name at least two times. The name of one of his brothers should be "Ram" Write a method: boolean isBadBoy(boolean hasGirlFriend , boolean isMarried, int age , String middleName , String lastName , String[] brotherName); isHaveGirlFriend is true if the person has a girlfriend isMarried is true if the person is married age is the age of the person middleName is the middle name of the person lastName is the last name of the person brotherName is the array of the names of his brothers

1425


What are the string functions? List some string functions available in c.

606