- 1

skip composite number (non prime number)

hi i want to make a coding that when i give an input (example input=12), the output will only print the prime number (example output=2 3 5 7 11)

11th Oct 2018, 1:47 PM
Yoaraci
Yoaraci - avatar
3 Réponses
+ 10
#include <iostream> using namespace std; bool PrimeNumber(int num); int main() { int num = 0; cin >> num; cout << "Number entered: " << num << endl << endl; for(int i = 1; i <= num; i++) { if(PrimeNumber(i)) cout << i << ". I will become a good boy \n"; else cout << i << ". \n"; } return 0; } bool PrimeNumber(int num) { if(num == 1) return false; for(int i = 2; i <= (num / 2); i++) { if(num % i == 0) return false; } return true; }
12th Oct 2018, 9:23 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 8
Show us your try.
11th Oct 2018, 8:48 PM
blACk sh4d0w
blACk sh4d0w - avatar
- 1
#include<iostream> using namespace std; int main () { int t, n; cin >> t; for (int c=0; c<t; c++) { cin >> n; for (int d=0; d<n; d++) if (n==2||n==3||n==5||n%2!=0||n%3!=0||n%5!=0) cout << "I will become a good boy\n"; } } actually i only want use the composite number for the condition. so the first input is to show how many time i want to input and output. and the second input is how many i will become a good boy i want to repeat but the composite number have to be skipped. so when i input 4 the output is 1. 2. i will become a good boy 3. i will become a good boy 4. number 1 and 4 dont have sentence because they are composite numbers
11th Oct 2018, 11:52 PM
Yoaraci
Yoaraci - avatar