i want to write a program that will output all of the factorials up to a maximum number provided....if anyone can guide me, thanks...

Dani AI

Generated

A few practical points that build on the thread so far.

Nested inner loops (the approach suggested by and used in ’s solution) work, but they re-compute products repeatedly. If the goal is "print all factorials up to N" it is far more efficient to keep the previous factorial and multiply by the next integer (facti = fact{i-1} * i). For small N this is fine; for larger N the main issues become numeric range and performance of big-integer math rather than loop structure.

Use System.Numerics.BigInteger for arbitrary-size factorials and the single-accumulator (incremental) approach to avoid O(N^2) work. Example (C#/.NET):

using System;
using System.Numerics;

class Program {
  static void Main() {
    int n = int.Parse(Console.ReadLine() ?? "0");
    BigInteger fact = 1;
    for (int i = 1; i <= n; i++) {
      fact *= i;
      Console.WriteLine(i + "! = " + fact);
    }
  }
}

Watch data-type limits and precision. As other posters discovered, 32-bit int overflows around the low teens of factorials; 64-bit long holds up to 20! but not 21!. Using double avoids overflow but loses exact integer precision once values exceed the 53-bit mantissa (so it’s not reliable for exact factorials). For detection while debugging, run arithmetic inside a checked block to get an OverflowException instead of silent wraparound. See the runtime docs for details: System.Numerics.BigInteger, System.Int32 / System.Int64, and System.Double.

If extreme scale or speed is required, implementing or using libraries that use fast big-integer multiplication (Karatsuba, FFT-based methods) is the path forward (a readable overview is at the Karatsuba algorithm page). This lets you follow ’s suggestion about high-performance big-number methods without reimplementing everything from scratch.

Recommended Answers

All 9 Replies

youll need an outer loop which loops as high as the amount of factorials you want to compute, and an inner loop which will compute the factorial at the outer loops number.

pseudocode:

input n
set x = 1
loop n times
set num = 1
set y = 0
loop from x to 1
multiply num by y and re-store it into num
end loop
print num
end loop

that should do the job....unless i overlooked something

Well, first of all you need to know what a factorial is. You haven't demonstrated that you know anything on how to solve this problem yet. If you need a description on what a factorial is here's a link.
http://mathworld.wolfram.com/Factorial.html
You would have to have a loop inside of a loop in order to make this work. Here's a little bit of code to get you started on your merry way:

int factorial = 4;
            int result = 1;
            for (int i = 0; i < factorial; i++)
                result *= i+1;
            System.Console.WriteLine("result:" + result);

the factorial is saved to the int result. If you want multiple answers just put the whole thing in a for loop incrementing factorial till you get to the desired value.
Hope this is helpful. I don't want to solve the whole thing for you though...

thanks a lot for the help both of ya guys..the way i did using the code above as following and worked perfectly fine for the results required...

static void Main(string[] args)
{
int num;
int j;


Console.WriteLine("Enter the number: ");
num = int.Parse(Console.ReadLine());


for (int i = 1; i <=num; i++)
{
Console.Write("1");


int total = 1;
for (j=1; j <= i; j++)
{
Console.Write("X" + j);
total = total*j;
}
Console.WriteLine(" = " + total);


}
}

That looks great. I'm glad I could help you. I just took a math assessment and I needed a little encouragment. lol. Don't hesitate to ask if you need help on anything else.

Nice program! I ran it and for factorial 13 I got 1932053504 when it should be 6227020800.

Nice program! I ran it and for factorial 13 I got 1932053504 when it should be 6227020800.

That because he used int type to total variable which can hold 2,147,483,647 as maximum positeve value.
You can change this line:

int total = 1;

to:

double total = 1;

Thanks for that response. That's what I was going to say. This problem does require a little extra thinking for really large numbers. You need to have an understanding about basic programming.

Member Avatar for Member #46692

For really large number you need to consider using strings.

Perhaps even karbutstra's algo, for efficient multiplication.

Member Avatar for Member #46692

And if you can manage it I think there is a possibility to integrate java's big number class with vb.net or so I've read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.