Page 1 of 1

## 1 Replies - 10527 Views - Last Post: 02 June 2007 - 07:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=28866&amp;s=5b8fe4125115f3c024b46c634f6a7978&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 momina.k

Reputation: -1
• Posts: 3
• Joined: 19-April 07

Posted 02 June 2007 - 03:26 AM

this is my assignment
Write a computer program that asks the user to enter two numbers and print their sum.

Input
The input will be two numbers A and B.
0 < A,B < 9999999999999999999999999999999999999999.
Hint: There is no datatype to support 40 digit integers so use character arrays instead of integers.
Output
The output will be the sum of A and B.

Sample Input/Output

Input Output
A: 1111111111111111111111111111111111111111 3333333333333333333333333333333333333333
B: 2222222222222222222222222222222222222222
A: 1021 1031
B: 10

i have written the following code:
```#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
char a[40],i=0,d,f,sum[40];
char b[40],ch;
int n,m;

clrscr();

printf("enter numer of integers in A, the number should be less than 40");

scanf("%d",&n);

do
{

ch=getch();

if (ch>= '0' && ch<= '9')
{
a[i]=ch;
printf("%c",ch);
}
i++;
}
while(i<n);
printf("\n");

printf("enter numer of integers in b, the number should be less than 40");
scanf("%d",&m);

for (i=0;i<m;i++)
{
ch=getch();
if (ch>= '0' && ch<= '9')
{
b[i]=ch;
printf("%c",ch);
}

}

printf("\n");
for(i=0;i<m;i++)
{
for(i=0;i<n;i++)
{
sum[i]=a[i]+b[i];

printf("%c",sum[i]);

}
}
getch();
return 0;
}
```

it doesnt give the desired output,,can u plz explain how to add char arrays,,
i'll be very thankful.

Is This A Good Question/Topic? 0

### #2 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

Posted 02 June 2007 - 07:43 AM

First off the values of the characters '0'-'9' do not really add well this way. Make sure you convert to and from numbers vs characters.

Value of digit = char - '0';
char of digit = value of digit + '0';

with out this conversion '0' + '0' = 48 + 48 = 96 not 0

but '9' - '0' = 57 - 48 = 9 which just happens to be the tab character.

Your program can not handle carries. for example 9 + 9 = 18 but if you tried 99 + 99 in your program you get 1818

```//Make sure you initalize carry to 0 before you enter the loop
//then inside the loop add in logic to take the carry into account.
Sum[i] = a[i] + b[i] + carry;
carry = Sum[i]/10;
Sum[i] = Sum[i] % 10;
```

Your input routine needs some work. I can input "12 I HATE MATH 99" and it will have a string whose first tow chars are "12" and whose last two are "99" but there is no telling what is in between since the program just skips over them.