diff --git a/C/Bubble_Sort.c b/C/Bubble_Sort.c new file mode 100644 index 00000000..bdb395d3 --- /dev/null +++ b/C/Bubble_Sort.c @@ -0,0 +1,46 @@ +// Bubble sort in C + +#include + +// perform the bubble sort +void bubbleSort(int array[], int size) { + + // loop to access each array element + for (int step = 0; step < size - 1; ++step) { + + // loop to compare array elements + for (int i = 0; i < size - step - 1; ++i) { + + // compare two adjacent elements + // change > to < to sort in descending order + if (array[i] > array[i + 1]) { + + // swapping occurs if elements + // are not in the intended order + int temp = array[i]; + array[i] = array[i + 1]; + array[i + 1] = temp; + } + } + } +} + +// print array +void printArray(int array[], int size) { + for (int i = 0; i < size; ++i) { + printf("%d ", array[i]); + } + printf("\n"); +} + +int main() { + int data[] = {-2, 45, 0, 11, -9}; + + // find the array's length + int size = sizeof(data) / sizeof(data[0]); + + bubbleSort(data, size); + + printf("Sorted Array in Ascending Order:\n"); + printArray(data, size); +} diff --git a/java/KeithNumber.java b/java/KeithNumber.java new file mode 100644 index 00000000..e2f5f1c9 --- /dev/null +++ b/java/KeithNumber.java @@ -0,0 +1,57 @@ +import java.util.*; +class KeithNumberExample1 +{ +//user-defined function that checks if the given number is Keith or not +static boolean isKeith(int x) +{ +//List stores all the digits of the X +ArrayList terms=new ArrayList(); +//n denotes the number of digits +int temp = x, n = 0; +//executes until the condition becomes false +while (temp > 0) +{ +//determines the last digit of the number and add it to the List +terms.add(temp%10); +//removes the last digit +temp = temp/10; +//increments the number of digits (n) by 1 +n++; +} +//reverse the List +Collections.reverse(terms); +int next_term = 0, i = n; +//finds next term for the series +//loop executes until the condition returns true +while (next_term < x) +{ +next_term = 0; +//next term is the sum of previous n terms (it depends on number of digits the number has) +for (int j=1; j<=n; j++) +next_term = next_term + terms.get(i-j); +terms.add(next_term); +i++; +} +//when the control comes out of the while loop, there will be two conditions: +//either next_term will be equal to x or greater than x +//if equal, the given number is Keith, else not +return (next_term == x); +} +//driver code +public static void main(String[] args) +{ +//calling the user-defined method inside the if statement and print the result accordingly +if (isKeith(19)) +System.out.println("Yes, the given number is a Keith number."); +else +System.out.println("No, the given number is not a Keith number."); +if(isKeith(742)) +System.out.println("Yes, the given number is a Keith number."); +else +System.out.println("No, the given number is not a Keith number."); +if(isKeith(4578)) +System.out.println("Yes, the given number is a Keith number."); +else +System.out.println("No, the given number is not a Keith number."); +} +}