Sunday, June 26, 2016

Program to identify the Prime Number and Factorial with the use of recursion.

In many Interview, your interviewer can ask you to write the program for prime number and when you will listen this question than a cute smile will come at your face because internally you are feeling that this is very easy question.

And you are right, definitely its a simple program, but run time it can be happen that might be you will not able to recall the logic behind it.

Prime Number A Prime number can be divided evenly only by 1 or itself. And it must be a whole number greater 1.

a = Inputbox("Enter the Number")
Flag =0
For i=2 to a-1
If (a mod i =0) Then
Flag =1
Exit for
End If
Next

If (Flag=1) Then
Msgbox a& " - Input Number is not Prime Number"
elseif (a="0" or a="") then
Msgbox "Invalid Input"
else
Msgbox a&" - Input Number is a Prime Number"
End If

When you Run this Program it will ask for your input, please input the Number


Now My Program will divide this number by all the numbers lower than 13 and check whether this number is prime or not



Factorial With use of Recursion:
Preview:
c=Inputbox( "Enter the Number for Factorial")
f = Fact (c)
msgbox "Factorail of number "&c&" "&"is"&" "&":"&" "&f

Function Fact (n)
If (n = 0) Then
Fact = 1
Else
Fact = n * Fact(n - 1)

End If
End Function



When you Run this Program it will ask for your input, Enter the Number for Factorial


Output would be:





No comments:

Post a Comment