Write this program in C language, please. I will rate the answer. 1. Addition of
ID: 3605159 • Letter: W
Question
Write this program in C language, please. I will rate the answer.
1. Addition of Two Big Integers As we all have known that integers in C have limited range. But sometimes that limitation just has ta be conquered. And now we are gonna do it. Input an integer addition expression (addition between two positive integers), with the number of digits of each integer being at most 1,000. Please note that the result could be 1001 digits. Example lease input your expression The answer is: Hints 1) Get the input expression as a string t by 'into two integer arrays, say: int num1 [1001] = t ak nun2[18e1] = { e }; With every array element holding only 1 digit. Be aware of what you need to do about the conversion from a char to an int. 3) Reverse numl and nun2 so that the two numbers are aligned by lower digits. See below 123456 654321 381 183 4) Perform digit-by-digit addition by using a loop to iterate throug nm1 and num2 at the same time, and be cautious when there is a carry. See below 654321 ± 738321 183 5) Finally, print the result reversely to get the answer of the expression. Other Requirements At least have the following functions defined and called in your code: void neverseflot array[], {}, to perform in-place reverse of an integer array nt terintchar c) {}, to convert a number character to an integer [e.g. convert '1, to 1). void add(UE numlu. num2[], eoe[]) {}, to perform digit-by-digit addition of num1 and num2 and store the result into ans, . · .Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#include <values.h>
#include <time.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
int to_int(char ch);
int to_int(char ch)
{
int x=ch-'0';
return x;
}
void reverse(int a[],int len)
{
//swapping first value with last , sec with sec last and so on to
//reverse the array
int i=0,j=0,temp=0;
for(i=0,j=len-1;i<len/2;i++,j--)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
void add(int a[],int b[],int c[],int len)
{
int x=0,i=0,j=0;
for(i=0;i<len;i++)
{
x=x+a[i]+b[i];
if(x>=0 && x<=9) //sum of digits is a single digit num so there is no carry
c[j++]=x;
else
c[j++]=x%10; // sum is double digit last digit is kept
x=x/10; // to take out the carry
}
}
int main(void)
{
clrscr();
char s[2002];
int i=0,j=0,c1=0,c2=0;int num1[1001]={0}; int num2[1001]={0};
int ans[2002]; int len=0;
printf(" enter an addition expression....");
gets(s);
for(i=0;i<strlen(s);i++)
{
if(s[i]=='+') break;
num1[i]=to_int(s[i]);
c1++;//number of digits in first array
}
i++;
for(;i<strlen(s);i++)
{
num2[j++]=to_int(s[i]);
c2++;//number of digits in sec array
}
reverse(num1,c1);
reverse(num2,c2);
// the arrays are of unequal length so for digit by digit addition length of
//the larger array is considered
if(c1>c2)
len=c1;
else
len=c2;
add(num1,num2,ans,len);
//printin the result in reverse
for(i=len-1;i>=0;i--)
printf("%d",ans[i]);
getch();
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.