### Function Programming : Check Prime in C

Write a Program to check whether the given input in Prime or Not . User defined function is mandatory to use.

Source Code in C Language:

#include<stdio.h>
int main()
{
int inp;
scanf("%d",&inp);
printf("\nThis number is: ");
pri(inp);
}
int pri(int p)
{
int i,t;
t=1;
for(i=2; i<p; i++)
{
if(p%i==0)
{
t=0;
break;
}
}

if(t==0)
printf("Not prime");
else
printf("Prime");
}

1. #include
main()
{
int inp;
scanf("%d",&inp);
printf("this number is\n");
pri(inp);
}
int pri(int p)
{
int i,t;
t=1;
for(i=2;i<p;i++)
{
if(p%i==0)
{
t=0;
break;
}
}
if(t==0)
printf("not prime");
else
printf("prime");
}

2. #include
main()
{
int inp;
scanf("%d",&inp);
printf("this number is\n");
pri(inp);
}
int pri(int p)
{
int i,t;
t=1;
for(i=2;i<p;i++)
{
if(p%i==0)
{
t=0;
break;
}
}
if(t==0)
printf("not prime");
else
printf("prime");
}

3. #include
int pri(int inp);
main()
{
int inp;
scanf("%d",&inp);
pri(inp);
}
int pri(int x)
{
int i,t=1;
t=1;
for(i=2;i<x;i++)
{
if(x%i==0)
{
t=0;
break;
}
}
if(t==1)
printf("prime number");
else
printf("not prime");
}

4. #include
main()
{
int n;
scanf("%d",&n);
prime(n);
}
int prime(int p)
{
int i,temp;
for(i=2;i<p;i++)
{
temp=1;
if (p%i==0)
{
temp=0;
break;
}
}
if(temp==0)
printf("not prime number");
else
printf("prime number");
}

5. #include
#include

int check_prime(int);
int main()
{
int n,result;
printf("\n enter number for check if it is prime or not; ");
scanf("%d",&n);
result=check_prime(n);
getch();
return 0;

}

int check_prime(int a)
{
int i;
for(i=2;i<a;i++)
{
if(a%i==0)
printf("It is not prime");
else
printf("\n It is prime");
getch();
return i;

}

}

6. This comment has been removed by the author.

7. #include
#include

int check_prime(int);
int main()
{
int n,result;
printf("\n enter number for check if it is prime or not; ");
scanf("%d",&n);
result=check_prime(n);
getch();
return 0;

}

int check_prime(int a)
{
int i;
for(i=2;i<a;i++)
{
if(a%i==0)
printf("It is not prime");
else
printf("\n It is prime");
getch();
return i;

}

}

8. #include
#include
int check_prime(int);
int main()
{
int n,rem;
printf("\n Enter number: ");
scanf("%d",&n);
rem=check_prime(n);
getch();
return 0;

}

int check_prime(int p)
{
int i;
i=2;
do
{
if(p%i==0)
printf("\n it is not prime");
else
printf("\n It is prime");
i++;
return i;
}while(i<p);
}

9. #include
#include
int check_prime(int);
int main()
{
int result,n;
printf("\n Enter number ");
scanf("%d",&n);
result=check_prime(n);
getch();
return 0;

}

int check_prime(int a)
{
int i;
i=2;
while(i<a)
{
if(a%i==0)
printf("\n It is not prime");
else
printf("\n It is prime");
i++;
return i;
}

}

10. #include
#include
int check_prime(int);
int main()
{
int n,rem;
printf("\n Enter number:");
scanf("%d",&n);
rem=check_prime(n);
getch();
return 0;
}

int check_prime(int a)
{
int i;
for(i=2;i<a;i++)
{

if(a%i==0)
printf("\n %d is not a prime number.",a);
else
printf("\n %d is a prime number.",a);
return a;
}
}

11. #include
#include
int check_prime(int);
int main()
{
int n,rem;
printf("\n Enter number:");
scanf("%d",&n);
rem=check_prime(n);
getch();
return 0;
}

int check_prime(int a)
{
int i;
for(i=2;i<a;i++)
{

if(a%i==0)
printf("\n %d is not a prime number.",a);
else
printf("\n %d is a prime number.",a);
return a;
}
}

12. #include

int main(){

int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}

13. #include
void main()
{
int num,result=0;
printf("/n enter the number");
scanf("%d",&num);
result=prime(num);
if(result==0)
printf("/n%d the number is prime");
else
printf("/n %d the number is not prime");
getch();
}
int prime(int n)
{
int i
for(i=2;i<n;i++)
{
if(n%i==0)
continue;
else
return 1
}
return 0
}

14. #include
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}

15. #include
int checkPrime(int n)
{
int i, flag=0;
for(i=2; i<=n/2; ++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
{
printf("%d is a prime number.",n);
}
else
{
printf("%d is not a prime number.",n);
}
return 0;

}

int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
checkPrime(n);
}

