Week 5 Assignment
Week 5 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>
sum[k] = '\0';
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';
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];
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: