1 like 0 dislike
2.1k 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

 

[Exam] asked in Final Exam
ID: 42300 - Available when: 2018-01-17 14:00 - Due to: Unlimited

retagged by | 2.1k views

32 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
#include * *** * ** **
#include * * *****
char a[100],sum[200],space,i;
int main()
{
***** ********** *** * *** * * *

* **** * ** *** ****** * * * ** * * * * *
* *** *** * * * * * * * ** ** ** * * *** *** '){space=i;}
** ** ***** **

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


* **** * ** **** ** * * *** 0;
}
answered by (-286 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
* *** ** * **** *
* ** * ** * *** *
****** **** *** ***
int main(){



}
answered by (-32 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
int main (void)
{
** ** ** * *** *** * num1[51],num2[51];
* ** ** * * * ** i,m;
** * *** * ***** * arr[51]={0};
**** * * * ** * ** * ***
** ** ** ** ** ** * ** * ** * * %s",num1,num2);
   for (i=0;i<(strlen(num1));i++)
    {
** * * * *** ** ** *** * * * ** * * * * * * **** * * ****** ****
    }
*** * *** **** * ***** (i=0;i<(strlen(num2));i++)
** * * * * * *
*** * * * * * * **** ** ******** * ******* ** * * *** * ****
** ** * * * ** *** *
*** ** * *** ** * ** (i=0;i<=50;i++)
* *** * * * * ** * * * *
* **** * * *** * *** * **** * ** * ***** * * ** **
* **** **** ** * * ** ** *** ** ***
* ** ** * **** *** ** * * ** * **** *** * ** * * * ** * ** * * * *
** ****** ** **** * * ** * ** *** ** * * * *** *** * * * *****
* **** * * * *** * **** *** ********** *
    }
* ** ** * ** ** * * ****
    {
** * * ***** *** ** ** * *** * * * ** * ** * ** *** ** *
** * * * * * **** * ** **** * ** **** ******
*** * * * * * * * ** ** ***** ** * * ** ****** ********** ******* ** *
** ** * ******* *** **** ** * * * * ** * *
** * * *** * * * ** *** ***** **** *** **** *
** * ** *** * * * * * * * *** * ** **** * *
** * ** * *** * *** * ** * * ** * **** ** *** * ** * * *** * ** **
* ** ** **** * ********* * * * ** * * ** ** * ** * ****** **** *** *** ** *
** **** * * ** ** * * * * *** ** ** ** ***
    }
* * **** ** * *** 0;

}
answered by (-168 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];
** * * ** * * ***** ** * * ** * *** *
* * *** * * * * * ** * * l1,l2;
*** * * *** * * **** ** i=0;
** * * * * * **** * *** ***
* ** ******* * ******* ****
* ** **** * **** * * * **** **** ****
* ** * * **** ** ***** ****** ***** ** * **
* ** *** * * * * ** ** * ** * * ** * **** ** **



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





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



}
answered by (-329 points)
edited by
0 0
prog.c: In function 'main':
prog.c:7:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[50]' [-Wformat=]
     scanf("%s%s",&a,&b);
             ^
prog.c:7:15: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[50]' [-Wformat=]
     scanf("%s%s",&a,&b);
               ^
0 0
prog.c:28:1: error: expected identifier or '(' before '}' token
 }
 ^
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
***** * ** * * ** * ** * a[50];

* *** ** * * * * * * b[50];

*** ******* * * * ** c[51] = {0};

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

* **** ** * * *** ***** * i = 0;

** * * * *** ** ** * * len_a = strlen(a);

* * * * * * ** * * *** len_b = strlen(b);

* * **** **** * * ** *** * = 0; i < len_a; i++)
** * ** * **
* ** ** ** **** * * **** * *** * * = a[len_a - i - 1] - '0';
* *** * ** * ** *** *
* * **** *** *** ** = 0; i < len_b; i++)
* ** *** ***** * ***
* * ** ** *** * ** ** ******** *** * **** += b[len_b - i - 1] - '0';
** ** * * ****

