C Programing Code Please Problem Statement In the C language, it is trivial to a
ID: 2085066 • Letter: C
Question
C Programing Code Please
Problem Statement 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 place 100's place 10's place Place place Carry Sum = N1+N2Explanation / Answer
The answer to this problem is given below: (The explanations are given in the comment structure in the C program. Hope this helps.
#include<stdio.h>
#include<string.h>
int main(){
int a[255],b[255],sum[255]; //assuming both the numbers can store upto 255 digits
int a1=0,b1=0,s1=0;
int remainder = 0,i;
char n1[255],n2[255],*p1,*p2;
printf("Enter first number : ");
printf(“ It should contain more than 100 digits ”);
scanf("%s",&n1); //first number from user in string format
printf(" Enter second number : ");
printf(“ It should contain more than 100 digits ”);
scanf("%s",&n2); //second number from user in string format
pa = n1;
pb = n2; //pointer to first & second number
//storing first string number in the integer array
while(*p1){ //loop while run till it get null character
a[a1++] = *p1++ - 48; //48 is ASCII value of chara1ter zero
}
//placing first string number in the integer array as per requirement
while(*p2){
b[b1++] = *p2++ - 48;
}
//now adding the two numbers
if(a1<b1){
for(i = a1;i > 0;i--){
sum[s1++]= ((a[i-1] + b[--b1]) + remainder)%10;
remainder = ((a[i-1] + b[b1]) + remainder)/10;
}
while(b1>0){
sum[s1++] = b[--b1] + remainder;
remainder = 0;
}
}
else{
for(i = b1;i > 0;i--){
sum[s1++]= ((b[i-1] + a[--a1]) + remainder)%10;
remainder = ((b[i-1] + a[a1]) + remainder)/10;
}
if(a1==0 && remainder==1)
sum[s1++] = remainder;
while(a1>0){
sum[s1++] = a[--a1] + remainder;
remainder = 0;
}
}
printf("Sum of both number is : ");
for(i=s1-1;i>=0;i--){
printf("%d",sum[i]);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.