In the C language, it is trivial to add together two variables that contain inte
ID: 3830900 • Letter: I
Question
In the C language, it is trivial to add together two variables that contain integer values, as long as the data type is large enough to represent the numbers and their sum. For example, a 32-bit variable supports integers up to ten digits. With 64 bits, the number of digits is twenty. For most applications, this is more than sufficient. However, what if you wanted to add two integers that had 25 digits? Or 1000 digits? Clearly, storing the values in standard variables would not work. Your task is to add two integer numbers that can have an arbitrarily large number of digits. Background A simple way to perform addition is to use the method taught to children. You align the columns of each number (starting with the one's place) and add the individual digits within each column being sure to account for the "carry", if any, from a previous summation. Example: What is the value of 35 28? (Answer: 63) 10000's 1000's 100's Place place place place Sum N1+N2 Example: What is the value of 1500 21? (Answer: 1521) 10000's 1000's 100's place place Place place place Example: What is the value of 9908 4252? (Answer: 14160) 10000's 1000's 100's Place L place place L place placeExplanation / Answer
#include<stdio.h>
void add (int *p, int *q, int *s, int n1, int n2){ // p is first number, q is second number, n1 length of first number, n2- length pf second number
int sum; // s is the answer
int carry = 0;
int n;
int *p1, *p2, *p3;
if (n1 >= n2){
n = n1;
p1 = p;
p2 = q;
}
if (n1 < n2){
n = n2;
p1 = q;
p2 = p;
}
for (int i = 0; i<n; i++){
sum = *(p1+i) + *(p2+i) + carry;
*(p3 + i) = sum % 10;
carry = sum /10;
}
if (carry > 0)
*(p3+n) = carry;
}
void disp(int *p, int n){
for (int i=n; i>=0; i--){
if (i == n){
if (*(p+i) > 0)
printf("%d", *(p+i));
}
else
printf("%d",*(p+i));
}
}
void main() {
int first[100];
int second[100];
int answer[101];
char num1[100];
char num2[100];
char *p;
int flag;
int count;
int n1,n2;
int choice;
do {
printf("1.Add Numbers ")'
printf("2.Quit");
scanf("%d", &choice);
if (choice == 2)
break;
for (int i =0; i<100; i++){
first[i] = 0;
second[i] = 0;
}
printf(" Enter first number");
count = 0;
do {
scanf("%c",ch);
num1[count] = ch;
count++;
}while (ch != ' ')
if (count > 100){
printf(" Invalid input :count exceeds 100 ");
return;
}
p = num1;
for (int i = 0; i<count; i++){
if (*(p+i) < '0' && *(p+i) > '9'){
printf("Invalid number ");
return;
}
}
n1 = count;
printf(" Enter second number");
count = 0;
do {
scanf("%c",ch);
num2[count] = ch;
count++;
}while (ch != ' ')
if (count > 100){
printf(" Invalid input :count exceeds 100 ");
return;
}
p = num2;
for (int i = 0; i<count; i++){
if (*(p+i) < '0' && *(p+i) > '9'){
printf("Invalid number ");
return;
}
}
n2 = count;
p = num1;
for (i = 0; i<n1; i++){
first[n1-1-i] = *(p +i);
}
p = num2;
for (i = 0; i<n2; i++){
second[n2-1-i] = *(p +i);
}
add(&first[0],&second[0], &answer[0], n1,n2);
disp(&first[0]);
disp(&second[0]);
disp(&answer[0]);
} while (choice != 2)
}