: 64% ? > Thu 8:10 PM ac lecture11,pdt (page 16 of 16) Q Search Homework 9 Q1: W
ID: 3702724 • Letter: #
Question
: 64% ? > Thu 8:10 PM ac lecture11,pdt (page 16 of 16) Q Search Homework 9 Q1: Write a program that lets the user enter any number n and then finds the first prime number larger than n.(use function primetest) Q2: Write a program that contains interleave function. Interleave function should take two integer argument, and print two number alternatively. If two user input is different by more than one digit, then program should let user re-enter two new integer. Example: Interleave(123,456) should print 142536 Interleave(123,4567) should print 4152637 interleave(1234,567) should print 1526374Explanation / Answer
#include <iostream>
using namespace std;
// function that returns 1 for prime and 0 for non-prime numbers
int primetest(int n)
{
for(int i=2; i<n; i++)
{
if(n%i == 0)
return 0;
}
return 1;
}
int main() {
// taking user input of a number
int number;
cout << "Enter a number: ";
cin >> number;
// looping as long as the number is not prime
while(primetest(number) == 0)
{
number++;
}
// printing output
cout << "Next prime number is " <<number;
}
/*SAMPLE OUTPUT
Enter a number: 1234
Next prime number is 1237
*/
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.