C Program to Support Arbitrary Precision Arithmetic

«
»
This C Program creates a support for infinite precision arithmetic which allows storage of large integers which is beyond the range of the integral limit. C is architecture dependent so primitive data type such as int is not capable of storing large integral values. So we make use of linked list in order to create and store such great integral values.

Here is a source code of the C program that supports infinite precision arithmetic and store a number as list of digits. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Support Infinite Precision Arithmetic & Store a
  3.  * Number as a List of Digits  
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8.  
  9. struct node
  10. {
  11.     int num;
  12.     struct node *next;
  13. };
  14.  
  15. int feednumber(struct node **);
  16. void release(struct node **);
  17. void display(struct node *);
  18.  
  19. int main()
  20. {
  21.     struct node *p = NULL;
  22.     int pcount = 0, qcount = 0;
  23.  
  24.     printf("Enter number of any length\n");
  25.     pcount = feednumber(&p);
  26.     printf("Number of integers entered are: %d\n", pcount);
  27.     printf("Displaying the number entered:\n");
  28.     display(p);
  29.     release(&p);
  30.  
  31.     return 0;
  32. }
  33.  
  34. /*Function to create nodes of numbers*/
  35. int feednumber(struct node **head)
  36. {
  37.     char ch, dig;
  38.     int count = 0;
  39.     struct node *temp, *rear = NULL;
  40.  
  41.     ch = getchar();
  42.     while (ch != '\n')
  43.     {
  44.         dig = atoi(&ch);
  45.         temp = (struct node *)malloc(sizeof(struct node));
  46.         temp->num = dig;
  47.         temp->next = NULL;
  48.         count++;
  49.         if ((*head) == NULL)
  50.         {
  51.             *head = temp;
  52.             rear = temp;
  53.         }
  54.         else
  55.         {
  56.             rear->next = temp;
  57.             rear = rear->next;
  58.         }
  59.         ch = getchar();
  60.     }
  61.  
  62.     return count;
  63. }
  64.  
  65. /*Function to display the list of numbers*/
  66. void display (struct node *head)
  67. {
  68.     while (head != NULL)
  69.     {
  70.         printf("%d", head->num);
  71.         head = head->next;
  72.     }
  73.     printf("\n");
  74. }
  75.  
  76. /*Function to free the allocated list of numbers*/
  77. void release (struct node **head)
  78. {
  79.     struct node *temp = *head;
  80.  
  81.     while ((*head) != NULL)
  82.     {
  83.         (*head) = (*head)->next;
  84.         free(temp);
  85.         temp = *head;
  86.     }
  87. }

$ gcc infinitedigits.c 
$ ./a.out 
Enter number of any length
932429842394820948234948098391830283192193818310398291830131209
Number of integers entered are: 63
Displaying the number entered:
932429842394820948234948098391830283192193818310398291830131209

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.