Question

THE PROGRAMMING LANGUAGE IS C++ Part 1.Create a program that decides whether a given integer is...

THE PROGRAMMING LANGUAGE IS C++

Part 1.Create a program that decides whether a given integer is prime or not. The program should at the end of its analysis print the number followed by prime or composite.

Part 2.Make the program from part 1 print the prime factors of any composite number entered (an additional feature to Part 1).

THE PROGRAMMING LANGUAGE IS C++

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

PART 1:

#include<iostream>
using namespace std;
int main()
{
int n,i,count=0;
printf("Enter a number: ");
scanf("%d",&n);
if(n==1)
{
printf("%d is neither prime or composite number.",n);
}
else
{
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("%d is prime number.",n);
else
printf("%d is composite number.",n);
}
return 0;
}

PART 2:

#include<iostream>
using namespace std;
int main()
{
int n,i,count=0;
printf("Enter a number: ");
scanf("%d",&n);
if(n==1)
{
printf("%d is neither prime or composite number.",n);
}
else
{
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("%d is prime number.",n);
else
{
printf("%d is composite number.",n); //part 2
printf("\nPrime factors are: ");
int j=2;
while(n>1)
{
while(n%j==0)
{
printf("%d ",j);   
n=n/j;
}
j++;
}
}
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
THE PROGRAMMING LANGUAGE IS C++ Part 1.Create a program that decides whether a given integer is...
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
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