Small program, PRIME/ PERFECT SQUAREs

This is a discussion on Small program, PRIME/ PERFECT SQUAREs within the C++ Programming forums, part of the General Programming Boards category; I wrote this code for finding out if a number entered is a prime number or a perfect square. It ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    16

    Small program, PRIME/ PERFECT SQUAREs

    I wrote this code for finding out if a number entered is a prime number or a perfect square. It only works if the number entered is a perfect square already. all else totally fails.

    0----------------------------------

    #include <iostream>
    #include <iomanip>


    void main()

    {

    int num;
    bool prime = true;

    cout << "Enter a number" << endl;
    cin >> num;


    for (int p=1; p < num; p++)
    {
    if ( num % p == 0 )
    {

    prime = false;

    if (p * p == num )
    {
    cout << num << " is a perfect square. Its square root is " << p << endl<<endl;
    }
    }



    }

    if ( prime )
    cout << num << " is a prime number."<<endl<<endl;

    }


    ---------END



    Any ideas?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    i would recommend placing this in your other thread. also, listen to not using void main. do a search if you would like to know why...

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    16
    lol, really your advice has been well taken, as I have haerd of the horror stories reguarding it. however, our teacher prefers us to use void main as of right now. i think he wants us to learn a lesson or something later on.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by thynksheraze
    our teacher prefers us to use void main as of right now.
    Just add this to your code:
    Code:
    /* your headers here */
    
    #ifdef MY_TEACHER_IS_A_MORON
    void
    #else
    int
    #endif
    main ( )
    {
        /* your code here */
    
    #ifndef MY_TEACHER_IS_A_MORON
        return 0;
    #endif
    }
    Then, when you turn in your home work, make the first line of your program:

    #define MY_TEACHER_IS_A_MORON

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by quzah
    Just add this to your code:
    Code:
    /* your headers here */
    
    #ifdef MY_TEACHER_IS_A_MORON
    void
    #else
    int
    #endif
    main ( )
    {
        /* your code here */
    
    #ifndef MY_TEACHER_IS_A_MORON
        return 0;
    #endif
    }
    Then, when you turn in your home work, make the first line of your program:

    #define MY_TEACHER_IS_A_MORON

    Quzah.
    lol. talk about not getting on his teacher's good side. but seriously, just use it. my teacher somewhat teaches void main, she knows it is supposed to be int, but...i use int anyways.

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    The problem is that the loop is starting from 1..

    WHat is
    10%1
    40%1
    7%1


    see the problem..... Start the loop from 2.... And i would recomend that you stop the loop at num/2 that is.

    instead of for (int p=1; p < num; p++) use for (int p=1; p < num/2; p++)


    Since there is no need to run the loop after that...
    If you want to find say wheather 7 is prime..

    is 7 divisible by 2 ---no
    7 divisible by 3 ---no
    you dont need to do it for 4 ,5 and other numbers because multiplying it by 2 will give a value greater than 7...

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Or even better, instead of stopping at num/2 you could stop at sqrt(num) since any number checked after that would have to be multiplied by a lower number, and you've already checked all the lower numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. C Program for Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 12
    Last Post: 02-19-2003, 04:55 AM
  3. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  4. Help with program code re prime numbers
    By Anna Lane in forum C Programming
    Replies: 3
    Last Post: 11-16-2002, 10:48 AM
  5. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21