diff --git a/Shivanya P M/DiffBetweenPostandPreIncrement.java b/Shivanya P M/DiffBetweenPostandPreIncrement.java new file mode 100644 index 0000000..ef4731b --- /dev/null +++ b/Shivanya P M/DiffBetweenPostandPreIncrement.java @@ -0,0 +1,6 @@ +package TargetAssignment; + +public class DiffBetweenPostandPreIncrement { + //PRE-increment(++i) is used when you want to use the incremented value of the variable in that expression. + //POST-increment(i++) uses the original value before incrementing it. +} diff --git a/Shivanya P M/EqualNotEqual.java b/Shivanya P M/EqualNotEqual.java new file mode 100644 index 0000000..6713c12 --- /dev/null +++ b/Shivanya P M/EqualNotEqual.java @@ -0,0 +1,24 @@ +package TargetAssignment; + +import com.sun.source.doctree.SystemPropertyTree; + +public class EqualNotEqual { + public static void main(String args[]){ + System.out.println("arg count: "+args.length); + for(int i=0;i b) + return gcd(a-b, b); + return gcd(a, b-a); + } + + + public static void main(String[] args) + { + Scanner input = new Scanner(System.in); + System.out.println("Type a number:"); + int a = input.nextInt(); + System.out.println("Type another number:"); + int b = input.nextInt(); + + System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b)); + } + } + +} diff --git a/Shivanya P M/Question11.java b/Shivanya P M/Question11.java new file mode 100644 index 0000000..ecd0bee --- /dev/null +++ b/Shivanya P M/Question11.java @@ -0,0 +1,23 @@ +package TargetAssignment; +import java.util.Scanner; +public class Question11 { + public static class DecimalToBinaryExample2{ + public static void toBinary(int decimal){ + int binary[] = new int[40]; + int index = 0; + while(decimal > 0){ + binary[index++] = decimal%2; + decimal = decimal/2; + } + for(int i = index-1;i >= 0;i--){ + System.out.print(binary[i]); + } + System.out.println();//new line + } + public static void main(String args[]){ + Scanner input=new Scanner(System.in); + System.out.println("Enter a number: "); + int a =input.nextInt(); + toBinary(a); + }} +} diff --git a/Shivanya P M/Question12.java b/Shivanya P M/Question12.java new file mode 100644 index 0000000..dc2a83c --- /dev/null +++ b/Shivanya P M/Question12.java @@ -0,0 +1,34 @@ +package TargetAssignment; + +public class Question12 { + public static class Checkerboard { + + public static void main(String[] args) { + int n=Integer.parseInt(args[0]); + + for (int i = 0; i <= n; i++) + { + if (i % 2 == 0) + { + System.out.print("* "); + } + else + { + System.out.print(" "); + } + for (int j = 0; j <= n; j++) + { + if (j % 2 == 0) + { + System.out.print(" "); + } + else + { + System.out.print("* "); + } + } + System.out.println(""); + } + } + } +} diff --git a/Shivanya P M/Question5 b/Shivanya P M/Question5 new file mode 100644 index 0000000..d731fc6 --- /dev/null +++ b/Shivanya P M/Question5 @@ -0,0 +1,6 @@ +To join two conditions together in the for loop we can use the operator && + +for( initialization ; condition1 && condition2 ; increment) + Example: + for (int i = 0, j = 9; i <= 9 && j > 0; i = i + 1 , j = j-1) { + System.out.println("ComplicatedForLoop"); \ No newline at end of file diff --git a/Shivanya P M/Question6 b/Shivanya P M/Question6 new file mode 100644 index 0000000..f526551 --- /dev/null +++ b/Shivanya P M/Question6 @@ -0,0 +1,10 @@ + We can use double variable as a loop control variable especially if you want to loop through fractional values or increment by fractional values. + For Example: + + double dx = (to - from) / (n-1); + +for (double x = from; x <= to; x += dx) + { + double y = (Double) f.invoke(null, x); + System.out.printf(%10.4f | %10.4f%n" + y, x, y); + } \ No newline at end of file diff --git a/Shivanya P M/Question7 b/Shivanya P M/Question7 new file mode 100644 index 0000000..f4a936d --- /dev/null +++ b/Shivanya P M/Question7 @@ -0,0 +1,8 @@ +Every for loops can be replaced by while loops, +and vice-versa. It depends upon which one is appropriate to the task. + +In general, you should use a for loop when you know +how many times the loop should run. +If you want the loop to break based on a condition +other than the number of times it runs, +you should use a while loop. \ No newline at end of file diff --git a/Shivanya P M/Question8.java b/Shivanya P M/Question8.java new file mode 100644 index 0000000..bfc1871 --- /dev/null +++ b/Shivanya P M/Question8.java @@ -0,0 +1,38 @@ +package TargetAssignment; + +public class Question8 { + void bubbleSort(int arr[]) + { + int n = arr.length; + for (int i = 0; i < n-1; i++) + for (int j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + { + // swap temp and arr[i] + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + } + } + + /* Prints the array */ + void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i= 0; i--) + heap(arr, n, i); + + // One by one extract an element from heap + for (int i=n-1; i>=0; i--) + { + // Move current root to end + int temp = arr[0]; + arr[0] = arr[i]; + arr[i] = temp; + + // call max heapify on the reduced heap + heap(arr, i, 0); + } + } + + // To heapify a subtree rooted with node i which is + // an index in arr[]. n is size of heap + void heap(int arr[], int n, int i) + { + int largest = i; // Initialize largest as root + int l = 2*i + 1; // left = 2*i + 1 + int r = 2*i + 2; // right = 2*i + 2 + + // If left child is larger than root + if (l < n && arr[l] > arr[largest]) + largest = l; + + if (r < n && arr[r] > arr[largest]) + largest = r; + if (largest != i) + { + int swap = arr[i]; + arr[i] = arr[largest]; + arr[largest] = swap; + heap(arr, n, largest); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i