Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Solving the question Prime Number with iteration as well as recursion HarshCasper#1372
  • Loading branch information
ErzaTitania-2001 authored and JayantGoel001 committed Dec 31, 2020
1 parent 8b318e7 commit 6c1a8e1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Java/cp/Prime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//check whether a number is Prime or not without recursion//
import java.util.Scanner;
class Prime
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number : ");
int n=(int)Math.abs(sc.nextInt());
int c=0;
for(int i=2;i<n;i++)
if (n%i==0)
c++;
System.out.println(n+((c==0)?" is a Prime Number." :" is not a Prime Number."));
}
}
//* Contributed By ErzaTitani-2001

25 changes: 25 additions & 0 deletions Java/cp/PrimeRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//check whether a number is Prime or not with recursion//
import java.util.Scanner;
class PrimeRecursion
{
public static int prime(int n,int div)
{
if(div<n)
{ if(n%div!=0)
prime(n,div+1);
else
return 0;
}
return 1;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number : ");
int n=(int)Math.abs(sc.nextInt());
int c=prime(n,2);
System.out.println(n+((c!=0)?" is a Prime Number." :" is not a Prime Number."));
}
}
//* Contributed By ErzaTitani-2001

0 comments on commit 6c1a8e1

Please sign in to comment.