Thread: Adding two big numbers in c (help)

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    1

    Adding two big numbers in c (help)

    I need to rewrite a code(below) that adds and multiply two big numbers,it needs to have functions and strings as required by my teacher but i'm not that great with them so i went ahead and wrote the code without the functions,the thing is i need your help and rewrite it with functions (and keep it as loyal as the source),i appreciate all the help ya'll guys can provide me,thanks in advance.


    Here is the code without functions:

    Code:
     
    
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char str1[101];
        char str2[101];
        int i,j,k,sum,carry,l,a,b,c,d;
        while(scanf("%s %s",str1,str2)!=EOF)
        {
            a=strlen(str1);
            b=strlen(str2);
        long int str3[110]={0};
            k=0;
            carry=0;
            sum=0;
            for(i=a-1,j=b-1;;i--,j--)
            {
                c=str1[i]-'0';
                d=str2[j]-'0';
                if(j<0)
                {
                    d=0;
                }
                if(i<0)
                {
                    c=0;
                }
                if(i<0&&j<0)
                {
                    break;
                }
                sum=c+d+carry;
                str3[k]=sum%10;
                carry=sum/10;
                k++;
            }
        printf("sum = ");
        for(l=k-1;l>=0;l--)
        {
            printf("%ld",str3[l]);
        }
        printf("\n");
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Try your code with 9 and 1.

    BTW, your variable names are terrible!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numbers adding together = bad
    By Zyk0tiK in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 04:37 PM
  2. Adding big numbers
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 03:36 PM
  3. adding numbers
    By Allen_Buchannon in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 05:43 PM
  4. Adding two numbers
    By Maybe in forum C Programming
    Replies: 2
    Last Post: 03-11-2003, 01:55 PM
  5. adding odd numbers
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 09-06-2001, 01:44 PM

Tags for this Thread