3 Replies - 216 Views - Last Post: 04 October 2012 - 12:00 AM Rate Topic: -----

#1 brady42589  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

help with my algortihm

Posted 03 October 2012 - 10:23 PM

I'm designing a code for class on factorials of long, short, long integers and floats but somewhere my algortihm is messing up can someone help me. Here's my code:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

int fact(int a)
{
    int i, n, result = 1;
 
  for (i = 1; i <= n; i++)
    result = result * i;
 
  printf("Factorial of %d = %d\n", a, result);

}
      

int main(int argc, char * argv[])
{
    int num;
    printf("Enter a number:\n");
    scanf("%d", &num);
    fact(num);
    
    system("PAUSE");
    return 0;
}

This post has been edited by Salem_c: 03 October 2012 - 11:41 PM
Reason for edit:: added [code][/code] tags - learn to use them yourself


Is This A Good Question/Topic? 0
  • +

Replies To: help with my algortihm

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: help with my algortihm

Posted 03 October 2012 - 10:28 PM

You aren't using the value of 'a' anywhere in your code until you print the result. N=0.
Factorials are: number x number - 1 x number - 2 x number - 3 and so on until it reaches 1.
Was This Post Helpful? 0
  • +
  • -

#3 brady42589  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-October 12

Re: help with my algortihm

Posted 03 October 2012 - 11:34 PM

View PostDimitriV, on 03 October 2012 - 10:28 PM, said:

You aren't using the value of 'a' anywhere in your code until you print the result. N=0.
Factorials are: number x number - 1 x number - 2 x number - 3 and so on until it reaches 1.

Got it. Thanks a lot. How would make it so the printed value is a short integer or long integer but my compiler is giving strange numbers for those values
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: help with my algortihm

Posted 04 October 2012 - 12:00 AM

Show us your code where you have a short integer and a long integer. Show us, as well, the "strange numbers" that you are getting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1