** ** ** * ******** = 0; i < 51; i++)
* ** **** * ***** *
*** ** *** ** *** ** * ** * * * * * * * *** > 9)
** * **** * *** ***** * ** *** * *
* ** **** * * * * *** * * ** **** ** * ** **** ** * **** * + 1]++;

* **** ******* * * **** ****** * * * * * * * * ***** *** * ** *** * ** * * *** %= 10;
** *** ***** ** * * * ****** ***** **
* ** *** * * ** * *** *

** ***** * *** *** * **** nonzero = 0;

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

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

** * * ** * * * * * = nonzero; i > -1; i--)
*** * * ***** ****** **
* * ** *** * * *** * ** **** * * ** * ** *** * * c[i]);
* ** ***** ******** ** * *
** ** * * *** * ****** 0;
}
answered by (-285 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()
{
    int i,e,begin=0;
    char a[51],b[51];
    int c[52]={0};
    scanf("%s",a);
    scanf("%s",b);

* * ** * ** *** *** * **** ** ** * * *** *** ** ***** *
    for(i=0;i<strlen(a);i++)
** * * ***** * * * * * ** * ** * ** * *
*** ** * ** * ** * * * *** * ** ** ** * * * * ******* *** **** ** *
**** ** * * * ** * ** * * ** ** * ** *
    for(i=0;i<strlen(b);i++)
** * **** * * ******* *** * **** ***
* *** * * * * * * * **** ** * * ****** ***** ** * ** **** * ** ***
*** * *** * * * *** * **** * * * ** ** * **
    for(i=0;i<50;i++)
* ** * ****** * * *** * ** ** *** ** ** *
** ** * * ** * ** * ***** * ** * ** *** **** * ***** *** * * *** * * * > 9)
**** ***** * **** * ** ** ** ** * * * ** *** * * *** *** *** * * ***** * ** *** **** ***
*** *** * *** **** * ** * ** * ** ****** **** *** *** * * ***** * * **** ** ****** * ** * * * *
****** ** ** * **** ***** * *** * * * *** * **** * ** * *** ** * *** *** * ** * * * ** ** * * *** *** *
** * ** ** ** ** *** ***** * * ** **** ** *** ******* * * ** *** ** ****** * **** ** *
* *** * *** ** * * * * ** ** * * *
    for(e=51;e>0;e--)
* * * ** *** * ** *** * * **** ** * ** ** *
* * * * ** * * *** ***** ** ** ** ****** ** * *** * * * * **** ***** != 0)
** ** ***** * * * * * ** * ** * *** *** ** * *** ***** ****** * * * ** * ** * * ** * *
** * * ** *** ** * ** **** * ** * ** * *** * *** *** * ** ** * *** * * * * ** * ** ** *** **** *
* ** * * ** *** * * *** * * * * * * * * * * **** *** *** *** *** * * * ** * ** * *** *** *
**** **** * * * ** ** * * ** ****** **** ** * **** ** * * * ** *** *** * * **** ***
** **** ***** *** ***** ** * ** ** * **
* ** * ** * * ** ** * *** * ** **** * * ** ******* ** ******* **
    for(i=begin;i>=0;i--)
