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
2 changes: 2 additions & 0 deletions desktop.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[.ShellClassInfo]
LocalizedResourceName=@Java,0
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
<artifactId>Java</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<assertj.version>3.27.6</assertj.version>
</properties>


<dependencyManagement>
<dependencies>
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/com/thealgorithms/conversions/USDtoIND.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.thealgorithms.conversions;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class USDtoIND {

// ✅ Free, keyless API endpoint
private static final String API_URL = "https://open.er-api.com/v6/latest/USD";

/**
* Fetches the live USD to INR rate from open.er-api.com
*
* @return the exchange rate, or -1 if an error occurs
*/
public static double fetchLiveRate() {
try {
// Step 1: Connect to the URL
URL url = new URL(API_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

// Step 2: Read API response
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();

// Step 3: Extract INR value manually from the JSON text
String json = response.toString();

// Find INR rate
int start = json.indexOf("\"INR\":") + 6;
int end = json.indexOf(",", start);
if (end == -1)
end = json.indexOf("}", start);
String rateStr = json.substring(start, end);

return Double.parseDouble(rateStr);
} catch (Exception e) {
System.out.println("Error fetching exchange rate: " + e.getMessage());
return -1;
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("USD ↔ INR Converter");
System.out.println("--------------------");
System.out.println("Choose conversion type:");
System.out.println("1. USD → INR");
System.out.println("2. INR → USD");
System.out.print("Enter your choice (1 or 2): ");
int choice = sc.nextInt();

// Fetch live exchange rate
double rate = fetchLiveRate();

if (rate <= 0) {
System.out.println("Failed to retrieve live exchange rate. Please check your internet connection.");
sc.close();
return;
}

// Conversion logic
if (choice == 1) {
System.out.print("Enter amount in USD: ");
double usd = sc.nextDouble();
double inr = usd * rate;
System.out.printf("%.2f USD = %.2f INR (Rate: %.2f)%n", usd, inr, rate);
} else if (choice == 2) {
System.out.print("Enter amount in INR: ");
double inr = sc.nextDouble();
double usd = inr / rate;
System.out.printf("%.2f INR = %.2f USD (Rate: %.2f)%n", inr, usd, rate);
} else {
System.out.println("Invalid choice. Please run the program again and select 1 or 2.");
}

sc.close();
}
}
132 changes: 132 additions & 0 deletions src/main/java/com/thealgorithms/graph/BipartiteGraphDFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.thealgorithms.graph;

import java.util.Arrays;
import java.util.Scanner;

/**
* This class provides an implementation to check whether a given graph is
* bipartite using Depth First Search (DFS).
*
* A bipartite graph is one in which the set of vertices can be divided into two
* disjoint subsets
* such that no two vertices within the same subset are adjacent.
*
* For more information, see
* <a href="https://en.wikipedia.org/wiki/Bipartite_graph">Wikipedia: Bipartite
* Graph</a>.
*
* <p>
* Example:
* </p>
*
* <pre>
* Input:
* 4 4
* 0 1
* 1 2
* 2 3
* 3 0
*
* Output:
* Graph is Bipartite: true
* </pre>
*
* Author: Ilma Akram Ansari
*/
public final class BipartiteGraphDFS {

// Private constructor to prevent instantiation
private BipartiteGraphDFS() {
}

/**
* Checks whether the given undirected graph is bipartite using Depth First
* Search (DFS).
*
* @param graph The adjacency list representation of the graph,
* where graph[i] contains an array of all vertices adjacent to
* vertex i.
* @return {@code true} if the graph is bipartite, otherwise {@code false}.
*/
public static boolean isBipartite(int[][] graph) {
int n = graph.length;
int[] color = new int[n];
Arrays.fill(color, -1); // -1 = uncolored

// Handle disconnected graphs
for (int i = 0; i < n; i++) {
if (color[i] == -1) {
if (!dfs(graph, i, 0, color)) {
return false;
}
}
}
return true;
}

/**
* Recursive DFS helper method to assign colors and check bipartiteness.
*
* @param graph The adjacency list of the graph.
* @param node The current vertex being explored.
* @param currColor The color to assign to this vertex (0 or 1).
* @param color Array storing colors of all vertices.
* @return {@code true} if no conflict is found, otherwise {@code false}.
*/
private static boolean dfs(int[][] graph, int node, int currColor, int[] color) {
color[node] = currColor;

for (int neighbor : graph[node]) {
if (color[neighbor] == -1) {
// Assign opposite color to neighbor
if (!dfs(graph, neighbor, 1 - currColor, color)) {
return false;
}
} else if (color[neighbor] == currColor) {
// Found same color on adjacent nodes -> not bipartite
return false;
}
}
return true;
}

/**
* Main method to take input and check if the graph is bipartite.
* Input format:
* n m
* u1 v1
* u2 v2
* ...
* um vm
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter number of vertices and edges:");
int n = sc.nextInt(); // number of vertices
int m = sc.nextInt(); // number of edges

// Using adjacency list representation
int[][] temp = new int[n][n]; // temporary adjacency matrix for building adjacency list
int[] degree = new int[n]; // degree of each vertex

System.out.println("Enter each edge (u v):");
for (int i = 0; i < m; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
temp[u][degree[u]++] = v;
temp[v][degree[v]++] = u; // since graph is undirected
}

// Convert to adjacency list (trim extra zeros)
int[][] graph = new int[n][];
for (int i = 0; i < n; i++) {
graph[i] = Arrays.copyOf(temp[i], degree[i]);
}

boolean result = isBipartite(graph);
System.out.println("Graph is Bipartite: " + result);

sc.close();
}
}
Loading