Program to get number factorials

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 6273 Views - Last Post: 05 November 2012 - 02:56 PM Rate Topic: -----

#1 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Program to get number factorials

Posted 05 November 2012 - 09:15 AM

Hey, im writing a code that gets the fatorial from all numbers from 1 to 300 but its not quite working as it returns that all factorials are 0. What am i doing wrong?
Code for now:
#include <stdio.h>
 
int main()
{
  int c, n, fact = 1;
 
 for(n=1;n<=300;n++)
 {
  for (c = 1; c <= n; c++)
    fact = fact * c;
 
  printf("Factorial of %d = %d\n", n, fact);
 }
  return 0;
}

Is This A Good Question/Topic? 0
  • +

Replies To: Program to get number factorials

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Program to get number factorials

Posted 05 November 2012 - 09:21 AM

Are you sure it is saying that the factorial is zero for everything? It seems to correctly show the correct factorial for n=1, and n=2.

It's just broken for the rest of the values of n.

Hint: You may want to consider re-initializing one of your variables.

Better Hint: Move code for computing the factorial into a function and it'll pretty much guarantee that you initialize your variables correctly.

This post has been edited by Skydiver: 05 November 2012 - 09:27 AM

Was This Post Helpful? 1
  • +
  • -

#3 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Re: Program to get number factorials

Posted 05 November 2012 - 09:24 AM

View PostSkydiver, on 05 November 2012 - 09:21 AM, said:

Are you sure it is saying that the factorial is zero for everything? It seems to correctly show the correct factorial for n=1, and n=2.

It's just broken for the rest of the values.

Oops, you are right, for me it shows values until 9 and correct values of 1 and 2.
Was This Post Helpful? 0
  • +
  • -

#4 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Re: Program to get number factorials

Posted 05 November 2012 - 01:15 PM

