#include #define SIZE 10 char skip_blanks(); int read_number(); void multiply_numbers(); void output_number(); /* Reads two positive numbers of at most SIZE many digits * and outputs their product. */ main() { char number1[SIZE]; //Store first number char number2[SIZE]; //store second number char product[SIZE*2]; // Stores the result of multiplication /* Read first number */ if (read_number(number1) == 0) // Wrong input return; /* Read second number */ if (read_number(number2) == 0) // Wrong input return; /* Multiply numbers */ multiply_numbers(number1, number2, product); /* Output the result */ output_number(product); } /* Reads a positive number of at most SIZE digits */ int read_number(char number[]) { char temp[SIZE]; int i; int size; // Records the number of digits in the read number char symbol; // Stores the current symbol read /*Initialising array number to zero */ for(i=0; i < SIZE; i++) number[i] = 0; // read number printf("Please enter the first number of atmost %d digits: ",SIZE); symbol = skip_blanks(); for(size = 0; 1; size++) { /* reading number digit-by-digit */ if ((symbol < '0')|| (symbol > '9')) break; /*breaking the loop on entering non digit*/ if(size == SIZE) { printf("The number is too large\n");/* If the number is too long then exit*/ return 0; // Return error } temp[size] = symbol - '0'; /*storing number in temporary location*/ symbol = getchar(); /*reading input digit by digit*/ } // reversing the digits for(i = 0; i < size; i++) { number[i] = temp[size-i-1]; } return 1; // return success } /* Multiplies two given numbers and stores the result in product array */ void multiply_numbers(char number1[], char number2[], char product[]) { int carry; // Stores the carry char temp[2*SIZE]; // Stores the multiplication of one digit of number1 with number2 int i; int j; int k; /* Initialize the arrays temp and product */ for (i = 0; i < 2*SIZE; i++) { temp[i] = 0; product[i] = 0; } /* Multiply two numbers by multiplying each digit of number1 to number2 and summing the results */ for(i = 0; i < SIZE; i++) { /*selecting the digit from first number*/ /* First, multiply the digit of number1 with number2 */ carry = 0; // carry is zero in the beginning of multiplication for(j = 0; j < SIZE; j++) { /* multiplying the selected digit with second num */ k = number2[j] * number1[i] + carry; temp[i+j] = k % 10; carry = k / 10; } /* Now add this to the sum of previous multiplications */ carry = 0; // carry is zero in the beginning of addition /* performing addition of the product of digits of second number with the first number*/ for(j = 0; j < 2*(SIZE); j++) { k = temp[j] + product[j] + carry; product[j] = k % 10; carry = k / 10; } for(j = 0; j < 2*SIZE; j++) // reset temp to zero temp[j] = 0; } } /* Outputs the number */ void output_number(char number[]) { int i; for (i = 2*SIZE-1; i >= 0; i--) if (number[i] > 0)// ignoring all zeros break; if (i == -1) /* the sum is zero! */ printf("The product is: 0\n"); else { printf("The product is: "); for (; i >= 0; i--) putchar(number[i]+'0'); printf("\n"); } } /* Skips white spaces and returns the first non-white space symbol */ char skip_blanks() { char c; c = getchar(); // read a symbol // Skip symbols until a non-white space is found for (; (c == ' ') || (c == '\n') || (c == '\t'); ) c = getchar(); return c; }