Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code for pronic number. Closes #83 #185

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}