-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from Rohxn16/main
Added code for pronic number. Closes #83
- Loading branch information
Showing
2 changed files
with
109 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,22 @@ | ||
import java.util.Scanner; | ||
public class PronicNumber{ | ||
public static boolean isPronic(int n){ | ||
for(int i = 0; i < Math.sqrt(n); i++){ | ||
if(n == i * (i+1)) return true; | ||
} | ||
return false; | ||
} | ||
public static void main(String args[]){ | ||
Scanner sc = new Scanner(System.in); | ||
int n; | ||
System.out.println("Enter a number : "); | ||
n = sc.nextInt(); | ||
if(isPronic(n)){ | ||
System.out.println(n + " is a pronic number."); | ||
} | ||
else{ | ||
System.out.println(n + " is not a pronic number"); | ||
} | ||
sc.close(); | ||
} | ||
} |
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,87 @@ | ||
import java.util.Scanner; | ||
class CycleSort | ||
{ | ||
/*function to implement to cycle sort*/ | ||
static void cycleSort(int a[], int n) | ||
{ | ||
int start, element, pos, temp, i; | ||
|
||
/*Loop to traverse the array elements and place them on the correct | ||
position*/ | ||
for (start = 0; start <= n - 2; start++) { | ||
element = a[start]; | ||
|
||
/*position to place the element*/ | ||
pos = start; | ||
|
||
for (i = start + 1; i < n; i++) | ||
if (a[i] < element) | ||
pos++; | ||
if (pos == start) /*if the element is at exact position*/ | ||
continue; | ||
while (element == a[pos]) | ||
pos += 1; | ||
if (pos != start) /*put element at its exact position*/ | ||
{ | ||
//swap(element, a[pos]); | ||
temp = element; | ||
element = a[pos]; | ||
a[pos] = temp; | ||
} | ||
/*Rotate rest of the elements*/ | ||
while (pos != start) | ||
{ | ||
pos = start; | ||
/*find position to put the element*/ | ||
for (i = start + 1; i < n; i++) | ||
if (a[i] < element) | ||
pos += 1; | ||
|
||
/*Ignore duplicate elements*/ | ||
while (element == a[pos]) | ||
pos += 1; | ||
|
||
/*put element to its correct position*/ | ||
if (element != a[pos]) | ||
{ | ||
temp = element; | ||
element = a[pos]; | ||
a[pos] = temp; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
static void print(int a[], int n) /*function to print array elements*/ | ||
{ | ||
int i; | ||
for(i = 0; i < n; i++) | ||
{ | ||
System.out.print(a[i] + " "); | ||
} | ||
} | ||
|
||
|
||
public static void main(String args[]) | ||
{ | ||
// int[] a = {87, 42, 27, 17, 7, 37, 57, 47, 2, 1}; | ||
// int n = a.length; | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter the length of the array : "); | ||
int n = sc.nextInt(); | ||
System.out.println("Enter the elements of the array : "); | ||
for(int i = 0; i < n; i++) | ||
{ | ||
arr[i] = sc.nextInt(); | ||
} | ||
sc.close(); | ||
System.out.print("Before sorting array elements are - \n"); | ||
print(a, n); | ||
cycleSort(a, n); | ||
System.out.print("\nAfter applying cycle sort, array elements are - \n"); | ||
print(a, n); | ||
} | ||
|
||
} |