I am trying to write a program in C, which performs the multiplication of two numbers without directly using the multiplication operator and must take into account sufficiently large numbers, so that even the usual addition of these two numbers cannot be performed by direct addition.
I was motivated by this, when I tried (and successfully did) to write a C program that does an add using character strings, I did the following:
#include<stdio.h>
#define N 100000
#include<string.h>
void pushelts(char X[], int n){
int i, j;
for (j = 0; j < n; j++){
for (i = strlen(X); i >= 0; i--){
X[i + 1] = X[i];
}
X[0] = '0';
}
}
int max(int a, int b){
if (a > b){ return a; }
return b;
}
void main(){
char E[N], F[N]; int C[N]; int i, j, a, b, c, d = 0, e;
printf("Enter the first number: ");
gets_s(E);
printf("\nEnter the second number: ");
gets_s(F);
a = strlen(E); b = strlen(F); c = max(a, b);
pushelts(E, c - a); pushelts(F, c - b);
for (i = c - 1; i >= 0; i--){
e = d + E[i] + F[i] - 2*'0';
C[i] = e % 10; d = e / 10;
}
printf("\nThe answer is: ");
for (i = 0; i < c; i++){
printf("%d", C[i]);
}
getchar();
}
"N". , ? -, , , , n ( 0 <= n <= 9). , ; (*). - ( ) . k ( a1a2..... ak) :
a1a2...ak = a1 x 10^(k - 1) + a2 x 10^(k - 2) + ... + ak-1 x 10 + ak
, , , (*).
x1x2..... xn, - y1y2.... yk, :
x1x2...xn x y1y2...yk = (x1x2...xn) x y1 x 10^(k-1) + .....
(*) (x1x2... xn) y1, 10 ^ (k-1) -1 ; , k , . , , , , . (x1x2.... xn) yi x 10 ^ (i-1), , , , , , . , char int . , , ; , , , . , , .
- ? .