0 like 0 dislike
12.3k 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 | 12.3k views

44 Answers

0 like 0 dislike
Hidden content!
** * ** * * * *
** ** ** * *
int main()
{
* * ***** ******* ** ** * i,alen,blen, d;
** * * ** * * ** * * a[50],b[50];
*** * * ** *** * *** sum[51]={0};
* * * * * ** **** **** * * ** %s",a,b);
** **** **
* *** * * *
* * * ** alen;i++)
{
* ** * **** * ******* - i - 1]-48;
}

**** *** blen;i++)
{
****** ** * ** ** * *** * * - i - 1]-48;
}

for(i =0; i<50; i++)
{
** ** * *** * *** * ** * (sum[i]>9)
* *** ** * * ** * * *** ****** ** * *** ***** * * ** ***
** ******* **** * ** * ** ** *** ****** * ** * ****
******* * **** **** * **** ***** * **** ** **
}

*** * * ********
**** **** ** **** !=0)
*** * **** * **** * *** *
** ** * * * ***** * * **** * *** ** ** *** * = i;
* ***** * * ** * ***** ** ** ** ****
* **** * * * * *
}

for(i=d; i>=0; i--)
* ** * ** * **** ** * ****** ***** ** sum[i]);


}
answered by (-336 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
* *** * *
**** ** * * * *
int main()
{
*** * ** ** ** * * * * ** * i,alen,blen, d;
* * * *** ******* * **** a[50],b[50];
* * *** * ** * ** * sum[51]={0};
** *** * *** * *** * * * *** ***** * **** ***
* ** * * ******
* ** ** *
for(i=0;i< alen;i++)
{
** *** * * * * ** *** * - i - 1]-48;
}

for(i=0;i< blen;i++)
{
** * * * ** * * * ** **** ***** - i - 1]-48;
}

for(i =0; i<50; i++)
{
* ** ** * * * * * (sum[i]>9)
* ** *** ** * * ** ** ***** * *** ** * ** * ** * **
***** ***** ** * ** * ** ** * * * ** *** * **
* * ** *** ** * * ** * ******** **
}

** * * ** *** *
*** **** *** * ** * *** !=0)
** * ** ** * ****** * *
* ** **** *** * **** *** * ** * ******** * ** ** = i;
* * * ** ** * ****** * *** * *** *
** * * * ** * ***
}

for(i=d; i>=0; i--)
*** * * *** ** ** * * * ** * * ** sum[i]);

}
answered by (-336 points)
0 0
prog.c: In function 'main':
prog.c:8:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[50]' [-Wformat=]
     scanf("%s %s",&a,&b);
             ^
