1. Program that inputs ten integers in an array and counts all prime numbers entered by the user. The program finally displays total number of primes in array.
Chapter 7:
Arrays
Programming Exercise
Problem # 1:
Write a program that inputs ten integers in an array and counts all prime numbers entered by the user. The program finally displays total number of primes in array.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int arr[10], i, p=0, c, prime=0;
for(i=0;i<10;i++)
{
cout<<"Enter an integer:";
cin>>arr[i];
}
cout<<"The prime numbers are:\n";
for(i=0;i<10;i++)
{
if(arr[i]>1)
{
p=1;
for(c=2;c<arr[i];c++)
{
if(arr[i]%c==0)
{
p=0;
break;
}
}
}
if(p==1)
{
cout<<arr[i]<<" is a prime number.\n";
prime++;
}
}
cout<<"total prime numbers entered by the user are: "<<prime;
}
#include<conio.h>
using namespace std;
int main ()
{
int arr[10], i, p=0, c, prime=0;
for(i=0;i<10;i++)
{
cout<<"Enter an integer:";
cin>>arr[i];
}
cout<<"The prime numbers are:\n";
for(i=0;i<10;i++)
{
if(arr[i]>1)
{
p=1;
for(c=2;c<arr[i];c++)
{
if(arr[i]%c==0)
{
p=0;
break;
}
}
}
if(p==1)
{
cout<<arr[i]<<" is a prime number.\n";
prime++;
}
}
cout<<"total prime numbers entered by the user are: "<<prime;
}
Output:
Let me know in the comment section if you have any question.
Previous Post:
51. Program to display the following output using loop.
Comments
Post a Comment