diff --git a/PronicNumber.java b/PronicNumber.java new file mode 100644 index 0000000..d2cd267 --- /dev/null +++ b/PronicNumber.java @@ -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(); + } +} \ No newline at end of file diff --git a/sorting/CycleSort.java b/sorting/CycleSort.java new file mode 100644 index 0000000..fbbd7c7 --- /dev/null +++ b/sorting/CycleSort.java @@ -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); +} + +} \ No newline at end of file