Quantcast
Channel: The Grey Fire Forum
Viewing all articles
Browse latest Browse all 15

Algorytm obliczania silni C++

$
0
0
Algorytm obliczania silni w C++

Ogólnym założeniem tego przykładu jest zastosowanie funkcji rekurencyjnej: "Polegającej na wywoływaniu samej siebie". Następuje tutaj takie wywołanie samej funkcji wewnątrz jej samej. Działa do n=16, powyżej następuje wyjście z zakresu liczb integer.
Kolorem czerwonym zaznaczyłem moment rekulencyjności funkcji, zaś zielonym moment wykorzystania rekurencyjnej funkcji.

[html]
<hr />
#include <iostream>
#include <conio.h>

using namespace std;

int silnia (int n){

if (n == 0){
       return 1;
    }
else {
<font color="red">return n*silnia(n-1);</font>
    }
}

void main () {

int n;
cout << \"Podaj liczbe N: \";
cin >> n;
cout << \"Wynik to: \" << <font color="#339966">silnia(n);</font>
getch();
}
[/html]

Viewing all articles
Browse latest Browse all 15

Trending Articles