1. Given Number is Prime or not?
Maths: A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Logic: Starting from 2 divide the given number till half of the number, if the remainder is zero then the number is not a prime number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| userInput = Inputbox ( "Enter any number" ) i=2 Do While i < userInput/2 If userInput Mod i = 0 Then flag = 1 Exit Do End If i=i+1 Loop If flag = 1 Then Msgbox userInput & " is not a Prime number" Else Msgbox userInput & " is a Prime number" End If |
Output:
5 is Prime number
2. Find the factors of a given number?
Maths: The factors of a number are all those numbers that can divide evenly into the number with no remainder.
Logic: starting from 1 divide the given number till half of the number, if the remainder is zero it is a factor of the given number.
1
2
3
4
5
6
7
| userInput = Inputbox ( "Enter any number other than Prime number" ) For i = 1 to userInput/2 If userInput Mod i = 0 Then print i End If Next print userInput |
Output:
1
2
4
5
8
10
20
25
40
50
100
200
3. Find factorial of given number?
Maths: Factorial is the product of all the numbers from 1 to n, where n is the user specified number.
1
2
3
4
5
6
| factorialValue = 1 userInput = Inputbox ( "Enter any number" ) For i = 1 to userInput factorialValue = factorialValue * i Next Msgbox factorialValue |
Output:
120
120
4. Find greatest of three numbers?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| number1 = Inputbox ( "First number: Enter any postive number" ) number2 = Inputbox ( "Second number: Enter any postive number" ) number3 = Inputbox ( "Third number: Enter any postive number" ) If number1 > number2 AND number1 > number3 Then Msgbox "First number: " & number1 & " is greatest" ElseIf number2 > number1 AND number2 > number3 Then Msgbox "Second number: " & number2 & " is greatest" ElseIf number3 > number1 AND number3 > number2 Then Msgbox "Third number: " & number3 & " is greatest" Else Msgbox "All numbers are equal" End If |
Output:
Second number:
587 is greatest
5. Swap 2 numbers without a temporary variable
1
2
3
4
5
6
7
8
9
10
11
12
| number1 = Inputbox ( "First number: Enter any postive number" ) number2 = Inputbox ( "Second number: Enter any postive number" ) Print "Before swapping number 1: " & number1 Print "Before swapping number 2: " & number2 number1 = number1 - number2 number2 = number1 + number2 number1 = number2 - number1 Print "After swapping number 1: " & number1 Print "After swapping number 2: " & number2 |
Output:
Before swapping number 1:45
Before swapping number 2:65
After swapping number 1:65
After swapping number 2:45
6. Write a program to find sub datatype of a variable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| userInput1 = 44 Msgbox TypeName(userInput1) userInput2 = "44" Msgbox TypeName(userInput2) 'Anything in the double quotes is considered as string. 'For integers Don't use double quotes userInput3 = "TestNBug" Msgbox TypeName(userInput3) userInput4 = True Msgbox TypeName(userInput4) |
Output:
String
String
7. Write a program to generate Random Number
We have a predefined formula for random number generation
You need to enter the minimum and maximum limits.
You need to enter the minimum and maximum limits.
1
2
3
4
5
| min = 1 max = 100 Randomize rNumber = Int((max-min+1)*Rnd+min) Msgbox rNumber |
Output:
66