/* gcc -ansi -Wall -Wextra -pedantic -s -static 0.c -o 0 */
#include <stdio.h>
int main ( )
{
int n , factorial = 1 ;
printf ( "enter the value of n\n") ;
scanf ( "%i" , & n ) ;
while ( n != 0 )
{
factorial *= n ;
n -- ;
}
printf ( "The factorial of n is\n%i\n" , factorial ) ;
return 0;
}
unsigned long factorial(unsigned long n) {
if (n = 2) return n else return n * factorial(n - 1);
} This is a recursive approach. Note very carefully that it will fail when the range of an unsigned long is exceeded. In a 32-bit environment, the maximum value of an unsigned long is 4,294,967,296, and the maximum value of n in this case is 12. The factorical of 12 is 479,001,600, which fits, but the factorial of 13 is 6,227,020,800, which does not fit. You could go unsigned long long (a 64-number), if you platform supports it, which would give you a maximum factorial of 20, which is 2,432,902,008,176,640,000. Anything larger, and you need an arbitrary precision decimal multiplier. You could implement this with linked lists.
#include <iostream>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::tolower;
long factorial(const int& N);
int main()
{
int N = 0; //factorial of N
char command = 'n';
do
{
cout << "Enter a number to calculate factorial: ";
cin >> N;
cout << endl << "Factorial of " << N << " is: " << factorial(N) << endl;
cout << "Do you want to continue (y/n)?";
cin >> command;
cout << endl;
} while ('y' 0)
{
return 1;
}
else
{
return (N * factorial(N - 1));
}
}
int main() { // Variable declarations. unsigned long int factorial = 1 , number = 1; // reads a number for finding its factorial. cout << "Input a non-negative number: "; cin >> number; while( number > 1 ) { factorial *= number * ( number - 1 ); number -= 2; } cout << "Factorial is: " << factorial << endl; system("PAUSE"); return 0;
#include <iostream>
using namespace std;
int fact(int x);
int main()
{
int x, total;
cout << "Enter a number: ";
cin >> x;
total = fact(x);
cout << "The factorial of " << x << " is: " << total << endl;
char wait;
cin >> wait;
return 0;
}
int fact(int x)
{
if(x == 1)
{
return 1;
}
return fact(x - 1) * x;
}
void main()
{
int i,b=1,n;
printf("enter the number:");
scanf("%d",&n);
for(i=n;i<=n;i--)
b=b*i;
printf("factorial number is:%d",b);
getch();
}
/*this is the program to find the factorial to n numbers*/
#include<stdio.h>
void main()
{
int n,f,factorial(int);
printf("which number facrorial you want?");
scanf("%d",&n);
f=factorial(n);
printf("The factorial upto %d is %d",n,f);
}
int factorial(int a)
{
if(a==0)
return 1;
else
return (a)*factorial(a-1);
}
Pseudo code+factorial
Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.
function factorial(n) { var x=1; while(n>1) x*=(n--); return x; }
That's not the factorial of any number. For a start, the factorial of any number greater than or equal to 2 is even, because of the factor 2. The factorial of any number greater or equal to five ends with 0. Another answer: I suspect the questioner meant to ask how to write 8*7*6*5*4*3*2*1 as a factorial. If so, then the answer is "8!"
seq 1 2 99
Pseudo code+factorial
write a program to display your name age class schoolname e-mail 2hobby based on choice
int factorial(int n) { int i; int f=1; for(i=2;i<=n;++i) f*=i; return f; }
Write a program which takes any number of days from the user. the program should display the number of years, number of months formed by these days as well as the remaining days.
//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
Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.
/*program to calculate factorial of a number*/ #include<stdio.h> #include<conio.h> void main() { long int n; int a=1; clrscr(); printf("enter the number="); scanf("%ld",&n); while(n>0) { a*=n; n--; } printf("the factorial is %ld",a); getch(); }
function factorial(n) { var x=1; while(n>1) x*=(n--); return x; }
That's not the factorial of any number. For a start, the factorial of any number greater than or equal to 2 is even, because of the factor 2. The factorial of any number greater or equal to five ends with 0. Another answer: I suspect the questioner meant to ask how to write 8*7*6*5*4*3*2*1 as a factorial. If so, then the answer is "8!"
seq 1 2 99
Write a function that implements an algorithm that checks to see if a particular integer is prime (returning a boolean). Write a program that uses that function on each number from 1 to 100, and if true, displays that number.
#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;