The shortest C program to calculate the multiplication of large numbers

Source: Internet
Author: User

#include <stdio.h>char s[99],t[99];int m,n;void r (int i,int c) {  int j=0,k=i;  while (k) c+=s[j++]*t[k---1];  if (i) r (I-1,C/10);  printf ("%d", c%10);} void Main () {  gets (s), gets (t);  while (S[n]) s[n++]-=48;  while (T[m]) t[m++]-=48;  R (m+n-1,0);}



Description
1. This program receives 2 integer input from the keyboard, calculates their product, and outputs the result.
The total length of the two integers entered cannot be greater than 99.
2. This program is no big deal, just for fun.
3. The main goal of this program is to use the shortest possible code to achieve large number multiplication. The above code
Can be compiled and run under VC. Compile under GCC to omit the # include statement and the Void keyword,
Remove carriage returns and unnecessary spaces, with a total length of only 196 bytes.
In addition, the program deliberately avoids using arrays to store intermediate and final results.
To do this, recursive procedures are used, and the use of recursion simplifies the code.
4. In the actual work, do not write such procedures, otherwise you will be scolded to death.
5. Do not use this procedure to test your students and interviewers, even if he claims to be proficient in C language.

The characteristics of such a shortest program
1. Global variables are often used, and the advantage of global variables is that
1). Automatic initialization of arrays and a single variable of 0 saves some variable initialization statements.
2). Initializing the array to 0 also makes the logic simpler, eliminating the judgment of certain boundary values.
3). In subroutines, you can omit certain parameter definitions and parameter passing statements by using global variables directly.

2. In expressions, a large number of operators, such as "+ +" or "--", are often used to
With stone effect, can shorten the code length effectively. But at work, I strongly oppose the use of
This type of operator.

3. In comparison statements, it is very rare to use statements such as if (I>=C), but instead use the "if (i)"
notation, which is 3 fewer letters than ">=c".

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The shortest C program to calculate the multiplication of large numbers

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.