Program 5 Write a Vb.net program to accept n numbers through InputBox and count the number of Armstrong and Perfect numbers among them and display their count by using messagebox.

Program 4 Write a Vb.net program to accept n numbers through InputBox and count the number of Armstrong and Perfect numbers among them and display their count by using messagebox.
Solution






Public Class P5
    Dim n, a(20), cntArmstrong, cntPerfect As Integer
   
    Private Sub btnEnterNumer_Click(sender As Object, e As EventArgs) Handles btnEnterNumer.Click
        cntArmstrong = 0
        cntPerfect = 0
        n = CInt(InputBox("How Many Number You Want To Accept(bellow 20)"))
        For index = 0 To n - 1
            a(index) = CInt(InputBox("Enter Number" & index))
        Next
        For index = 0 To n - 1
            If isArmstrong(a(index)) Then
                cntArmstrong += 1
            End If
            If isPerfect(a(index)) Then
                cntPerfect += 1
            End If
        Next

        lblCntArmstrong.Text = cntArmstrong & " are Armstrong Number"
        lblCntPerfect.Text = cntPerfect & " are Perfect Number"
    End Sub
    Private Function isArmstrong(ByVal value As Integer) As Boolean
        Dim num, reminder, sum As Integer
        sum = 0
        num = value
        While num > 9
            reminder = num Mod 10
            sum += (reminder * reminder * reminder)
            num \= 10
        End While
        sum += (num * num * num)
        If sum = value Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Function isPerfect(ByVal value As Integer) As Boolean
        Dim num As Integer
        Dim flag As Boolean = False
        For num = value To 1 Step -1
            If num Mod 2 = 0 Then
                flag = True
                Exit For
            End If
        Next
        Return flag
    End Function

End Class

No comments:

Post a Comment