a program of substraction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jenny

    a program of substraction

    /*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..W hy?Can you help me?
    Thanks a lot */

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


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


    scanf("%ld%ld", &integer1,&inte ger2);

    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;remain der1[i] == 0;)
    i--;

    for(j=20;remain der1[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",rem ainder[i]);
    }/*end if*/

    if(i==j){
    for(;remainder1[i]==remainder2[i];)
    i--;
    if(remainder1[i]>remainder2[i]){
    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[i]==0;)
    i--;

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

    if(remainder2[i]>remainder1[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(;remainder[i]==0;)
    i--;

    printf("-");

    for(;i>=1;i--)
    printf("%d",rem ainder[i]);
    }/*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",rem ainder[j]);
    }/*end if*/

    return 0;
    }
  • Richard Heathfield

    #2
    Re: a program of substraction

    Jenny said:
    /*I made this program to do substraction between two integers
    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

    Comment

    Working...