Skip to content

Commit 72e3893

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#122 from ashishtheman/master
Add three new codes
2 parents 21dab04 + 7ed9d6d commit 72e3893

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

misc/Collatz.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
collatz conjecture: a series for a number n in which if n even then the next number is n/2 ,but if n is odd then the next number is 3n+1.
3+
this series continues till it reaches 1*/
4+
5+
#include<stdio.h>
6+
int main()
7+
{
8+
int n,curr_no;
9+
scanf("%d",&n); //input number
10+
curr_no=n; //curr_no stores input number n
11+
while(curr_no!=1) //loop till series reaches 1
12+
{
13+
if(curr_no%2==0) //condition for even number
14+
{
15+
curr_no=curr_no/2;
16+
printf("%d->",curr_no);
17+
}
18+
else
19+
{
20+
curr_no=(curr_no*3)+1; //condition for odd number
21+
printf("%d->",curr_no);
22+
}
23+
}
24+
printf("1");
25+
return 0;
26+
}

misc/catalan.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
code for computing nth catalan number
3+
*/
4+
#include<stdio.h>
5+
long int factorial(int x) //long int for more than 10 factorial
6+
{
7+
int i;
8+
long int fac; //fac stores x factorial
9+
fac=x;
10+
for(i=1;i<x;i++) //loop to calculate x factorial
11+
{
12+
fac=fac*(x-i);
13+
}
14+
return fac; //returning x factorial
15+
}
16+
int main()
17+
{
18+
long int f1,f2,f3; //long int for more than 10 factorial
19+
int n;
20+
float C; //C is catalan number for n;
21+
scanf("%d",&n);
22+
f1=factorial(2*n);
23+
f2=factorial(n+1);
24+
f3=factorial(n);
25+
C=f1/(f2*f3); //formula for catalan number for n
26+
printf("%0.2f",C);
27+
return 0;
28+
}

misc/factorial_trailing_zeroes.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
programme for computing number of zeroes at the end of factorial of a given number n
3+
*/
4+
#include<stdio.h>
5+
#include<math.h> //including math.h header file to use pow function
6+
int main()
7+
{
8+
int i,n,test=0,count=0;
9+
//taking input number n
10+
scanf("%d",&n);
11+
12+
//looping from 1 till loop break
13+
for(i=1;;i++)
14+
{
15+
test=n/pow(5,i);//division of n by ith power of 5(storing in integer form)
16+
if(test!=0) //condition for zeroes at end corresponding individual ith case
17+
{
18+
count=count+test;
19+
}
20+
else
21+
break; //break the loop for if test=0
22+
}
23+
printf("%d\n",count);
24+
return 0;
25+
}

0 commit comments

Comments
 (0)