LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-22-2003, 09:50 PM   #1
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Rep: Reputation: 15
c programming pointers (a factorial calculation)


Hi everyone,
I'm trying to write a program to calculate the factorial of a given integer. I would like to get some pointers on how to go about doing it. I was thinking of using a for loop but i haven't been able to put the right expressions in. here is where i am at so far:

#include <stdio.h>

int main(void)
{
int x;
printf("Enter number for factorial calculation: ");
scanf("%d", &x);

/*and here is where i am stuck*/


i well definitely appreciate any pointers in the right direction,
Thanks,
Randall
 
Old 02-22-2003, 09:57 PM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 45
Think about the way you would calculate a factorial by hand. Say, 5 factorial. "Okay, that's 5x4x3x2x1." In other words, it's n times n-1 times n-2 ... all the way down to 1 (or 2, if you want to save the redundant step of multiplying by 1).

So you are on the right track with a for loop. How about, for i from n to 1, i = i - 1... multiply the total by i.
 
Old 02-22-2003, 10:28 PM   #3
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
that's where i was headed,
and here's what i have so far

#include <stdio.h>

int main(void)

{
int n,i, factorial;
printf("Enter number for factorial calculation: ");
scanf("%d", &n);

for (i = n; i <= 0 ; i --)
factorial = n*(n-1);
printf("%d\n", factorial);

printf("%c", getchar());
return (0);
}


but that just returns garbage
i get 134513294

thanks for the help so far!

Randall
 
Old 02-23-2003, 06:06 AM   #4
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Code:
#include<stdio.h>

int Factorial(int x) {
  if (x > 1)
    x *= Factorial (x - 1);
  return x;
}

int main() {
  int x;
  printf("Enter a number: ");
  scanf("%d",&x);
  printf("The factorial is %d\n", Factorial(x) );
  return 0;
}
 
Old 02-23-2003, 06:33 AM   #5
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Or else:

Code:
#include<stdio.h>

int main() {
  int num;
  int factorial;

  printf("Enter a number: ");
  scanf("%d",&num);

  for (factorial = num; num > 1; --num) {
    factorial *= (num - 1 );
  }

  printf("The factorial is: %d\n", factorial);
  return 0;
}
Note that:
Code:
factorial *= (num - 1);
Expands (by the compiler) into:
Code:
factorial = factorial * (num - 1);

Last edited by GtkUser; 02-23-2003 at 06:35 AM.
 
Old 02-23-2003, 12:47 PM   #6
Randall
Member
 
Registered: Oct 2001
Location: Ontario, Canada (for now, i'm from NJ )
Distribution: Redhat 7.2
Posts: 106

Original Poster
Rep: Reputation: 15
Thanks a lot!!
That really helped, i was getting really confused about the middle statement that terminated the loop, and i believe i made an error with the decrementation. i don't think i would have figured out the assignment of factorial *= (n -1); though

anyway, thanks a lot for the help, i really appreciate it,
i added a if statement to check for negetaive numbers so ... it works pretty well now.


best regards,
Randall
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C programming pointers & recursive exvor Programming 8 08-16-2005 09:24 AM
Help needed regarding bandwidth calculation russoue Programming 2 07-13-2005 09:51 AM
C Programming: Help with Pointers to Functions devinWhalen Programming 2 03-04-2005 01:00 PM
Drive space calculation? Redhatusr Linux - Software 2 01-07-2005 01:11 PM
c math calculation alaios Programming 3 06-01-2004 01:46 AM


All times are GMT -5. The time now is 03:09 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration