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

Armstrong Number In Java #449

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Binary file added Armstrong_Number.class
Binary file not shown.
23 changes: 23 additions & 0 deletions Armstrong_Number.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
3 changes: 3 additions & 0 deletions IpAddress/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions IpAddress/.idea/IpAddress.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions IpAddress/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions IpAddress/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions IpAddress/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions IpAddress/IpAddress.java
Original file line number Diff line number Diff line change
@@ -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() );



}
}
3 changes: 3 additions & 0 deletions IpAddress/out/production/IpAddress/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions IpAddress/out/production/IpAddress/.idea/IpAddress.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions IpAddress/out/production/IpAddress/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions IpAddress/out/production/IpAddress/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions IpAddress/out/production/IpAddress/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
59 changes: 59 additions & 0 deletions KadanesAlgo/Kadanes_Algorithm.java
Original file line number Diff line number Diff line change
@@ -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<n; i++){
arr[i]=scan.nextInt();
}
long res = maxSubarraySum(arr,n);
System.out.println(res);
}

// function for algo

public static long maxSubarraySum(int arr[], int n){
long res = Integer.MIN_VALUE;
long sum = 0;

for(int e:arr){
sum+=e;
res = Math.max(res,sum);
if(sum<0)sum=0;

}

return res;
}
}
31 changes: 31 additions & 0 deletions SortingAlgo/Cyclic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.Arrays;

public class Cyclic {
public static void main(String[] args) {
int arr[] = { 3, 5, -1, 2, 3, 4 };

int i = 0;
while (i < arr.length) {
int correct = arr[i] - 1;
if (arr[correct] != arr[i]) {
int temp = arr[correct];
arr[correct] = arr[i];
arr[correct] = temp;

} else {
i++;
}

}



System.out.println(Arrays.toString(arr));
}

static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}
79 changes: 79 additions & 0 deletions SortingAlgo/bubble&selection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

import java.util.*;

class bubble&selection {
public static void main(String[] args) {
int a[] = { 4, 3, 2, 7, 8, 2, 3, 1 };

Cyclic(a);
// int bubble[] = selecticyclion(a);
// System.out.print(Arrays.toString(bubble));
}

// swap func
static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}

// *NOTE*
// CYCLIC SORT uses the indexes to sort the array when its from 1 to n , meaning
// {1 ,2, 3 ,4} has the index 1-0 , 2-1 , 3-2 , 4-3 (index = value -1 )
// swap the value with index(this.value-1)

static void Cyclic(int arr[]) {
int i = 0;
while (i < arr.length) {
int correct = arr[i] - 1;
if (arr[correct] != arr[i]) {
swap(arr, correct, i);

} else {
i++;
}
}

System.out.println();
System.out.print("sorted" + Arrays.toString(arr));
}


// *NOTE*
// for every pass the last element will be at the correct place therefore innerloop will run till length-i times
static int[] bubble(int a[]) {
for (int i = 0; i < a.length; i++) {
for (int j = 1; j < a.length - i; j++) {
if (a[j] < a[j - 1]) {
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
return a;
}



static int[] selection(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int last = arr.length - i - 1;
int max = getMaxIndex(arr, 0, last);
swap(arr, max, last);
}
return arr;
}


static int getMaxIndex(int[] arr, int start, int end) {
int max = start;
for (int i = start; i <= end; i++) {
if (arr[max] < arr[i]) {
max = i;
}
}
return max;

}
}
21 changes: 21 additions & 0 deletions SortingAlgo/insertion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.Arrays;

class insertion{
public static void main(String args[]){
int arr[] = {1,3,2,5,4};

for(int i = 0 ; i < arr.length-1 ; i++){
for(int j = i+1 ; j > 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));
}
}
Loading