#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int i=1,fact=1,n;
printf("enter a no.");
scanf("%d"n);
while(i<=n)
{
fact=i*fact;
i++;
}
printf("factorial=%d",fact);
getch();
}
1. You should write the program for it is your homework.
2. You should be able to use Google.
3. Here you are:
int fact (int n)
{
if (n<=1) return 1;
else return n*fact(n-1);
}
A C, C#/Java code segment:
int Factorial(int n) {
if (n <=1) return 1; // of course, if n < 0 should be an error...
return Factorial(n-1) * n;
}
The following program will find the factorial of any number using both recursion and non-recursion, using different precisions typically available, in this case to the Microsoft Visual Studio platform. It includes an arbitrary precision decimal package that will calculate large factorials, showing the comparison of results between the different methods. Note that stack utilization is high for large numbers, so you may need to tell your linker/binder to increase the run-time stack.
// Factorial.cpp : Defines the entry point for the console application.
//
#ifndef _WIN32_WINNT /* Microsoft stuff - possibly not needed */
#define _WIN32_WINNT 0x0600 /* Microsoft stuff - possibly not needed */
#endif /* Microsoft stuff - possibly not needed */
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h> /* possibly not needed */
/* Microsoft 32-bit iterative */
unsigned long NFactLongIterative (unsigned long N) {
unsigned long result = N;
if (N < 2) return 1;
if (N 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_s ("%d", &N);
} else {
N = atoi (argv[1]);
}
printf ("Long: %u! = %u\n", N, NFactLongIterative (N));
printf ("LongLong: %u! = %I64u\n", N, NFactLongLongIterative (N));
printf ("Recursive: %u! = %I64u\n", N, NFactLongLongRecursive (N));
printf ("Double: %u! = %.0f\n", N, NFactDouble (N));
/* note: arbitrary is exact - if the others don't match, arithmetic overflow occurred */
printf ("Arbitrary: %u! = ", N);
decimal_NFactIterative (&Decimal, N);
decimal_print_digits (&Decimal, true);
return 0;
}
/*71.PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION*/ #include<stdio.h> #include<conio.h> int fact(int); void main() { int n,f; clrscr(); printf("Enter number whose factorial is to be calculated: "); scanf("%d",&n); if(n>0) { f=fact(n); printf("factorial of %d is %d",n,f); } else printf("Factorial of numbers less than 1 does not exist"); getch(); } int fact(int n) { int facto=1; if(n>1) facto=n*fact(n-1); else return 1; return(facto); }
A flowchart for factorial of number can be made using different software's. Microsoft Word is the most popular software for beginners to make simple flowcharts.In order to draw the flowchart write the number at the top of the paper and then draw two lines under it going slightly to the sides. Decide what two numbers can be multiplied to equal that number. Keep going until you can no longer get to smaller numbers.
factorial using recursion style in c++ is unsigned int fact(unsigned int a) { if (a<=1) return 1; else { f*=fact(a-1); return a; } } when using looping structure factorial is unsigned int fact (unsigned int n) { unsigned int i,f=1; for(i=1;i<=n;i++) f*=i ; return f; }
Write a program using recursion which should take two values and display 1st value raised to the power of second value.
#include <iostream> using namespace std; int main() { int i, number=0, factorial=1; // User input must be an integer number between 1 and 10 while(number<1 number>10) { cout << "Enter integer number (1-10) = "; cin >> number; } // Calculate the factorial with a FOR loop for(i=1; i<=number; i++) { factorial = factorial*i; } // Output result cout << "Factorial = " << factorial << endl;
no answer....pls post
/*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); }
kjhk
/*71.PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION*/ #include<stdio.h> #include<conio.h> int fact(int); void main() { int n,f; clrscr(); printf("Enter number whose factorial is to be calculated: "); scanf("%d",&n); if(n>0) { f=fact(n); printf("factorial of %d is %d",n,f); } else printf("Factorial of numbers less than 1 does not exist"); getch(); } int fact(int n) { int facto=1; if(n>1) facto=n*fact(n-1); else return 1; return(facto); }
A flowchart for factorial of number can be made using different software's. Microsoft Word is the most popular software for beginners to make simple flowcharts.In order to draw the flowchart write the number at the top of the paper and then draw two lines under it going slightly to the sides. Decide what two numbers can be multiplied to equal that number. Keep going until you can no longer get to smaller numbers.
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.
123
//program to find the factorial value f any number using while loop #include<stdio.h> void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&n); i=n; while (i>=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
A recursive method (or function) is one that calls itself. Here is a popular example: The factorial function n! (read the exclamation mark as: factorial of n, or n factorial), for a positive integer, is the product of all numbers up to that number. For example, 4! = 1 x 2 x 3 x 4. In math, the factorial is sometimes defined as: 0! = 1 n! = n x (n-1)! (for n > 0) You can write a function or method, using this definition. Here is the pseudocode: function factorial(n) if (n = 0) return 1 else return n * factorial(n - 1) Note that this is not very efficient, but there are many problems that are extremely complicated without recursion, but which can be solved elegantly with recursion (for example, doing something with all files in a folder, including all subfolders).
int factorial(int n) { int i; int f=1; for(i=2;i<=n;++i) f*=i; return f; }
factorial using recursion style in c++ is unsigned int fact(unsigned int a) { if (a<=1) return 1; else { f*=fact(a-1); return a; } } when using looping structure factorial is unsigned int fact (unsigned int n) { unsigned int i,f=1; for(i=1;i<=n;i++) f*=i ; return f; }
Write a program using recursion which should take two values and display 1st value raised to the power of second value.