Skip to content
Open
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
6 changes: 6 additions & 0 deletions Shivanya P M/DiffBetweenPostandPreIncrement.java
Original file line number Diff line number Diff line change
@@ -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.
}
24 changes: 24 additions & 0 deletions Shivanya P M/EqualNotEqual.java
Original file line number Diff line number Diff line change
@@ -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<args.length;i++){
System.out.println("Enter your input: "+args[i]);
if(i==2){
if(args[0]==args[1] && args[1]==args[2]){
System.out.println("Equal");
} else if (args[0] != args[1] || args[0] != args[2] || args[1]!=args[2]){
System.out.println("Not Equal");
}

}
}
// System.out.println("Enter your first input: "+args[1]);
// System.out.println("Enter your second input: "+args[1]);
// System.out.println("Enter your third input: "+args[2]);

}
}
15 changes: 15 additions & 0 deletions Shivanya P M/PowersOfTwo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package TargetAssignment;

public class PowersOfTwo {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int i = 0;
int powerOfTwo = 1;
while (i <= n) {
System.out.println(i + " " + powerOfTwo);
powerOfTwo = 2 * powerOfTwo;
i = i + 1;
}
}
}

39 changes: 39 additions & 0 deletions Shivanya P M/Question10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package TargetAssignment;
import java. util. Scanner;
public class Question10 {

static class Test
{

static int gcd(int a, int b)
{

if (a == 0)
return b;
if (b == 0)
return a;


if (a == b)
return a;


if (a > 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));
}
}

}
23 changes: 23 additions & 0 deletions Shivanya P M/Question11.java
Original file line number Diff line number Diff line change
@@ -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);
}}
}
34 changes: 34 additions & 0 deletions Shivanya P M/Question12.java
Original file line number Diff line number Diff line change
@@ -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("");
}
}
}
}
6 changes: 6 additions & 0 deletions Shivanya P M/Question5
Original file line number Diff line number Diff line change
@@ -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");
10 changes: 10 additions & 0 deletions Shivanya P M/Question6
Original file line number Diff line number Diff line change
@@ -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);
}
8 changes: 8 additions & 0 deletions Shivanya P M/Question7
Original file line number Diff line number Diff line change
@@ -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.
38 changes: 38 additions & 0 deletions Shivanya P M/Question8.java
Original file line number Diff line number Diff line change
@@ -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<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// Driver method to test above
public static void main(String args[])
{
Question8 ob = new Question8();
int arr[] = {27,33,42,12,72,58,96};
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}


74 changes: 74 additions & 0 deletions Shivanya P M/Question8HeapSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package TargetAssignment;

public class Question8HeapSort {
// Java program for implementation of Heap Sort
public static class HeapSort
{
public void sort(int arr[])
{
int n = arr.length;

// Build heap (rearrange array)
for (int i = n / 2 - 1; 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<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}

// Driver program
public static void main(String args[])
{
int arr[] = {105,227,72,45,16,23,2};
int n = arr.length;

HeapSort ob = new HeapSort();
ob.sort(arr);

System.out.println("Sorted array is");
printArray(arr);
}
}

}
37 changes: 37 additions & 0 deletions Shivanya P M/Question9.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package TargetAssignment;
import java.util.Scanner;

public class Question9 {

public static void main(String[] args) {

int year;
Scanner scan = new Scanner(System.in);
System.out.println("Enter any Year:");
year = scan.nextInt();
scan.close();
boolean isLeap = false;

if(year % 4 == 0)
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
}
else
isLeap = true;
}
else {
isLeap = false;
}

if(isLeap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}

27 changes: 27 additions & 0 deletions Shivanya P M/SortLinearTimes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package TargetAssignment;
import java.util.Arrays;
public class SortLinearTimes {
public static void sort(int[] A)
{
int zeros = 0;
for (int value: A)
{
if (value == 0) {
zeros++;
}
}
int k = 0;
while (zeros-- != 0) {
A[k++] = 0;
}
while (k < A.length) {
A[k++] = 1;
}
}
public static void main (String[] args)
{
int[] A = { 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 };
sort(A);
System.out.println(Arrays.toString(A));
}
}