Is it a prime?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeNoobster
    New Member
    • Sep 2013
    • 52

    Is it a prime?

    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
    Code:
            Dim input As Integer
            Dim test As Double
            Dim i As Integer = 1
    
            Console.WriteLine("Enter a number to see if it is a prime number")
    
            input = Console.ReadLine()
    
            Console.WriteLine(" ")
    
            While (i <= input)
                test = input Mod i
                Console.WriteLine(test)
                i = i + 1
            End While
    ANY help would be greatly appreciated guys ;)
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • CodeNoobster
      New Member
      • Sep 2013
      • 52

      #3
      hmmmmmm true rabbit, ohk I have remixed the program
      Code:
              Dim num1 As Integer
              Dim test As Integer
              Dim finalnum As Integer = (num1 - (num1 - 1)) + 1
      
              Console.WriteLine("Enter a number to check if it is a prime")
              num1 = Console.ReadLine()
      
              While (finalnum <= (num1 - 1))
                  test = num1 Mod finalnum
                  If (test = 0) Then
                      Console.WriteLine("number is not a prime number")
                      Exit While
                  ElseIf (test <> 0) Then
                      Console.WriteLine("number is a prime number")
                      Exit While
                  End If
                  finalnum = finalnum + 1
              End While
      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?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        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.

        Comment

        • jack003
          New Member
          • Sep 2013
          • 1

          #5
          Try this code :
          C++ program to enter a no and print it is Prime number or not
          Code:
          #include <iostream>
          #include <math.h>
          using namespace std;
          int main()
          {
          int no, i;
          
          cout << "Enter an integer : ";
          cin >> no;
          int limit = sqrt(no);
          i = 2;
          while(no%i != 0 && i <= limit)
          i++;
          if(i > limit)
          cout<<no<<" is Prime\n";
          return 0;
          }
          Check the no is Prime or not And compile code online:

          Comment

          • CodeNoobster
            New Member
            • Sep 2013
            • 52

            #6
            @ 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.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              No problem, good luck with the rest of your project.

              Comment

              Working...