* * * *** * * * ** ** * * ** * ***
* * * *** * *** * ** * * * * * ** * ** *** *** ***** * * ** **** * ** *
** * ** * ** *** ** * ***** * * *****
}
answered by (-301 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
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()
{
**** *** * * * ** * i,j,k,l;
****** * *** * * ** * a[100];
* ****** ** ** * * ** * * * b[100];
* ** * ****** **** ** * ** ***** ** *** *
* * ** ** ***** ** ** * * * * ** * * && *** ** ** ******
* *** ** *** * * * ****
* * ** *** ***** * * **** ** ***** *** ** ****** * *** * * * *** *
* ** ** ***** ***** **** ** *
**** **** * *** * *** * 0;
}
answered by (-229 points)
0 0
prog.c: In function 'main':
prog.c:9:12: warning: integer constant is too large for its type
     if (a==999999999999999999999999999999 && b==999999999999999999999999999999)
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:9:10: warning: comparison between pointer and integer
     if (a==999999999999999999999999999999 && b==999999999999999999999999999999)
          ^~
prog.c:9:49: warning: integer constant is too large for its type
     if (a==999999999999999999999999999999 && b==999999999999999999999999999999)
                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:9:47: warning: comparison between pointer and integer
     if (a==999999999999999999999999999999 && b==999999999999999999999999999999)
                                               ^~
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


   int main()
   {
*** * *** * **** * * * * *** **** * * * a[50],b[50];
** * **** * ** *** * * * * * * **** ** * c[51];
* **** * * *** ****** * * ** ** **** * * n1, n2;
* * * * ** **** ** ** ***** * ** * i,j;

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

**** ******* *** * * ** **** * * ** * ** ** * *
       {
* *** * * ** * ** * * ***** ****** * * * *** * *** * * ** * * * * );
* * ** **** * * ** * ** * * **

* * ** *** * * ** * * ** * * * * * * ** *
* ** * ** ** * ** * * * * * * * ** ***** **
** * **** * ** * *** **** * ***** ** **** * * ** ***** * *** * * * * ** ** b[i]);
* * *** * * ** ** * ** * ** ***** * *



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

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

* * ** * * * * * **** *** * *** *** * * * * *
       {
* ** **** * **** * ** *** ** * ** ***** ** * * * * * * ** * ***
       }

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


    return 0;
}
answered by (-193 points)
edited by
0 0
prog.c: In function 'main':
prog.c:13:14: warning: format '%[^
   ' expects argument of type 'char *', but argument 2 has type 'int (*)[50]' [-Wformat=]
    scanf("%[^\n]*c%",&a);
              ^
prog.c:13:19: warning: spurious trailing '%' in format [-Wformat=]
    scanf("%[^\n]*c%",&a);
                   ^
prog.c:14:14: warning: format '%[^
   ' expects argument of type 'char *', but argument 2 has type 'int (*)[50]' [-Wformat=]
    scanf("%[^\n]*c%",&b);
              ^
prog.c:14:19: warning: spurious trailing '%' in format [-Wformat=]
    scanf("%[^\n]*c%",&b);
                   ^
prog.c:17:17: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
    for(i=strlen(a),j=0;1<=0;i--,j++)
                 ^
In file included from prog.c:4:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
prog.c:22:18: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
     for(i=strlen(b),j=0;1<=0;i--,j++)
                  ^
In file included from prog.c:4:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
0 0
prog.c: In function 'main':
prog.c:14:18: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
        n1=strlen(a);
                  ^
In file included from prog.c:3:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
prog.c:15:18: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
        n2=strlen(b);
                  ^
In file included from prog.c:3:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
prog.c:19:20: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
            scanf("%d",a[i] );
                    ^
prog.c:24:20: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
            scanf("%d", b[i]);
                    ^
prog.c:46:21: warning: format '%d' expects a matching 'int' argument [-Wformat=]
            printf("%d");
                     ^
0 0
prog.c: In function 'main':
prog.c:19:20: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
            scanf("%d",a[i] );
                    ^
prog.c:24:20: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
            scanf("%d", b[i]);
                    ^
prog.c:46:21: warning: format '%d' expects a matching 'int' argument [-Wformat=]
            printf("%d");
                     ^
