473,749 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it a prime?

CodeNoobster
52 New Member
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
6 3731
Rabbit
12,516 Recognized Expert Moderator MVP
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
CodeNoobster
52 New Member
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 Recognized Expert Moderator MVP
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
jack003
1 New Member
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
CodeNoobster
52 New Member
@ 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 Recognized Expert Moderator MVP
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
8396
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, but a prime number tester would also be nice. I'm dealing with numbers in the 10^6-10^8 range so it would have to fairly efficient Dag
9
2717
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 (testPrimes.py) with two algorithms that both check for primes by testing only odd numbers using factors up to the square root of the value, where Primes1 is based on all of the existing primes so far, and Primes2 is based on all odd numbers, I would...
11
5940
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 (inclusivly) evenly divides n.
11
7158
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 the exact code ( well maybe a little) but the logic or algorithm to tell whether or not a number is prime....
0
1727
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 complicated. we are on a functions chapter. i am usung the square root to find the prime. so far i have accomplished this but i cant fgure how to divide this by ten so i can find the right prime. i am using c. #include <stdio.h> #include <math.h> ...
25
3886
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 complicated. we are on a functions chapter. i am usung the square root to find the prime. so far i have accomplished this but i cant fgure how to divide this by ten so i can find the right prime. i am using c. #include <stdio.h>
2
4233
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 { int primeCount = 2; //declares primeCount as an int variable and sets it equal to 2 while(primeCount < prime) //checks if primeCount is less than prime {
10
4437
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 has time to critic and offer my some feedback I'd be grateful Thanks Joel
60
1950
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 ending in 5 ..which obviously cannot be prime numbers) can anyone please help me out with the reason?? /*Generate Prime Numbers*/ #include<stdio.h>
7
4901
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.