hi i am still a beginner in c++ and still didn't learn functions and not allowed to use libraries . so a friend of mine challenged me to write a program that shows only prim numbers from an array what my program do when entering numbers from 1 to 10 it shows the 1,5,7,9,10 showing 1o and doesn't show the 3 can any body help.
i appreciate if your help you'd be using the void main not int main :)
#include <iostream>
usingnamespace std;
void main()
{
int NUM[10];
int r=0;
int count=0;
for (int i = 0; i < 10; i++)
{
cin>>NUM[i];
}
for (int k = 0; k < 10; k++)
{
count=0;
for (int i = 2; i <= NUM[i]; i++)
{
if (NUM[k] % i==1)
{
++count;
}
if (count==2)
{
cout<<"this number is prime : "<<NUM[k]<<endl;
}
}
}
}
Why do you store the input into array? Why not read one value, check if it is prime, and if yes, then append it to list? In tge end just show the list.
the inner loop tries every possible divisor so if the value in the array is divisible by i count will increase. in the second if, the prime should enter the if above 2 times ( divisible by 1 and its-self).
#include <iostream>
int main(void)
{
int NUM[10]; // define a 10-element array of type int
bool isprime; // define a bool to check if a number is prime or not
std::cout<<"This program gets 10 numbers and prints out the ones that are prime."<<std::endl<<std::endl; // tell the user what this program does
for(int counter=0;counter<10;counter++) // initialize the 10-element array by getting numbers from the user and storing them in the array
{
std::cout<<"Element #"<<counter+1<<": "; // tell the user to enter an element in
std::cin>>NUM[counter]; // get the number
std::cin.ignore(); // get the carriage return
}
for(int outer=0;outer<10;outer++) // we need this outer loop to loop through the 10 elements in the array
{
isprime=true; // set isprime to true
for(int inner=2;inner<NUM[outer];inner++) // we need this inner loop to loop through the numbers between 2 and one less than the user number
{
if(NUM[outer]%inner==0) // if we found a divisor that is not 1 and the number, this number is not prime
{
isprime=false; // set the checker to false, so we don't print out the composite number
break; // we don't need to check any more divisors because we know the number is a composite
}
}
if(isprime) // if the number is prime, print it out
{
std::cout<<"Element #"<<outer+1<<": "<<NUM[outer]<<" is prime."<<std::endl;
}
}
std::cin.ignore(); // keep the program open so the user can read the output until he/she hits the 'Enter' key
return(0); // exit safely
}
@usanfriends you need to add one condition in the last if, because we can't print that 1 is prime. or change the code in another way
This program gets 10 numbers and prints out the ones that are prime.
Element #1: 1
Element #2: 2
Element #3: 3
Element #4: 4
Element #5: 5
Element #6: 6
Element #7: 7
Element #8: 8
Element #9: 9
Element #10: 10
Element #1: 1 is prime.
Element #2: 2 is prime.
Element #3: 3 is prime.
Element #5: 5 is prime.
Element #7: 7 is prime.
I did this..
1 2 3 4
if(isprime&&NUM[outer]!=1) // if the number is prime and isn't 1 print it out
{
std::cout<<"Element #"<<outer+1<<": "<<NUM[outer]<<" is prime."<<std::endl;
}
Element #1: 1
Element #2: 2
Element #3: 3
Element #4: 4
Element #5: 5
Element #6: 6
Element #7: 7
Element #8: 8
Element #9: 9
Element #10: 10
Element #2: 2 is prime.
Element #3: 3 is prime.
Element #5: 5 is prime.
Element #7: 7 is prime.