Question

In C++ using For Loops; "Write a program that displays number facts for a number provided...

In C++ using For Loops;

"Write a program that displays number facts for a number provided by the end user between 1-100 in a table format that includes the number itself, whether the number is a prime number, whether the number is even or odd, whether the number is a perfect square and all of its factors. "

It should also print this information.

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

c++ Code:

#include<iostream>

#include<math.h>

using namespace std;

bool isPrime(int num){

bool flag=true;

for(int i = 2; i <= num / 2; i++) {

if(num % i == 0) {

flag = false;

break;

}

}

return flag;

}

bool isPerfectSquare(long double n)

{

long double sqr = sqrt(n);

return ((sqr - floor(sqr)) == 0);

}

int main(){

int n;

cout<<"Enter a number between 1-100: ";

do{

cin>>n;

}while(n<1 || n>100);

cout<<"Number entered: "<<n<<endl;

bool flag=isPrime(n);

if(flag==true)

cout<<n<<" is a prime number"<<endl;

else

cout<<n<<" is not a prime number"<<endl;

if(n%2==0)

cout<<n<<" is not odd number"<<endl;

else

cout<<n<<" is an odd number"<<endl;

if(isPerfectSquare(n))

cout<<n<<" is a perfect square"<<endl;

else

cout<<n<<" is not a perfect square"<<endl;

cout<<"Factors of "<<n<<" are: ";

for(int i=1;i<=n;i++){

if(n%i==0)

cout<<i<<" ";

}

}

if you like the answer please provide a thumbs up.

Add a comment
Know the answer?
Add Answer to:
In C++ using For Loops; "Write a program that displays number facts for a number provided...
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
  • In HTML write a program that calculates and displays all factors of a number with a...

    In HTML write a program that calculates and displays all factors of a number with a function. A factor is any number that divides into a number evenly. For examples: Factors of 20 are 1,2,4,5,10,20 For the program: Prompt the user for a number or use an input Call a function to calculate all factors using loops and conditionals and modulus Display all factors to the page Bonus +5 If the factor is Even : Print it in Green If...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Write a C++ program that does the following : Define a class myInt that has as...

    Write a C++ program that does the following : Define a class myInt that has as its single attribute an integer variable and that contains member functions for determining the following information for an object of type myInt: A. Is it multiple of 7 , 11 , or 13. B. Is the sum of its digits odd or even. C. What is the square root value. D.Is it a prime number. E. Is it a perfect number ( The sum...

  • 4. Write a logical function perfect Square that receives a positive integer number and checks if ...

    write the code in C please 4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or not. Note: perfect square numbers are 4, 9,16,25,36 etc.... Write a main function that makes use of the perfect Square function to find and print all perfect squares between nl and n2. nl and n2 are end values of a range introduced by the user. ■ (inactive CAT EXE) Enter end values...

  • Using Java loops Write a program that finds all the perfect squares and cubes of numbers...

    Using Java loops Write a program that finds all the perfect squares and cubes of numbers from 1 to a number entered by the user. If the number it is examining is a perfect square, it prints the number user followed by “ is a perfect square” If the number it is examining is a perfect cube, it prints the number user followed by “ is a perfect cube”

  • Using two for loops write a program to print a multiplicationtable. Ask the user to...

    Using two for loops write a program to print a multiplication table. Ask the user to input a number between 1 and 10 and print the multiplication table.

  • use C++            Project 6 1. Using vectors or arrays, write a function named word_rev() that reverse...

    use C++            Project 6 1. Using vectors or arrays, write a function named word_rev() that reverse any given word. 2. Write a program that will: Ask the user to enter a word. Save the entry as word_entered. Call word_rev on the entry Display the reversed entry (word_entered) 3. Write a function named prime() that determine whether or not a given number n (greater than one) is prime. The algorithm: If n is even then n is not a prime number...

  • IN JAVA 1. Create a program that prompts the user for an integer and then displays...

    IN JAVA 1. Create a program that prompts the user for an integer and then displays a message indicating whether or not the number is a perfect square. This can be determined by finding the square root of a number, truncating it (by casting the double result), and then squaring that result 2. Write a program that prompts the user for two numbers. The first number is a minimum value and the second number is a maximum value. RandomNum then...

  • Write a single program in java using only do/while loops for counters(do not use array pls)...

    Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

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
ADVERTISEMENT