prog.c:8:16: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[50]' [-Wformat=]
     scanf("%s %s",&a,&b);
                ^
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
#define N 50
int main()
{
** *** * * * * ** ** a[N];
** ***** * * ** * * * *** b[N];
* * ** * ** * ** ****** * a1[N]={0};
* * **** * * * * *** * *** * b1[N]={0};
*** ** * * ** ** c[N]={0};
** * *** * ****** * * i=0;
** * ** * **** **** * j=0;
* ***** * ** *** * * * *** * * ** ** *** **
** ****** * * **** *** * len1,len2;
* ** * * ** * * ** ** * * ** **
* * * * ** ** ** ** * *** *
* * ** ** ** ** * ** **** ** * * ** ** **

* * *** ** * *** ** *
* ****** *** * * * * ** (i=len1-1;i>=0;i--){
* ***** ** **** ** ** ** *** ** ** * ** * ** *** * * * ***
* *** * * * ** * * ** ** *** * * ** * * ** * ** ** *** ******
* * ** ** ** * * ***** * ** * * **** * * *** ***** *
    }
* * * **** * ** * * *
**** ** * * * ** ******* ** (i=0;i<N;i++){
*** * * *** * *** * ** ** * * * ** ********* * *** ** * * *
    }
* *** * ******* * **
* ****** * **** **** ** * **
* ** * ***** ** ** * (i=len2-1;i>=0;i--){
* * * * * ** ** ** ** **** *** ** ** * ** ** ** * ** * * * * * *
* * * ** * ** * * * * * ****** * **** ** * *** *
* * * * * ** ** * * * * * ********** ** * *
    }
** ** * *** * ** * * **
*** * ** *** ** * (i=0;i<N;i++){
* * * ****** * **** * * * * ** *** * ** * * **** ** * *** *
    }
* *** * * **** ** * ** *
* ** *** * * * **** ** * carry=0;
* * ** ** * * * * *** (i=N-1;i>=0;i--){
* * * * **** **** ** * * * * ** **** * * * ****** * *****
** *** * * * * ** * ** **** * * * * * ****** = c[i]/10;
*** * * ** * ** ** ** ****** ******* ****** c[i]%10;
    }
* *** ** ** ** *** ** ** * first=0;

*** ******* * ** * ** ** ** (i=0;i<N;i++){
** ** * * * * ***** * * ****** * * printf("%d",c[i]);

* ** ** * * * * ** * ** * * ** * * * ** * (c[i]!=0 || first==1){
* * * **** ** ** * ** *** * * ** *** ** *** * *** * * * ** * ** ****
** * * * ** * *** * ** * ***** * **** *** *** * * ** * * ** *
******** **** * * * *** * * *** * ** ** ** ** *

    }
}
answered by (-329 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()
{
* ***** * * **** ** * i,j,k,t=0;
** * * * ** ****** x,y,z, c[51] = {0};
* * * *** * ** ** ** *** a[51]={},b[51]={};
** ** * ** ** * *** ***** * *** %s",a,b);
* * * *** * *** ****** * **
* ** **** ** *** *** *** *****

** * * * * ** **** ** *** i<=j; i++)
* * * ** * * ** * * * *** * ****** * ** = a[j-i] - '0';

** ** ** ** * ** * i<=k; i++)
* ** ** *** ******* * * ** ***** * * ** += b[k-i] - '0';

** ** * * * ** * * * * ** i<50; i++)
    {
* * * *** *** * * * * ** * * * * * * ** *** > 9)
* * **** * * *** **** * * ** * **** **
** * * * ** * ** *** * ** * ***** *** *** *** * **** **** **** * **
* *** * ** * **** * ****** ** * * * ** **** *** ** ** = c[i] % 10;
** ** * * *** **** * ***** * *** ** ** ** * * * *
    }
