Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #define MAX 20
- void reverseCopy(char *from, char *to){
- int length = strlen(from);
- for(int i=0;i<length;i++){
- to[i] = from[length-i-1];
- }
- to[length]='\0';
- }
- void mult(char *first, char *sec, char *result){
- char F[MAX],S[MAX],temp[MAX];
- int i, j, r, t_len, carry, res;
- int f_len = strlen(first);
- int s_len = strlen(sec);
- reverseCopy(first,F);
- reverseCopy(sec,S);
- t_len = f_len+s_len;
- r = -1;
- for(i=0;i<=t_len;i++){
- temp[i] = '0';
- }
- temp[i]='\0';
- for(j=0;j<s_len;j++){
- carry = 0;
- for(i=0;i<f_len;i++){
- res = (F[i]-'0') * (S[j] - '0') + carry + (temp[i+j] - '0');
- temp[i+j] = res%10 + '0';
- carry = res/10;
- if(i+j > r){
- r = i + j;
- }
- }
- while(carry!=0){
- res = carry + temp[i+j] - '0';
- carry = res/10;
- temp[i+j] = res%10 + '0';
- if(i+j > r){
- r = i + j;
- }
- i++;
- }
- }
- for(;r>0 && temp[r]=='0';r--);
- temp[r+1]='\0';
- reverseCopy(temp,result);
- }
- int main(){
- char first[MAX], second[MAX], res[MAX];
- while(scanf("%s%s", &first, &second)){
- mult(first, second, res);
- int len = strlen(res);
- printf("Result : ");
- for(int i=0;i<len;i++){
- printf("%c", res[i]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement