i need the help of a turbo C master!!!!...one of my final exam problem in my subject has this problem:

The Department of mathematics has asked you to develop a program to help elementary students check their addition and subtraction computations. The program to be developed will ask the user for 2 numbers (n1 & n2) and displays the sum (n1+n2) and difference (n1-n2). The program stops executing when the user enters 0 for the 1st number.
Notice that the inputs of the user are very large whole numbers. One way to solve this is to use arrays. We assume that the user will enter positive numbers and not having more than 50 digits.

sample output:
1st number: 5281925125243
2nd number:1256972

Addition:
5281925125243
+ 1256972
-------------------------
5281926382215

Subtraction:
5281925125243
- 1256972
-----------------------
5281923868271
-------------------------------------------------------------------------------------
sample output:
1st number: 325124
2nd number:992102513985

Addition:
325124
+ 992102513985
-------------------------
99210284-109
Subtraction:
992102513985
- 325124
-----------------------
992102189861
---------------------------------------------------------------------------
sample output:
1st number: 0
(end of program)

Should we assume that you followed the rules, asked a specific question, and showed effort towards solving the problem? Or would you like the working code, freshly baked, straight from [Salem, Ancient Dragon, WaltP, Narue]'s oven? In the latter case you're more likely to get my stale humor instead.

:icon_rolleyes:

Comments
He/She did not help at all....

You will have to write your own functions to add and subtract strings because those numbers are too big for normal integers. The solution in Turbo C will be no different than for any other C compiler.

And don't ask me to do your homework without depositing a very very large sum of money (at least six figures) in my PayPal account.

Edited 6 Years Ago by Ancient Dragon: n/a

If you are having trouble getting started, think on the lines of linked lists. Store each number in 1 node of a linked list. So now the size of the number is not a criterion any more.
So for adding just scan both the lists and store the result in the third list. Take care of the carry though

Comments
Linked list -- good suggestion :)

Ok, get a pencil( or pen) and a paper. Then start adding two numbers.
This time instead of brainlessly adding, realize what you are doing in
each step. For example, when you add two single digit numbers, you
either get 1 single digit number as the answer or you get 2 digit number
as an answer. When would you get 1 digit number as an answer and when would you get 2 digit number as an answer when you add
2 single digit number.

I did try answering the problem, but as we know, integers have limitations (max of 32767)...so whenever i try to enter more than 5 digits..it's giving me a negative output...

then i tried using arrays (see code below)...it's still wrong...i am not an expert in turbo C...that's why I am asking anybody who's proficient in this language to help me and not to judge me...but if you're just gonna laugh at me...go ahead(I'm glad somehow I made you laugh)...i guess you don't know how to answer this problem as well ...anyway, thank you for you effort in reading this thread and bothering to write a comment.

#include<stdio.h>
#include<conio.h>


main()
{

	int n1[50],n2[50],num1,num2,index1, index2,x,fem,sem, total;

	clrscr();
	printf("\n Enter a 1st number: ");
	scanf("%d", &num1);
	printf("\n Enter a 2nd number: ");
	scanf("%d", &num2);
	index1=0;
	while(num1>0){
		fem=num1%10;
		num1=num1/10;
		n1[index1]=fem;
		index1++;
	}
	index2=0;
	while(num2>0){
		sem=num2%10;
		num2=num2/10;
		n2[index2]=sem;
		index2++;
	}
	if (num1>num2){
		total=0;
		for (x=index1; x>=0; x--){
			total= total+n1[x] +n2[x];
		}
	}
	else {
	total=0;
		for (x=index2; x>=0; x--){
			total= total+n1[x] +n2[x];
		}
	}

	printf("\n\t%d",total);
	getch();
	return 0;
}

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

Wouldn't it make more sense to put each digit of one number, into it's own element of one array, and do the same with the second number, into the second array.

Then, in a loop, starting at the one's (right hand column), start adding, and remember, any sum above nine, has a carry to the left adjacent column.

You can make elegant stacks, and use postfix notation (wikipedia for that), but just two arrays, being added or subtracted into a third, seems very intuitive for addition and subtraction. Can't you just see that picture?

@romarie: we already told yuu integers will not work because they can not hold large numbers. Either use a character array or a linked list of digits, both have already been suggested.

>>but if you're just gonna laugh at me...go ahead(I'm glad somehow I made you laugh)...i guess you don't know how to answer this problem as well

On the contrary, we are not laughing at you. In fact, I wasn't even
laughing. I apologize if my post sounded offensive, it was not meant to be. What we are trying to tell you is that you can add 2 large numbers
just like you would add it in pen and paper.

For example :

123
+456
----
579

what you did there was add the last digit of both numbers. Then since
it was below 10, we did not carry it to the next number so the result, 9
is our last number of our answer. Then we added the middle number 2 and 5. Thus 2 + 5 = 7. Since this is not over 10, we don't carry. So 7
is our middle number in our answer. Finally we add 1 and 4 to get 5.
Since this does not exceed 10. We don't need to carry, thus 5 is our first
digit. And our answer is 579.

The main thing we did there is start at the last digit of each number
and add them and move left until we reach the first position. This
essentially is the idea.

Instead of using int to hold a number, you can use a integer array
to hold each digit. For example, the number 123 can be stored in
an int Array[3] = {1,2,3}. It is convenient to use Arrays since we can
access the last digit by saying ( for this case ) Array[2]. That access
the value 3. Here is an example that might make it clearer.

int num1[4] = {1,2,3,4};
int num2[4] = {4,3,2,1};
int result[4] = {0};

//now we add num1 + num2, which is basically 1234 + 4321 = 5555
//usually done with for loops... but I am trying to show something here
result[3] = num1[3] + num2[3]; // 4 + 1 = 5
result[2] = num1[2] + num2[2]; // 3 + 2 = 5
result[1] = num1[1] + num2[1]; // 2 + 3 = 5
result[0] = num1[0] + num2[0]; // 1 + 4 = 5

print(result); // prints 5555

You see thats the main idea. The only thing you have to worry about
is the carry when you add. For example 55 + 45 = 100.
You see 5 + 5 = 10. So we carry a 1. You have to take care of that
case in your code. There are also some other cases you might observe
while you code this.

@ first person:

thank you for the immediate response( i really appreciate it)..i will try again solving the problem using your codes...i will post the codes if I get to run the program correctly...

This article has been dead for over six months. Start a new discussion instead.