I’m a student and our instructor asked us to create a program that would determine if a number is a prime number or not. In addition, if the input is not a whole number, the program should return an error stating "You have entered a wrong number!".
I’m a little clueless right now and would like some help on this.
Any programming language can be used in creating the program but I hope you guide me through the simplest one.
Thank you so much.
Answered By
Lycan
0 points
N/A
#122326
I need a little help with programming
Helle Mate!
There are a lot of ways you can solve and code this problem using Visual Basic, This very simple project described below creates a single function that will return true or false values after testing the number passed in.Â
Paste in a Form Module
Â
Private Sub Command1_Click()
p = InputBox("enter a no.", "Checking number")   <——— This will take a no. from user
switch1 = False     Â
Â
For i = 2 To p – 1         <—– We are checking all no. smaller then entered no. except 1 and itself.
Â
If p Mod i = 0 Then       <—– If the no. gets divided by a no. less then it leaving no reminder but if it gets perfectly divided.
switch1 = True           <—— Then the counter or alarm is on
End If
Â
Next i             Â
Â
If switch1 = True Then                    <— if "switch1" counter or alarm is true because it gets perfectly divided by a number.
MsgBox ("Not a Prime Number")
Else                                    <—– Else it will be a prime number
MsgBox ("A Prime Number")
Else
Msgbox ("This is not a Number')
End If
Â
End Sub
Â
this is a very simple code that you can try experimenting on. Just PM me if you have questions I hope this helps.
Lycan
I need a little help with programming
Hey friend,
I know a simple solution for your programming problem. and I will provide you a step by step guide on how to create a program in visual basic 6.0 that will determine if the inputed number is a prime numbers or not. This code is sure working. I tested them first and I also added validation.
Step 1.
Open your Visual Basic 6.0 Programming software. Create a standard .EXE project.
Â
Step 2
Create 2 Textbox and a command button. You will input a number in text1 and when you clicked the command button the result will be displayed at text2.
Â
.Â
Â
Now Lets do the codings…
Step 3.
Click the command button to view the command button code section then copy and paste the codes below. Ignore or delete the text in the red font color Its just an explanation provided so you can understand the codes.
Dim z as Integer                       ' Declare an Integer
Dim fn as Integer
Â
fn = 1                                         'set default value of an integer
z = 1
If Isnumeric (text1) then       'check if you entered a whole number
while z <>Â Val(text1)
If Val(Text1) Mod z = 0 then      'determine if the number entered is a prime number or not. Prime number is divisible by itself and 1. example of prime numbers are 2,3,5,7. If the number is divisible by any other number therefore the number is not a prime number.
fn = fn + 1
End if
z = z + 1
Wend
If fn <= 2 ThenÂ
Text2 = "The number " + Text1 + " is a prime number", vbExclamation + VbOkOnly 'display a message when the number entered is a primeÂ
Else
Text2 = "The number " + Text1 + " is not a prime number",vbExclamation + vbOkOnly 'display a message when the number entered is not a prime
End if
Else
Msgbox "You have entered a wrong number" 'display a message when you did'nt entered a whole number
Text1=""                'clear text1
Text2=""                'clear text2
Â
Let's give it a try
press f5 to run the program.
Enter a desired number in text1 then click the command button.
If the entered number is a prime number Text2 will display a message like this:
Â
But when the entered number is not a prime number text2 will display a message like this:
Â
For the validation, If you didn't enter a whole number, A message box will appear and display a notification like this:
Problem solved.
Happy Codings
I need a little help with programming
Hey,
Here is the C++ code that I wrote –Â
Â
#include<iostream>
#include<algorithm>
#include<queue>
#define ll long long int
using namespace std;
Â
bool IsPrime(int);
Â
int main()
{
int number;
Â
cout << "Enter an integer (>1): ";
cin >> number;
Â
if (IsPrime(number))
{
cout << number << " is prime." << endl;
}
else
{
cout << number << " is not prime." << endl;
}
Â
return 0;
}
Â
bool IsPrime(int number)
{
   int i;
Â
for (i=2; i<number; i++)
{
if (number % i == 0)
{
return false;
}
}
Â
return true;
}
Â
Regards,
Sanders Danny