You are on page 1of 5

Week 5 Home Assignment

Satadru Roy
Roll:225101

PROBLEM 1:
WAP in C to handle BigIntegers.
A BigInteger X consist of two normal integers X1,X2 .
If you add two normal integers, sometimes there is an overflow.
Suppose A+B is so large that there is an overflow. The result would be a
BigInteger X, where the X2 will
be the resultant lower integer, and X1 will be the overflow integer.
Your program should handle addition and multiplication for the time
being. Use the concept outlined in
the Gr2 lab on 30th March. (This will be explained in Gr1 lab on 5th
April).
For addition: BigInteger A + BigIntegerB
A is A1 (all 0) and A2. B is B1 (all 0’s) abd B2.
C = A + B = C1 + C2 as described above.
For multiplication, use suitable notations.

CODE:
#include <stdio.h>
#include <string.h>

#define MAX_DIGITS 1000

void addBigIntegers(char num1[], char num2[], char sum[]) {


int carry = 0;
int len1 = strlen(num1);
int len2 = strlen(num2);
int i = len1 - 1;
int j = len2 - 1;
int k = 0;

while (i >= 0 || j >= 0 || carry > 0) {


int digit1 = i >= 0 ? num1[i--] - '0' : 0;
int digit2 = j >= 0 ? num2[j--] - '0' : 0;
int digitSum = digit1 + digit2 + carry;
carry = digitSum / 10;
sum[k++] = digitSum % 10 + '0';
}

sum[k] = '\0';

// Reverse the sum


int lenSum = strlen(sum);
for (i = 0, j = lenSum - 1; i < j; i++, j--) {
char temp = sum[i];
sum[i] = sum[j];
sum[j] = temp;
}
}

void multiplyBigIntegers(char num1[], char num2[], char product[]) {


int len1 = strlen(num1);
int len2 = strlen(num2);

int i, j;
int result[MAX_DIGITS] = {0}; // Array to store intermediate
multiplication results

// Multiply each digit of num1 with num2 and store the result in result
array
for (i = len1 - 1; i >= 0; i--) {
int carry = 0;
int digit1 = num1[i] - '0';

for (j = len2 - 1; j >= 0; j--) {


int digit2 = num2[j] - '0';
int product = digit1 * digit2 + carry + result[i + j + 1];
carry = product / 10;
result[i + j + 1] = product % 10;
}

result[i + j + 1] = carry;
}
// Convert the result array to a string
int k = 0;
for (i = 0; i < len1 + len2; i++) {
if (result[i] != 0 || k > 0) {
product[k++] = result[i] + '0';
}
}

product[k] = '\0';

if (k == 0) {
strcpy(product, "0");
}
}

int main() {
char num1[MAX_DIGITS];
char num2[MAX_DIGITS];
char sum[MAX_DIGITS];
char product[MAX_DIGITS];

printf("Enter the first number: ");


scanf("%s", num1);

printf("Enter the second number: ");


scanf("%s", num2);

addBigIntegers(num1, num2, sum);


multiplyBigIntegers(num1, num2, product);

printf("Sum of the two numbers: %s\n", sum);


printf("Product of the two numbers: %s\n", product);

return 0;
}
OUTPUT:
Enter the first number: 6574633828273646334
Enter the second number: 3627282828347434738373
Sum of the two numbers: 3633857462175708384707
Product of the two numbers:
23848056387969154416287882276304020574582
PROBLEM 2:
WAP in C to do the following:
a) Input three integers from keyboard, separated by “$” character.
b) Print all digits of the above integers as characters, where characters
representing one integer is
separated from the next one by the “#” character.

CODE:
#include <stdio.h>

int rev(int n)
{
int dig = 0; int rev = 0;
while(n>0)
{
dig = n%10;
n = n/10;
rev=rev*10 + dig;
}
return rev;
}

int main()
{
int num1,num2,num3 = 0; int dig = 0;
printf("Enter three integers seperated by $ sign.");
scanf("%d$%d$%d",&num1,&num2,&num3);
printf("The numbers entered are %d, %d, %d\n",num1,num2,num3);
num1 = rev(num1); num2 = rev(num2); num3 = rev(num3);
printf("DIGITS OF NUM1:");
while(num1>0)
{
dig = num1%10;
num1 = num1/10;
printf("%d#",dig);
}
printf("\nDIGITS OF NUM2:");
while(num2>0)
{
dig = num2%10;
num2 = num2/10;
printf("%d#",dig);
}
printf("\nDIGITS OF NUM3:");
while(num3>0)
{
dig = num3%10;
num3 = num3/10;
printf("%d#",dig);
}

OUTPUT:

Enter three integers seperated by $ sign.12$23$34


The numbers entered are 12, 23, 34
DIGITS OF NUM1:1#2#
DIGITS OF NUM2:2#3#
DIGITS OF NUM3:3#4#

You might also like