0 like 0 dislike
11.8k 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.8k views

44 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>
int main (){
int num1[255],num2[255],sum[255];
char s1[255],s2[255];
int l1,l2;
printf("enter number:");
* ** * * * * * ** ** **
printf("enter number: ");
** *** *** * *** ******
for (l1=0;s1[l1]!='\0';l1++)
*** ** ****** ** * *** ** *
for(l2=0;s2[l2]!='\0';l2++)
* * ** **** * * * * * ** * * *
int carry=0;
int k=0;
int i=l1-1;
int j=l2-1;
* *** * *** * * * * *
*** ***** ** * * * * **** * ** *
** ** ** * * **** ** *******
}
if(l1>l2){
** ***** * ** ** ***** * **** * *
***** ** ****** * * ******* **** * * * ** * * **** * *
** * **** ** ****** * * * ** ** ** *** *** * * **** **
* ****** * ** * **
    }
** * *** * * ** ** ** if(l1<l2){
* ** * * *** * ***** * * * ** ** *** * * * (j>=0){
* * *** *** *** * ***** * * **** *** * ** ***** * ***** * *** * ***** ** ** * * *
* * ******* *** * **** * * **** * ***** *** * ** * * ** ** * * * *
* ** *** * **** ** * * * ** ** ***** * *
* * **** *** * * ****** *** **** *** ***** *
* * * ***** ***** * ****** **** ** ** **** * ********
*** ** ******* * * ** ** * * *** **** * ** ** *** * *
*** ** ** ** * * ****** * ** * *** * * **
* ** * ** ** * * * *** * * * * * ** ** * ******* * ** * **
*** *** * ** ******* * * * ** * * * ***** *** ** *
*** * ** ** ** **** * * *** * * *** ** * **** ** *** ** *** * * ******* * *** ** ** *** *
return 0;
}
answered by (16 points)
0 0
prog.c: In function 'main':
prog.c:8:9: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[255]' [-Wformat=]
 scanf("%s",&s1);
         ^
prog.c:10:9: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[255]' [-Wformat=]
 scanf("%s",&s2);
         ^
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>

int main(){
    char a[50],b[50],c,d[51]={0},e;
**** * *** ** *** ** * * *** * %s",a,b);
**** *** *** **** ******** ** * **** * * * ** **
******** * **** * * ** * * ***
*** **** ** * * ** ****** ***** * * * *** * ** * ** ****
    }
** ** * * ** ** ** * *** * * *
** ** ** * * * *** ** ****** ** ** ** *** ***** * **
    }
* *** *** * ** ***** ** ** **** *
*** * * **** ** ** * * * * ** *** * * ** **** *
* *** ** ** * ** * * ** ***** * * ** *** * **** * ****
*** * ** * *** * ** * * ** *** **** * ** ** ** * * * * * *****
* **** * * ** * * * *** ** * *
    }
* * ***** * * * ****** ** * ****** * **
**** ** * ** * ** **** ******* *** * * * * ** ***
* * ****** * * * ** * * *** * **** *** * ******* * * * ** ****
* *** ****** *** ** *** * * * * ***** * ** ** ** * * **** *
* * * * * ** ** *** ** ** * ** ** * * ** * *
    }
** *** *** ****** * *** ***** *
** * * * ** * * ** ** **** * * ** * ***** ** ****** *** * * * * * ** * *** * *
    }
* * **** *** ** * *** * * * * 0;
}
answered by (-140 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()
{
*** * ** ** **** **** ** string1[50];
* * **** * * * ** string2[50];

** **** ** *** * ******** *** * ** * string1);
* ** ** * ** ** ** ** * ** ** ***** string2);
* * * * *** * * * * * arr[51]={0},i;

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

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

* * * ** ****** * * * ** i<50; i++)
** ** **** *** **** * * *
* * * ** * * * *** * ** ** * *** * * **** > 9)
******** ** *** ** ** * ** * * * * ****** * ***
* * * * * * *** * *** * * * * * * ** * ****** * ** * *** ** ** ** ** * ** -= 10;
**** * * ** ******** * * *** ** **** ***** **** ** * **** **** ** *****
**** ***** *** *** ** * ** *** *** * *** *
** * ** *** * * ** **

* * * *** * *** ** **** ** i>=0; i--)
** * *** ** * ** * * ***
** ** *** * ** * * ** * * * * ** **** * ****** * * != 0)
** ** * ** ****** *** * ** * ** ** * *** * ** ** * * *** * *******
* **** ** ** ** *

