0 like 0 dislike
11.9k views

You are requested to help in writing a program to perform addition of two large number up to 50-digits. In C there is no builtin datatype for large number, but we can use characters and array to solve the problem.

Input: The input will contain two positive integers are separated by a blank, the two positive integers do not exceed 50-digits.

Output: Sum.

寫一個程式輸入兩個大數(最大50個位數),計算其總合。C語言不提供大數資料型態,但我們可以用字元和陣列來解決這個問題。

Example input:

999999999999999999999999999999 999999999999999999999999999999

Example output:

1999999999999999999999999999998

 

[Exercise] Coding (C) - asked in Chapter 13: Strings by (5.2k points)
ID: 40714 - Available when: 2018-01-04 18:00 - Due to: Unlimited

edited by | 11.9k views

44 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>
#include <string.h>

int main(){
** *** ****** * a[50],b[50];
*** * * * ** ******** * ** * * c[51]={0},i,d,e=0;
* * ** * * *** **** * * * %s",a,b);

* *** * * *** * * ** * * *** i<strlen(a); i++)
** *** ** * * * ** * * * ** *** * = a[strlen(a) - 1 - i] - 48;

* * * **** * ** * i<strlen(b); i++)
* * *** ** **** *** * ** ** ** * ** ** * ** *** * += b[strlen(b) - 1 - i] - 48;

**** * *** ** ** ** * * ** i<50; i++)
* ***** * *** * * * *
* * ***** *** *** * ** ***** ** ** *** *** * > 9)
* * ** * ** * ***** * * ** * ** ** * * *** **
** * ** * ** * ** *** *** ** * **** ** * ** * ****** * ** * * ** ** ** * **
**** * * * ** * ** * * * **** ** * * * ** * * ******* ** *** ***** * * = c[i]%10;
* ** * ** * ******** * * * *** **
* ****** * * ** * * * * * *

**** ** *** ** * * * * **** > strlen(b))
* * *** * ** * ****** * *** ** * ** ** * * *** * = strlen(a);
**** ** * * ** * ** *** *****
** ** ** * * **** * *** ******** * = strlen(b);

* * *** **** * * * ***** * * * * **
** ** ***** * * ** * * * ** ** ** * ** ** ** * * * ******* ** * * * *
* ** ** ** * ** ***
}
answered by (-498 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>

int main(void)
{
** ** *** * *** a[50],b[50];
* ** * ** * * ** ** * i,c[51]={0},sum, zero=1;

*** * * **** ** * *** %s",a,b);

** * ** * ** **** * * ** * *** ** ** * *
** * **** * * * * *
* ********* * * * *** **** ** * * * * * ** * * = a[strlen(a)-i-1]-48;
    }
* *** ****** **** *** * *** *** * ***
    {
** * * * ** ** * * * * *** ** ** ** ** * * += b[strlen(b)-i-1]-48;
    }
* ** ** ** *** **** ***** * **** * i<50; i++)
** * * * * * * ***
* * * *** ** ** ** * * ** ****** ** ** * ** * ** * **
* ** **** * ** * * *** ***** *** * * ** *****
*** ** * * *** *** ** * **** * ** ** ****** * **** ****** * += c[i]/10;
* * * ** ** *** * *** *** ** * ******** * ** ** * * * = c[i]%10;

***** * ** ** *** ** * ** * * ** *** * *******
*** * ** *

**** * * *** * ** * * * ** == 0)
** ** * *** * * * ** ** ** * * ** * ** * = 1;
**** * ** * * * * ***** ********** i>=0;i--)
*** ***** * * * * * ** *****
* * ***** * ***** * * **** * ** * ** *** *** != 0)
**** ** * **** * * ***** * * * ** **** **
******** * ** ** * *** ** * * *** ***** * ** * * * *** * * *** * * ** *** = i;
***** * * ** *** * **** * **** * ** ****** * ** * ***** * *** **
**** ** * * * *** * * ** * * **
** ****** * * ** ** ** **
* ***** * *** * * ** ** * i>=0;i--)
    {
***** ** * ***** * *** * ** * ** * ** *** * c[i]);
**** ** **** ** * ** **** *


