Loading...
More C# Posts
// 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// representationclass Graph{// No. of verticesprivate int _V;//Adjacency ListsLinkedList<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 graphpublic void AddEdge(int v, int w){_adj[v].AddLast(w);}// Prints BFS traversal from a given source spublic 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 BFSLinkedList<int> queue = new LinkedList<int>();// Mark the current node as// visited and enqueue itvisited[s] = true;queue.AddLast(s);while(queue.Any()){// Dequeue a vertex from queue// and print its = 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 itLinkedList<int> list = _adj[s];foreach (var val in list){if (!visited[val]){visited[val] = true;queue.AddLast(val);}}}}
// C# program to implement// Luhn algorithmusing System;class GFG {// Returns true if given// card number is validstatic 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 doublingnSum += d / 10;nSum += d % 10;isSecond = !isSecond;}return (nSum % 10 == 0);}// Driver codestatic public void Main(){String cardNo = "79927398713";if (checkLuhn(cardNo))Console.WriteLine("This is a valid card");elseConsole.WriteLine("This is not a valid card");}}
public class King {fewf}