Anda di halaman 1dari 8

Pascal Triangle

a. Background
Dalam tugas ini kami diberi tugas untuk mengerjakan bagaimana cara memunculkan
hasil dari segitiga pascal. Dengan ini kami akan tau bagaimana hasil dari jalannya
program ini seleain mengeluarkan angka, program ini juga mengeluarkan pola dalam
bentuk segitiga. Dengan itu pengguna akan mudah dalam mempelajari terutama
apabila dalam matematis menemukan pangkat yang cukup besar.
b. Pseudocode
1. Declare variable i, j as integer
2. Declare variable pascal as an array with the size of integer r and c
3. Input the size of Pascal Triangle and save the value to r and c
4. If r < 1
5. Write “must be higher than or equal to 1”
6. Repeat to step 3 until r > 1
7. For i = 1 to r
8. For j = 1 to c
9. If j = 1 or j = i
10. Set pascal[i,j] equals to 1
11. Else pascal[i,j] = pascal[i-1,j-1] + pascal[i-1,j]
12. Increase j by 1
13. Increase i by 1
14. Write all elements of pascal

c. Source Code
#include <stdio.h>
long fun(int y)
{
int z;
long result=1;
for (z=1 ; z<=y ; z++)
result = result*z;

return (result);
}
int main()
{
int x, y, z;
printf("masukkan row yang kamu inginkan :");
scanf("%d", &y);
for (x=0 ; x<y ; x++)
{
for (z=0 ; z <= (y - x - 2); z++)
printf(" ");
for (z=0 ; z<=x ; z++)
printf("%3d",fun(x)/(fun(z)*fun(x-z)));

printf("\n");
}
return 0;
}
d. explanation
e. result
Prime Number

a. Background
Dalam tugas ini yang melatarbelakangi yaitu bagaimana agar menemukan bilangan
prima dalam waktu yang cepat. Dengan itu maka kami akan mengetahui bilangan-
bilang prima dimulai dari 2.

b. Pseudocode
1. Declare variable i, j as integer
2. Declare variable prime as an array with the size of integer n
3. Input how many prime numbers to be printed and save it to variable n
4. If n < 1
5. Write “must be higher than or equal to 1”
6. Repeat step 3 until n>1
7. Set prime[1] equals to 2
8. If n = 1
9. Write elements of prime
10. Else j = prime[1]+1
11. For i = 2 to n
12. If j mod all elements of prime is not zero
13. prime[i] = j
14. Increase i by 1
15. Increase j by 1
16. Write all elements of prime

c. Source Code
#include <stdio.h>

int main()
{
int n, i =3, count, c;
printf("enter the number of prime number required\n");
scanf("%d",&n);

if(n>=1){
printf("first %d prime number are :\n",n);
printf("2\n");
}

for (count = 2 ; count <=n ;)


{
for (c=2 ; c<=i-1 ; c++)
{
if(i%c == 0)
break;
}
if ( c==i)
{
printf("%d\n", i);
count++;
}
i++;
}

return 0;
}
d. Explanation
e. Result
Fibbonaci
a. Background
b. Pseudocode
1. Declare variable i as integer
2. Declare variable fibonacci as array with the size of integer n
3. Input how many numbers of Fibonacci sequence to be printed and save the value to
n
4. If n < 1
5. Write “must be greater than or equal to 1”
6. Repeat step 3 until n >=1
7. For i = 1 to n
8. If i < 3
9. fibonacci[i] = 1
10. Else fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]
11. Increase i by 1
12. Write all elements of fibonacci

c. Source Code
#include <stdio.h>

int fibonacci (int n);

int main(void){
int angka;
printf("masukkan angka : ");
scanf("%d", &angka);
if (angka<=0){printf ("tidak terdefinisi");}
else if (angka>100){printf("eror");}
else {printf("%d", fibonacci(angka));}
}
int fibonacci(int n){

if (n==1) {return 1;}


else if (n==2){return 2;}
else {
return fibonacci(n-1) + fibonacci(n-2);}
}
d. Explanation
e. Result
Insertion Sort
a. Background
b. Psudocode
1. Declare variable
2. Declare variable insertion as an array with the size of integer m
3. Declare variable unsorted as an array with the size of integer n
4. Read input and save it to unsorted array
5. Set m equals to n
6. If n < 1
7. Write “give more input, please”
8. Repeat to step 3
9. If n = 1
10. Print the element of unsorted array
11. Set insertion[1] equals to unsorted[1]
12. For i = 1 to n
13. For j = 1 to i
14. If insertion[j] is higher than unsorted[i]
15. Set k equals to j
16. While k < i
17. Set insertion[k+1] equals to insertion[k]
18. Increase j by 1
19. Increase i by 1
20. Print all elements of insertion array

c. Source Code
#include<stdio.h>
int main(){

int i,j,s,temp,a[20];

printf("Enter total elements: ");


scanf("%d",&s);

printf("Enter %d elements: ",s);


for(i=0;i<s;i++)
scanf("%d",&a[i]);

for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}

printf("After sorting: ");


for(i=0;i<s;i++)
printf(" %d",a[i]);

return 0;
}
d. Explanation
e. Result

Anda mungkin juga menyukai