Kamis, 15 Maret 2012

StrukturData _Praktikum5


Tentang Factorial

Latihan Soal
Praktikum 5

Penyelesaianya
++ sedikit reverensi

1.Write a function with the

following prototype that returns the sum of the digits of an integer.
int sumOfDigit (int x){
if (x==0)
return 0;
if (x<0) return -1* sumOfDigit (-1*x); //ketika negatif
else
return x mod 10 + sumOfDigit (x/10);
}

4.Find the sum of the integers from 1 through n. Use recursion.
int sum(int x){
if (x==0)
return 0;
if (x<0) return sum (-1*x); //ketika negatif
else
return x + sum(x-1);
}
6. Count the number of zeros in an array of integers. Use recursion.
int zero(int x){
if (x==0)
return 1;
if (x<10)
return 0;
if (x%0)
return zero (x/10);
else
return (1+zero (x/10));
}

Tidak ada komentar:

Posting Komentar