Calculation of factorial in different algorithms
1. Dynamic programming approach.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<malloc.h>
#include<ctype.h>
#include<limits.h>
#include<stdbool.h>
int main()
{
int n;
scanf("%d",&n);
long long fact[n+1];
fact[1]=1;
int i;
for(i=2;i<=n;i++)
{
fact[i]=i*fact[i-1];
printf("%d!=%lld\n",i,fact[i]);
}
}
/* Result of calculating factorial when n=20
20
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
10!=3628800
11!=39916800
12!=479001600
13!=6227020800
14!=87178291200
15!=1307674368000
16!=20922789888000
17!=355687428096000
18!=6402373705728000
19!=121645100408832000
20!=2432902008176640000
--------------------------------
Process exited after 1.041 seconds with return value 20
Press any key to continue . . .
20
2432902008176640000
--------------------------------
Process exited after 1.408 seconds with return value 20
Press any key to continue . . .
*/
2. Ordinary method for calculating n!
int main()
{
int n,t;
scanf("%d",&n);
t=n;
long long fact=1;
while(n)
{
fact*=n;
n--;
}
printf("%d!=%lld\n",t,fact);
}
/* Yonekey metod netijesi:
20
20!=2432902008176640000
--------------------------------
Process exited after 1.026 seconds with return value 24
Press any key to continue . . .
*/
3. Recursion
long long fact(int n)
{
if (n==1) return 1;
else return (n*fact(n-1));
}
int main()
{
int n;
scanf("%d",&n);
printf("%lld\n",fact(n));
}
/* Recursion method n=20!
20
2432902008176640000
--------------------------------
Process exited after 0.8331 seconds with return value 20
Press any key to continue . . .
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<malloc.h>
#include<ctype.h>
#include<limits.h>
#include<stdbool.h>
int main() {
char archiveName[]="myzip.zip";
char outputDir[]=".";
char *commandTemplate="7z x %s -p%d -o%s -y";
int password;
//puts(commandTemplate);
for(password=1; password<=9999; password++) {
char command[100];
//char cmm[100];
sprintf(command,commandTemplate,archiveName,password,outputDir);
int result=system(command);
if(result==0) {
printf("Using the password %d unzipped file, success!\n",password);
break;
} else {
printf("Using the password %d failed)\n",password);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<malloc.h>
#include<ctype.h>
#include<limits.h>
#include<stdbool.h>
int main()
{
int a,b;
char *format;
scanf("%d%d",&a,&b);
format="a=%d,b=%d\n";
printf(format,a,b);
int width=7,precision=2;
float number=90.0;
printf("\n%*.*f",width,precision,number);
}