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.
Re: Somewhat new to c++, adding Prime number and Sqrt featre to program...
Wow, I have a lot to say here.
Quote:
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:cool: