|
-
August 25th, 2009, 08:21 AM
#1
large number multi plication
hi all i am trying to multiply to big numbers..the code is here as u scroll down to the last part i am unable to get the result for multiplication ..can some one help me out please the addition is successful
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
#include<ncurses.h>
#include<string.h>
using namespace std;
int main ()
{
int i=0,k=0;
int tmp1=0, tmp2=0, tmp3=0;
char * a;
a= (char *) malloc(30 * sizeof(char));
char * b;
b= (char *) malloc(30 * sizeof(char));
int * x;
x= (int *) malloc(30 * sizeof(int));
int * y;
y= (int *) malloc(30 * sizeof(int));
int * z;
z= (int *) malloc(61 * sizeof(int));
int * w;
w= (int *) malloc(31 * sizeof(int));
int * v;
v= (int *) malloc(61 * sizeof(int));
int * solution;
solution= (int *) malloc(61 * sizeof(int));
int carry = 0;
int c[61];
int d[61];
int f[61];
int g[61];
cout<<"\nEnter a number (up to 30 digits in length)> "; cin >> a;
i=0;
while(i < strlen(a)){
c[i] = a[i] - '0';
i++;}
cout << "\nThe first number is: ";
for(i=0; i < strlen(a); i++){
cout << c[i];}
cout <<"\n";
//string reversal
for(i=0, k = 29; i < strlen(a); i++){
x[k] = c[(strlen(a)-1)-i];
k--;}
cout << "\nEnter a second number (up to 30 digits in length)> "; cin >> b;
i=0;
while(i < strlen(b)){
d[i] = b[i] - '0';
i++;}
cout << "\nThe second number is: ";
for(i=0; i < strlen(b); i++){
cout << d[i];}
cout << "\n";
//string reversal
for(i=0, k = 29; i < strlen(b); i++){
y[k] = d[(strlen(b)-1)-i];
cout <<"\nThe sum of the two numbers is: ";
for(k=30; k>=0; k--)
{
w[k] = 0; //w[k] initialised
}
//addition
for (k = 29; k >= 0; k--){
w[k] = x[k] + y[k] + w[k]; //+w[k] is accounting for the carry
//carry taken care of
if (w[k] >= 10){
w[k] = w[k]%10;
w[k-1] = 1;}
}
for(i= -1; i < 30; i++){
cout << w[i];}
cout <<"\n";
for(k = 60; k>=0; k--){
z[k] = 0;
f[k] = 0;
g[k] = 0;
solution[k] = 0;
}
for(k = 0; k < 30; k++){
f[k+31] = x[k];
g[k+31] = y[k];
}
cout <<"\nThe product of the two numbers is: ";
/*The multiplication algorithm begins here*/
for(i= 60; i >= 0; i--) {
for(k=60; k>=0; k--)
{ tmp1 = f[i] * g[k];
z[k] = tmp3 + z[k-1];
tmp3 = tmp1 % 10;
z[k-1]= tmp1 / 10;
}
}
/*The multiplication algorithm ends here*/
for(i = 0; i < 61; i++){
cout << z[i];}
cout <<"\n\n";
return 0;
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|