** ** ** * *** **** * * ** i>=0; i--)
* ** *** * ** * ** *** * *** * ** * ** ** ** *
*** *** * * * * * * ******* **** * * * **** * * * ** * ** *** ******* ** * c[i]);
*** * ***** * * * * * **** * *** * * **** ** * * ** ** * ***
** * **** ** ***** ** * * * * * * *
*** *** * * ** * * * 0;
}
answered by (54 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>
#define N 50
int main()
{
* * * *** **** **** a[N];
** * ** *** * * **** *** b[N];
** * * * ** ** * a1[N]={0};
* * * **** * ** *** * b1[N]={0};
** *** *** *** * ***** c[N]={0};
* **** *** ** * * ** i=0;
**** ** *** ** **** **** j=0;
** ****** * ** * * * ** * * ** **
** * * *** * * ** ***** len1,len2;
* * * *** * ** * *
*** ** * * * * *** *
*** ** * * * * * ***** ** * ** * ** ** *

** * ** **** * *** * **
* **** * *** *** ***** (i=len1-1;i>=0;i--){
*** * **** * * * ** * * ** ***** * * ** * *** *** * * * **** *
* * **** * ** *** * ** ***** * * * ** * * ** ** *
** * **** * * * * * * * ** * ** ** * ** ** * *
    }
    /*
* ** * ** * * * * *** (i=0;i<N;i++){
** * ** **** **** * * ** *** * ** * *** * * * ** ** * ***** *
    }
    */
** *** *** *** ** * ** * * *
** ** ** *** *** **** ** (i=len2-1;i>=0;i--){
* * * *** * ***** * * ** *** ** *** ** *** ** ***** * ** ***** **
** ** * * ** * * ** * * *** * * *** * * * * ** *** * *
**** ** * ** ** * * *** * **** * ** *
    }
    /*
*** ** * ** *** *** ** (i=0;i<N;i++){
*** **** * * ** * * **** * *** ** ** ** * * ** ** ** ** *
    }
    */
** ** ** * *** * ** * carry=0;
* *** * * * * (i=N-1;i>=0;i--){
* * * ** * ** * * ***** **** * * ** **** * * * * *****
****** * * *** * * * *** *** * *** * * ** *** ** ** = c[i]/10;
*** * * * * *** ** **** ** * * * ** * *** ** * c[i]%10;
    }

* ** ****** * ** (i=0;i<N;i++){
***** *** * ** * *** * * ** * printf("%d",c[i]);
* *** ** ** * *** * ** * ** ** * ** ** * *** first;
*** *** * ******* * **** ***** * ** * * * * * ** ** ** ** **
* *** * ** * * * *** * * *** ****** * ** * ** ** ***** * ** *
*** * * *** * *** * * * ***** * * *** * * ** * ** ** * ** * * * * *
* ** * * * *** *** ** * * *** * * *** * ***** * * **** *** * * * ** *** * *** *
** * * ** * ** ** * * * ******* ** ** ** * ** * *** ** * ** ***
* * * * * * ** * * ** * ***** * **** ** * * ** * ** ** * *
* ****** * ** *** **** **** * ** * * ** **** *** * ** **** ** * ** *** *** * * * * ******* ** ** * ** *
* *** * * ** **** * *** * * *** *** ***** *** ***** *** *** ** ****
* * ** * ** ** * ** *** *** * * * *****

    }
}
answered by (-329 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
int main()
{
***** * * *** ****** * a[N];
** *** * * * * * ***** * b[N];
*** * **** ** ** * **** a1[N]={0};
*** * * * ** * * *** b1[N]={0};
* * * * *** * * * ** c[N]={0};
*** **** * ** ** * ** *** i=0;
* ***** **** ****** * j=0;
* ** * ***** * * ** **** * ** ** * * *
* * * **** * **** * * * len1,len2;
* * ** * * *
*** ** ** ***** * * * ** *
* **** * * *** *** * * ** * *** * ** *** * **

** *** *** * **** *** *
* ** ** * *** * ** ** (i=len1-1;i>=0;i--){
* * ** ** ** * * *** * **** *** * *** ** * * ** * ** * * ** ***
** * * **** *** *** ** **** * * * ** ** * ** *** * *** * *
* ** * * *** *** * * * * * * * * ** ** *
    }
    /*
*** ** *** **** ** (i=0;i<N;i++){
** *** ** * ** *** **** * *** ** * * * ** * * ** * ** *** * * ***
    }
    */
** * **** *** ****** *
**** ** ** * * (i=len2-1;i>=0;i--){
**** * * * *** * **** * ******* * ** *** * ** * * ****** ** ** *** *** * * ***** *
***** *** **** * * *** * ** * * ******* ** * **** * * ** *
* * * *** * ** * ** ** * * ** * ******** * ***
    }
    /*
** * ** * **** * * * (i=0;i<N;i++){
* **** * ** ** *** * * ** ** **** *** ** ** * *** *** * ** *** ***** * *
    }
    */
* * * ** * **** * * * carry=0;
* ** * * ** ** *** *** * (i=N-1;i>=0;i--){
* *** * * * ** ** * ** * *** *** * * * **** * ** * * ** * *
** * * ** * ** *** * ** * ** ** * * * *** ** = c[i]/10;
******** *** * * * * * ** ** * ** **** ***** * *** * *** c[i]%10;
    }

** *** ** ** ******* * * (i=0;i<N;i++){
** * ****** * * * **** * * * ** ** * ** printf("%d",c[i]);
******* * * *** * * ** * ** ** ** * * * * * * ** first;
** ****** * * *** * ** ** * ** ****** * *** * * **** *
** ******* **** * *** *** *** * ** * **** **** ** * ******* * ** ** * **
** ** ****** *** ********** * *** ** ** ** * * ***** *** * ** * ******
** * **** * * * * * *** * *** * ** ** * *** * ** * *
* * * * ** **** ***** ** * ***** * * * * ** * ****** **** *
* ***** *** ******* ** * *** *** ******** * **** ** * * * * * **
* * * * *** * * * * ** * * *** * * ** ** *** ** ** ** * ******* **** * *** ** * * ** * * ****
* *** * ** *** * *** * * * * * * ** *
* ** ** * ** * ** ** * *** * * * * * *

    }
}
answered by (-329 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
int main()
{
** * *** * * * * * a[N];
* * ***** *** * * * * b[N];
** * * ** * a1[N]={0};
** ** * * ** ** * ****** * * * b1[N]={0};
*** ***** * * *** c[N]={0};
* *** ** * *** * **** ** i=0;
***** ****** * *** ** * j=0;
* * * * * * ******* * * *** * * *
** * * * *** *** * ** len1,len2;
* ** * * * ** * * * ** **
* *** * *** ***** * **** *
****** * *** *** * ** ** * *** * **** * * **

* * ***** **** *
**** **** * * ** (i=len1-1;i>=0;i--){
*** * * ***** **** ** * * *** ***** * * * * * ****** * **** * ***
* **** * * *** * * * * ** * * * **** * *** * **** * * **** *****
** *** * ** * ** *** ** * ** **** *** **** *
    }
    /*
** ** ***** * * * * (i=0;i<N;i++){
* *** * * * * * * ** ** ******* * * * ***** ** * * ** ** * **** * *
    }
    */
*** ** ** ** ** ** ***** *
** ******* * * * (i=len2-1;i>=0;i--){
* ** * * ***** ** ** ** * *** * ** ** *** * **** *** ***** ***** ** *
** ** *** ** *** *** * ***** * ** * * * ****
*** * *** * * *** * ** * * *** ** * *** **** * ** *
    }
    /*
* **** **** * ** (i=0;i<N;i++){
**** ****** ** *** * * **** * * *** * * ** ********** * * * ** *
    }
    */
**** ** **** **** * carry=0;
*** ** * * * (i=N-1;i>=0;i--){
** * * ** * * *** *** * **** * ***** * * * ****** * ** * *
** ** ** * **** **** * **** *** * * * = c[i]/10;
** * * * **** ** ** * ** * *** * * ** ** * c[i]%10;
    }

** ***** * * * **** * (i=0;i<N;i++){
** * * ** *** * * * **** * *** printf("%d",c[i]);
* * *** * *** * **** * * **** ** * * ** * ** first;
* * ** **** *** * ** * * ****** * * **** * * ** ***
* * * ** * * *** * ** * * * ** *** * *** ** ** ** **** *
*** ** * **** * ** ***** *** *** * ** * ** * ****** * * ** *** * *** **
* ** ** * * ** *** * * * * *** * **** *** ** ** ** *** ******* *********** ** * * ****
* ***** * ** ***** *** * * * * ** *** *** ** * * * ** * *
* ** ** * * * ** **** *** * *** * * * ****** ***** ** * ** * **********
***** * * * * * * ** ** * * * * ******* *** * * * * *** *** *** *** *** * * * * * *** * * ***** **** * * * **
* ******** *** **** ** ** ** ** * * * * **** * * * * * **** **
** * ** ******** * ***** *** ** * *** **** *

    }
}
answered by (-329 points)
0 0
prog.c: In function 'main':
prog.c:54:20: warning: zero-length gnu_printf format string [-Wformat-zero-length]
             printf("");
                    ^~
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
#define N 50
int main()
{
**** * * * ** * ** *** ** a[N];
* * ** ** *** ** * b[N];
**** * ***** *** * * ** a1[N]={0};
** * * * * * * * * * * ** b1[N]={0};
* * * * * ** ** * c[N]={0};
** ***** * *** * ** * ** i=0;
** ** * * ****** * j=0;
* **** ** *** * * **** * ** *** ****
* ** * * ** ******* * * len1,len2;
* **** * *** * ***** *** * ****
** ** **** ***** *** * ***
* * ** *** * ***** * ** * * *** * *** ** **

** *** ** ** * * * *
*** * ** * * * ** (i=len1-1;i>=0;i--){
* **** * ** *** * *** * * * * ** ** * * * ****** * * **** *** **** * *
* * **** ** * **** * **** ** * **** **** * * *** ** **
** * *** ***** * ** **** ** * **** * ****** ** *
    }
    /*
* ** **** ** * * * **** (i=0;i<N;i++){
*** ** ** ** ******** * **** * ** ****** ***** * * * ** ** ** *** *** **
    }
    */
* ** * ** * * * ** * *
**** * * * * * * * * ** (i=len2-1;i>=0;i--){
* * ***** * * * * * * * ** **** *** * * * ** * *** ** *** * * *
** **** **** * * ** ***** ** * ** * * ** * ** **
* ** ** ** * * ** * ** * **** **** ** ** ** * * ** * **
    }
    /*
* ** * * ** ** * ****** * ** (i=0;i<N;i++){
** ** * * * **** * ** ***** * * ** * * *** * * * ** * **
    }
    */
** *** * * * ** ** carry=0;
*** **** * * * * **** * (i=N-1;i>=0;i--){
*** ****** * ** ** ***** ** * *** * * * **** * * *
** **** ** **** * ** * * * * * *** * ** * **** * ** = c[i]/10;
** * **** *** ***** ** * *** * * **** ****** * *** c[i]%10;
    }

* ** ** * *** ** *** ** (i=0;i<N;i++){
*** *** * * ** ******* ** * *** * printf("%d",c[i]);
* * ***** * * * * * **** * ***** * * * * first;
* ** * **** * * ** * **** * * *** **** *** ** * ** * **
* * * * ** * * * * ** *** * * * * * *** * *** * * * * * * * * * ***
** * * **** * *** * ** **** *** ******** * ***** ****** * ** ** ****
** * * ** * ** ** *********** *** *** * * * ** ** * * ** ** *
** ****** **** * * * * ** * * ** **** ****** ** ***** ** ** * * ** * *
**** *** * **** ** * * * * * * **** * ** * * * ** * *** * * * * *** * * * * * * ** ** **
** * *** ** **** ** * ** ** * ** *** * * * ****** ** ** * **** * ** *
* * ***** * * ** ** *** *** *** * * **

    }
}
answered by (-329 points)
0 0
prog.c: In function 'main':
prog.c:13:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[50]' [-Wformat=]
     scanf("%s%s",&a,&b);
             ^
prog.c:13:15: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[50]' [-Wformat=]
     scanf("%s%s",&a,&b);
               ^
prog.c:54:20: warning: zero-length gnu_printf format string [-Wformat-zero-length]
             printf("");
                    ^~
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>

int main(){
*** * * ** ** * *** a[50],b[50];
    int c,d[51]={},e;
** ***** ** * * ** *** ** ** * ** ** ** %s",a,b);
* *** * ** * ** * ** * *** * *** * * * **
* * * *** ***** **** *** *** * * * * ** ** ** ** * * * *
    }
* ** * * ***** * ***** * ** * * * *
** ** * ****** *** * * **** * * ** **** * ******** * **** * *
    }
* *** ****** * * * * **** * * *** ***
* ******* * * **** *** ***** ***** ** * ** *** *
* * * *** ** **** **** * *** * ** ** ***** ***** * * * * **
****** *** ** * * *** *** * * * * *** * * * *** * ** ** *** * *
* * * * ** * ** ** ** * ** *** *****
    }
***** *** ****** * *** ** ** * ***
* * * * *** ** * ****** * * *** * * ****
***** ****** * * * ** * ** *** ** *** * * * ** * * *** * ** * *
* * * * **** ** *** *** * **** **** * *** ** *** ** ** * *
* * ********* ** *** * * * * *
    }
***** ** ******* ** *** ** * * *** ******
** ** * ** * **** ** *** * **** *** *** * * ** *
    }
* * * ** *** * **** 0;
}
answered by (-498 points)
edited by
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>
#include <string.h>
int main()
{
***** ** ** * ** * *** * i,j,k;
** *** ** ** * * ** ** * x,y,z, c[51] = {0};
** * ** *** * * **** a[51]={},b[51]={};
****** * * * *** ** * ** %s",a,b);
* ** **** *** * ** *** * ***
******** * * * ** ** ** * *** ***

* ** **** ** *** i<=j; i++)
*** * ** * ***** * ** ** **** * ** * * * ** = a[j-i] - '0';

**** * * ***** ** ** ** i<=k; i++)
* * ** *** *** ** * * * * ** * **** += b[k-i] - '0';

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

* * * * * ** * **** > j)
** * ** ** * ** ** *** * ******** * = k;

** ** ****** ** * ** * * i>=0; i--)
* * *** * * *** *** **** * * ** ** ***** **** ****** ** * * ** * ** * c[i]);

** ** ** ****** *** 0;
}
answered by (54 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.197.64
©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
| 22.3k views
12,783 questions
183,442 answers
172,219 comments
4,824 users