Implement a C function that can perform addition of two 15-digit (positive) integers. As in C the maximum integer number that can be stored in memory is 2147483647 it is not possible to use primitive data types in order to add very large whole numbers. For this reason, you are required to represent very long integer numbers using arrays of chars and then implement a function that can add two such numbers.

Can any one please help me !

Recommended Answers

All 10 Replies

Hi doris welcome to Daniweb. :)
Take a pencil and a paper.
Write two arbitrary large numbers on it and notice what you do when you add them together.
For your program:
Drop those numbers in two arrays. Start by doing the same as on paper. Take the last digits of the arrays (the units) add them and place the result in a new array. If the sum gets bigger than 9 take a carry.Etc. with the next last digit.
Success.

I dont get it sorry :(

What part do you not understand?
I assume you can add two numbers together on a piece of paper, can you?
Now do the same in your program.
Start with for example:
char number1[] = "123456789123456789";

but the characters can not be defined no? if i do chat num1[]=123456789123456789 doesnt that mean my answer has to come 123456789123456789 ?

Don't forget the quotes and the semicolon.
The statement says, define a character array of 18 chars;
Because indexing an array goes from 0 to 17 in this case number1[5] would be equal to the character '6'

so i define it as char[18]; and the user can then input any number he wants ? or shall i define it as char[]="123456789123456789"; and the user can still input any number they want ??

my problem is that i do not know how to write an array of characters as i was sick for that lecture

My example showed a string literal.
If you want to input a string, you could read this example

Use long variables.
long i,j,result;
i=j=999998888877777;
result=i+j;

printf("%ld\n",result);

What he was trying to say was that you can add larger amounts of any given data and pass it to an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.