/*** returns N!, assume N >= 0 ***/
int Factorial(int n) {
if (n <=1 ) /* 0! = 1! = 1 */ return 1;
/** you can place as many know values as you want, like 2!, 3!, etc **/
/** but, recursive call will do just fine, ... **/
return n * Factorial(n - 1); /** compute N * (N-1)! **/
}
/*mycfiles.wordpress.com Program for Factorial*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1;
clrscr();
printf("Enter any Number:\n\n");
scanf("%d",&n);
while(n>1)
{
fact=fact*n;
n-;
}
printf("\nFactorial is=%d",fact);
getch();
}
factorial of a given number using pointers.
#include<stdio.h>
#include<conio.h>
main()
{
int n, factorial;
printf("\nEnter a number");
scanf("%d", &n);
factcompute(n, &factorial);
printf("\nThe factorial of %d is %d", n, factorial);
}
factcompute(a, b)
int a, *b;
{
int m;
*b = 1;
for (m = 1; m <= a; m++)
*b = *b * m;
getch();
}
This program will compute N Factorial for any N, and does not suffer from overflow, because it uses a bin technique and handles each digit individually. For large values of N, you may need to increase your run-time stack size.
/* Portable arbitrary length decimal iterative */
/* one node of a linked list of digits, the first node being low-order */
struct _decimal {
int digit;
struct _decimal *next;
};
typedef struct _decimal decimal;
/* Portable arbitrary length decimal iterative */
/* Initialize the list - necessary on second pass, if main recoded */
void decimal_initialize (decimal *d, int n) {
decimal *next, *nextsave;
d->digit = n;
nextsave = d->next;
d->next = NULL;
next = nextsave;
while (next != NULL) {
nextsave = next->next;
free (next);
next = nextsave;
}
return;
}
/* Portable arbitrary length decimal iterative */
/* Append a digit at the high order position */
void decimal_add_digit (decimal *d, int n) {
decimal *new_digit = (decimal*) malloc (sizeof (decimal));
while (d->next != NULL) d = d->next;
new_digit->digit = n;
new_digit->next = NULL;
d->next = new_digit;
return;
}
/* Portable arbitrary length decimal iterative */
/* Print the digits in reverse order - recursive */
void decimal_print_digits (decimal *d, int last_digit) {
if (d->next != NULL) decimal_print_digits (d->next, false);
printf ("%d", d->digit);
if (last_digit) printf("\n");
return;
}
/* Portable arbitrary length decimal iterative */
/* multiply the list by N */
void decimal_multiply (decimal *d, int N) {
int carry = 0;
while (d != NULL) {
d->digit = d->digit * N + carry;
carry = d->digit / 10;
d->digit %= 10;
if (carry != 0 && d->next 2) {
decimal_initialize (d, 2);
return;
}
while (N > 2) {
decimal_multiply (d, N);
N--;
}
return;
}
/* Example main line */
/* Generates all variations to show differences in results */
int main (int argc, char *argv[]) {
int N;
decimal Decimal = {2, NULL};
if (argc < 2) {
printf ("Enter N (or use command line) : ");
scanf ("%d", &N);
} else {
N = atoi (argv[1]);
}
printf ("Arbitrary: %u! = ", N);
decimal_NFactIterative (&Decimal, N);
decimal_print_digits (&Decimal, true);
return 0;
}
#include
#include
void main()
{
int a,b,c=1;
clrscr();
{
printf("enter the number b=");
scanf("%d",&b);
}
for(a=b;a>=1;a--)
c=c*a;
{
printf("the factorial of the num is%d",c);
}
getch();
}
#include<iostream.h>
#include<conio.h>
int fact(int a)
{ int p=1,i;
for(i=1;i<=a;i++)
p*=1;
return (p);
}
void main()
{ int no,p;
cout<<"Enter the number";
cin>>no;
p=fact(no);
cout<<"The Factorial of "<< no <<"is"<<p;
getch();
}
#include<stdio.h>
int factorial(int *);
main()
{
int n,fact;
printf("Enter any integer number you wish to find its factorial\n");
scanf("%d",&n);
fact=factorial(&n);
printf("\n");
printf("The factorial of %d is %d\n",n,fact);
return 0;
}
int factorial(int *num)
{
int i,product=1;
for(i=1;i<=*num;i++)
product*=i;
return product;
}
#include <stdio.h> int main() { int c, n, fact = 1; printf("Enter a number to calculate it's factorial\n"); scanf("%d", &n); for (c = 1; c <= n; c++) fact = fact * c; printf("Factorial of %d = %d\n", n, fact); return 0; }
unsigned factorial (unsigned num) {
return num <2 ? 1 : num * factorial (num-1);
}
double factorial(double N){double total = 1;while (N > 1){total *= N;N--;}return total; // We are returning the value in variable title total//return factorial;}int main(){double myNumber = 0;cout > myNumber;cout
<html> <script language="vbscript"> n=cint(inputbox("Enter a number")) dim f f=1 if n<0 then Msgbox "Invalid number" elseif n=0 or n=1 then MsgBox "The factorial of given number "&n&" is :"&f else for i=n to 2 step -1 f=f*i next MsgBox "The factorial of given number "&n&" is :"&f end if </script> </html>
write an algorithm to print the factorial of a given number and then draw the flowchart. This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).
A recursive function is one that calls upon itself until a given result in the original call is met. Take a look at this example. Program Recursion; Uses crt; Var number:longint; Function Factorial(number:longint):longint; Begin if number > 0 then factorial:=number*factorial(number-1) else factorial:=1; End; Begin clrscr; readln(number); writeln(factorial(number)); readln; End. Note how the function factorial calls itself.
// returns n! int fact(final int n) { // keep track of factorial calculation in f // f starts at n, and we will multiply it by all integers less than n int f = n; // loop from n-1 down to 2 for(int i = (n - 1); i > 1; --i) { // increase our total product f *= i; } return f; }
kjhk
Factorials are the product of 1 and all the integers up to the given number. Simply put, 5 factorial or 5! = 5*4*3*2*1
/*program to find the factorial of a given number*/ #include<stdio.h> #include<conio.h> int fact(int); void main() { int n,c; printf("\n enter the number for which you want to find the factorial"); scanf("%d",&n); c=fact(n); printf("\n the factorial of the number %d is %d",n,fact); getch(); } int fact(int n) { int k; if(n==0) return(1); else k=n*fact(n-1); return(k); }
i need a pic of cuson
double factorial(double N){double total = 1;while (N > 1){total *= N;N--;}return total; // We are returning the value in variable title total//return factorial;}int main(){double myNumber = 0;cout > myNumber;cout
answer:32 programme to print factorial of a given number in c languages
no answer....pls post
The number of diagonals in an n-sided polygon is given by nC2 - n (where n is the number of sides of the polygon) or in the expanded form: factorial (n) _______________________ {factorial (2) * factorial (n-2)} substituting (n = 6) for a hexagon we get the number of diagonals as 9. Similarly, substituting (n=5) for a pentagon we get the number of diagonals as 5.
<html> <script language="vbscript"> n=cint(inputbox("Enter a number")) dim f f=1 if n<0 then Msgbox "Invalid number" elseif n=0 or n=1 then MsgBox "The factorial of given number "&n&" is :"&f else for i=n to 2 step -1 f=f*i next MsgBox "The factorial of given number "&n&" is :"&f end if </script> </html>
write an algorithm to print the factorial of a given number and then draw the flowchart. This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).
Kat
factorial n is given by formula n! = n.(n-1)....1 int i; long x; x =1; for (i=n;i>1;i--) x = x*i ; will calculate factorial. I have put x as long to avoid integer overflow. checks for n is positive etc. to be added.