C Program for Adding Two Large Numbers as Strings

This C program reads two large numbers represented as strings, then adds them together digit by digit. It handles carry-over and outputs the result. It includes error handling for commas in the number strings. Perfect for understanding string manipulation in C!
mail@pastecode.io avatar
unknown
c_cpp
12 days ago
3.3 kB
4
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char r[200];
int main(void)
{
    int i, j;
    char n1[150], n2[150];

    scanf("%s", n1);
    scanf("%s", n2);

    int len1 = strlen(n1);
    int len2 = strlen(n2);

    int d1 = 0;
    if(len1 == len2)
    {
        int n = len1;
        for(i = n-1;i >= 0;i--)
        {
            int c = 0;

            if(n1[i] == ',')
            {
                r[i] = ',';
                continue;
            }
            else
            {
                c = n1[i] - '0' + n2[i] - '0' + d1;
                //printf("%3d", c);
                r[i] = (c % 10) + '0';
                d1 = c/10;
            }

        }

        int sum = 0;
        for(j = 0;j<3;j++)
            {
                if(r[j] == ',')
                {
                    sum = 1;
                    break;
                }
            }


        if(d1>0)
        {
            //printf("%d\n", sum);
            if(sum == 1)
                printf("%d%s\n", d1, r);
            else
                printf("%d,%s\n", d1, r);
        }
        else
        {
            printf("%s\n", r);
        }
    }

    int s1 = len1 - len2;
    int d2 = 0;
    if(len1 > len2)
    {
        for(i = len2-1;i>=0;i--)
            n2[i+s1] = n2[i];

        for(i = 0;i<s1;i++)
            n2[i] = '0';
        //printf("%s", n2);

        int n = len1;
        for(i = n-1;i >= 0;i--)
        {
            int c = 0;

            if(n1[i] == ',')
            {
                r[i] = ',';
                continue;
            }
            else
            {
                c = n1[i] - '0' + n2[i] - '0' + d2;
                //printf("%3d", c);
                r[i] = (c % 10) + '0';
                d2 = c/10;
            }

        }
               
        int sum = 0;
        for(j = 0;j<3;j++)
            {
                if(r[j] == ',')
                {
                    sum = 1;
                    break;
                }
            }

        if(d2>0)
        {
           if(sum == 1)
                printf("%d%s\n", d2, r);
            else
                printf("%d,%s\n", d2, r);
        }
        else
        {
            printf("%s\n", r);
        }
    }


    int b2 = len2 - len1;
    int d3 = 0;
    if(len1 < len2)
    {
        for(i = len1-1;i>=0;i--)
            n1[i+b2] = n1[i];

        for(i = 0;i<b2;i++)
            n1[i] = '0';
        //printf("%s", n2);

        int n = len2;
        for(i = n-1;i >= 0;i--)
        {
            int c = 0;

            if(n2[i] == ',')
            {
                r[i] = ',';
                continue;
            }
            else
            {
                c = n1[i] - '0' + n2[i] - '0' + d3;
                //printf("%3d", c);
                r[i] = (c % 10) + '0';
                d3 = c/10;
            }
 
        }
        
        int sum = 0;
        for(j = 0;j<3;j++)
            {
                if(r[j] == ',')
                {
                    sum = 1;
                    break;
                }
            }

        if(d3>0)
        {
            if(sum == 1)
                printf("%d%s\n", d3, r);
            else
                printf("%d,%s\n", d3, r);
        }
        else
        {
            printf("%s\n", r);
        }
    }

    return 0;
}
Leave a Comment