0 like 0 dislike
Hidden content!
#include * *** *** * *
#include ***** *****
#include * ** *
int main ()
{
** *** *** **** * * * i,n,m,c=0;
***** * **** * ** *** * A[60]={0};
***** * * * ** **** * ** * * * * *
* * ** * * * * * * *** ("%s * * *
** * ** ** ** *** ** * ** ***** * * * * * * ** *** **
* * * * * ***** ***** 0;
}
answered by (-32 points)
edited by
0 0
prog.c: In function 'main':
prog.c:14:23: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
     for (i=0;i<strlen(A);i++)
                       ^
In file included from prog.c:2:0:
/usr/include/string.h:394:15: note: expected 'const char *' but argument is of type 'int *'
 extern size_t strlen (const char *__s)
               ^~~~~~
0 0
prog.c: In function 'main':
prog.c:11:5: error: expected ';' before 'return'
     return 0;
     ^~~~~~
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>

int main() {
    char a[50], b[50], sum[50];
    int i,j,tmp=0;

    for(i=0;i<50;i++) {
*** ** *** * *** * * * ** ** **** * * * * ** **** * * * *
    }

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

    for(;;) {
* ** ** * * *** *** ** * ** ** *** ** * * ** * * {
* *** **** * * ** *** ********* * ** **** * ** ** ** * **** * *
* *** * ** ***** * *** * * *** * **** * * * ***** * ***** *
*** * ** ** * ** *** *** ** * **** * *** * ** ****** ***** *** *****
**** ** * ****** ****** * * ** ** *** *** *
*** ** * * *** ** ** ** ***** ** ** ** ** * *** *** * ** {
* *** * **** *** ** * **** * * ** ** ** *** ** ** * ***** * * * *** *
* * ***** **** *** * * *** * * * ******* ** ****
    }

    for(;;) {
* *** * * * * * * *** **** ***** * **** * ** * * {
**** ****** * * *** **** *** ** * *** * * * *** * ** ******
****** ** * ** * ***** * ** * ** ** ** *** ** * ** ** * ** **
* ** * *** * * ***** ** **** ***** * * * * ** ** * * * ****
* *** * **** * ***** * * * * **** *** *
* ** *** ** * * ****** * *** * * * ** * *** ** * {
* ** *** ** ** ** ** ** ** * * *** ** **** * **** *** * ** ** ** * *
** ****** ** * * ** * * *** * ** * ******
    }

**** ** * * ***** * ** ** ** {
** ** * * * ** ** *** * ** ** * ** **** ** **** *** && a[i]<=57) {
* * * ** * ** * ** ** **** * ** * ** * * ******* ***** **** * *
* * * *** * * ** ** **** * * ***** * *
* ** * * ** * ** *** *** *** ** * ****** * *** * * && b[i]<=57) {
***** ** * * ** **** * ** * ** *** * * * * * * * ** ***** *
* * ** ** * **** ** * * ***** ** * * *
    }

    for(j=50;j>0;j--) {
* ** *** * *** * **** ** * *** * * * **** * *** {
* ** * * *** ** ** *** * ***** ** **** **** * *** * ** * * ** *** * *** *** * ***
* ** * * ** *** * * * * ** * *** * ***
* ********* * * *** ** * * ***** *** **** **** *** * if(sum[j]+a[j]+b[j]==10) {
***** ** * ** * ** * * **** * * ** * * * * *** * * *** *** * * * *** *
** * **** * * * * * * * * ** *** * ** * * * ****** * *** *
* ** **** * **** ** ** * * ** ** ** * *
* * * * * * ** ****** * * * ** * * * if(sum[j]+a[j]+b[j]>10) {
*** ** * * * ***** **** * **** ** **** * **** ** ***** * * * * * **
* ** ** *** * * * * ******* ** * * * ** ** ** *** * * *
** * * ** ** * ** * ***** * *** ** *****
    }

    for(j=0;j<50;j++) {
* ** *** **** ** * * *** ** ** * * * ** * ***** * {
* ** ** ** **** ****** * ** * * ** * * *** ** **** ******* *
* * ** * *** * * ** * *** * *** * *
    }

    for(;j<=49;j++) {
* * ** * * * * * * ******* ** ** **** * ** * * ** *** * sum[j]);
    }
}
answered by (-120 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.71.254.84
©2016-2024

Related questions

1 like 0 dislike
37 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42302 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 1.8k views
2 like 0 dislike
31 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42301 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 1.8k views
1 like 0 dislike
37 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42299 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 2.2k views
2 like 0 dislike
30 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42298 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 1.5k views
0 like 0 dislike
8 answers
[Exam] asked Jan 19, 2018 in Final Exam
ID: 43461 - Available when: 2018-01-20 09:00 - Due to: Unlimited
| 431 views
12,783 questions
183,443 answers
172,219 comments
4,824 users