16. #include
using namespace std;
void prime_check(int p)
{

int i,r;
for(i=2;i<=p/2;i++){

r=p%i;
if(r==0)
break;
}

if(r==0){
cout<<"not prime"<>p;
prime_check(p);
}

17. Write a Program to check whether the given input in Prime or Not . User defined function is mandatory to use.

#include
using namespace std;
int pr(int pri);
main()
{
int pri;
cin>>pri;
pr(pri);
}
int pr(int j)
{
int i,k;
k=1;
for(i=2;i<j;i++)
{
if(j%i==0)
{
k=0;
}
}
if(k!=0)
cout<<"This digit is prime";
else
cout<<"This digit is not prime";
}

18. Id-201210803

#include
using namespace std;

int chk_prime(int num)
{
int i;
if(num<=1)
return 0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
return 0;
}
return 1;
}
int main()
{
int n,p;
cout<<"enter a number";
cin>>n;
p=chk_prime(n);
if(p==1)
cout<<"Its a prime number";
else
cout<<"Not Prime";
}

19. #include
using namespace std;

int chk_prime(int num)
{
int i;
if(num<=1)
return 0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
return 0;
}
return 1;
}
int main()
{
int n,p;
cout<<"enter a number";
cin>>n;
p=chk_prime(n);
if(p==1)
cout<<"Its a prime number";
else
cout<<"Not Prime";
}

20. #include
using namespace std;

class prime
{
public:
int num;
prime();
void show();
~prime();
};

prime::prime()
{
cout<<"Enter a number: ";
cin>>num;
}

void prime::show()
{
int i,flag=0;
for(i=2; i<=num/2; i++)
{
if(num%i==0)
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
cout<<endl<<num<<" is a prime number.";
else
cout<<endl<<num<<" is not a prime number.";
}
prime::~prime()
{

}
main()
{
prime ob1,ob2;
ob1.show();
ob2.show();
}

21. #include
using namespace std;
class prime
{
public:
int num;
prime();
void show();
~prime();
};
prime::prime()
{
cout<<"Enter a number: ";
cin>>num;
cout<<endl;
}
void prime::show()
{
int i,flag=0;
for(i=2; i<=num/2; i++)
{
if(num%i==0){
flag=1;
break;
}
else
flag=0;
}
if(flag==1)
cout<<"Not prime number..."<<endl;
else
cout<<"Prime number..."<<endl;
}
prime::~prime()
{

}
int main()
{
prime obj;
obj.show();
return 0;
}

22. #include
using namespace std;
class prime
{
public:
int n,i=2;
prime();
void show();
~prime()
{

}

};
prime::prime()
{
cout<<"Input Number To check prime: ";
cin>>n;
}
void prime::show()
{
for(int i=2;i<=n-1;i++)
{
if(n%i==0)
{

cout<<"This is Not prime Number"<<endl;
break;
}
else
cout<<"This is prime number."<<endl;
}

}
int main()
{
prime appel;
appel.show();
}

23. #include
using namespace std;
class prime
{
public:
int n,i=2,flag;
prime();
void show();
~prime()
{

}

};
prime::prime()
{
cout<<"Input Number To check prime: ";
cin>>n;
}
void prime::show()
{
for(int i=2;i<=n-1;i++)
{
if(n%i==0)
{
flag=1;
break;
}
else
flag=0;
}
if(flag==1)
cout<<"This is Not prime Number"<<endl;
else
cout<<"This is prime number."<<endl;

}
int main()
{
prime appel;
appel.show();
}

24. using C++

#include
using namespace std;

class cons
{
public:
int num;
cons(int i);
~cons();

void result();
};

cons::cons(int i)
{
num = i;
}

void cons::result()
{
string check = "ok";

for (int i = 2; i < num; i++){
if (num % i == 0){
check = "no";
break;
}
}

if (check == "no")
cout << num << " is not a prime number --\n";
else
cout << num << " is a prime number --\n";
}

cons::~cons()
{
cout << "[+]MEMORY RELEASED[+]\n";
}

int main()

{
int i;
cout << "Eneter the number that you want to check --: ";
cin >> i;

cons obj(i);
obj.result();

return 0;
}

25. using constructor

#include
using namespace std;
class prime
{
public:
int i,t,p;
prime();

~prime();
void result();
};
prime::prime()
{
cout<<"Enter a number :" ;
cin>>p;
}
prime::~prime()
{

}
void prime::result()
{
t=1;
for(i=2;i<p;i++)
{
if(p%i==0)
{
t=0;
break;
}
}
if(t==0)
cout<<"This is not prime number:";
else
cout<<"This is prime number :"<<"\n";
}
int main()
{
prime sb;
sb.result();
}

26. #include
using namespace std;

class prime
{
public:
int num,i;
string check;
prime();
~prime()
{
cout<<"Distructor active";
}
int func()
{
for(i=2;i>num;
}

int main()
{
prime obj;
obj.func();
return 0;
}

id:201510660

27. #include
using namespace std;

class prime
{
public:
int a,j;
string check;
prime();
~prime()
{
cout<<"Distructor active";
}
int func()
{
for(j=2;i>a;
}

int main()
{
prime obj;
obj.func();
return 0;
}

id:201510992