From 40898696c7880d877b111749ed7076d904e4cc14 Mon Sep 17 00:00:00 2001 From: MishaalAslam <109417825+MishaalAslam@users.noreply.github.com> Date: Mon, 24 Oct 2022 16:44:24 +0500 Subject: [PATCH] Added a twin prime number code I have added a Twin prime number find code in C --- Algorithms/C++/dfs.cpp | 63 +++++++++++++++++++++++++--------------- Algorithms/C/twinPrime.c | 46 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 Algorithms/C/twinPrime.c diff --git a/Algorithms/C++/dfs.cpp b/Algorithms/C++/dfs.cpp index ca0c3039..4dfef951 100644 --- a/Algorithms/C++/dfs.cpp +++ b/Algorithms/C++/dfs.cpp @@ -1,24 +1,41 @@ -class Graph +#include +#include +#include +using namespace std; +int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10]; +int main() { -public: - map visited; - map> adj; - void addEdge(int v, int w); - void DFS(int v); -}; - -void Graph::addEdge(int v, int w) -{ - adj[v].push_back(w); -} - -void Graph::DFS(int v) -{ - visited[v] = true; - cout << v << " "; - list::iterator i; - for (i = adj[v].begin(); i != adj[v].end(); ++i) - if (!visited[*i]) - DFS(*i); -} - \ No newline at end of file + int m; + cout <<"Enter no of vertices: "; + cin >> n; + cout <<"Enter no of edges: "; + cin >> m; + cout <<"\nEDGES \n"; + for(k=1; k<=m; k++) + { + cin >>i>>j; + cost[i][j]=1; + } + cout <<"Enter initial vertex to traverse from: "; + cin >>v; + cout <<"DFS ORDER OF VISITED VERTICES: "; + cout << v <<" "; + visited[v]=1; + k=1; + while(k=1; j--) + if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) + { + visit[j]=1; + stk[top]=j; + top++; + } + v=stk[--top]; + cout< +int main() +{ + + int num; + int prime; + printf("Enter a positive Integer: "); + scanf("%d", &num); //taking input + + while (num < 0) + { + printf("Invalid input!\nEnter Positive Integer Again: "); + scanf("%d", &num); + } + + for (int i = 3; i < num; i++) + { + prime = 0; + for (int j = 3; j < i; j++) + { + if (i % j == 0 || (i + 2) % j == 0) + { + prime = 1; + } + } + + if (prime == 0) + { + //printf("Twin prime numbers between 2 and %d are ", num); + printf("(%d,%d)", i, i + 2); + printf("\n"); + } + } + + + + + + + + + return 0; +} \ No newline at end of file