***** ** * **** ** ** ** * 0;
}
answered by (-281 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main()
{
***** *** * **** * * * * A[51],B[51];
*** * ** ** ***** ** ** * l1,l2,sum[52],a[51],b[51];
*** * * * * ** ** ** *** ** %s",A,B);
* * * * ** * *** ** * (l1=0;A[l1]!='\0';l1++)
* * * * * *** * ** * *** ** * *
* ** ** * ***** * *** ** (l2=0;B[l2]!='\0';l2++)
** * * **** ** ** * * ** * * **** ** * * ** *

* ** **** * ** * *** carry=0,k=0,i=l1-1,j=l2-1;
* * ** * ** *** * ** (;i>=0 && j>=0;i--,j--,k++)
    {
** **** * * ** *** * * * * ** ***** * * * * * ** *
*** * * ** **** ***** * ** * ** *** * * * *** * ** **
    }
* *** * * ** ** * *** * (l1>l2)
    {
**** * ** * * *** * ***** * * ***** ** * * * * (i>=0)
* * * * ** * * **** * * ** * ** **** ** *
** * * *** * ** ** **** * ****** * * ** * ** * ** ** ** ** * * ***
** * * ** *** ** ** * * *** ***** * ***** ** * * * ** *** ***** * * **
* * * * *** * ***** ** * ** *** *** ** *
* * * * ** * **
** ***** ** ** * * if (l1 < l2)
** * ** * * *** * *
** ** **** * *** ** ** *** * * ** ** * ** * * * *** (j >= 0)
*** * * ** ****** *** ** * **
***** * * * * * * * * * *** * * **** * * ******* * * ** **** ** * = (b[j] + carry) % 10;
* ** * *** * * * * * * ****** * *** **** * ***** ****** * * = (b[j--] + carry) / 10;
** * * * * * ** * * ** * **** **
** * ***** * ** ** *
* ***** ** ** *** * *
** *** *** * *** * **
*** * *** * * * *** * ** *** ** **** *** (carry > 0)
* * ** * ** * * * ** * ** *** * * *** ** *** * = carry;
* *** * * * *
* * ** **** * * * ** ** ** * (k--; k >= 0; k--)
* *** * ** * * * *** * * ** * ** *** * * *** * **** ** **** * sum[k]);

** * **** **** * *** * **** 0;
}
answered by (-249 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>

int main(void)
{
** * *** * ** * ***** * a[50],b[50];
* **** * * * ** ** ** ** i,c[51]={0},sum;

* ** * ** * ** * *** ** %s",a,b);

* ** * ****** ** *** ** *** * * **** ** * ****
* ****** * ** * * **
** ***** ** * *** * * ** * * * ********* * = a[strlen(a)-i-1]-48;
* *** ***** * * ****
******* ** ***** * * ** * * *
**** **** ** * ** *** * **
****** *** * ***** * * * * ** * * * ** * * += b[strlen(b)-i-1]-48;
** * * * * * * * *
**** * * * *** *** *** * ** * i<50; i++)
* ** ******* ** * *** *
******* *** *** **** *** * *** * * * * * ****** *
* * ** ** ** ** ** **** * * **** * ** * **** ** *
** * ** ******** ** * ** * * *** ** *** ** * * ****** *** ** * * ** * += c[i]/10;
*** * ****** ***** ** * *** * * **** * *** * ** * * * * = c[i]%10;

* ** ** *** * ** ** * ** * *** ****** * *
** * * ** * * * * *
** * ** ******* * * i>=0;i--)
    {
* ****** * ** ** * * ***** * *** * ** ** **
* ** ** ********** * * * * **** ** **** ** * ** ** *** *** * * * **** **
    }


* * *** * * *** * * * * 0;
}
answered by (-281 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include <string.h>

int main(){
****** ** * * * * * * ** a[50],b[50];
* * *** ** * * c[51]={0},i,d,e=0;
* * **** **** * **** ** ******* %s",a,b);

** ** ****** ** *** * * ** i<strlen(a); i++)
***** *** * * ** ** ** * * ** * * ** * = a[strlen(a) - 1 - i] - 48;

* ** *** * * ** **** ** i<strlen(b); i++)
* ** ** * **** ** ** ***** * ****** ** * * * **** += b[strlen(b) - 1 - i] - 48;

