VB icon

Non-negative integer Multiplication of ANY LENGTH!!

Email
Submitted on: 1/3/2015 3:06:00 PM
By: Adnan Faisal (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C, C++ (general), Borland C++, UNIX C++
Views: 608
 
     Please rate me if it worked for you.It is an extremely efficient Non-Negative INTEGER MULTIPLICATION of ANY LENGTH!!I assure you it is very quick..Any CS student may need it.It is my first Submission.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Non-negative integer Multiplication of ANY LENGTH!!
// Description:Please rate me if it worked for you.It is an extremely efficient Non-Negative INTEGER MULTIPLICATION of ANY LENGTH!!I assure you it is very quick..Any CS student may need it.It is my first Submission.
// By: Adnan Faisal (from psc cd)
//**************************************

//Char to Char MUL
//WORKS 100% :)
// No Strrev!!
//20-4-2002
/*It is an extremely efficient Non-Negative 
INTEGER MULTIPLICATION of ANY LENGTH!!
I assure you it is very quick..Any CS student may need it.
Increse L in order to increase the input length.
 (Default L=50) */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define L 500
#define START 5
char mul[2*L];
long totallen=0;
long Multiply(char a[],char b[],long len1,long len2)
{
 long i,j,k=START,m;
 int temp ;
 m=START; //len1+len2
// mul = (char *) malloc((2*L+5)*sizeof(char));
 for(i=0;i<=len1+len2+5;i++)
 mul[i]=0;
 for(i=len1-1;i>=START;i--)
 {
for(j=len2-1;j>=START;j--)
{
temp = mul[k] + (a[i]-48) * (b[j]-48) ;
mul[k] = temp % 10 ;
mul[k+1] = mul[k+1] +temp /10;
k++;
}
m++;
k=m;
 }
// mul[k]='\n';
 return m; //no. of digits in resultant
}
void print(long k)
{
 long i;
// finding starting position from last
 for(i=totallen;i>=START;i--)
 { if ( mul[i]>=1 && mul[i]<=9) break;}
 k = i;
 //printing
 for(i=k;i>=START;i--)
printf("%d",mul[i]);
 printf("\n");
}
void main()
{
 char a[L],b[L];
 char c;
 long i=START,len1,len2,x=0,k,j=START;
 // freopen("prod.txt","r",stdin);
 //a = (char *) malloc (L*sizeof(char));
 //b = (char *) malloc (L*sizeof(char));
 strcpy(a,"");
 strcpy(b,"");
 strcpy(mul,"");
 while(scanf("%c",&c)==1)
{
if(x==0)
	if(c!='\n' && c!=' ')
	 { a[i++] = c; continue; }
	else { x=1; a[i]='\n'; len1 = i ;continue;}
if(x==1)
	 if(c!='\n' && c!=' ')
	{b[j++] = c; continue; }
	 else { x = 0 ; b[j]='\n'; len2 = j; }
	totallen = len1+len2;
	if(a[START]=='0' || b[START]=='0')
	{ printf("0\n"); goto end;}
	 k = Multiply(a,b,len1,len2);
	 //no use of this k
	 print(k+START);
end:
strcpy(a,"");
strcpy(b,"");
strcpy(mul,"");
//a = (char *) malloc (L*sizeof(char));
//b = (char *) malloc (L*sizeof(char));
x=0;
i =j = START;
}//end of while
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.