i am a begginer in c++ programming and im stuck at using loops so plz help me!!!
i need to write a program that analyze one or more # (between 1 and 1000) entered by a user. Then determin if it is a prime number or perfect number.
And i have to use the loops at a bigginer level. so plz help me to get through this with a simple program!!!!!!!!!!!!TANX
19 8531
We'd be glad to help you through it - so what do you have so far?
ok i have the code for finding the devisor of any number!!!!!i just cant find how to write a code that show the number entered by user is a prime number?and also how to get the sum of factors for perfect number??????TANX FOR YOU HELPS
so far i have this basic code~~~~dont laugh!!! -
#include<iostream>
-
include<cmath>
-
using namespace std;
-
int main()
-
{
-
int num,root,dev;
-
const int SENTINEL=-1; //constant and integers
-
cout<<"ENTER #<enter -1 to stop>:";
-
-
cin>>num; //# entered by user
-
while(num!=SENTINEL)
-
{
-
root=pow(num,0.5); //second root of that #
-
cout<<"devisors are: 1"<<" "<<num<<" ";
-
for (root;1<root;root--)
-
{
-
if(num%root==0) //the loop and conditions
-
{
-
dev=num/root;
-
cout<<root<<" "<<dev<<" ";
-
-
}
-
else
-
continue;
-
}
-
cout<<endl;
-
cin>>num;
-
}
-
return 0;
-
}
Heres a hint you should try using the mod operator"%"..
It will give you single digits for perfect number evaluation.
And prime is repeated use of operator "mod" until remainder is not equal to zero
Tanx but i still need to fin a code for adding the devisors (except the # itself).
and a code for determining a entered number as a prime or perfect or just a number!!!!
Banfa 9,065
Expert Mod 8TB
For determining perfect numbers in the users input the most efficient method would be to use a look-up table. This is beacuse there are only 5 perfect numbers with-in range for a 32-bit int and only 8 in range of a 64 bit int.
Perfect numbers are very rare and you can easily look up a list of the lower ones on the internet.
Tanx but i still need to fin a code for adding the devisors (except the # itself).
and a code for determining a entered number as a prime or perfect or just a number!!!!
Why do you want to add the divisors? Is that part of the assignment?
Also, given the site's rules on homework, what have you tried on this?
Banfa 9,065
Expert Mod 8TB
Why do you want to add the divisors? Is that part of the assignment?
Part of the definition of a perfect number which is for a number n
if 2n = SUM(All divisors of n) then n is perfect.
if 2n < SUM(All divisors of n) then n is deficient.
if 2n > SUM(All divisors of n) then n is abundent.
Part of the definition of a perfect number which is for a number n
if 2n = SUM(All divisors of n) then n is perfect.
if 2n < SUM(All divisors of n) then n is deficient.
if 2n > SUM(All divisors of n) then n is abundent.
Ah. Okay. (Sorry, next time I'll research the familiar-sounding terms ;). Well that shouldn't be too difficult, could use an array or list to hold any of the divisors...
Part of the definition of a perfect number which is for a number n
if 2n = SUM(All divisors of n) then n is perfect.
if 2n < SUM(All divisors of n) then n is deficient.
if 2n > SUM(All divisors of n) then n is abundent.
Tanx Banfa that helped alot. the only thing i need is a code for determining prime numbers in the users inputs.
Banfa 9,065
Expert Mod 8TB
Part of the definition of a perfect number which is for a number n
if 2n = SUM(All divisors of n) then n is perfect.
if 2n < SUM(All divisors of n) then n is deficient.
if 2n > SUM(All divisors of n) then n is abundent.
Eeeek I have detected an error the correct forumlas are
if 2n = SUM(All divisors of n) then n is perfect.
if 2n > SUM(All divisors of n) then n is deficient.
if 2n < SUM(All divisors of n) then n is abundent.
ok i have the code for finding the devisor of any number!!!!!i just cant find how to write a code that show the number entered by user is a prime number?and also how to get the sum of factors for perfect number??????TANX FOR YOU HELPS
so far i have this basic code~~~~dont laugh!!! -
#include<iostream>
-
include<cmath>
-
using namespace std;
-
int main()
-
{
-
int num,root,dev;
-
const int SENTINEL=-1; //constant and integers
-
cout<<"ENTER #<enter -1 to stop>:";
-
-
cin>>num; //# entered by user
-
while(num!=SENTINEL)
-
{
-
root=pow(num,0.5); //second root of that #
-
cout<<"devisors are: 1"<<" "<<num<<" ";
-
for (root;1<root;root--)
-
{
-
if(num%root==0) //the loop and conditions
-
{
-
dev=num/root;
-
cout<<root<<" "<<dev<<" ";
-
-
}
-
else
-
continue;
-
}
-
cout<<endl;
-
cin>>num;
-
}
-
return 0;
-
}
I wrote a code to determine a entered number as a prime number, ijust wanna know is it a good code or not? and also how can i relate this code with the code above with a loop to make a perfect code????
here is my code for primes!!!!!>>>> -
#include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int num, range=1000;
-
int n;
-
const int SENTINEL=-1;
-
cout<<"enter numbers between 1 and 1000 <enter -1 to exit> :";
-
cin>>num; //entered number by user
-
if(0<num && num<=range)
-
{ //LOOps and conditions
-
while(num!=SENTINEL)
-
{
-
for ( n=2; n < num; n++ )
-
-
if (num%n==0 )
-
break; //formula to determine a prime number
-
if ( n==num )
-
cout << num<<" is prime"<< endl;
-
-
cin>>num;
-
}
-
}
-
else
-
cout<<"invalid Number, number should be between 1 and 1000";
-
return 0;
-
}
It's not the most optimized code ever written, but does the job. For a quick optimization, think of what property all primes but 2 have and consider only numbers with that property.
can anybody give me a loop to connect the two code above?
i mean, user can input as many numbers as he/she desires and then get that these numbers are prime or perfect. and when they input a negative # or over 1000 it give error but not a input failure?
TANX
can anybody give me a loop to connect the two code above?
i mean, user can input as many numbers as he/she desires and then get that these numbers are prime or perfect. and when they input a negative # or over 1000 it give error but not a input failure?
TANX
Once again, given the site's policy on homework, what did you get when you tried?
-
#include <iostream>
-
using namespace std;
-
int main ()
-
{
-
int num, range=1000;
-
int n;
-
const int SENTINEL=-1;
-
cout<<"enter numbers between 1 and 1000 <enter -1 to exit> :";
-
cin>>num; //entered number by user
-
if(0<num && num<=range)
-
{ //LOOps and conditions
-
while(num!=SENTINEL)
-
{
-
for ( n=2; n < num; n++ )
-
-
if (num%n==0 )
-
break; //formula to determine a prime number
-
if ( n==num )
-
cout << num<<" is prime"<< endl;
-
-
cin>>num;
-
}
-
}
-
else
-
cout<<"invalid Number, number should be between 1 and 1000";
-
return 0;
-
}
[/quote]
ok, considering above code!!!!!when i tried it, it determines ane number between 1 and 1000 as prime or not, correctly. but when a negative or a # greater than 1000 entered it gives an input failure and exites the program.
now i just want somebody to help me to find a loop that when an invalid # has been entered it gives an error but then loop to the first line (whithout input failure). Gives the user another chance to enter the number!!!!!
TANX
You will have to do exactly that: create a loop that continually asks the user for input until it is valid input.
for this code i added { while((num<0)||(1001<num)) } inorder to avoid input failure when user enteres a #r<0 or #>1001. BUt when i ran the program it just worked for the 1st #. for example if a user enters 1002 or negative # it worked great, but when the user enters 1002 2 1002 the program output was like
{{{{{{{ENTER #<enter -1 to stop>:1002 2 1002
ENTER #<enter -1 to stop>:devisors are: 1 2
devisors are: 1 1002 6 167 3 334 2 501
}}}}}}}}}}}}(it avoid the first 1002 but executed the 2nd one)
how can i correct it????? -
#include<iostream>
-
include<cmath>
-
using namespace std;
-
int main()
-
{
-
int num,root,dev;
-
const int SENTINEL=-1; //constant and integers
-
cout<<"ENTER #<enter -1 to stop>:";
-
-
-
while((num<0)||(1001<num))
-
{
-
cin>>num; //# entered by user
-
while(num!=SENTINEL)
-
}
-
{
-
root=pow(num,0.5); //second root of that #
-
cout<<"devisors are: 1"<<" "<<num<<" ";
-
for (root;1<root;root--)
-
{
-
if(num%root==0) //the loop and conditions
-
{
-
dev=num/root;
-
cout<<root<<" "<<dev<<" ";
-
-
}
-
else
-
continue;
-
}
-
cout<<endl;
-
cin>>num;
-
}
-
return 0;
-
}
You will have to do exactly that: create a loop that continually asks the user for input until it is valid input.
but how? i tried that on above post, but it did'nt work !!!!!Any idea Why????
tanx y all. I got it
i just added
while(num)
{
------
------
if(num==-100)
break;
else
{
----------------
----------
}
return 0;
Sign in to post your reply or Sign up for a free account.
Similar topics
by: brian.digipimp |
last post by:
Write a program that prompts the user to input a positive integer. It
should then output a message indicating whether the number is a prime
number. (Note: An even number is prime if it is 2. An odd...
|
by: Hemal |
last post by:
Hi All,
I need to know the memory required by a c program. Is there any
tool/utility which can give me the memory usage in terms of DATA
segment, TEXT segment, BSS segment etc.
I am working...
|
by: zacariaz |
last post by:
Once again i have tryed my lyck with classes and it seem that im
getting the hang of it, but before i get too self confident, i would
like to get an expert oppinion.
I have made a working...
|
by: newstips6706 |
last post by:
1, 2, 3, 5, 7... PRIME Numbers
________________________________
Definitions
What is a PRIME Number ?
|
by: QHorizon |
last post by:
Hello, I'm new to Python (I've learned everything up to iterators so
far) and fairly new to Programming. This would be my first real
program:
#Coordinate Geometry (The whole program is not...
|
by: cchris |
last post by:
Hi, I'm a beginning C student and I'm working on a program that is supposed to display all the prime numbers between 50 and 100. I'm having trouble though. Can anyone help me out. This is what I have...
|
by: UofFprogrammer |
last post by:
For a fun challenge, I am trying to create a C++ program that will determine if a number is prime. I am getting to problems when I reach a large values (over 9 or so digits).
How can I increase the...
|
by: buomque |
last post by:
I'm writing a program that determine if that is prime or perfect numbers and output factors of them. If 6 is the first input, the output will say it's a perfect number but if i input 6 again it says...
|
by: =?ISO-8859-1?Q?Rog=E9rio_Brito?= |
last post by:
Hi, All.
I'm just getting my feet wet on Python and, just for starters, I'm coding some
elementary number theory algorithms (yes, I know that most of them are already
implemented as modules, but...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |