473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Is it a prime?

CodeNoobster
Hi guys, I'm embarrassed to ask but, I'm struggling with this problem. I've been instructed to check if a number (inputted by a user) is a prime number or not. So far I've managed to show the fact that for example, 7/7=1 and 7/1=7 and that those are the only operations that have no remainder.

Now the trick is, getting the program to check and make sure that those two operations must be the only ones to have no remainder in order for it to tell the user that 7 is a prime number rather than the user judging for him/herself.

here's what I've done so far
Expand|Select|Wrap|Line Numbers
  1.         Dim input As Integer
  2.         Dim test As Double
  3.         Dim i As Integer = 1
  4.  
  5.         Console.WriteLine("Enter a number to see if it is a prime number")
  6.  
  7.         input = Console.ReadLine()
  8.  
  9.         Console.WriteLine(" ")
  10.  
  11.         While (i <= input)
  12.             test = input Mod i
  13.             Console.WriteLine(test)
  14.             i = i + 1
  15.         End While
  16.  
ANY help would be greatly appreciated guys ;)
Sep 27 '13 #1

✓ answered by Rabbit

I think you're approaching this backwards. I would skip checking 1 and n because the results are always the same. Instead, you should check everything in between 1 and n. If any of those results in a division with no remainder, then you know it's not a prime. Full stop, no need to check the rest.

6 3715
Rabbit
12,516 Expert Mod 8TB
I think you're approaching this backwards. I would skip checking 1 and n because the results are always the same. Instead, you should check everything in between 1 and n. If any of those results in a division with no remainder, then you know it's not a prime. Full stop, no need to check the rest.
Sep 27 '13 #2
hmmmmmm true rabbit, ohk I have remixed the program
Expand|Select|Wrap|Line Numbers
  1.         Dim num1 As Integer
  2.         Dim test As Integer
  3.         Dim finalnum As Integer = (num1 - (num1 - 1)) + 1
  4.  
  5.         Console.WriteLine("Enter a number to check if it is a prime")
  6.         num1 = Console.ReadLine()
  7.  
  8.         While (finalnum <= (num1 - 1))
  9.             test = num1 Mod finalnum
  10.             If (test = 0) Then
  11.                 Console.WriteLine("number is not a prime number")
  12.                 Exit While
  13.             ElseIf (test <> 0) Then
  14.                 Console.WriteLine("number is a prime number")
  15.                 Exit While
  16.             End If
  17.             finalnum = finalnum + 1
  18.         End While       
  19.  
it works, but now there is an accuracy problem. any number entered that's higher than 15 is an anomaly. Non prime numbers are prime numbers and vice verso. Any suggestions?
Sep 27 '13 #3
Rabbit
12,516 Expert Mod 8TB
Lines 11 and 13 to 15 shouldn't be there. That's making it print out every iteration in the loop. Instead you should be setting a flag within the loop and decide what to print out once you're out of the loop.
Sep 27 '13 #4
Try this code :
C++ program to enter a no and print it is Prime number or not
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. int main()
  5. {
  6. int no, i;
  7.  
  8. cout << "Enter an integer : ";
  9. cin >> no;
  10. int limit = sqrt(no);
  11. i = 2;
  12. while(no%i != 0 && i <= limit)
  13. i++;
  14. if(i > limit)
  15. cout<<no<<" is Prime\n";
  16. return 0;
  17.  
Check the no is Prime or not And compile code online:
http://cbtsam.com/cppl1/cbtsam-cppl1-051.php
Sep 28 '13 #5
@ Rabbit : Sweet thanks man, i did another remix. It works perfectly. Did it as a console to get a working version of the program. Now that its working, gonna make an interface for it now.

@ jack 003 : thanks man. Though i got a working version, there's no harm in looking for an easier way to do it.
Sep 28 '13 #6
Rabbit
12,516 Expert Mod 8TB
No problem, good luck with the rest of your project.
Sep 28 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
0
by: ETM11871 | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
2
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { ...
10
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.