Untitled - MARKUP 3.61 KB
                                
                                    zad 1
Module Module1

    Function silnia_rek(x As Integer) As Integer
        If x <= 1 Then 'albo x = 0 or x = 1
            Return 1
        Else
            Return x * silnia_rek(x - 1)
        End If
    End Function

    Function silnia_iter(x As Integer) As Integer
        Dim n As Integer = 1
        Dim silnia As Integer = 1

        Do While n <= x
            silnia *= n
            n += 1
        Loop

        Return silnia
    End Function

    Sub Main()
        Dim x As Integer
        Console.Write("podaj x: ")
        x = Console.ReadLine
        Console.WriteLine("rekurencyja: {0}! = {1}", x, silnia_rek(x))
        Console.WriteLine("iteracyjnie: {0}! = {1}", x, silnia_iter(x))

        Console.ReadKey()
    End Sub

End Module

zad 2.
Module Module1

    Function fibb_reku(x As Integer) As Integer
        If x = 0 Then
            Return 0
        ElseIf x = 1 Then
            Return 1
        End If

        Return fibb_reku(x - 1) + fibb_reku(x - 2)
    End Function

    Function fibb_itera(x As Integer) As Integer
        Dim a As Integer = 0
        Dim b As Integer = 1

        For i As Integer = 0 To x - 1
            Dim temp As Integer = a
            a = b
            b = temp + b
        Next

        Return a
    End Function

    Sub Main()
        Dim x As Integer
        Console.Write("podaj x: ")
        x = Console.ReadLine
        Console.WriteLine("rekurencyja: {0}! = {1}", x, fibb_reku(x))
        Console.WriteLine("iteracyjnie: {0}! = {1}", x, fibb_itera(x))

        Console.ReadKey()
    End Sub

End Module

zad 3.
Module Module1

    Function fun_rek(x As Double, n As Integer) As Integer
        If n = 0 Then
            Return 1
        End If
        Return x * fun_rek(x, n - 1)
    End Function

    Function fun_ite(x As Double, n As Integer) As Integer
        Dim l As Integer = 1
        Dim wynik As Double = 1

        Do While l <= n
            wynik *= x
            l += 1
        Loop

        Return wynik
    End Function

    Sub Main()
        Dim x As Double
        Dim n As Integer
        Console.Write("podaj x: ")
        x = Console.ReadLine
        Console.Write("podaj n: ")
        n = Console.ReadLine
        Console.WriteLine("rekurencyjnie {0}^{1}: {2}", x, n, fun_rek(x, n))
        Console.WriteLine("iteracyjnie {0}^{1}: {2}", x, n, fun_ite(x, n))

        Console.ReadKey()
    End Sub

End Module

zad 4.
Module Module1

    Function suma_ite(tab() As Integer) As Integer
        Dim suma As Integer = 0
        For Each x In tab
            suma += x
        Next

        Return suma
    End Function

    Function suma_rek(tab() As Integer, n As Integer, suma As Integer) As Integer
        If n >= 0 Then
            suma = suma + tab(n) + suma_rek(tab, n - 1, suma)
        End If
        Return suma
    End Function

    Sub Main()
        Dim tab() As Integer = {1, 2, 3, 4, 5}
        Console.Write("elementy tablicy: ")

        For Each x In tab
            Console.Write("{0} ", x)
        Next

        Console.WriteLine()

        Dim n As Integer = tab.Length
        Dim suma As Integer = 0
        Console.WriteLine("rekurencyjnie suma = {0}", suma_rek(tab, n - 1, suma))
        Console.WriteLine("iteracyjnie suma = {0}", suma_ite(tab))

        Console.ReadKey()
    End Sub

End Module
                                
                            

Paste Hosted With By Wklejamy.pl