Thread: Allowing larger numbers

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    Allowing larger numbers

    If I wanted to improve this program that would allow me to enter larger integers than 10, what would I do. I think I would use long. Any help or ideas??


    #include <stdio.h>

    int fact(int n);

    int main(void) {
    int current;

    printf("Enter a positive integer [to terminate enter non-positive] > ");
    scanf("%d", &current);
    while (current > 0) {
    printf("The factorial of %d is %d\n", current, fact(current));
    printf("Enter a positive integer [to terminate enter non-positive] > ");
    scanf("%d", &current);
    }
    }

    /* n is a positive integer. The function returns its factorial */
    int fact(int n) {
    int lcv; /* loop control variable */
    int p; /* set to the product of the first lcv positive integers */

    for(p=1, lcv=2; lcv <= n; p=p*lcv, lcv++);
    return p;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    When calculating numbers that grow so quickly you either need to set a hard limit based on the available type sizes or use a library that allows for arbitrary length numbers. The available types can only hold so much, and if you're trying to write portable code then your limits are even smaller.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    Ok, so how do I do that. I looked at the libraries and there are over 122K search replies from "library" in our response?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Ok, so how do I do that.
    It depends on what you want to do. The library approach is by far the easiest, but you'll need to go to a place where you can download the library and install it. Just looking at the google search hits page won't do you much good.

    If you want to write your own (never a bad idea when learning) then I suggest looking at the previous contests that we've held. The results of one of them can be adapted to suit your needs. The basic idea is to take an array of values and manually perform mathematical operations on it, thus simulating very large numbers.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    ????

    I don't know what you mean by contests, and if I found the right library I probably wouldn't know that. Keep in mine I have been in a C class for 3 weeks, not 3 years.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't know what you mean by contests
    While reading this post, hit the back button on your browser. Then do it again. Look for a board called Contests Board. It's under General Discussions. Look around and you'll find contest entries for arbitrary precision integers, that's what you want.

    >and if I found the right library I probably wouldn't know that
    Bummer.

    >Keep in mine I have been in a C class for 3 weeks, not 3 years.
    Then you would be better off placing a limit on N than implementing arbitrary length factorials.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to reverse numbers with sizeof operator
    By JoelearningC in forum C Programming
    Replies: 13
    Last Post: 03-09-2008, 11:53 AM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM