Thread: Please help me with this "prime checker" exercise!

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    Question Please help me with this "prime checker" exercise!

    Hi all guys,
    I'm stucking in this exercise, which tells me to write a C++ program to check whether a number is prime. This is my code, I've debugged it and there are no errors or warnings. But when I enter 9, it said 9 is a prime!!! In general, whenever I enter a composite ODD number, it alwas considers it as a prime. If you find my mistakes in this code, please tell me so that I could finish this crazy exercise. Thank you very much!!!

    Code:
    #include<stdio.h>
    
    void main(void)
    {
    	int n, i, r;
    	printf("Enter a number:");
    	scanf("%d", &n);
    	if(n==1) {
    		printf("This is neither a prime nor a composite number.");
    	}
    	for(i=2;i<=n;i++)
    	{
    	r=n%i;
    	if(r==0) {
    		printf("This is not a prime.");
    		break;
    	} else {
    		printf("This is a prime.");
    		break;
    	}
    	}
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Again, this is no C++ !
    Devoted my life to programming...

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by blackhawk65 View Post
    Hi all guys,
    I'm stucking in this exercise, which tells me to write a C++ program to check whether a number is prime. This is my code, I've debugged it and there are no errors or warnings. But when I enter 9, it said 9 is a prime!!! In general, whenever I enter a composite ODD number, it alwas considers it as a prime. If you find my mistakes in this code, please tell me so that I could finish this crazy exercise. Thank you very much!!!

    Code:
    #include<stdio.h>
    
    void main(void)
    {
        int n, i, r;
        printf("Enter a number:");
        scanf("%d", &n);
        if(n==1) {
            printf("This is neither a prime nor a composite number.");
        }
        for(i=2;i<=n;i++)
        {
        r=n%i;
        if(r==0) {
            printf("This is not a prime.");
            break;
        } else {
            printf("This is a prime.");
            break;
        }
        }
    }
    Your algorithm tries to determine if a number is prime too early (first iteration of the loop). Testing numbers up to (and including) sqrt(N) is a sufficient approach, BTW. Oh, and 'main' returns an 'int', not 'void'.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You must first get your thinking correct before you can get your code correct.
    Can you explain in your own words, how to know whether a number is prime or not?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-02-2010, 01:49 AM
  2. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 06:22 AM
  3. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM