diff --git a/Armstrong_Number.class b/Armstrong_Number.class new file mode 100644 index 00000000..7674a990 Binary files /dev/null and b/Armstrong_Number.class differ diff --git a/Armstrong_Number.java b/Armstrong_Number.java new file mode 100644 index 00000000..57f3631a --- /dev/null +++ b/Armstrong_Number.java @@ -0,0 +1,23 @@ +// Armstrong Number In Java +import java.util.Scanner; + +public class Armstrong_Number { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter a number to check if it is an Armstrong number or not: "); + int num = sc.nextInt(); + int originalNum, remainder, result = 0; + originalNum = num; + while (originalNum != 0) { + remainder = originalNum % 10; + result += Math.pow(remainder, 3); + originalNum /= 10; + } + if (result == num) { + System.out.println(num + " is an Armstrong number."); + } else { + System.out.println(num + " is not an Armstrong number."); + } + sc.close(); + } +} \ No newline at end of file diff --git a/IpAddress/.idea/.gitignore b/IpAddress/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/IpAddress/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/IpAddress/.idea/IpAddress.iml b/IpAddress/.idea/IpAddress.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/IpAddress/.idea/IpAddress.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/IpAddress/.idea/misc.xml b/IpAddress/.idea/misc.xml new file mode 100644 index 00000000..03f397ce --- /dev/null +++ b/IpAddress/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/IpAddress/.idea/modules.xml b/IpAddress/.idea/modules.xml new file mode 100644 index 00000000..2ff920b3 --- /dev/null +++ b/IpAddress/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/IpAddress/.idea/vcs.xml b/IpAddress/.idea/vcs.xml new file mode 100644 index 00000000..b2bdec2d --- /dev/null +++ b/IpAddress/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/IpAddress/IpAddress.java b/IpAddress/IpAddress.java new file mode 100644 index 00000000..d71b8cef --- /dev/null +++ b/IpAddress/IpAddress.java @@ -0,0 +1,26 @@ +//vineeTagarwaL-code +// a program to print IP address and host name +// 20th oct , 2023 + +import java.net.InetAddress; +import java.net.UnknownHostException; + +public class IpAddress { + + + + public static void main(String[] args) throws UnknownHostException { + + getHost(); + } + + static void getHost() throws UnknownHostException{ + InetAddress ip = InetAddress.getLocalHost(); + System.out.println("Host : "+ip.getHostName() ); + System.out.println("Host 2 "+ip.getCanonicalHostName()); + System.out.println("Ip of my system is : " +ip.getHostAddress() ); + + + + } +} diff --git a/IpAddress/out/production/IpAddress/.idea/.gitignore b/IpAddress/out/production/IpAddress/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/IpAddress/out/production/IpAddress/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/IpAddress/out/production/IpAddress/.idea/IpAddress.iml b/IpAddress/out/production/IpAddress/.idea/IpAddress.iml new file mode 100644 index 00000000..b107a2dd --- /dev/null +++ b/IpAddress/out/production/IpAddress/.idea/IpAddress.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/IpAddress/out/production/IpAddress/.idea/misc.xml b/IpAddress/out/production/IpAddress/.idea/misc.xml new file mode 100644 index 00000000..03f397ce --- /dev/null +++ b/IpAddress/out/production/IpAddress/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/IpAddress/out/production/IpAddress/.idea/modules.xml b/IpAddress/out/production/IpAddress/.idea/modules.xml new file mode 100644 index 00000000..2ff920b3 --- /dev/null +++ b/IpAddress/out/production/IpAddress/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/IpAddress/out/production/IpAddress/.idea/vcs.xml b/IpAddress/out/production/IpAddress/.idea/vcs.xml new file mode 100644 index 00000000..b2bdec2d --- /dev/null +++ b/IpAddress/out/production/IpAddress/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/IpAddress/out/production/IpAddress/IpAddress.class b/IpAddress/out/production/IpAddress/IpAddress.class new file mode 100644 index 00000000..280023a1 Binary files /dev/null and b/IpAddress/out/production/IpAddress/IpAddress.class differ diff --git a/KadanesAlgo/Kadanes_Algorithm.java b/KadanesAlgo/Kadanes_Algorithm.java new file mode 100644 index 00000000..03bd1ad9 --- /dev/null +++ b/KadanesAlgo/Kadanes_Algorithm.java @@ -0,0 +1,59 @@ +/** +#Algorithm : +********************************************************************************************************************************************* +Algorithm maxSubarraySum(arr, n): +Input: arr - an integer array of length n + n - the size of the array +Output: res - the maximum subarray sum + +1. Initialize two long variables: + - res to Integer.MIN_VALUE (set to the smallest possible value) + - sum to 0 (to keep track of the current subarray sum) + +2. Iterate through the array elements using a for-each loop: + For each element e in arr: + a. Add e to sum, updating the current subarray sum. + + b. Update res to be the maximum of its current value and the sum. This keeps track of the maximum subarray sum encountered so far. + + c. Check if sum is less than 0 (i.e., if the current subarray sum becomes negative): + - If it is negative, reset sum to 0. This ensures that we start a new subarray whenever the sum becomes negative, as negative sums are not part of the maximum subarray sum. + +3. After iterating through all elements in the array, return the value of res, which represents the maximum subarray sum. + +4. The function has now successfully computed and returned the maximum subarray sum, and the algorithm terminates. +**************************************************************************************************************************************************************** +*/ + +import java.util.Scanner; + +public class Kadanes_Algorithm { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter the size of the array : "); + int n = scan.nextInt(); + + int[] arr = new int[n]; + for(int i=0; i 0 ; j--){ + if(arr[j] < arr[j-1]){ + int temp = arr[j]; + arr[j] = arr[j-1]; + arr[j-1]= temp; + }else{ + break; + } + } + } + + System.out.println(Arrays.toString(arr)); + } +} \ No newline at end of file diff --git a/rectangle.java b/rectangle.java new file mode 100644 index 00000000..74ed3fc2 --- /dev/null +++ b/rectangle.java @@ -0,0 +1,105 @@ +package practice_java; +import java.util.*; +import java.lang.*; + +class OutOfRange extends Exception//creating exception +{ + Double a; + OutOfRange(Double b) { + a=b; + } + public String toString(){ + return ("Out Of Range Value provided "+a) ; + } +} + + +public class Rectangle +{ Scanner sc=new Scanner(System.in); + Double length, width; + private String area; + private String perimeter; + + + public Double getLength() + { + return this.length; + } + public Double setLength(Double length) + { + try { + if(length < 0.0 || length > 20.0)//checking exception + { + throw new OutOfRange(length); + } + } + catch (OutOfRange e) + { + System.out.println(e); + } + + return this.length = length; + } + public Double getWidth() + { + return this.width; + } + public Double setWidth(Double width) + { + try { + if(width < 0.0 || width > 20.0) + { + throw new OutOfRange(width); + } + } + catch (OutOfRange e) + { + System.out.println(e); + } + + return this.width = width; + } + public double area() + { + return (this.length*this.width); + } + public double perimeter() + { + return (2*(this.length+this.width)); + } + + + public Rectangle(double x1, double x2, double x3, double x4, double y1, double y2, double y3, double y4)//taking cordinates + + { + + if(Math.hypot(x1-x2, y1-y2)==Math.hypot(x3-x4, y3-y4)) + { + System.out.println("Rectangle is valid "); + } + else if(x1==x3 && x3==x4 && y1==y2 && y3==y4) + { + System.out.println("It is square"); + + + } + else + System.out.println("Rectangle is invalid "); + + this.setLength(Math.sqrt((y4 - y1) * (y4 - y1) + (x4 - x1) * (x4 - x1))); + this.setWidth(Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1))); + System.out.println("Length is "+getLength()); + System.out.println("Width is" +getWidth()); + + + + } + + public static void main(String args[]) + { + + Rectangle obj=new Rectangle(3.0,5.0,4.0,5.0,3.0,7.0,4.0,7.0); + System.out.println("area is" +obj.area()); + System.out.println("perimeter is" +obj.perimeter()); + } + }