** * * * **** ** ** * * ** * * i--)
****** * * * ** * * ** *** ** * *** ** ** *** * ** * **** ** * * * arr[i]);
}
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()
{
** * * ** ** * * * * ** 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 (-229 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(){
    char a[50],b[50],c,d[51]={0},e;
** **** *** * ******* ** %s",a,b);
* * * * ** *** * * * * * ** ** **** *
*** * ***** * * ***** *** ** ** *
** * * ** ** ** ** **** ******* * * * * ***** ** ***** ***
    }
* * ** * **** * * * **** * ****
* *** * * * * ** * *** **** * * * * * * ********
    }
* ** *** ** * * * * ** * * * *** * *
** **** ** * *** *** * * * * * * ** * ** * * * * ** * *
* * *** * *** *** ** **** *** * * * **** ****** * * ***** * * *
*** ** **** ** **** ** ** *** * * *** ** *** * ** ** * * * ** ** * * *** * **
* *** * * * * *** ** **** ** ** * *
    }
**** *** * ** * ** * *** * **** * *
** * ** * ** ***** ****** * ****** ** ** ** ** **** *
*** * * **** * * * * * * ******* * **** **** * * * * *** **
* ** * *** ****** ** * * * * **** ** * *** *** *
* ** ** ** * * * ** *** *** * ****
    }
* * *** **** * ** ***** * ** ** * *
** * **** * *** ** * *** ** ** ***** * *** * * ** * * *** * ***** ** * * ** * * *
    }
*** ****** * **** * *** *** 0;
}
answered by (-301 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()
{
***** * ** * *** * * A[50],B[50];
** * * ***** * ** ** C[51];
**** * **** * * ** * **** * *** *** ***
*** * ** **** *** * * ** * ** ** * ****
 // **** ** ****** ** * * *
 // ** ** ** * * * **** *

*** *** * * * * *** length_A,length_B,length_MAX,length_DIF;
** ** ** ** * ** * * **** * * *
* ****** * * * * ** ** *** ** ** * * **

   // printf("ban dau length C la %d hoac %d\n",length_C, strlen(C));
 // ** * *** * * ****** ** *** * * *** * *
* * **** *** *** *** * **
* * * ** * ** ** * * ** *** * ** ** *
* * *** ** ** * **** * *
** * * ** **** * ***
* ** ** ***** * * ***** * **** * * * **** ****
* * * ** * *** ***** ** * **** *** ** ** * ** * *** * * * ** * *****
    }

* * * * * j=0, i, sum, carry_out=0;
* **** * ***** * * **** ** * * * ** *****
    {
* * * ** * * * * ****** ** ** ****** * * **** ** ***
** * ********* * ** *** * * * * * ** *** * *
* * * * * *** *** *** * * ** ** * * **** * * * ** **  printf("check tren\n");
*** *** * **** ** ***** ** * * * ** * * ** * *** ** * * * ****** ** * * *** *
* * * * ** ** * ** ** ** ** * * ** ** * *** * * * ** * ** * * **** ** * * * **** *** *** ** ** * * *** ****
* ***** * * * * **** **** ** * * ** * * * ** ** *** * ***** ** *** ** * **** * * * *
**** * ***** *** ** * * ** * * ** *** ***** *** ** ** * **** *** *** ** *
****** **** * *** *** ** ** *** *
* ***** *** * ***** ** * * * ** * **
* ** ** * ** *** * * * * *** * * ** * ** ** *** *** * **
* * * * ** ** ** * * **** ** * *** ** ** ** **
***** *** * *** **** ** * * ** **** *** * * * ******* * **** * * *** ** *** **** * duoi\n");
* * * * ** ** * * * *** ** ** * **** ** * *** **** *** ** ** * * * * * ***
* *** *** *** ** ** * **** * ** *** ** * * ** * **** * *** *** **** ** ******** *** *** *
*** * * ** * **** ** * **** * * * ** * ******* *** ** * * * ** * **** sum=carry_out+A[i]-48;}
* *** * **** **** ** * *** * ** *  printf("luc nay sum=%d\n",sum);
* **** *** ** **** ** *******  printf("luc nay length cua C la %d\n",strlen(C));
**** *** * ** * * * * ** * * ** * * ** **** * * * * ****
** * * *** ** * *** * ** *** **** ** * * ** *** * ** * *** * * *
*** ** * * *** **** * ** *****  printf("luc nay length cua C la %d\n",strlen(C));
*** ** * ** **** *** * * **** * ** ** *
* **** *** * * * **** *** * * ** ** ** * * * *
***** * * * *** * *** ** * * * ** * ** * *** **
** * ** ** * ** ** * * * * * **** * printf("sum=%d\n",sum);

* *** * ** ** ** * ** * *
 // ****** * **** * *** ***** ** * **

* **** ** ** * * * * * * * * **
    {   C[j]=sum/10+48;
** ** *** **** * * * * * * * * ** *
*** * **** *** * * **
// ** * * **** * ***** *****
 // * * * * ** ***** * * * ** cua C la %d\n",strlen(C));
//    strrev(C);
** *** *** * * ****** **
    {
* * * * ** *** ** ****** **** * ** * ** ** * * *
** * *** ** ***** * *
}
answered by (-196 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
**** * * * ** *
** * *
* ** * * * ** *
* ** * **
** * *

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


** * * * *

** * * * **** * **
* * ** * - **

* = * * * **
* * * * *
*
* * ** *
*
** * * *
*
*** * **** * ** * * ** ** *
* ** * * *** *
** ** *** + ** * + ** /

* *** * * ** {

* * * * **** *
** = **** * * * * *
* * ** *

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



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

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

* * * **
{
** *** * *** * * *

* * * * * * b

****** * * ** int * = 0;

* ***** * * ** * int * =

*** * *** * * * ** * * a, b);

* * * * *** int * = * *

* * * * int * * = ***

** * * *** ** i = 0; i * **
***** ******* {
*** * ***** ******* ** *** * * = ** * - 1 - i] - ***** ***** * * * '0' = *
* * ** * }
*** ** * * * i = 0 ;i ** * * i++)
** * * * * * {
**** * * * ** * **** * += **** - 1 - i] - '0';
* * * * }

* * ** ** * * i = 0; i * * * 50; *
* ** *** {
* * ** ** ** ** * * ** * * * ****** 9)
***** * * ** * ** *** * {
* * * ** ** * ** ** * ** * * c[i + *

* * * *** * ** * ** * * *** = ** % 10;
*** ** *** * **** **** ** }
* *** * **** * }
** *** ** *** i = 50; i * -1; **
** * ** **** {
*** * *** *** *** * ** * * != 0)
** ** *** * * *** * {
*** * ** ** ** * * *** * *** * *** = i;

* ** * *** * *** * * ** ** * * * **
* * * ** ** * * }
* *** ** * }

