Skip to main content

Posts

Showing posts with the label 5th semester

Prim’s Algorithm For Minimum Spanning Tree In C Programming

#include #include #define size 20 #define infi 9999 void prim(int L[][size],int node) { int T[size],i,j,k; int min_dist,v1,v2,total=0; for(i=1;i OUTPUT ---------------------------------------------------- PRIM`s MINIMUM SPANNING TREE ALGORITHM ---------------------------------------------------- Enter Number of nodes in the Graph:7 Enter Number of Edges in the Graph:12 --------Enter edges and length---------- Enter edge in form {v1,v2}:1 2 Enter Length from 1 to 2:1 Enter edge in form {v1,v2}:1 4 Enter Length from 1 to 4:4 Enter edge in form {v1,v2}:2 3 Enter Length from 2 to 3:2 Enter edge in form {v1,v2}:2 4 Enter Length from 2 to 4:6 Enter edge in form {v1,v2}:2 5 Enter Length from 2 to 5:4 Enter edge in form {v1,v2}:3 6 Enter Length from 3 to 6:6 Enter edge in form {v1,v2}:3 5 Enter Length from 3 to 5:5 Enter edge in form {v1,v2}:4 5 Enter Length from 4 to 5:3 Enter edge in form {v1,v2}:4 7 Enter Length from 4 to 7:4 Enter edge in form {v1,v2}:...

Bubble Sort in C Programming

#include< stdio.h> #include<conio.h> void display(int*,int); void bubble_sort(int*,int); void main() { int i,*a,n; clrscr(); printf("\n Enter number of element you want:"); scanf("%d",&n); for(i=0;i < n;i++) { printf("\n Enter element[%d]:",i); scanf("%d",&a[i]); } printf("\n------------------------------------------"); printf("\n Elements before sorting:"); display(a,n); printf("\n------------------------------------------"); bubble_sort(a,n); printf("\n------------------------------------------"); printf("\n Elements after sorting:"); display(a,n); printf("\n------------------------------------------"); getch(); } void bubble_sort(int *a,int n) { int pass,i,temp; for(pass=0;pass<= n-1;pass++) { for(i=0;i< n-pass-1;i++) { if(a[i]>=a[i+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } printf("\...

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 OUTPUT javac Matrix.java java Matrix Enter row and column for matrix1: 3 3 Enter the elements of first matrix 1 2 3 4 5 6 7 8 9 Enter row and column for matrix2: 3 3 Enter the elements of second matrix 9 8 7 6 5 4 3 2 1 multiplication of m1*m2 matrices -------------------------------- 30 24 18 84 69 54 138 114 90

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 a[i]) min=a[i]; if(max OUTPUT javac MinMax.java java MinMax Enter number of element you want in array. 10 Enter elementa[0]: -5 Enter elementa[1]: 11 Enter elementa[2]: 88 Enter elementa[3]: 77 Enter elementa[4]: 66 Enter elementa[5]: 55 Enter elementa[6]: 44 Enter elementa[7]: 33 Enter elementa[8]: 22 Enter elementa[9]: 11 Content of Array: -5 11 88 77 66 55 44 33 22 11 Largest element from array is:88 Smallest element from array is:-5

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 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 OUTPUT javac Quadratic.java java Quadratic Enter the value of a: 2 Enter the value of b: 5 Enter the value of c: 3 The Real roots are unequal -1.0 and -1.5 java Quadratic Enter the value of a: 0 Enter the value of b: 1 Enter...

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

Chain Matrix Multiplication in C Programming

#include #include 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 OUTPUT Enter number of element for p Matrix:6 Enter dimension for P matrix:30 3 15 5 10 20 8 --------------------M matrix-------------------- 0 1350 675 1275 2775 2175 -1 0 225 375 975 1455 -1 -1 0 750 2500 2400 -1 -1 -1 0 1000 1800 -1 -1 -1 -1 0 1600 -1 -1 -1 -1 -1 0 --------------------S matrix-------------------- -1 1 1 1 1 1 -1 -1 2 3 4 5 -1 -1 -1 3 3 3 -1 -1 -1 -1 4 5 -1 -1 -1 -1 -1 5 ...

Knapsack using Greedy Algorithm in C Programming

# include # include 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 u) break; else { x[i]=1.0; tp= tp+value[i]; u=u-weight[i]; } } if(i OUTPUT ------------------------------------------------------------------------------------ KNAPSACK PROBLEM USING GREEDY APPROACH ------------------------------------------------------------------------------------ Enter number of Objects you want:5 ----------------Enter the weights and values of each object-------------- Enter weight and value for object1:10 20 Enter weight and value for object2:20 30 Enter weight and value for object3:30 66 Enter weight and value for object4:40 40 Enter weight and value for object5:50 60 Enter the capacity of knapsack:100 ------------------------------------------------------------------------------------ weight | 30.00 10.00 20.00 50.00 40.00 value | 66.00 20.00 30.00 60.00 40.00 x ...

Kruskal’s Algorithm For Minimum Spanning Tree in C Programming

#include #include #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 OUTPUT ---------------------------------------------------- KRUSHKAL`s MINIMUM SPANNING TREE ALGORITHM ----...

make a change using Greedy Algorithm in C Programming

#include #include 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 -1;i--) { if((s+C[i]) OUTPUT ------------------------------------------------------------------------------- MAKING CHANGE USING GREEDY ALGORITHM ------------------------------------------------------------------------------- Enter amount you want:196 ---------------------------AVAILABLE COINS------------------------- 1 5 10 25 100 ------------------------------------...

Knapsack Using Backtracking in C Programming

#include #include #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 final_profit)&&(k==n)) { final_profit=new_cp; final_wt=new_cw; for(j=1;j =final_profit) { temp[k]=0; if(k final_profit)&&(k==n)) { final_profit=cp; final_wt=cw; for(j=1;j OUTPUT ------------------------------------------------------- KNAPSACK PROBLEM USING BACKTRACKING -------------------------------------...

N-Queen Problem C Programming

#include #include #include 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 OUTPUT ------------------------------------------------------- n-Queen problem using Backtracking ------------------------------------------------------- Enter number of Queen:4 -------------Solution 1--------------- 1 2 3 4 ------------------------------------------------------- 1 | - Q - - 2 | - - - Q 3 | Q - - - 4 | - - Q - --...

Breath First Search (BFS) in C Programming

#include #include #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 =MAX) printf("\n Invalid Vertex"); else { printf("\n The Depth First Search of the Graph is:"); printf("\n----------------------------------------------------\n"); BFS(i); printf("\n----------------------------------------------------\n"); } printf("\n Dou want to Traverse By other node?"); ans=getche(); }while(ans=='y' ||...