Question

Assignment Develop a program to analyze one or more numbers entered by a user. The user...

Assignment

  1. Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000.
  2. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.).
  3. For non-prime numbers, output a list of all divisors of the number. Format your output so there are between 5 and 10 numbers per line to conserve paper. DO NOT output one number per line.
  4. Include whether a number is a perfect number. A perfect number is when all divisors add up to the number itself. Example: 6 is a perfect number. 1 + 2 + 3 = 6

Check your solution with at least 6 numbers: at least 3 prime numbers and at least 3 numbers that are not prime. Check your solution with at least two sets of invalid data.

Use good program design, efficient code, and document your code with explanatory comments throughout. Be sure to include adequate error handling in your program and error data when you run the program to demonstrate error handling.

Input

One or more numbers entered by user, entered one at a time. Program must loop back to the beginning until the user indicates that they are done.

Sample Output

     Input a number between 1 and 1000:   -5

     That number is not valid. Input another number.    

     Input a number:   17

     That is a prime number.

     Enter a new number: 28

    That is not a prime number. Divisors are:

     1     2    4     7     14

     HOW DOES THIS PROGRAM END?

Note

Adequately check entered data for validity. Use adequate test data to process all valid data and representative data to show how your program handles invalid data.

Label all output clearly. Be sure your output file contains user prompts and what was entered by the user in addition to the results of your program processing.

Here is my incomplete program and I'm stuck! Please help me complete it!

#include
using namespace std;

int main()
{
   int num, i, total;
   bool isPrime = true;
  

   cout << "Enter a number from 1 to 1000: " << endl;
   cin >> num;
   while (cin.fail()) {
           cin.clear();
           cin.ignore(200, '\n');
           cout << "Invalid input. Try again." << endl;
           cin >> num;
       }
  
   for (total = 0; num < 1 || num > 1000; cin >> num)
   {cout << "Invalid input: The number must be between 1 and 1000. Try again." << endl;}
  
   for (i = 2; i <= num / 2; ++i)
   {
       if (num % i == 0)
       {
           isPrime = false;
           break;
       }
   }
   if (isPrime)
       cout << "This is a prime number" << endl;
   else
   {
       cout << "This is not a prime number. The divisors of "<< num << " are" << endl;
       for (i = 1; i <= num; ++i)
           if (num % i == 0)
               cout << i << " ";
   }
     

   system("pause");
   return 0;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here Iam providing the code and the output for the given problem.

Code:-

Code in text format:-

#include <iostream>
using namespace std;
int checkPrime(int number);
int main()
{
int number ,i , prime = 0 , result , sum = 0,newline =0,rem; //declairng variables
std::cout << "Input a number betwenn 1 and 1000" << std::endl;
std::cin >> number;
while(number < 0 || number > 1000){ //asking user to enter valid number loop terminates if valid
std::cout << "The number is invalid.Input another number:" << std::endl;
std::cin >> number;
}
  
result = checkPrime(number); //calling checkPrime fucntion
  
while(result != 1){ //if the number is a prime number we ask again to enter
std::cout << "That is a prime number" << std::endl;
std::cout << "Enter another number" << std::endl;
std::cin >> number ;
result = checkPrime(number);
}
  
std::cout << "This is not a prime number.Divisors are :" << std::endl; //printing the Divisors
for (int i=1;i<number;i++){
if (number%i==0){
cout << i <<" ";
newline = newline+1;
if(newline%5 == 0) //if printing length more than 5 we print newline
cout << "\n";
  
}
  
}
  
for(i = 1 ; i <=(number -1); i++){ //checking the number is a perfect number or not
rem = number%i;
if (rem==0)
{
sum= sum+i;
}
}
if (sum == number)
std::cout <<"\nThe entered number is a perfect number" << std::endl;
  
  
}


int checkPrime(int number){ //fucntion to check if prime or not
int prime = 0,i;
  
for(i = 2 ; i <= number/2; i++){
if(number%i == 0){
prime = 1;
break;
}
}
return prime;
}

Output:-

Add a comment
Know the answer?
Add Answer to:
Assignment Develop a program to analyze one or more numbers entered by a user. The user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream>...

    CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...

  • So the assignment is to write a program that have the user entered 3 numbers and...

    So the assignment is to write a program that have the user entered 3 numbers and than it will sort it by ascending order. Here are the code I wrote, but it won't compiled and I kept on getting an error message "Using uninitiazed memory" for all the variables. Can someone please help take a look and see whats going on? #include <iostream> using namespace std; int main() { //variables int num1, num2, num3; int lowest, middle, highest; //user inputs...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • I'm trying to write in C++ a way to have the user input an double one...

    I'm trying to write in C++ a way to have the user input an double one at a time and have the program output (on entering 0) the number of times those variables differ by at least 1. For example if I enter 1, 1.7, 0.8, -0.1, -1, 0 ... I should get back 4. (or if I just input 0, the output should just be 0). I'm having trouble understanding how to use a while/for operation to make this...

  • C++ Object Oriented assignment Can you please check the program written below if it has appropriately...

    C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT