Wednesday, April 28, 2010

Write a VB Script to check whether a given number is prime or not?

We can use a For Next Loop or Do While Loop to check whether a number given is a prime number or not in VB Script. The challenge lies in writing a VB script without using any built in functions of VB Script.


Method 1

a = cint(Inputbox("Enter a number to check whether it is a prime number or not"))
count = 0
b = 1
Do while b<=a
aaaif a mod b = 0 Then
aaaaaacount = count +1
aaaEnd If
b=b+1
Loop
If count = 2 Then
aaamsgbox "The number "&a& " is a prime number"
Else
aaamsgbox "The number "&a& " is not a prime number"
End if


Method 2

val=Inputbox("Please enter any number")
valprime = 0
For i=2 to Int(val/2)
aaaIf val mod I = 0 Then
aaamsgbox "The number"&val&" is not a prime number"
aaavalprime=1
aaaExit for
End If
Next
If valprime=0 Then
aaamsgbox "The number"&val&" is a prime number"
End If

6 comments:

RP Singh said...

Superb.Excellent articles for learning sql...........

Asis padhy said...

Very nice reall

siddu said...

dear frnds i need domain knowledge about life insurance in detail regarding interview ...help me plz

Kanikaram Kiranpaul said...

@ Siddu: I work in Insurance domain and in an interview you might be asked about the way the policy is created. How the administrative work is carried our. How does the underwriter decides the premium for a customer like based on the risks they want on the policy etc. You can google it. In the mean time if i find some details would post it.

raghav said...

can anyone tell me why b=b+1 stat. is present there in dat while loop???

Unknown said...

can you please post the code to find out the list of prime numbers in the given number for example 1 to 100
all the prime numbers should display

Post a Comment