Add two large numbers

Source: Internet
Author: User

Two large numbers are added by a string array

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 //maximum number of digits in large numbers6 #defineMAX 1007 8 //and the maximum number of digits9 #defineN 101Ten  One intMainintargcChar*argv[]) A { -     intI//Working pointer -     intNUM1, num2, num3;//records the number of digits of a, B, two, and the number of digits after the sum of two numbers the     intc =0;//Record Rounding -     intSum//record the sum of a bit -  -     CharTemp[n];//the sum of the records added +     //allocating memory dynamically for two numbers -     Char*a = (Char*)malloc(max*sizeof(Char)); +     Char*b = (Char*)malloc(max*sizeof(Char)); A  atprintf"Enter the first number:\n"); -scanf"%s", a); -printf"Enter the second number:\n"); -scanf"%s", b); -  -NUM1 =strlen (a); innum2 =strlen (b); -  to     //a higher number of record digits +     if(Num1 <num2) -     { the         Char*p; *         inttemp; $p =A;Panax NotoginsengA =b; -b =p; the  +temp =NUM1; ANUM1 =num2; thenum2 =temp; +     } -num3 = NUM1;//record a larger number of digits $  $     //Add -      for(i =0; i < NUM1; i++) -     { the         //The working pointer is less than the smaller number of digits -         if(I <num2)Wuyi         { the             //the sum of the records added -sum = a[num1-i-1]-'0'+ b[num2-i-1]-'0'+C; Wu             if(Sum >=Ten)//and greater than ten -             { Aboutc =1; $Temp[i] = (Char)((int)('0') + Sum-Ten); -             } -             Else//and less than ten -             { Ac =0; +Temp[i] = (Char)((int)('0') +sum); the             } -         } $         Else{//I >= num2 thesum = a[num1-i-1] -'0'+C; the             if(Sum >=Ten) the             { the                 if(i = = (NUM1-1)) -num3++; inc =1; theTemp[i] = (Char)((int)('0') + Sum-Ten); the}Else{ Aboutc =0; theTemp[i] = (Char)((int)('0') +sum); the             } the         } +     } -  the     //Reverse print two number of andBayiprintf"The sum is:\n"); the      for(i = num3-1; I >=0; i--) theprintf"%c", Temp[i]); -printf"\ n"); -  the     return 0; the}

Operation Result:

Add two large numbers

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.