** ** ** * ***** *** ** * i<50; i++)
* ** * * * * * * * * *
** * * * ** * ** ** ** ** * ** * * ****** *** ** > 9)
* ** ** * ***** ** *** ** * * * ***
** *** * * * ** * * ***** **** ** ******** * ** *** * ** ***
* * * ** **** *** * * * * *** * * * * ** ****** *** * ** * = c[i]%10;
* * * *** ** *** ** ** * *** ** * * * * * * ** *
*** **** ** ** **
** * * ** *** ** ** * ** *** * ** *
***** * * * * ** * * *** * * ** * * * * **** ***** * != 0)
* * * **** *** * * * **** * * **** * * * *** * ** * * ***** * * ****
** * * ** * ** ** *
}
answered by (-498 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>
#define N 50

void add(int a[N], int b[N], int c[N])
{
* *** * ** *** * * * * * **** *** * i = 0,carry = 0;
** * ** *** ** ** ******* * * * * ** = N-1; i >=0; i--){
* **** * *** * * * * ** ****** * ******** ***** * ** * * * ** **** *** **** * ** = a[i] + b[i] + carry;
*** ** * *** ** *** ** ** * * ** * * * ** * * *** ** * * ** *** *** * **** = c[i] / 10;
*** **** * * ** * * ****** *** ** * **** ** *** * * * * *** **** **** ** * ** ******* **** %= 10;
** * * * * * * * * * * ** ** ** ***
}

int main(){

***** ** * *** * * * ** ** * * *** *** * ** * * * *
* * ***** * * * a1[N]={0},b1[N]={0},c[N]={0};
** * * ** * ** ** i=0,j=N-1,first=0;

* * * ** * * * ** ** * ** * %s",a,b);
* * *** ** * * *** len=strlen(a);
*** ** *** * * * * **** (i=(len-1);i>=0;i--){
**** ** * * * ***** * * *** * * * * ** * ** ** * * ** * *
* ***** * ** ** *** ** ** **** ** * * * * ***** * *
** ***** ** * * * * ***** ** * ** *** * *
* * *** *** * ** ** ***

** * * * ** * * ** *** * **
* ** * ****** * * *
** ** * * ******* * * (i=(len-1);i>=0;i--){
* * *** * * * ***** ******** * ** ** **** ** ** ** **** **** * *** * **** **
* ******* * ** * * ** **** *** * ** *** ******
* * ** *** ** ****** * * * * *** ** **
    }

** ** * * * **** ***

** * * ****** ** (i=0;i<N;i++)
    {
* *** *** **** *** *** ** * ** * ***** * ** ***** (c[i]==0 && first==0)
*** ** * ** ** **** * * **** ******* ***** ** * *** * ** **** *
*** *** ***** * * **** * * *** * *** * * * ** *
** * * * ** * * ** * ** ***** ** * *** ** * *** * * ** ***

* *** ***** ** ****** ** ** **** **** * * * * (first==1)
* ** ** * * * * * * ** * ** ** ** * * ** ** * * * ** * * * * ** * * ** * * **
    }
}
answered by (52 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#define N 50

void add(int a[N], int b[N], int c[N])
{
** *** * * ** ** ***** * * *** *** * * ** * i = 0,carry = 0;
** ** ** ** * *** * * * *** **** ** ** * = N-1; i >=0; i--){
* * * ** ** * ** * * * *** * * *** ******* * ** *** ** ** ***** * ** * *** * *** * = a[i] + b[i] + carry;
** **** *** **** ** * ** * ****** * ** * ** ** * * * * ** * ** * ** ** * * **** = c[i] / 10;
**** * *** *** * ********* ** ** * *** * * * ****** ** ***** **** * * ****** * ** * *** %= 10;
* * * *** ** ******* *** ** * *** *
}

int main(){

* **** * * ** ** * ** * * * * * * ******* * * * *** *
* * *** **** **** *** * * * a1[N]={0},b1[N]={0},c[N]={0};
*** * * ** * **** i=0,j=N-1,first=0;

* * ** * * *** * * * * * * * * * %s",a,b);
*** *** * * * *** * ** len=strlen(a);
** ** * ** * * * (i=(len-1);i>=0;i--){
** * * **** * * ** * *** * * * ** * * ** * ******** ** *
******** * *** * * * * *** * * * ** ** *** ** ***
** ** ** ** * ** ** * **** * ** * * * **
    }

******* * * **** * * *** **
* * ** **** * ** * **
*** **** **** ** ** (i=(len-1);i>=0;i--){
** * ** ** * ***** ** *** * * * * ***** *** * * **** ***** * * **** ** *****
** ********** ** * ** * ** ******* *** ** * *
* * * ** ** * * *** *** ** * * * * ** * * *
    }

** * ** * ***** ** * ** * *** *

* * * ** * * * (i=0;i<N;i++)
    {
* * * ** * ****** ** *** * * * ***** (c[i]==0 && first==0)
**** * **** * ** *** ** * * **** * ** ** * * * *** * * * *
* * ** * ** ** *** ** * * * ***** ** * **
** ** **** * * *** * ** * * ** *** ** * **** *** * ***** ** *

* ** * *** *** *** * **** * *** ** ** * * * (first==1)
** * * * *** ******* *** * * * *** * ** *** *** * ** ** **** * * * * * *** * **** ** *
    }
}
answered by (52 points)
0 0
prog.c: In function 'main':
prog.c:21:13: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     int len=strlen(a);
             ^~~~~~
prog.c:21:13: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:21:13: note: include '<string.h>' or provide a declaration of 'strlen'
0 like 0 dislike
Hidden content!
* ******** *** **
*
* * ** ** ***
* ** ** ***
** *
*** * * ***
* * * *** **** * ** * *

** *** ** * * ** **

** * * ** * * * **
* * * * * *

** ** * * * **
** ** *

** * **
**
* *
** j = * - *
* * i ** * 0 * ** * j ***** ** {
* ** = *** + * * * *
** * * ** * *

** * * **** **
** * * ** {
* * * * * **
** * * ** / **
*
* * * * {
* * * * * {
* *** = ** ** + ** * % *
*** * * * *

* *
* * * * *
* * * **

* * * ** **
* * * *** ***** * *
* ** *
**
answered by (-32 points)
0 0
prog.c: In function 'main':
prog.c:6:9: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[255]' [-Wformat=]
 scanf("%s", &s1);
         ^
prog.c:7:9: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[255]' [-Wformat=]
 scanf("%s", &s2);
         ^
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>

int main() {
    char A[50], B[50];
    int i, k, tmp=0, sum[50];
    for(i=0;i<50;i++) {
***** * * ** * ** **** ** ** ** * * *
* * ***** * * ***** **** ** *** ****** * ***
* * * ** ** * * *** ** ****** * ** * *** ***
    }
** ** * * ** * ** * ** * ** * *** A);
** * **** * * * * * ** ** ** *** * B);

    for(i=49;i>=0;i--) {
****** ** ** * * **** * ****** * ** ** ** **** *** {
* * **** * * *** * * *** * * ** * * ***** ** *** * ** * ** * {
*** * ** *** ****** * *** *** * *** *** *** * **** * * * * ***** * * * ** *** *** **** * * **
**** * ** * * * * **** * ** ** ***** * * *** *** ** ****** **** ** * *** * * **
** ** * * *** ** * * ** ** * ** ****** **** * * ** ***** ** **** * * * * ** ** * * ***
* ** *** ** ****** ** * **** ** * * ** * * * * * * *** * *** * *** ****
*** **** **** * ** * * * **** * ** * ** *
** *** ** ***** *** ** * *** **** ** * ** *** ** * {
* * **** *** * * *** *** * **** ** ** * * ** * *** ** *** * * * * ****** * {
** * * *** ** * * * * ****** ***** * * ** * * *** ** ************* * **
* * * * ** ** * ** ** *** * ** ** **** ***** ***** * * * * *** * *** *** **
* *** * * ** ** * *** ** *** *** * ******* * * **** **** * ** * * * * * ** * **** ** *
*** * ** ** * **** * *** * **** *** * * **** * * * * **** **** * **
* * * ***** * * * * **** *** ******** * *
    }

    for(i=49;i>=0;i--) {
** ** ***** * * * *** * * * * ** * ** * {
* * * * ******* ** * ** ** ** ** ** **** * ** **** * **** * *** ** * *
*** ** * ** * ** * ** *** ** ** * * ** *****
* ** * * * ** ** * ** *** **** *** * ********* ** * *** {
* * * * * * **** **** * * * *** ****** * * *** * ** ** * *** * *
* * **** *** * ** * ** *** * * ** * ** * * * *
    }

    for(i=49;i>=0;i--) {
* ** * * * * * * **** ** *** * * ** * * *** * * * * ** * * * {
* * * * ** * ****** * ** * * * * ** **** ** **** * ** ** ** ** * * * *
* * * ** ** * * ** * * * *** * *** *** *
* * * * * ** ** * * **** **** * ** **** * if(A[i]+B[i]+sum[i]>=10) {
* ** ******* ** * ** ** * * * * ** * ** * ** ***** * ** ** * **** * * * * *
** *** **** ** * ** * * * * ** * ** **** * * * * ** * *** ****
* * **** *** * *** * *** * ****** * **
    }

    for(i=0;i<50;i++) {
** ** * ** *** * ** * * * **** * * * *** ** {
** * ** *** * ** * *** * * **** ** *** ** * * ****** ***** ** ******
*** ** * * ** * ** * ** * * ** ** ** ** ** *
    }
    for(;i<50;i++) {
* * * *** ***** * ** ** * ** ** * **** * * *** **** *** sum[i]);
    }
}
answered by (-120 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include ** * *
#include * * ** *

#define LEN 51

int * *** n[])
{
* * * ** * ** tmp[LEN];
** * ** **** * i,m;
* **** * * * *** **  { **** *  }
*** ** ** ** ** ***** * ** ****  { ********* -1;  }
**** ** *** * * **
* *** ** * ** ** *  { ** ** * ** ***  }
}

void ** * * * n[])
{
* * * * i,p,f=0;
* *** ***** ** * * **
* * * *** * ** * *** ** ** ** ***** ** ** * **  }  }
** ** * ***** ** *
* ** * * * **** * * ** * * ********* *** ****  }
* * * * * **  { * * *** * * * * * ***
}

void *** * * a[], char b[], char c[] )
{
****** * *** i,tmp=0;
* *** * **** * ****
*** ** * *** * **
* * ***** * * * * * **** *
* * * *** * **** ** * ** * ** * *** *** * * * ***  }
** * * ** ***** * * **** tmp=0; * * * *
*** ** * * *
}

int main(){
* **** * * * * * ** * * * *** *****
** *** * * ** * ** ** * ****
* ******* ****** * * *** * * **
** ** *** ** * **** * * * ***** **
** *** * *** * * **
** ** * * ** *** *** ** * * 0;
}
answered by (-116 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.159.115.9
©2016-2026

Related questions

1 like 1 dislike
85 answers
[Exercise] Coding (C) - asked Oct 26, 2017 in Chapter 6: Loops by semicolon (5.2k points)
ID: 28914 - Available when: 2017-10-26 18:00 - Due to: Unlimited
| 21.8k views
12,783 questions
183,442 answers
172,219 comments
4,824 users