Skip to content

Commit

Permalink
Merge pull request #185 from Rohxn16/main
Browse files Browse the repository at this point in the history
Added code for pronic number. Closes #83
  • Loading branch information
sujana-kamasany authored Oct 16, 2023
2 parents 638fcf1 + 54cdc0b commit 372ab27
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
22 changes: 22 additions & 0 deletions PronicNumber.java
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();
}
}
87 changes: 87 additions & 0 deletions sorting/CycleSort.java
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);
}

}

0 comments on commit 372ab27

Please sign in to comment.