fatorial en c

October 20, 2010 at 11:50:48
Specs: Windows Vista
I have this program to obtain the factoail of a number and no mather wich number i put the result is always 1, what is wrong? thanks for you help!

#include <stdio.h>
double factorial(n)
{
double resultado=1;
double i;
for(i = 1; i >= n; i++){
resultado = resultado * i;
}
printf("resultado %g", resultado);
return resultado;
}
main ()
{
int n;
printf("ingresa el numero del que quieres obtener el factorial\n");
scanf("%d", n);
printf("El factorial es %g", factorial(n));
}


See More: fatorial en c

Report •


#1
October 20, 2010 at 12:18:25
I'm no programmer. Sone guys here can do this in their sleep.

But why DOUBLE?


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
October 20, 2010 at 13:00:47
I try whit int but I obtain the same

Report •

#3
October 20, 2010 at 22:45:11
for(i = 1; i >= n; i++)

It returns 1 because resultado is initialized to 1. The loop won't execute for any value greater than 1.

i <= n

Your factorial is likely to grow very quickly... you might want to use the unsigned int or unsigned long and include limits.h to help prevent overflow.

HTH

-------------------------------------------
I have no aspiration for preaching. ;-)


Report •

Related Solutions

#4
October 20, 2010 at 23:50:21
Hi, sorry Im a begginner programer, I try what you say whit the loop whit i<=n and the loop never ends and if don´t initialize the resultado then i get a number like 19382983982 :(

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

int factorial(int n)
{
int i,resultado;

for(i = 0; i <= n; i++){
resultado = resultado * (i+1);
}
printf("resultado %d", resultado);
return resultado;
}
main ()
{
int n;
printf("ingresa el numero del que quieres obtener el factorial\n");
scanf("%d", n);
printf("El factorial es %d", factorial(n));
return 0;
}


Report •

#5
October 21, 2010 at 00:03:45
I have the solution!!!!! :) :) I try whit while and thats works

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


int factorial(n)
{
int resultado = 1;
int i;
while( n > 0){
resultado = resultado * n;
n--;
}
return resultado;
}
main ()
{
int n;
printf("ingresa el numero del que quieres obtener el factorial\n");
scanf("%d", &n);
printf("El factorial es %d", factorial(n));
return 0;
}

Thanks you everybody for your help!


Report •

#6
October 21, 2010 at 00:41:03
After a great deal of bumbling around, I came up with this.

==================================

#include <iostream.h>

main()
{
int n=0;
unsigned long f=1;

cout << "factorial for? \n";
cin >> n;
for (int i = 1; i <= n; ++i )
{
f = f * i ;
}
cout << f;
return 0;
}


=====================================
Life is too important to be taken seriously.

M2


Report •

#7
October 21, 2010 at 01:11:54
You were pretty close. It's always great to solve things for yourself though - good job. :)

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

int factorial(int n)
{
int i,resultado = 1;

for(i = 1; i <= n; i++){
   resultado  *= i;
}
printf("resultado %d", resultado);
return resultado;
}

int main(void)
{
int n;
printf("ingresa el numero del que quieres obtener el factorial\n");
scanf("%d", &n);
printf("El factorial es %d", factorial(n));
return 0;
}

-------------------------------------------
I have no aspiration for preaching. ;-)


Report •

#8
October 21, 2010 at 07:23:09
The power of C:
unsigned factorial(int n) {
    unsigned resultado = n;
    while (--n)
        resultado *= n;
    return resultado;
}

How To Ask Questions The Smart Way


Report •

#9
October 21, 2010 at 07:51:56
thanks for you help :)

Report •

#10
October 21, 2010 at 07:55:51
thanks! great example! well i´ll try to be better more and more whit practice, No promisse let you rest my friends ; ) ; ) :)

Report •


Ask Question