a program of substraction

J

Jenny

/*I made this program to do substraction between two integers
whose digits are beyond the limit of a long int on my system
but the program can only execute within 10 digits.What's more only
under the condition "i>j"and "i==j"does the program work
successfully..Why?Can you help me?
Thanks a lot */

#include<stdio.h>
#include<stdlib.h>
int remainder[21],remainder1[21],remainder2[21];


int main()
{
long integer1,integer2;
int i,j,n;


scanf("%ld%ld",&integer1,&integer2);

for(n=1;n<=20;n++){
remainder1[n] = integer1 % 10;
integer1 = (integer1 - remainder1[n]) / 10;


remainder2[n] = integer2 % 10;
integer2 = (integer2 - remainder2[n]) / 10;
}

for(i=20;remainder1 == 0;)
i--;

for(j=20;remainder1[j] == 0;)
j--;

if(i>j){
for(n=0;n<=20;n++){
if(remainder1[n]<remainder2[n]){
remainder1[n]+=10;
remainder1[n+1]-=1;
}/*end if*/
remainder[n]=remainder1[n]-remainder2[n];
}/*end for*/

for(;i>=1;i--)
printf("%d",remainder);
}/*end if*/

if(i==j){
for(;remainder1==remainder2;)
i--;
if(remainder1>remainder2){
for(n=0;n<=20;n++){
if(remainder1[n]<remainder2[n]){
remainder1[n]+=10;
remainder1[n+1]-=1;
}/*end if*/
remainder[n]=remainder1[n]-remainder2[n];
}/*end for*/
for(;remainder==0;)
i--;

for(;i>=1;i--)
printf("%d",remainder);
}/*end if*/

if(remainder2>remainder1){
for(n=0;n<=20;n++){
if(remainder2[n]<remainder1[n]){
remainder2[n]+=10;
remainder2[n+1]-=1;
}/*end if*/
remainder[n]=remainder2[n]-remainder1[n];
}/*end for*/
for(;remainder==0;)
i--;

printf("-");

for(;i>=1;i--)
printf("%d",remainder);
}/*end if*/
}/*end if*/


if(j>i){
for(n=0;n<=20;n++){
if(remainder2[n]<remainder1[n]){
remainder2[n]+=10;
remainder2[n+1]-=1;
}/*end if*/
remainder[n]=remainder2[n]-remainder1[n];
}/*end for*/

for(;j>=1;j--)
printf("%d",remainder[j]);
}/*end if*/

return 0;
}
 
J

Jenny

Jenny said:


Yeah, it has the same problem as your addition program.

See my reply re your addition question in acllcc++.

If you must post the same article to more than one group (rarely a good
idea), cross-post rather than multi-post.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Yeah,may be you're right.But today I've just finish another two
programs(substraction and mutiplication),again and again I almost met
the same problem,so I make multi-post.I think in this way i will have
a more clear idea of my mistakes.next time I won't go wrong again.
I need your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top