Ok, i got it fixed a little, now it gets all correct values up to about 11 or so then it crashes again. Does anyone know how to fix this? I thought of using BigIntegers but couldnt find out how to use it with C.
Code for now:
#include <stdio.h>
#include <numeric>
int main()
{
  long c, n, fact = 1;
 
 for(n=0;n<=20;n++)
 {
  for (c = 1; c <= n; c++)
   {
	   fact = fact * c;
  }
  printf("Factorial of %d = %d\n", n, fact);
  fact = 1;
  c=1;
 }
  return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is online

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Program to get number factorials

Posted 05 November 2012 - 01:23 PM

After I removed the include <numeric> the program compiled and ran without errors for me. The output wasn't correct but it did produce output.

What compiler and compiler version are you using? What is the maximum value your long can hold?

Jim
Was This Post Helpful? 1
  • +
  • -

#6 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Re: Program to get number factorials

Posted 05 November 2012 - 01:30 PM

View Postjimblumberg, on 05 November 2012 - 01:23 PM, said:

After I removed the include <numeric> the program compiled and ran without errors for me. The output wasn't correct but it did produce output.

What compiler and compiler version are you using? What is the maximum value your long can hold?

Jim

Compiler: Visual Studio 2012
Version: 11.0.50727.1 RTMREL
Long limit (Source): –2,147,483,648 to 2,147,483,647
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is online

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Program to get number factorials

Posted 05 November 2012 - 01:40 PM

As I said the program ran for me, it did start having problems after about 14 or 15. I solved this problem by switching to a long long. But if you're trying to print the Factorials to 300 you will need to find some kind of big number library because even a long long will not be large enough to hold your number. You may want to look into something like GMP, I don't know whether this can be compiled with Visual C or not, I have never tried, nor have I checked the GMP docs to see.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Re: Program to get number factorials

Posted 05 November 2012 - 01:49 PM

View Postjimblumberg, on 05 November 2012 - 01:40 PM, said:

As I said the program ran for me, it did start having problems after about 14 or 15. I solved this problem by switching to a long long. But if you're trying to print the Factorials to 300 you will need to find some kind of big number library because even a long long will not be large enough to hold your number. You may want to look into something like GMP, I don't know whether this can be compiled with Visual C or not, I have never tried, nor have I checked the GMP docs to see.

Jim

I'll see how to install a new library like GMP, i really have no idea but im sure google knows how to do it.
When i switch it to a long long data type i get all results = 0, like thisAttached Image
Do you know why is that happening? (i double checked and troubles start around 15 with me too)
Code for now:
#include <stdio.h>
int main()
{
     long long c;
	 long long n;
	 long long fact = 1;
 
 for(n=0;n<=20;n++)
 {
  for (c = 1; c <= n; c++)
   {
	   fact = fact * c;
  }
  printf("Factorial of %d = %d\n", n, fact);
  fact = 1;
  c=1;
 }
  return 0;
}

This post has been edited by Raminator: 05 November 2012 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is online

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Program to get number factorials

Posted 05 November 2012 - 01:56 PM

Yes, you need to provide the proper specifier for the printf() function the "%d" specifier is for an int, not a long or long long.

Jim
Was This Post Helpful? 1
  • +
  • -

#10 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Re: Program to get number factorials

Posted 05 November 2012 - 02:04 PM

View Postjimblumberg, on 05 November 2012 - 01:56 PM, said:

Yes, you need to provide the proper specifier for the printf() function the "%d" specifier is for an int, not a long or long long.

Jim

I cant find the specifier for long or longlong, can you help me? Im ussil %ll instead of %d but now it just wont return anything... it returns "Factorial of %d = " for all numbers. (it still says wich number the factorial was from)

This post has been edited by Raminator: 05 November 2012 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#11 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Re: Program to get number factorials

Posted 05 November 2012 - 02:15 PM

View PostRaminator, on 05 November 2012 - 02:04 PM, said:

View Postjimblumberg, on 05 November 2012 - 01:56 PM, said:

Yes, you need to provide the proper specifier for the printf() function the "%d" specifier is for an int, not a long or long long.

Jim

I cant find the specifier for long or longlong, can you help me? Im ussil %ll instead of %d but now it just wont return anything... it returns "Factorial of %d = " for all numbers. (it still says wich number the factorial was from)

Just found it :) %llu but it gets everything messed up, now almost everything is wrong :/
Was This Post Helpful? 0
  • +
  • -

#12 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 409
  • View blog
  • Posts: 885
  • Joined: 27-June 09

Re: Program to get number factorials

Posted 05 November 2012 - 02:23 PM

I think you would want %lli. I would also explicitly declare the values "long long int" although I believe this is the default for "long long"
Was This Post Helpful? 1
  • +
  • -

#13 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Re: Program to get number factorials

Posted 05 November 2012 - 02:31 PM

View Postmojo666, on 05 November 2012 - 02:23 PM, said:

I think you would want %lli. I would also explicitly declare the values "long long int" although I believe this is the default for "long long"

Still not working :/Attached Image
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is online

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Program to get number factorials

Posted 05 November 2012 - 02:32 PM

The correct specifiers should be "%d" for int, "%ld" for long, and "%lld" for long long.

Jim
Was This Post Helpful? 1
  • +
  • -

#15 Raminator   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 292
  • Joined: 16-July 12

Re: Program to get number factorials

Posted 05 November 2012 - 02:36 PM

View Postjimblumberg, on 05 November 2012 - 02:32 PM, said:

The correct specifiers should be "%d" for int, "%ld" for long, and "%lld" for long long.

Jim

Changed it, same problem still happening.Attached Image
#include <stdio.h>
int main()
{
    long long c;
	long long n;
    long long fact = 1;
 
 for(n=0;n<=20;n++)
 {
  for (c = 1; c <= n; c++)
   {
	   fact = fact * c;
  }
  printf("Factorial of %d = %lld\n", n, fact);
  fact = 1;
  c=1;
 }
  return 0;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2