Skip to main content

Untitled

Aug 21, 2023AustinLeath
Loading...

More C# Posts

Breadth-First Search in C#

Oct 15, 2022CodeCatch

0 likes • 57 views

// C# program to print BFS traversal
// from a given source vertex.
// BFS(int s) traverses vertices
// reachable from s.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// This class represents a directed
// graph using adjacency list
// representation
class Graph{
// No. of vertices
private int _V;
//Adjacency Lists
LinkedList<int>[] _adj;
public Graph(int V)
{
_adj = new LinkedList<int>[V];
for(int i = 0; i < _adj.Length; i++)
{
_adj[i] = new LinkedList<int>();
}
_V = V;
}
// Function to add an edge into the graph
public void AddEdge(int v, int w)
{
_adj[v].AddLast(w);
}
// Prints BFS traversal from a given source s
public void BFS(int s)
{
// Mark all the vertices as not
// visited(By default set as false)
bool[] visited = new bool[_V];
for(int i = 0; i < _V; i++)
visited[i] = false;
// Create a queue for BFS
LinkedList<int> queue = new LinkedList<int>();
// Mark the current node as
// visited and enqueue it
visited[s] = true;
queue.AddLast(s);
while(queue.Any())
{
// Dequeue a vertex from queue
// and print it
s = queue.First();
Console.Write(s + " " );
queue.RemoveFirst();
// Get all adjacent vertices of the
// dequeued vertex s. If a adjacent
// has not been visited, then mark it
// visited and enqueue it
LinkedList<int> list = _adj[s];
foreach (var val in list)
{
if (!visited[val])
{
visited[val] = true;
queue.AddLast(val);
}
}
}
}

Luhn Algorithm

Nov 18, 2022AustinLeath

0 likes • 11 views

// C# program to implement
// Luhn algorithm
using System;
class GFG {
// Returns true if given
// card number is valid
static bool checkLuhn(String cardNo)
{
int nDigits = cardNo.Length;
int nSum = 0;
bool isSecond = false;
for (int i = nDigits - 1; i >= 0; i--)
{
int d = cardNo[i] - '0';
if (isSecond == true)
d = d * 2;
// We add two digits to handle
// cases that make two digits
// after doubling
nSum += d / 10;
nSum += d % 10;
isSecond = !isSecond;
}
return (nSum % 10 == 0);
}
// Driver code
static public void Main()
{
String cardNo = "79927398713";
if (checkLuhn(cardNo))
Console.WriteLine("This is a valid card");
else
Console.WriteLine("This is not a valid card");
}
}

Untitled

Aug 5, 2023usama

0 likes • 5 views

public class King {
fewf
}