0% found this document useful (0 votes)
12 views5 pages

C Program for BigInteger Operations

The document contains two programming problems. The first problem involves writing a C program to handle addition and multiplication of large integers (big integers) by storing them as two separate integers. The second problem involves writing a C program to take three integers as input separated by '$' and output the digits of each integer separated by '#' characters. The code provided implements functions to perform big integer addition and multiplication for the first problem and reverses and prints the digits of three input integers with the specified separators for the second problem.

Uploaded by

Satadru Roy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

C Program for BigInteger Operations

The document contains two programming problems. The first problem involves writing a C program to handle addition and multiplication of large integers (big integers) by storing them as two separate integers. The second problem involves writing a C program to take three integers as input separated by '$' and output the digits of each integer separated by '#' characters. The code provided implements functions to perform big integer addition and multiplication for the first problem and reverses and prints the digits of three input integers with the specified separators for the second problem.

Uploaded by

Satadru Roy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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#

Common questions

Powered by AI

The output when the program is run with the inputs '12$23$34' is as follows: The numbers entered are 12, 23, 34; DIGITS OF NUM1: 1#2#; DIGITS OF NUM2: 2#3#; DIGITS OF NUM3: 3#4#. Each digit of the numbers is printed separately and followed by a '#' character, demonstrating the program's functionality of reversing and displaying digits .

Dividing and recombining tasks in building BigInt operations involves creating modular functions for each distinct arithmetic operation, such as addition and multiplication. This modular approach allows for testing and optimization at each functional level independently, enhancing reliability and maintainability. Every component can then be recombined within a main controlling function, which oversees input retrieval, task delegation, and output management, facilitating streamlined updates or expansions within individual modules without impacting others .

Characters are used to separate integers and digits to enhance readability and parsing simplicity. In the first C program, the '$' character separates entire integers, which scans cleanly with scanf. In the second task, the '#' character separates digits for clarity during output, emphasizing individual digits per integer. These separators improve the logical breakdown of input and output, aiding user interpretation and ensuring correct internal handling within operations .

The 'rev' function in the C program reverses the digits of an integer. It works by repeatedly extracting the last digit of the current integer, shifting the remaining digits right, and building the reversed integer from the extracted digits. It continues this process until no more digits are left in the original number, effectively reversing the order of digits in the integer. This function is used for each of the three input integers separated by the '$' character .

Multiplying two large integers using arrays in the provided C code involves performing elementary multiplication similar to manual multiplication. Each digit of the first number is multiplied by each digit of the second number. The results are stored in an intermediate results array. Each multiplication includes the addition of a carry from the previous calculation. The carry and the product are divided to update the current position and maintain the carry to the next higher position. Finally, the results array is converted back to a string for representation, ensuring a check to handle leading zeroes .

The algorithm for adding two big integers involves iterating through the digits from the least significant position towards the most significant. For each position, it adds the corresponding digits from both numbers along with any carry from the previous position. The carry is computed by dividing the digit sum by 10. The result or sum digit is the remainder of this division. This process continues until all digits are processed. Finally, the computed digits are stored in reverse and reversed back to get the final sum .

The multiplication function distinguishes significant digits by accumulating the results in an array where each index corresponds to a digit position in the final product. As each digit of one operand is multiplied by each digit of the other operand (considering positional offset), the results naturally accumulate towards their significant places. Additional checks ensure that the conversion from the result array to a string handles leading zeroes appropriately by skipping over them until a non-zero digit is encountered, thus starting the resultant string appropriately .

The C program handles user input for three integers using a custom separator by requiring the user to input the numbers separated by a '$' character. This is managed using the scanf function with the format specifier "%d$%d$%d", which tells the program to expect each integer to be distinguished by the '$' symbol. This method simplifies the parsing of input and allows each number to be identified and processed correctly .

The 'addBigIntegers' function includes logic to handle trailing zeros effectively by utilizing conditional checks during the summation process. It processes each digit starting from the least significant, hence naturally accounting for and eliminating leading zeroes in the final result during digit conversion and storage in the reverse order. This ensures that any unnecessary zeroes do not appear in the echoed sum .

Overflow in C when adding two large integers can be handled by breaking the result into two parts: the overflow part and the lower part. In this context, the program defines a BigInteger X with components X1 (overflow part) and X2 (lower part). When two integers A and B are added, the result is split such that X1 holds the overflow and X2 holds the remaining sum. This prevents overflow during arithmetic operations and stores results as a composed structure of two integers .

You might also like