Skip to main content

Posts

Showing posts from January, 2014

Email validation and separating email id from email server name in java

//file name--> Email.java import java.io.*; import java.util.*; class Email{ public static void main(String args[]) { Scanner input=new Scanner(System.in); String mail; boolean flag1=false,flag2=false; System.out.println("Enter Email Address:"); mail=input.next(); char emailid[]=mail.toCharArray(); char server[]=new char[15]; char id[]=new char[25]; int i=0,j=0; for(char temp: emailid){ if(temp=='@') flag1=true; if(flag1) { if(temp=='.') flag2=true; } if(!flag1) { id[i++]=temp; } else { if(temp!='@') server[j++]=temp; } } if(flag1 && flag2) { System.out.println("\nOutput:It is valid address."); System.out.print("Email id:"); for(char temp: id){ System.out.print(temp); } System.out.print("\nEmail server address:"); for(char temp: server){ System.out.print(temp); } }

matrix multiplication in java

Matrix.java import java.util.*; class Matrix{ public static void main(String args[])throws Exception { Scanner s=new Scanner(System.in); int r1,r2,c1,c2; System.out.println("Enter row and column for matrix1:"); r1=s.nextInt(); c1=s.nextInt(); int[][] m1=new int[r1][c1]; System.out.println("Enter the elements of first matrix"); for(int i=0;i < r1;i++) { for(int j=0;j < c1;j++) { m1[i][j]=s.nextInt(); } } System.out.println("Enter row and column for matrix2:"); r2=s.nextInt(); c2=s.nextInt(); if(c1!=r2) { System.out.println("Matrix multiplication is not possible"); } else { int m2[][] = new int[r2][c2]; int multiply[][] = new int[r1][c2]; int sum=0; System.out.println("Enter the elements of second matrix"); for(int i=0;i < r2;i++) { for(int j=0;j < c2;j++) { m2[i][j]=s.nextInt(); } } for(int i=0;i < r1;

Largest and smallest number from array in java

//File name--> MinMax.java import java.io.*; class MinMax{ public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n,min=0,max=0; System.out.println("Enter number of element you want in array."); n=Integer.parseInt(br.readLine()); int[] a=new int[n]; for(int i=0;i < n;i++) { System.out.println("Enter elementa["+i+"]:"); a[i]=Integer.parseInt(br.readLine()); } min=max=a[0]; System.out.print("Content of Array:"); for(int i=0;i < n;i++) { System.out.print("\t"+a[i]); if(min>a[i]) min=a[i]; if(max < a[i]) max=a[i]; } System.out.println("\nLargest element from array is:"+max); System.out.println("Smallest element from array is:"+min); } } OUTPUT javac MinMax.java java MinMax Enter number of element you want in array. 10 Enter elementa[0]: -5 Enter element

Armstrong number in java

//File name--> Armstrong.java import java.io.*; class Armstrong{ public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int no,temp,sum=0,rem=0; System.out.println("Enter number:"); no=temp=Integer.parseInt(br.readLine()); while(no>0) { rem=no%10; sum+=(rem*rem*rem); no/=10; } if(sum==temp) { System.out.println("given number "+temp+" is Armstrong number."); } else { System.out.println("given number "+temp+" is not Armstrong number."); } } } OUTPUT javac Armstrong.java java Armstrong Enter number: 123 given number 123 is not Armstrong number. java Armstrong Enter number: 153 given number 153 is Armstrong number. java Armstrong Enter number: 307 given number 307 is not Armstrong number. java Armstrong Enter number: 370 given number 370 is Armstrong number.

Sum of digit in java

//File name-->SumOfDigit.java import java.io.*; class SumOfDigit{ public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int no,sum=0,temp; System.out.println("Enter number:"); no=temp=Integer.parseInt(br.readLine()); while(no>0) { sum+=(no%10); no=no/10; } System.out.println("Sum of Digit for number "+temp+" is:"+sum); } } OUTPUT javac SumOfDigit.java java SumOfDigit Enter number: 12345 Sum of Digit for number 12345 is:15

Calculator using switch case in java

//File name--> Calculator.java import java.io.*; class Calculator{ public static void main(String args[])throws Exception { int n1,n2; String choice; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter number1:"); n1=Integer.parseInt(br.readLine()); System.out.println("press \n + for addition \n - for subtraction \n * for multiplication \n / for division: "); choice=br.readLine(); System.out.println("Enter number2:"); n2=Integer.parseInt(br.readLine()); switch(choice) { case "+": System.out.println("Addition: "+n1+"+"+n2+" is:"+(n1+n2)); break; case "-": System.out.println("Subtraction:"+n1+"-"+n2+" is:"+(n1-n2)); break; case "*": System.out.println("Multiplication:"+n1+"*"+n2+" is:"+(n1*n2)); break; case "/"

Fibonacci series in java

//file name--> Fibonacci.java import java.io.*; class Fibonacci{ public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int p=0,c=1,n=p+c,num; System.out.println("Enter number of Series you want:"); num=Integer.parseInt(br.readLine()); for(int i=0;i < num-1;i++) { if(i==0) System.out.print(p); System.out.print("\t "+n); n=c+p; p=c; c=n; } } } OUTPUT javac Fibonacci.java java Fibonacci Enter number of Series you want: 10 0 1 1 2 3 5 8 13 21 34

Star patterns in java

//File name--> Pattern.java import java.io.*; class Pattern{ public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your choice to print different pattern(1-5):"); int choice=Integer.parseInt(br.readLine()); System.out.println("Enter Number of lines you want:"); int n=Integer.parseInt(br.readLine()); switch(choice) { case 1: for(int i=1;i < =n;i++) { System.out.println(); for(int j=1;j < =i;j++) System.out.print(" "); int a=i; for(int k=0;k < =n-i;k++) { System.out.print(" "+a); a+=i; } } break; case 2: for(int i=0;i < n;i++) { System.out.println(); for(int j=0;j < i;j++) System.out.print(" "); for(int k=0;k < n-i;k++)

Java program to print all real solutions to the quadratic eq. ax2+b+c=0. Read a,b,c values and use the formula (–b+sqrt(b2 4ac))/2a

Quadratic.java import java.io.*; class Quadratic { public static void main(String args[]) throws Exception { double a,b,c; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value of a:"); a=Double.parseDouble(br.readLine()); System.out.println("Enter the value of b:"); b=Double.parseDouble(br.readLine()); System.out.println("Enter the value of c:"); c=Double.parseDouble(br.readLine()); double delta=(b*b)-(4*a*c); if(a==0) { System.out.println("No solution possible"); } else if ( delta == 0 ) { double root = - b / (2 * a); System.out.println("The Real roots are equal:" + root ); } else if(delta < 0) { System.out.println("The roots are imaginary"); } else { double Root1 = (- b + Math.sqrt(delta)) / (2 * a); double Root2 = (- b - Math.sqrt(delta)) / (2 * a); System.out.println("Th

Area of rectangle in Java

//File name --> Rectangle.java class Rectangle { public static void main(String args[]) { int width=15,height=10; int area=width*height; System.out.println("Area of Rectangle having width "+width+" and height "+height+" is:"+area); } } OUTPUT javac Rectangle.java java Rectangle Area of Rectangle having width 15 and height 10 is:150

VB.net Database Connectivity step by step tutorial

This tutorials will explain you how to create database in VB.net and how to connect database from VB.net application. I have explain all the steps with snapshot so that you can easily follow the steps. CREATE DATABASE IN VISUAL STUDIO 2010 CREATE TABLE IN DATABASE INSERT VALUES IN TABLES CREATE CONNECTION OBJECT AND OPEN CONNECTION DATA ADAPTER AND DATA SET BINDING DATA GRID VIEW TO DATABASE BINDING CONTROL TO DATABASE BINDING FORM WITH DATABASE FOR NAVIGATING USING BINDINGBASEMANAGER  OBJECT COMMAND OBJECT FOR ADD,DELETE,UPDATE OPERATION  Final Output: Download tutorial If you have any query or doubt just leave the comment.

Implementation of Binary Tree in C Programming

#include < stdio.h> #include < conio.h> struct node { int data; struct node *right, *left; }*root,*p,*q; struct node *make(int y) { struct node *newnode; newnode=(struct node *)malloc(sizeof(struct node)); newnode->data=y; newnode->right=newnode->left=NULL; return(newnode); } void left(struct node *r,int x) { if(r->left!=NULL) printf("\n Invalid !"); else r->left=make(x); } void right(struct node *r,int x) { if(r->right!=NULL) printf("\n Invalid !"); else r->right=make(x); } void inorder(struct node *r) { if(r!=NULL) { inorder(r->left); printf("\t %d",r->data); inorder(r->right); } } void preorder(struct node *r) { if(r!=NULL) { printf("\t %d",r->data); preorder(r->left); preorder(r->right); } } void postorder(struct node *r) { if(r!=NULL) { postorder(r->left); postorder(r->right); printf("\t %d",r->data); } } void main()

Add two polynomial using Doubly Linked list in C Programming

#include #include typedef struct pnode { float coef; int exp; struct pnode *next; }p; p *getnode(); void main() { p *p1,*p2,*p3; p *getpoly(),*add(p*,p*); void display(p*); clrscr(); printf("\n enter first polynomial."); printf("\n-------------------------"); p1=getpoly(); printf("\n-------------------------"); printf("\n enter second polynomial"); p2=getpoly(); printf("\n-------------------------"); printf("\nthe first polynomial is: "); display(p1); printf("\n-------------------------"); printf("\nthe second polynomial is :"); display(p2); printf("\n-------------------------"); p3=add(p1,p2); printf("\naddition of two polynomial is :\n\n"); display(p3); printf("\n-------------------------"); } p *getpoly() { p *temp,*New,*last; int flag,exp; char ans; float coef; temp=NULL; flag=1; printf("\nenter the polynomial in descending order of

Circular Linked List in C Programming

/*Operations on Circular linked list */ #include < stdio.h> #include < conio.h> typedef struct node { int data; struct node *link; }node; node *start=NULL; node *create(node *); node *display(node *); node *insert_beg(node *); node *insert_end(node *); node *insert_before(node *); node *insert_after(node *); node *delete_beg(node *); node *delete_end(node *); node *delete_node(node *); node *delete_after(node *); node *delete_list(node *); void main() { int choice; clrscr(); do { printf("\n1)Create a List.\n2)display\n3)Insert at beg\n4)Insert at end \n5)Insert before given node\n6)Insert after given node\n7)Delete from beg\n8)Delete from end\n9)Delete given node\n10)Delete node after given node\n11)Delete list"); printf("\n12)Quit"); printf("\nEnter your Choice : "); scanf("%d",&choice); switch(choice) { case 1: start=create(start); printf("\n Linked List created.");

Doubly Linked List in C Programming

/*Operations on Doubly linked list */ #include < stdio.h> #include < conio.h> typedef struct node { int data; struct node *next,*prev; }node; node *start=NULL; node *create(node *); node *display(node *); node *insert_beg(node *); node *insert_end(node *); node *insert_before(node *); node *delete_beg(node *); node *delete_end(node *); node *delete_node(node *); node *delete_after(node *); node *delete_list(node *); main() { int choice; clrscr(); do { printf("\n1)Create a List.\n2)display\n3)Insert at beg\n4)Insert at end \n5)Insert before given node\n6)Delete from beg\n7)Delete from end\n8)Delete given node\n9)Delete node after given node\n10)Delete list"); printf("\n11)Quit"); printf("\nEnter your Choice : "); scanf("%d",&choice); switch(choice) { case 1: start=create(start); printf("\n Linked List created."); break; case 2: start = display(start); break;

Singly Linked List in C Programming

#include #include #define NULL 0 void main() { struct node{ int data; struct node *link; }; typedef struct node node; int val,ch,ch2,cnt,loc,flag=0,num; node *first=NULL,*temp,*ptr,*prv,*next; char c='y'; clrscr(); while(c=='y') { printf("\n1.Insert \n2.Delete \n3.Display \n4.Search \n5.Exit"); printf("\n Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\n1.Insert beginning \n2.Insert End \n3.Insert before given node\n4.insret after given node"); printf("\nEnter your choice:"); scanf("%d",&ch2); printf("\n Enter value:"); scanf("%d",&val); switch(ch2) { case 1: temp=(node*)malloc(sizeof(node)); temp->data=val; if(first==NULL) temp->link=NULL; temp->link=first; first=temp; break; case 2: temp=(node*)malloc(sizeof(node)); temp->data=val;

Circular Queue using Array in C Programming

#include< stdio.h> #include< conio.h> #define MAX 3 #define NULL -1 void main() { int Q[MAX],i,j,f=NULL,r=NULL,val,ch,item; clrscr(); while(1) { printf("\n1.Insert \n2.Delete \n3.Display \n4.Exit"); printf("\n Enter Your choice:"); scanf("%d",&val); switch(val) { case 1: if((f==0 && r==(MAX-1))||(f==r+1)) printf("\nCircular Queue Overflow."); else { if(r==MAX-1) r=0; else r++; printf("\n Enter value for circular queue:"); scanf("%d",&item); Q[r]=item; if(f==NULL) f=0; } break; case 2: if(f==NULL) printf("\n circular queue is empty."); else { printf("\n Deleted element is :%d",Q[f]); if(f==r) f=r=NULL; else if(f==(MAX-1)) f=0; else f++; } break; case 3: if(f==NULL) printf("\n Circular queue is em

Implementation of Queue using Linked List in C Programming

#include< stdio.h> #include< conio.h> #define NULL 0 void main() { struct node{ int data; struct node *link; }; typedef struct node node; int val,ch; node *front=NULL,*rear=NULL,*temp; char c='y'; clrscr(); while(c=='y') { printf("\n1.Insert \n2.Delete \n3.Display \n4.Exit"); printf("\n Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter value:"); scanf("%d",&val); temp=(node*)malloc(sizeof(node)); temp->data=val; temp->link=NULL; if(front==NULL) front=rear=temp; else rear->link=temp; rear=temp; break; case 2: if(front==NULL) printf("\n Queue is empty."); else { temp=front; printf("\n Deleted element is:%d",temp->data); if(front==rear) front=rear=NULL; else front=front->link; free(temp); } break; case 3:

Implementation of Queue using array in C Programming

#include< stdio.h> #include< conio.h> #define MAX 3 void main() { int Q[MAX],f=-1,r=-1,ch,val,i; char cho='y'; clrscr(); while(cho=='y' || cho=='Y') { printf("\n1.Insert \n2.Delete \n3.Show \n4.Exit "); printf("\n Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: if(r>=(MAX-1)) { printf("\n Queue overflow"); } else { printf("\n Enter value:"); scanf("%d",&val); r++; Q[r]=val; if(f==-1) { f=f+1; } } break; case 2: if(f==-1) { printf("\n Queue underflow"); } else { val=Q[f]; printf("\n Deleted element is:%d",val); if(f==r) { f=-1; r=-1; } else f++; } break; case 3: if(f==-1) { printf("\n Queue empty"); } else { printf("\n content of queue:\n"

Stack Using Linked List in C programming

#include< stdio.h> #include< conio.h> #define NULL 0 void main() { struct node{ int data; struct node *link; }; typedef struct node node; int val,ch; node *top=NULL,*temp; char c='y'; clrscr(); while(c=='y') { printf("\n1.Insert \n2.Delete \n3.Display \n4.Exit"); printf("\n Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter value:"); scanf("%d",&val); temp=(node*)malloc(sizeof(node)); temp->data=val; if(top==NULL) temp->link=NULL; temp->link=top; top=temp; break; case 2: if(top==NULL) printf("\n Stack is empty."); else { temp=top; printf("\n Deleted element is:%d",temp->data); top=top->link; } break; case 3: if(top==NULL) printf("\n Stack is empty."); else { temp=top; printf("\n Stack content:"

Stack using Array in C programming

#include< stdio.h> #include< conio.h> #define MAX 3 void main() { int S[MAX],i,val,c,top=-1,loc; char ch='y'; clrscr(); while(ch=='y') { printf("\n1.Push \n2.Pop \n3.Peep \n4.Change \n5.Display \n6.Exit"); printf("\n Enter your choice:"); scanf("%d",&c); switch(c) { case 1: if(top>=(MAX-1)) printf("\n Stack Overflow"); else { top++; printf("\n Enter value:"); scanf("%d",&val); S[top]=val; } break; case 2: if(top==-1) { printf("\n Stack underflow"); } else { printf("\n Deleted element is:%d",S[top]); top--; } break; case 3: printf("\n Enter index number:"); scanf("%d",&loc); if((top-loc)+1<= -1) printf("\n Invalid index."); else { printf("\n At index %d element is %d",loc,S[(

Chain Matrix Multiplication in C Programming

#include< stdio.h> #include< conio.h> void MatrixChain(long*,long); void pop(long[10][10],long,long); long s[10][10],m[10][10]; void main() { long *p,n,i,j,str,end; clrscr(); printf("\n Enter number of element for p Matrix:"); scanf("%ld",&n); printf("\n Enter dimension for P matrix:"); for(i=0;i< =n;i++) { scanf("%ld",&p[i]); } MatrixChain(p,n+1); printf("\n-------------------------------------------"); printf("\nEnter starting and Ending matrix:"); scanf("%ld %ld",&str,&end); printf("\n-------------------------------------------"); printf("\n Parenthesization order for Matrix Chained Multiplication(MCM) is:"); printf("\n-------------------------------------------\n"); pop(s,str,end); printf("\n-------------------------------------------"); getch(); } void MatrixChain(long p[10],long len) { long n,i,j,l,q,k; n=len-1;

Knapsack using Greedy Algorithm in C Programming

# include< stdio.h> # include< conio.h> void knapsack(int n, float weight[], float value[], float W) { float x[20], tp= 0; int i, j, u; u=W; for (i=0;i< n;i++) x[i]=0.0; for (i=0;i< n;i++) { if(weight[i]>u) break; else { x[i]=1.0; tp= tp+value[i]; u=u-weight[i]; } } if(i< n) x[i]=u/weight[i]; tp= tp + (x[i]*value[i]); printf("\n------------------------------------------------"); printf("\nweight | "); for(i=0;i< n;i++) { printf("%1.2f\t",weight[i]); } printf("\nvalue | "); for(i=0;i< n;i++) { printf("%1.2f\t",value[i]); } printf("\n x | "); for(i=0;i< n;i++) { printf("%1.2f\t",x[i]); } printf("\n------------------------------------------------"); printf("\n Maximum value that knapsack carry is:%.2f", tp); printf("\n------------------------------------------------"

Kruskal’s Algorithm For Minimum Spanning Tree in C Programming

#include< stdio.h> #include< conio.h> #define INFINITY 999 typedef struct Graph { int v1,v2,length; }GR; GR G[20]; int tot_edge,tot_node; void create(); void krushkal(); int find(int,int[]); void merge(int i,int j,int parent[]); void main() { clrscr(); printf("\n----------------------------------------------------"); printf("\nKRUSHKAL`s MINIMUM SPANNING TREE ALGORITHM"); printf("\n----------------------------------------------------"); create(); printf("\n Enter any key to see Minimum Spanning Tree?"); getch(); krushkal(); getch(); } void create() { int k; printf("\n Enter Number of nodes in the Graph:"); scanf("%d",&tot_node); printf("\n Enter Number of Edges in the Graph:"); scanf("%d",&tot_edge); printf("\n--------Enter edges and length----------\n"); for(k=0;k< tot_edge;k++) { printf("\nEnter edge in form {v1,v2}:"); scanf(&

make a change using Greedy Algorithm in C Programming

#include< stdio.h> #include< conio.h> int C[]={1,5,10,25,100}; void make_change(int n); int bestsol(int,int); void main() { int n; clrscr(); printf("\n------------------------------------------------"); printf("\n MAKING CHANGE USING GREEDY ALGORITHM "); printf("\n------------------------------------------------"); printf("\n Enter amount you want:"); scanf("%d",&n); make_change(n); getch(); } void make_change(int n) { int S[100],s=0,x,ind=0,i; printf("\n----------------AVAILABLE COINS-----------------\n"); for(i=0;i<= 4;i++) printf("%5d",C[i]); printf("\n------------------------------------------------"); while(s!=n) { x=bestsol(s,n); if(x==-1) {} else { S[ind++]=x; s=s+x; } } printf("\n-------------MAKING CHANGE FOR %4d-------------",n); for(i=0;i < ind;i++) { printf("\n%5d",S[i]); } printf("\

Rabin-Karp Algorithm String Matching in C Programming

#include< stdio.h> #include< conio.h> #define tonum(c) (c >= 'A' && c <= 'Z' ? c - 'A' : c - 'a' + 26) int mod(int a,int p,int m) { int sqr; if (p == 0) return 1; sqr = mod(a,p/2,m) % m; sqr = (sqr * sqr) % m; if (p & 1) return ((a % m) * sqr) % m; else return sqr; } int RabinKarpMatch(char *T,char *P,int d,int q) { int i,j,p,t,n,m,h,found; n = strlen(T); m = strlen(P); h = mod(d,m-1,q); p = t = 0; for (i=0; i < m; i++) { p = (d*p + tonum(P[i])) % q; t = (d*t + tonum(T[i])) % q; } for (i=0; i<= n-m; i++) { if (p == t) { found = 1; for (j=0; j < m; j++) if (P[j] != T[i+j]) { found = 0; break; } if (found) return i; } else { t = (d*(t - ((tonum(T[i])*h) % q)) + tonum(T[i+m])) % q; } } return -1; } void main() { char *str; char *p; int ans,q; clrscr(); printf("\n Enter String:"); gets(str); printf("\n En

Knapsack Using Backtracking in C Programming

#include < stdio.h> #include < conio.h> #define MAX 20 float final_profit; int w[MAX]; int p[MAX]; int n,m; int temp[MAX],x[MAX]; float final_wt; float Bound_Calculation(int,int,int); void BackTracking(int,int,int); void main() { int i; clrscr(); printf("\n-------------------------------------------------------"); printf("\n KNAPSACK PROBLEM USING BACKTRACKING"); printf("\n-------------------------------------------------------"); printf("\n Enter number of Objects you want:"); scanf("%d",&n); printf("\n-------------------------------------------------------"); for(i=1;i < =n;i++) { printf("\n Enter Weight and value for object%d:",i); scanf("%3d %3d",&w[i],&p[i]); } printf("\n Enter Capacity of Knapsack:"); scanf("%d",&m); getch(); printf("\n-------------------------------------------------------"); printf("\n Wei

N-Queen Problem C Programming

#include < stdio.h> #include < conio.h> #include < math.h> int board[20]; int count; void print_board(int); void Queen(int,int); int place(int,int); void main() { int n,i,j; clrscr(); printf("\n-------------------------------------------------------"); printf("\n n-Queen problem using Backtracking"); printf("\n-------------------------------------------------------"); printf("\n Enter number of Queen:"); scanf("%d",&n); Queen(1,n); } void print_board(int n) { int i,j; printf("\n-------------Solution %d---------------\n ",++count); for(i=1;i < =n;i++) { printf(" %d",i); } printf("\n-------------------------------------------------------"); for(i=1;i < =n;i++) { printf("\n%2d | ",i); for(j=1;j < =n;j++) { if(board[i]==j) printf(" Q"); else printf(" -"); } } printf("\n------------------

Breath First Search (BFS) in C Programming

#include < stdio.h> #include < conio.h> #define MAX 20 #define TRUE 1 #define FALSE 0 int g[MAX][MAX]; int v[MAX]; int Q[MAX]; int n; int front,rear; void create(); void BFS(int); void main() { int i,j; char ans; clrscr(); printf("\n----------------------------------------------------"); printf("\n BREADTH FIRST SEARCH"); printf("\n----------------------------------------------------"); create(); getch(); printf("\n Adjacency Matrix for the graph is:"); printf("\n----------------------------------------------------"); for(i=0;i < n;i++) { printf("\n"); for(j=0;j < n;j++) printf("%4d",g[i][j]); } getch(); do { for(i=0;i < n;i++) v[i]=FALSE; printf("\n Enter Vertex from which you Want to traverse:"); scanf("%d",&i); if(i>=MAX) printf("\n Invalid Vertex"); else { printf("\n The Depth First S

Depth First Search (DFS) in C Programming

#include < stdio.h> #include < conio.h> #define MAX 20 #define TRUE 1 #define FALSE 0 int g[MAX][MAX]; int v[MAX]; int n; void create(); void DFS(); void main() { int i,j; char ans; clrscr(); printf("\n----------------------------------------------------"); printf("\n DEPTH FIRST SEARCH"); printf("\n----------------------------------------------------"); create(); getch(); printf("\n Adjacency Matrix for the graph is:"); printf("\n----------------------------------------------------"); for(i=0;i < n;i++) { printf("\n"); for(j=0;j < n;j++) printf("%4d",g[i][j]); } getch(); do { for(i=0;i < n;i++) v[i]=FALSE; printf("\n Enter Vertex from which you Want to traverse:"); scanf("%d",&i); if(i>=MAX) printf("\n Invalid Vertex"); else { printf("\n The Depth First Search of the Graph is:");

Greatest Common Divisor.in C Programming

#include #include int GCD(int,int); void main() { int n1,n2; clrscr(); printf("\n Enter two numbers for GCD:"); scanf("%d %d",&n1,&n2); printf("\n Greatest common divisor of %d and %d is:%d",n1,n2,GCD(n1,n2)); getch(); } int GCD(int m,int n) { if(n==0) return m; else { if(n>m) { n=n+m; m=n-m; n=n-m; } return GCD(n,m%n); } } OUTPUT Enter two numbers for GCD:5 15 Greatest common divisor of 5 and 15 is:5

Tower Of Hanoi in C Programming

#include #include #include void tower_of_hanoi(int,char,char,char); void main() { int disk,move; clrscr(); printf("\n Enter number of disks:"); scanf("%d",&disk); move=pow(2,disk)-1; printf("\n No. of moves required is:%d",move); printf("\n A-->Source peg\n B-->Destination peg \n C-->Auxiliary peg"); tower_of_hanoi(disk,'A','B','C'); getch(); } void tower_of_hanoi(int disk,char from,char to,char aux) { if(disk==1) { printf("\n move disk %d from %c to %c",disk,from,to); } else { tower_of_hanoi(disk-1,from,aux,to); printf("\n move disk %d from %c to %c",disk,from,to); tower_of_hanoi(disk-1,aux,to,from); } } OUTPUT Enter number of disks:4 No. of moves required is:15 A-->Source peg B-->Destination peg C-->Auxiliary peg move disk 1 from A to C move disk 2 from A to B move disk 1 from C to B move disk 3 from A to C move disk 1 from B to A

MVT (Multiprogramming Variable Task) in C Programming

#include< stdio.h> #include< conio.h> void main() { int i,os_m,nPage,total,pg[25]; clrscr(); printf("\nEnter total memory size:"); scanf("%d",&total); printf("\nEnter memory for OS:"); scanf("%d",&os_m); printf("\nEnter no. of pages:"); scanf("%d",&nPage); for(i=0;i< nPage;i++) { printf("Enter size of page[%d]:",i+1); scanf("%d",&pg[i]); } total=total-os_m; for(i=0;i< nPage;i++) { if(total>=pg[i]) { printf("\n Allocate page %d",i+1); total=total-pg[i]; } else printf("\n page %d is not allocated due to insufficient memory.",i+1); } printf("\n External Fragmentation is:%d",total); getch(); } OUTPUT Enter total memory size:1024 Enter memory for OS:256 Enter no. of pages:4 Enter size of page[1]:128 Enter size of page[2]:512 Enter size of page[3]:64 Enter size of page[4]:512 Allocate page 1 Al

Deadlock Prevention using Banker’s Algorithm in C Programming

#include< stdio.h> #include< conio.h> void main() { int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15]; int pno,rno,i,j,prc,count,t,total; count=0; clrscr(); printf("\n Enter number of process:"); scanf("%d",&pno); printf("\n Enter number of resources:"); scanf("%d",&rno); for(i=1;i< =pno;i++) { flag[i]=0; } printf("\n Enter total numbers of each resources:"); for(i=1;i<= rno;i++) scanf("%d",&tres[i]); printf("\n Enter Max resources for each process:"); for(i=1;i<= pno;i++) { printf("\n for process %d:",i); for(j=1;j<= rno;j++) scanf("%d",&max[i][j]); } printf("\n Enter allocated resources for each process:"); for(i=1;i<= pno;i++) { printf("\n for process %d:",i); for(j=1;j<= rno;j++) scanf("%d",&allocated[i][j]); } printf("\n avai

Optimal Page replacement algorithm in C Programming

#include< stdio.h> #include< conio.h> int fsize; int frm[15]; void display(); void main() { int pg[100],change[15],nPage,i,j,k,l,index,pf=0,temp,flag=0,flag1=0,found,max; clrscr(); printf("\n Enter frame size:"); scanf("%d",&fsize); printf("\n Enter number of pages:"); scanf("%d",&nPage); for(i=0;i< nPage;i++) { printf("\n Enter page[%d]:",i+1); scanf("%d",&pg[i]); } for(i=0;i< fsize;i++) frm[i]=-1; printf("\n page | \t Frame content "); printf("\n--------------------------------------"); for(j=0;j< nPage;j++) { flag=0; flag1=0; for(i=0;i< fsize;i++) { if(frm[i]==pg[j]) { flag=1; flag1=1; break; } } if(flag==0) { for(i=0;i< fsize;i++) { if(frm[i]==-1) { frm[i]=pg[j]; pf++; flag1=1; break; } } } if(flag1==0) { for(i=0;i< fsize;i++) change[i]=0; for(i=0;i

Least Recently Used (LRU) Page replacement algorithm in C Programming

#include< stdio.h> #include< conio.h> int fsize; int frm[15]; void display(); void main() { int pg[100],valid[15],nPage,i,j,k,l,index,pf=0,temp,flag=0,flag1=0; clrscr(); printf("\n Enter frame size:"); scanf("%d",&fsize); printf("\n Enter number of pages:"); scanf("%d",&nPage); for(i=0;i< nPage;i++) { printf("\n Enter page[%d]:",i+1); scanf("%d",&pg[i]); } for(i=0;i< fsize;i++) frm[i]=-1; printf("\n page | \t Frame content "); printf("\n--------------------------------------"); for(j=0;j< nPage;j++) { flag=0; flag1=0; for(i=0;i< fsize;i++) { if(frm[i]==pg[j]) { flag=1; flag1=1; break; } } if(flag==0) { for(i=0;i< fsize;i++) { if(frm[i]==-1) { frm[i]=pg[j]; pf++; flag1=1; break; } } } if(flag1==0) { for(i=0;i< fsize;i++) valid[i]=0; for

First Come First Serve (FCFS) Page replacement algorithm in C Programming

#include< stdio.h> #include< conio.h> int fsize; int frm[15]; void display(); void main() { int pg[100],nPage,i,j,pf=0,top=-1,temp,flag=0; clrscr(); printf("\n Enter frame size:"); scanf("%d",&fsize); printf("\n Enter number of pages:"); scanf("%d",&nPage); for(i=0;i< nPage;i++) { printf("\n Enter page[%d]:",i+1); scanf("%d",&pg[i]); } for(i=0;i< fsize;i++) frm[i]=-1; printf("\n page | \t Frame content "); printf("\n--------------------------------------"); for(j=0;j< nPage;j++) { flag=0; for(i=0;i< fsize;i++) { if(frm[i]==pg[j]) { flag=1; break; } } if(flag==0) { if(top==fsize-1) { top=-1; } pf++; top++; frm[top]=pg[j]; } printf("\n %d |",pg[j]); display(); } printf("\n--------------------------------------"); printf("\n total page fault:%d",pf); getch(); }

Round Robin (RR) process scheduling algorithm in C Programming

#include< stdio.h> #include< conio.h> void main() { int njob,pID[15],aTime[15],bTime[15],jTime[15],taTime[15],wTime[15],i,j,temp,sTime[15],aBtime[15]; int texe=0,qtime,aexe=0; float totalWait,totalTA; clrscr(); printf("\n Enter number of process:"); scanf("%d",&njob); printf("\n Enter quantum time:"); scanf("%d",&qtime); for(i=0;i< njob;i++) { printf("\n Enter Burst time for process[%d]:",i); scanf("%d",&bTime[i]); aBtime[i]=bTime[i]; } //count total exe time for(i=0;i< njob;i++) { texe=texe+bTime[i]; } printf("%d",texe); //Execute Round robin for given quantum time while(aexe< texe) { for(i=0;i< njob;i++) { if(bTime[i]>=qtime) { aexe=aexe+qtime; bTime[i]=bTime[i]-qtime; jTime[i]=aexe; } else { if(bTime[i]!=0) { aexe=aexe+bTime[i]; bTime[i]=0; jTime[i]=aexe; } } } } for