Last active
April 27, 2021 16:15
-
-
Save defrindr/8acce45e98eeb38fe7ac8ee854ef0395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void to_zero(int n) | |
{ | |
printf("angka ke-%d\n", n); | |
if(n == 0){ | |
return; | |
} | |
to_zero(n-1); | |
} | |
main() | |
{ | |
to_zero(10); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int faktorial(int x) | |
{ | |
if(x<=1) | |
return 1; | |
else | |
return x * faktorial(x-1); | |
} | |
void pascal(a, i, j, k){ | |
//terminate program | |
if( a == i ){ | |
return; | |
} | |
// print padding (semakin kebawah semakin kecil) | |
if(j <= a-i-1){ | |
printf(" "); | |
j++; | |
} | |
// print bilangan | |
if(j > a-i-1 && k<=i){ | |
printf("%ld ", faktorial(i) / (faktorial(k) * faktorial(i - k))); | |
k++; | |
} | |
// increment variable | |
if(j > a-i-1 && k > i){ | |
printf("\n"); | |
j=0; | |
k=0; | |
i++; | |
} | |
//rekursi | |
pascal(a,i,j, k); | |
} | |
int main() { | |
int a, i, j, k; | |
printf("Masukkan nilai: "); | |
scanf("%d", &a); | |
pascal(a, 0, 0, 0); | |
return 0; | |
} | |
// Output : | |
// 1 | |
// 1 1 | |
// 1 2 1 | |
// 1 3 3 1 | |
// 1 4 6 4 1 | |
// 1 5 10 10 5 1 | |
// 1 6 15 20 15 6 1 | |
// 1 7 21 35 35 21 7 1 | |
// 1 8 28 56 70 56 28 8 1 | |
// 1 9 36 84 126 126 84 36 9 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void to_n(int n, int temp) | |
{ | |
printf("angka ke-%d\n", temp); | |
if(n == 0){ | |
return; | |
} | |
to_n(n-1, temp+1); | |
} | |
main() | |
{ | |
to_n(10, 0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void is_exist(int arr[], int idx, int search) | |
{ | |
if(arr[idx] == search){ | |
printf("%d ditemukan dalam index ke %d\n", search, idx); | |
return; | |
}else if(arr[idx] == NULL){ | |
printf("Data tidak ada\n"); | |
return; | |
} | |
idx+=1; | |
is_exist(arr, idx, search); | |
} | |
main() | |
{ | |
int arr[] = {1,3,4,7,9,5,90}; | |
is_exist(arr, 0, 4); | |
is_exist(arr, 0, 80); | |
is_exist(arr, 0, 7); | |
is_exist(arr, 0, 90); | |
is_exist(arr, 0, 3); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void is_exist(int arr[], int idx, int search) | |
{ | |
if(arr[idx] == search){ | |
printf("%d ditemukan dalam index ke %d\n", search, idx); | |
return; | |
}else if(arr[idx] == NULL){ | |
printf("Data tidak ada\n"); | |
return; | |
} | |
idx+=1; | |
is_exist(arr, idx, search); | |
} | |
main() | |
{ | |
int arr[] = {1,3,4,7,9,5,90}; | |
is_exist(arr, 0, 4); | |
is_exist(arr, 0, 80); | |
is_exist(arr, 0, 7); | |
is_exist(arr, 0, 90); | |
is_exist(arr, 0, 3); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void check_prime(int is_prime, int start) | |
{ | |
// bilangan prima adalah bilangan yang hanya habis dibagi oleh 2 bilangan yaitu 1, dan dirinya sendiri | |
if(start == is_prime){ | |
printf("%d adalah bilangan prima\n", is_prime); | |
return; | |
} | |
if(is_prime % start != 0){ | |
start++; | |
}else{ | |
printf("%d bukan bilangan prima\n", is_prime); | |
return; | |
} | |
check_prime(is_prime, start); | |
} | |
main() | |
{ | |
int start = 2; | |
check_prime(5, start); | |
check_prime(25, start); | |
check_prime(6, start); | |
check_prime(9, start); | |
check_prime(11, start); | |
check_prime(7, start); | |
check_prime(19, start); | |
check_prime(81, start); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int fact(int n) | |
{ | |
if (n < 0) | |
return 0; | |
else if (n == 0) | |
return 1; | |
else if (n == 1) | |
return 1; | |
else | |
return n * fact(n-1); | |
} | |
int fact_it (int n) | |
{ | |
int temp, i; | |
temp = 1; | |
if (n < 0) | |
return 0; | |
else if (n == 0) | |
return 1; | |
else if (n == 1) | |
return 1; | |
else | |
for (i=2; i<=n; ++i) | |
temp = temp * i; | |
return (temp); | |
} | |
int main(){ | |
printf("Dengan rekursi :"); | |
printf("%d", fact(5)); | |
printf("\nDengan iterasi :"); | |
printf("%d", fact_it(5)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int faktorial(int x) | |
{ | |
if(x==1) | |
return x; | |
else | |
return x * faktorial(x-1); | |
} | |
void main() | |
{ | |
int N; | |
printf("Masukkan N = "); | |
scanf("%d", &N); | |
printf("Hasil %d! = %d\n", N, faktorial(N)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int facttail(int n, int a) | |
{ | |
if (n < 0) | |
return 0; | |
else if (n == 0) | |
return 1; | |
else if (n == 1) | |
return a; | |
else | |
return facttail(n-1,n*a); | |
} | |
int main(){ | |
printf("Rekursi tail :"); | |
printf("%d", facttail(4,5)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int faktorial(int x, int a) | |
{ | |
if(x==1) | |
return a; | |
else | |
return faktorial(x-1,x*a); | |
} | |
void main() | |
{ | |
int N; | |
printf("Masukkan N = "); | |
scanf("%d", &N); | |
printf("Hasil %d! = %d\n", N, faktorial(N,1)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void Tidak_Berhenti() { | |
printf("Ctrl-Break untuk berhenti.\n"); | |
Tidak_Berhenti(); | |
} | |
main() { | |
Tidak_Berhenti(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int fibo(int x) | |
{ | |
if (x<=0 || x<=1) | |
return x ; | |
else | |
return fibo(x-2) + fibo(x-1); | |
} | |
void main() | |
{ | |
int n, i; | |
printf("Masukkan jumlah deret = "); | |
scanf("%d", &n); | |
printf("Deret fibonanci dari %d = ", n); | |
for(i=0;i<n;i++) | |
printf("%d ", fibo(i)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void Berhenti_N_Kali(int n) | |
{ | |
static int i=0; | |
if (n<=0) return; | |
printf("%d kali\n",++i); | |
Berhenti_N_Kali(n-1); | |
} | |
main() | |
{ | |
int N=5; | |
Berhenti_N_Kali(N); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<math.h> | |
int prime(int number, int index) { | |
if (index == 1) | |
return 1; | |
else if (number % index == 0) | |
return 0; | |
else | |
return prime(number, --index); | |
} | |
void main() { | |
int num,i; | |
printf("Masukkan bilangan sampai dengan : "); | |
scanf("%d", & num); | |
printf("Deret bilangan prima : "); | |
for (i = 1; i <= num; i++) | |
if (prime(i, (int) sqrt(i))) | |
printf("%d ", i); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int pangkat(int x, int y) { | |
if (y == 0) | |
return 1; | |
else | |
return x * pangkat(x, y - 1); | |
} | |
void main() { | |
int x, y; | |
printf("Bilangan x pangkat y : "); | |
scanf("%d %d", & x, & y); | |
printf("%d pangkat %d = %d\n", x, y, pangkat(x, y)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment