forked from HarshCasper/NeoAlgo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solving the question Prime Number with iteration as well as recursion HarshCasper#1372
- Loading branch information
1 parent
8b318e7
commit 6c1a8e1
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|