-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1.cpp
More file actions
57 lines (52 loc) · 1.16 KB
/
Q1.cpp
File metadata and controls
57 lines (52 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Number Manipulation and Prime Numbers
// Write a C++ program to take a positive integer n as input and:
// 1. Check whether n is a prime number.
// 2. If it is not prime, find all its factors.
// 3. If it is prime, find the next prime number greater than n
#include<iostream>
using namespace std;
void factors(int n){
cout<<"Factors of "<<n<<" are : "<<endl;
for(int j=2;j<n;j++){
if(n%j == 0){
cout<<j<<" ";
}
}
}
void NextPrimeNum(int n){
int num;
while(true){
n++;
int point = 0;
for(int m=2;m<n;m++){
if(n%m == 0){
point = 1;
break;
}
}
if(point == 0){
num = n;
break;
}
}
cout<<"The next greatest prime number is "<<num<<endl;
}
int main(){
int n,flag = 0;
cout<<"Enter a number: ";
cin>>n;
for(int i=2;i<n;i++){
if(n%i == 0 ){
flag = 1;
break;
}
}
if(flag == 1){
cout<<"Number is not prime"<<endl;
factors(n);
}else{
cout<<"number is prime"<<endl;
NextPrimeNum(n);
}
return 0;
}