Add a large number to a typical C language question, and add a large number to a C language question

Source: Internet
Author: User

Add a large number to a typical C language question, and add a large number to a C language question

Adding a large number to a typical C language question

/* [Program 32] addition of large numbers * question: Calculate the sum of two very large numbers, for example, 123456789123456 + 123456789 * program analysis: and very large, int, long, long, and other data types cannot fit this number. What should I do? Returns an array. */# Include
 
  
# Include
  
   
// Strlen () memset () # define SIZE 1000 void add_oper (const char num1 [], const char num2 []); void output (int num [], int len ); int main (int argc, char * argv []) {char num1 [SIZE] ={}; char num2 [SIZE] ={}; puts ("Input number1 :"); scanf ("% s", num1); fputs ("Input number2: \ n", stdout); scanf ("% s", num2); add_ut (num1, num2 ); return 0;} // calculate void add_oper (const char num1 [], const char num2 []) {unsigned int len1 = strlen (num1 ); unsigned int len2 = strlen (num2); int result_add [len1 + len2]; memset (result_add, 0, sizeof (result_add )); // convert and add for (int I = len1-1; I> = 0; I --) {result_add [I + len2] + = num1 [I]-'0 ';} for (int j = len2-1; j> = 0; j --) {result_add [j + len1] + = num2 [j]-'0 ';} // carry for (int I = len1 + len2-1; I> = 0; I --) {result_add [I-1] + = result_add [I]/10; result_add [I] % = 10;} output (result_add, len1 + len2);} // output void output (int result [], int len) {printf ("Result ="); int k, n = 0; while (result [n] = 0) {n ++;} for (k = n; k
   
    

 

Result:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.