Question

Write a C++ program that reads in a list of integers, separated by a space, into...

Write a C++ program that reads in a list of integers, separated by a space, into an array of size N. The last number is -1 but should not be added to the array. The number of integers will be < N. Then output only those numbers, one per line, that are prime, perfect, the sum of a prime or perfect number, or a member of the well-known Fibonacci sequence.

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

PLEASE GIVE IT A THUMBS UP

nikhilanikhil-Vostro-15-3568:-/Desktop/CODE$ ./a.out 12 20 27 60 35 - 1 12 20 60 nikhil@nikhil-Vostro-15-3568:~/Desktop/CODES

#include <iostream>
using namespace std;

bool isPrime(int n)
{   
if (n <= 1)
return false;
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
  
return true;
}

bool isSumPrime(int n){
for(int i = 2; i <= n/2; ++i)
{
if (isPrime(i) == 1)
{
if (isPrime(n-i) == 1)
{
return true;
}
}
}
return false;
}

bool isPerfect(long long int n)
{   
long long int sum = 1;
for (long long int i=2; i*i<=n; i++)
{
if (n%i==0)
{
if(i*i!=n)
sum = sum + i + n/i;
else
sum=sum+i;
}
}
if (sum == n && n != 1)
return true;

return false;
}

bool isSumPerfect(int n){
for(int i = 2; i <= n; ++i)
{
if (isPerfect(i) == 1)
{
if (isPerfect(n-i) == 1)
{
return true;
}
}
}
return false;
}

int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}

bool isFib(int n){
int i=0;
while(i<n){
if(fib(i)==n)
return true;
if(fib(i)>n)
break;
i++;
}
return false;
}

int main(int argc, char const *argv[])
{
int arr[100000], ind = 0;
while(true){
int n;
cin>>n;
if(n==-1)
break;
arr[ind] = n;
ind++;
}
for(int i=0;i<1000;i++){
if(isPrime(arr[i]) || isPerfect(arr[i]) || isSumPrime(arr[i]) || isSumPerfect(arr[i]) || isFib(arr[i]))
cout<<arr[i]<<endl;
}
return 0;
}

-/Desktop/CODE/demo.cpp (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences Hel

47 49 -/Desktop/CODE/demo.cpp (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferenc

-/Desktop/CODE/demo.cpp (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences Hel

Here we are taking input till the user input -1 and then we are having the no. in the array, now we made some function to check wether the number is prime, perfect, sum of prime or perfect , comes in fib series, and then by checking every number in the array, we are checking it any of the above condition becomes true, then print the number.

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reads in a list of integers, separated by a space, into...
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
  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • In C++: Write a program that reads a list of integers, and outputs the two smallest...

    In C++: Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The list is preceded by an integer indicating how many numbers are in the list. If the input is: 5 10 5 3 21 2, the output is: 2 3 To achieve the above, first read the integers into a vector.

  • Write a program that reads a list of integers ending with a negative, and that then...

    Write a program that reads a list of integers ending with a negative, and that then outputs that list in reverse. After the negative that ends the list, a character indicates what character should separate the output integers. If the input is: 3 5 2 - 1 * the output is: 2*5*3 LAB ACTIVITY 7.16.1: LAB: Reverse a list, with separating characters 2 / 10 main.cpp Load default template... 6 7 8 int numbers [100],i=0, num, size; char character; cin>>num;...

  • C++ please Question 4: 125 points] Write and test a program that reads two positive integers...

    C++ please Question 4: 125 points] Write and test a program that reads two positive integers nl and n2 with n2 > nl. The program then calculates the sum of the prime numbers, using is prime () function developed above, between nl and n2 (inclusive) A Sample input Enter values for nl and n2 (nl<n2): 3 9 Output: Thé Sum of prime numbers between 3 and 9 (inclusive) is 15

  • Task 1 : Write a Java program that prompts for and reads 10 integers from the...

    Task 1 : Write a Java program that prompts for and reads 10 integers from the user into an integer array of size 10, it then: - calculates the sum and average of positive numbers of the array and displays them. [Note: consider 0 as positive] Note: The program must display the message: "There are no positive values" if the array does not contain any positive value.

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Write a program that reads a sequence of integers into an array and that computes the...

    Write a program that reads a sequence of integers into an array and that computes the alternating sum of all elements in the array. For example, if the program is executed with the input data: 2 1 4 9 16 9 7 4 9 11 Then it computes 2 - 1 + 4 - 9 + 16 - 9 + 7 - 4 + 9 – 11 = 4 Use a scanner object to gather the inputs from the user....

  • Write a program that reads k integers and displays their sum. Write a program that reads...

    Write a program that reads k integers and displays their sum. Write a program that reads k values representing family income from the keyboard and place them into the array. Find the maximum income among these values. Count the families that make less than 10 percent of the maximum income. (JAVA)

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