answersLogoWhite

0


Best Answer

/*program for finding the factorial*/

void main()

{

int a,b=1;

clrscr();

printf("Please enter any number to find its factorial\n");

scanf("%d",&a);

while(a>1)

{

b=b*a;

a--;

}

printf("factorial is %d",b);

getch();

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
User Avatar

Christine Salvador

Lvl 1
3y ago
Did not work
More answers
User Avatar

Wiki User

14y ago

//Factorial using recursive function

#include <stdio.h>

//To increase the limit, the return type should be changed to unsigned long

int fact(int number)

{

if (number == 0)

return 1;

return fact(number-1) * number;

}

int main(void)

{

int number ;

printf("Enter the number to find the factorial:");

scanf("%d",&number);

printf("The factorial is:%d",fact(number));

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include <cstdio>

int fact(int x);

int main()

{

int x, total;

printf("Enter a number: ");

scanf("%d", &x);

total = fact(x);

printf("The factorial of %d is %d", x, total);

char wait;

scanf("%s", wait);

return 0;

}

int fact(int x)

{

if(x == 1)

{

return 1;

}

return fact(x - 1) * x;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include <iostream>

unsigned int factorial(const unsigned int n)

{

// The factorial of 0 and 1 (0! and 1!) is 1.

if(n<2)

return(1);

// All other values are the product of n and n-1.

return(factorial(n-1)*n);

}

int main()

{

std::cout<<"The factorial of 5 is "<<factorial(5)<<std::endl;

}

Note that a 32-bit unsigned int can accomodate factorials up to 12! while 64-bits would allow up to 20!. Floating point types with 2-digit exponents can accomodate up to 69!. Even the scientific calculator in Windows 7 and 8 can only handle up to 3248!. To accomodate larger values you must implement a user-defined dynamic integer that uses as many bits as necessary.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

This program uses a non recursive portable factorial algorithm that supports arbitrary length decimal arithmetic in a linked list. The primary algorithm is decimal_NFactIterative. For vary large values, you may need to increase your binder stack size.

#include <stdlib.h>

#include <stdio.h>

/* 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 */

int main (int argc, char *argv[]) {

int N;

decimal Decimal = {2, NULL};

if (argc < 2) {

printf ("Enter N (or use command line) : ");

scanf_s ("%d", &N);

} else {

N = atoi (argv[1]);

}

printf ("Arbitrary: %u! = ", N);

decimal_NFactIterative (&Decimal, N);

decimal_print_digits (&Decimal, true);

return 0;

}

This answer is:
User Avatar

User Avatar

TheProgrammingX

Lvl 2
3y ago

factorial is simply multiplication of n-1 to 1

there are majorly two approaches

  1. recrsive 2. looping

both have advantages on each other.

both codes and algorithm is shred on theprogrammingx.co.uk just visit and confirm.

This answer is:
User Avatar

User Avatar

Learn bay

Lvl 8
2y ago

Let's see the factorial Program using loop.

#include

int main()

{

int i,fact=1,number;

printf("Enter a number: ");

scanf("%d",&number);

for(i=1;i

This answer is:
User Avatar

User Avatar

Madhu Rao

Lvl 5
2y ago
  1. #include

  2. int main()

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

l

This answer is:
User Avatar

User Avatar

Anonymous

Lvl 1
3y ago

hope you like the answers

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c program to find factorial non recursive?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write the Pseudocode to find the factorial of a number?

Pseudo code+factorial


Using while loop Write a program find the factors of a number?

by this program you can find the factorial: #include&lt;iostream&gt; using namespace std; main() { int n,x,f=1; cin&gt;&gt; n; x=0; while(x&lt;n) { x++; f= f*x; } cout&lt;&lt;"factorial is"&lt;&lt;f&lt;&lt;"\n"; system("pause"); return 0; }


How to write a program to find the delimiter matching in stacks?

== == using recursions: unsigned int Factorial( unsigned int x) { if(x&gt;0) { return ( x * Factorial(x-1)); } else { return(1); } } factorial: unsigned int Factorial( unsigned int x) { unsigned int u32fact = 1; if( x == 0) { return(1); } else { while(x&gt;0) { u32fact = u32fact *x; x--; } } }


How do you write a C program to calculate the factorial within recursive function?

// Iterative solution unsigned long iterativeFactorial(const unsigned long n) { unsigned long i, factorial = 1; for(i = 1; i &lt;= n; i++) { factorial *= i; } return factorial; } // Recursive solution unsigned long recursiveFactorial(const unsigned long n) { if(n &lt;= 1) { return n; } return n * recursiveFactorial(n - 1); } // Sample calls int main() { unsigned long n; printf("Enter a number to find its factorial: "); scanf("%u",&amp;n); printf("Iterative factorial: %u\n", iterativeFactorial(n)); printf("Recursive factorial: %u\n", recursiveFactorial(n)); return 0; }


What is simulation recursion in C?

The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }

Related questions

Jntu 2-2 oops through java answers?

write a java program to find factorial using recursive and non recursive


Which is faster to find factorial whether recursive or dowhile?

recursion is always slower than iteration


Write a c program to find GCD of two given integers by using both recursive n non recursive functions?

i love u darling


Write the Pseudocode to find the factorial of a number?

Pseudo code+factorial


Write a c program to find the factorial of 125?

int main (void) { puts ("18826771768889260997437677024916008575954036487149242588759823150835\ 31563316135988668829328894959231336464054459300577406301619193413805\ 97818883457558547055524326375565007131770880000000000000000000000000\ 000000"); return 0; }


Implement a program to find out the reverse of an integer using recursive functions?

Note: You may need a larger data type, factorials become very big very quickly and may cause an overflow long factorial(int x) { if(x == 1) return 1; . return((long) factorial(x-1) * x);


C program to find factorial of a no?

this is a code for calculating it recursivelly: float Factorial (float n) { if (n&lt;=1) return 1.0; else return n* Factorial(n-1); }


Using while loop Write a program find the factors of a number?

by this program you can find the factorial: #include&lt;iostream&gt; using namespace std; main() { int n,x,f=1; cin&gt;&gt; n; x=0; while(x&lt;n) { x++; f= f*x; } cout&lt;&lt;"factorial is"&lt;&lt;f&lt;&lt;"\n"; system("pause"); return 0; }


Write a program using while loop?

//program to find the factorial value f any number using while loop #include&lt;stdio.h&gt; void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&amp;n); i=n; while (i&gt;=1) { fact=fact*i; i--; } printf("The factorial value=%d",fact); } the above is a program for calculating tha factorial value of any number which is entered by the user


Write a program in java to find factorial of a number?

I suggest to use a for loop, more or less like this (assuming the parameter is "n"): product = 1; for (int i = 1; i &lt;= n; i++) { product *= i; }


Write a sell program to find the factorial of a number?

#file.sh#run as sh file.shecho "Enter the no."read ni=nr=1while [ $i -ge 1 ]dor=`expr $r \* $i`i=`expr $i-1`doneecho Factorial is $rIf the ans helps you,plz increase the trust point.


How to write a program to find the delimiter matching in stacks?

== == using recursions: unsigned int Factorial( unsigned int x) { if(x&gt;0) { return ( x * Factorial(x-1)); } else { return(1); } } factorial: unsigned int Factorial( unsigned int x) { unsigned int u32fact = 1; if( x == 0) { return(1); } else { while(x&gt;0) { u32fact = u32fact *x; x--; } } }