* * * * * * ** i = *** i * -1; i--)
** ** * ** * {
***** ** ** * * * ** * * *** * * ** **
** *** * ** }
}
answered by (-285 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()
{
** ** ** ***** *** * * * * string1[50];
* * ** ** * * *** ** *** * string2[50];

**** ** ** ** **** * ** ** * * * ** string1);
***** * * ** * ** *** * * ** * * ** string2);
** * * * ** * * * arr[51]={0},i;

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

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

* *** * * ** * ** * **** i<50; i++)
*** * * ** *** * **
* * * * *** ****** ** * * * ** * ** *** * **** > 9)
*** *** **** ********** * ** ** * * * * ***
* * *** ** * ** ****** * * * * * * *** * ** * **** * * * ** * ** *** ** ** -= 10;
******** *** * * ** * ***** ** * *** ** * ***** *** **** ** * ** * * *
*** **** * **** * ***** *** *** * * ** **
* * ** **** ********

* **** *** * * * * i>=0; i--)
* **** *** * * *
* *** * ***** * ***** * * ** *** *** ** * * * ** * ** != 0)
*** ***** ** * * *** * * * * **** * * * * * * * ** **** * *
* * **** ***** * * *** * **

** * * * * * ** ** ***** ** i--)
* * *** ** * * **** ** * * * *** * * ** * **** * * *** arr[i]);
}
answered by (-167 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()
{
* * * *** ** * *** * ** a[50],b[50];
*** *** ** *** * * *** c[51] = {0},i,x;
** *** * * ** *** *** ** * %s",a,b);

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

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

* ** ***** * * * ** = 50 ; i >= 0 ; i--)
    {
** **** * **** * *** * * * * * *** * ****** ** * != 0)
**** *** * *** * * * * ** **** * * * ** * * **
* ** * *** * * *** ** *** *** *** *** *** * * *** ** * = i;
* **** * ** * * * ** ** *** * * **** ** *** ** *** * **** * * ** * * *** **
* * *** *** *** * ** *** * * ****
** *** * ****** * * *

** ** * ** ** ** ** * = x ; i >= 0 ; i--)
*** * * ******** * ** * * * ****** ** *** * ** * * ** * * *

** *** * ** * ** * * 0;
}
answered by (-284 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.32
©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.3k views
12,783 questions
183,442 answers
172,219 comments
4,824 users