Thread: Somewhat new to c++, adding Prime number and Sqrt featre to program...

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

    Somewhat new to c++, adding Prime number and Sqrt featre to program...

    Hey there all. I'm taking C++ at school, and I am currently doing a project to calculate perfect numbers and other related things. However, i need really good features to get a good grade on this program (extras not mentioned in the instructions.)

    Maybe if you guys could help me out here? I've thought of finding out if the number entered is a Prime number, or maybe if it's a perfect square. Though im not sure how to do this. we may only use for or while loops, if statements, all the basics. And we may only use the basic header files (not even sure what that means), like cstring, iomanip, iostream. if you would like to take a gander at my program, it's here. I like the program simply because the logic was so hard for me to figure out.



    ---------------+++++++++++++++++++++++---------------------------
    /*Azhar Sheraze
    4th 6 weeks program
    46weeks.cpp
    */

    #include <iostream>
    #include <iomanip>
    #include <cstring>

    void main()

    {
    bool run = true;
    bool prime = true;

    int num, i, add, add2, j, k, int_minus, ready, p;

    string runagain;


    add = 0;
    j = 1;
    k = 1;



    /* Variable Definitions
    • num is the number entered
    • i is used in the first for loop to make the program loop until termination
    • add is used to add up all the values divisible by num
    • add2 is used to add up all the values divisible by num for a second loop
    • j is used similarly to i, just in another loop
    • k is used similarly to i and num, just in another loop
    • int_minus is the diffence between the integer and the sum of divisors
    • ready is used to progress the program
    • runagain tests whether user wants to run the program again
    • p is used to find a perfect square and a prime number
    */

    while(run)
    {



    // Start Program
    // Title

    cout << endl;
    cout << " %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
    cout << " % %" << endl;
    cout << " % P e r f e c t N u m b e r %" << endl;
    cout << " % %" << endl;
    cout << " % F i n d e r %" << endl;
    cout << " % %" << endl;
    cout << " %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl << endl << endl;






    // Enter an integer


    cout << "Enter a positive Integer (decimal values will be disregarded). ";
    cin >> num;
    cout << endl;




    // This checks to see that the integer entered is a positive integer.

    if(num <= 0)
    {
    cout << "Sorry, the integer must be positive. Enter in a new integer. ";
    cin >> num;
    cout << endl << endl;
    }

    if(num > 0)
    {




    // This checks all the values divisible by the number entered

    cout << num << "'s divisor(s) is(are) as follows:" << endl;
    for(i=1; i<num; i++)
    {
    if(num%i==0)
    {
    cout << " : " << i << endl;
    add = add + i;
    }
    }










    // This is the sum of all the divisors, which will be used to tell if its perfect, deficient, or abundent

    cout << " The sum of all the divisors of " << num << " is " << add << "." << endl <<endl;



    cout << "Press any key and press enter when ready to see if " << num << " is abundent, deficient, or Perfect... ";
    cin >> ready;
    cout << endl << endl;






    // Checking whether the integer is Deficient

    if(add<num)
    {
    cout << " -F A C T S about " << num << "-"<< endl << endl;
    cout << " 1. Your integer, " << num << ", is known as Deficient, since all of its divisors' sum is smaller then the integer itself. " << endl;






    // Finding the difference between the sum of the divisors and the integer


    int_minus = num - add;

    cout << " 2. The difference between the integer and the divisors' sum is " << int_minus << "." << endl;
    cout << " 3. " << int_minus << " is how far off " << num << " is from being a perfect number." << endl;
    cout << " 4. Perfect numbers that are lower then "<< num << " are as follows." << endl;






    // Checking numbers lower then the integer entered for Perfect Numbers

    for (k=1; k<num; k++)
    {
    for (int j=1; j<k; j++)
    {
    if (k%j==0)
    {
    add2 = add2 + j;
    }
    }
    if (add2==k)
    {
    cout << " " <<add2 << endl;
    }

    add2=0;
    }
    }







    // Checking whether the integer is Abundent

    if(add>num)
    {
    cout << " -F A C T S about " << num << "-"<< endl << endl;
    cout << " 1. Your integer, " << num << ", is known as Abundent, since all of its divisors' sum is larger then the integer itself. ";

    // Finding the difference between the sum of the divisors and the integer

    int_minus = add - num;
    cout << " 2. The difference between the integer and the divisors' sum is " << int_minus << ". " << endl;
    cout << " 3. " << int_minus << " is how far off " << num << " is from being a perfect number." << endl << endl;
    cout << " 4. Perfect numbers that are lower then "<< num << " are as follows." << endl;



    // Checking numbers lower then the integer entered to see if they are Perfect

    for (k=1; k<num; k++)
    {
    for (int j=1; j<k; j++)
    {
    if (k%j==0)
    {
    add2 = add2 + j;
    }
    }
    if (add2==k)
    {
    cout << " : " <<add2 << endl;
    }

    add2=0;
    }
    }










    // Checking whether the integer is Perfect

    if(add==num)
    cout << "Your integer, " << num << " is perfect, since all of its divisors' sum is equal to the integer itself." <<endl;











    // Check and see if integer is prime, this does not work for now


    for (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;
    }
    }



















    /*Doesn't work here--- HELP ME aaaaahhhhh
    for (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;
    }

    */













    cout << endl;
    cout << "Would you like to test another integer? (Y/N)" << endl; cin >> runagain;

    if(runagain == "y" || runagain =="Y" || runagain == "Yes")
    run = true;

    else
    run = false;



    }
    }
    }


    ------ e n d p r o g r a m ----------------------------




    any help? Btw, for some strange reason my loop for running the program again works SOMETIMES, and other times it just shows up the cout, and just stops the program any. Any ideas on what the prob is? THanks a bunc people.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Somewhat new to c++, adding Prime number and Sqrt featre to program...

    Wow, I have a lot to say here.

    Originally posted by thynksheraze
    Hey there all. I'm taking C++ at school, and I am currently doing a project to calculate perfect numbers and other related things. However, i need really good features to get a good grade on this program (extras not mentioned in the instructions.)
    Kudos, man, for doing more than required.

    Use code tags. It makes your code much easier to read.
    Don't ever, ever use void main. Just don't. Please don't argue this. If you do, you will arouse the fury of every other programmer around here. They will come at you quoting programming standards and the creator of C++ himself. If you want to learn the many reasons why void main should never be used, search this board. You'll find tons.
    Try to use functions in all of your programs. Modularity is great for many reasons. Functions are your friend.
    Code:
    // This checks all the values divisible by the number entered
    
    				cout << num << "'s divisor(s) is(are) as follows:" << endl;
    	for(i=1; i<=sqrt(num); i++)	// this loop need only go to i <= sqrt(num) cuz that is the largest possible divisor that does not have a lesser pair
          	{
       			if(num%i==0)
          	 	 {
    					cout << "   : " << i << endl;
                	add = add + i;
               	 }
             }
    
    // Check and see if integer is prime, this does not work for now
    
    
    for (p=1; p < num && prime; p++)	// the loop should exit once the first divisor is found
    	{
    	if ( num % p == 0 )
    	{
    		prime = false;
    
    		if (p * p == num )	// this shouldn't be nested in the if statement to test if it is prime
    		{
    				cout << num << " is a perfect square. Its square root is " << p << endl<<endl;
    		}
    	}
    
       if ( prime )
    	{
    				cout << num << " is a prime number."<<endl<<endl;
    	}
    }
    
    
    if(runagain == "y" || runagain =="Y" || runagain == "Yes")	// I kinda like the way you set this up
    	run = true;
    
    else
    	run = false;
    
    
    
    }
    }
    }
    
    
    ------ e n d    p r o g r a m ----------------------------
    [/B][/QUOTE]

    I hope that helped. I'm not exactly sure what you wanted to do with square numbers. If you want to know if an inputted number is a perfect square, you could use the square root function in <cmath>. If you need more help, post your revised code and ask another question.

    Good luck
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    16
    joshdick, thanks so much for your input. It's very much appreciated. Thanks for adding comments too, i appreciate it.


    But one thing im having problems with, is why my whole Prime number/ Perfect square finder loop doesn't even seem to run in the program. It just skips it. I can't figure out what I did wrong. you also commented on how the loop should not be nested, but i can't figure out how else to do it. Maybe if you could help me out here? I don't know what the problem is.

    Thanks a lot for your time, and hope to here from you, and hopefully others, soon.

    *edit: oh, also, i don't know how to exit a loop.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Exiting a Loop

    Many times in a program there is a loop that should be exited once a certain condition is met. This can be achieved by declaring a boolean variable. ex:
    Code:
    bool found;
    while(!found)
    {
    	// search for something
    }
    In a for loop this can be done:
    Code:
    for(int i = 0; i < n && !found; ++i)
    {
    	// search here
    }
    I haven't had time to look over all of your code, but here are a few suggestions if a loop seems to not be entered. First, make sure that all of your variables are initialized before being used. Next, ensure that they are within ranges acceptable to your program. This can be done using assertions. Write up pre and post conditions for your functions and loops.

    Speaking of functions, use them. By using fuctions, you can test modules of your program independantly. This is very helpful in debugging. Once you get one module working exactly as it should, you can stop worrying about that module not working and can, therefore, spend more time concentrating on problematic chunks of code.

    I wish I could take the time to look over your code and give specific help, but school's a *****. I gotta go work on boring history crap. I hope someone else can help you more.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed