Write a program that inputs fifteen integer values in an array. It finds out all the prime numbers stored in an array and displays the result on screen.
So let’s Write Code:
Let’s build logic First:
Algorithm for code:
- Step 1: Start
- Step 2: Declare an array and variables n, count, d and p
- Step 3: Initialize variable count = 0, p=1, d=2;
- Step 4: Input Numbers from User.
- Step 5: if(arr[n]%d==0 ) set p=0 and break.
- Step 6: if remainder is 1 then Count ++.
- Step 7: Print Prime Numbers
- Step 8: Stop
Write a program that inputs fifteen integer values in an array. It finds out all the prime numbers.
#include<iostream>
using namespace std;
main()
{
int arr[15], n, count = 0, d, p;
for(n = 0;n<=14; n++)
{
cout<<"Enter a value in element "<<n<< " of the array ? ";
cin>>arr[n];
}
for(n = 0;n<=14; n++)
{
p = 1;
for(d = 2; d < arr[n]; d++)
if (arr[n]%d ==0)
{
p = 0;
break;
}
if (p == 1)
{
cout<<arr[n]<<endl;
count++;
}
}
cout<<"\n Prime numbers stored in an array are:"<<count;
}
Hope You will learn Something. Happy Coding.
Visit my YouTube Channel as Well😇 https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Click on the below Links for more Programming Exercises:
Conditional Structure Exercises: 👉https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/
Array Exercises: 👉https://myustaadg.com/category/programming-exercises/arrays-exercises/