Binary Search using Recursion

#include<stdio.h> void bins(int lb,int ub,int item,int a[]); void main() { int i,n,item,a[20]; printf(“Enter the size of an array: “); scanf(“%d”,&n); printf(“Enter the elements of the array: ” ); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } printf(“Enter the number to be search: “); scanf(“%d”,&item); bins(0,n-1,item,a); } void bins(int lb,int ub,int item,int a[]) { int mid,flag=0; if(lb<=ub) { mid=(lb+ub)/2; if(item==a[mid]) […]

Read More

Solve All-Pairs Shortest Path Problem using Floyd – Warshall Algorithm

#include<stdio.h> #include<conio.h> #define inf 999 void main() { int i,j,k,n,w[20][20]; printf(“\n Enter the no. of vertices : “); scanf(“%d”,&n); printf(“\n Enter the weights : “); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { printf(“w[%d][%d]= “,i,j); scanf(“%d”,&w[i][j]); if(i!=j && (w[i][j]==0)) w[i][j]=inf; } for(i=1;i<=n;i++) { printf(“\n”); for(j=1;j<=n;j++) printf(“%d\t”,w[i][j]); } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(w[i][k]+w[k][j]<w[i][j]) w[i][j]=w[i][k]+w[k][j]; } } } printf(“\n The […]

Read More

Find the Factorial of a number

Summary:¬†Factorial is represented using ‘!’, so five factorial will be written as (5!),n factorial as (n!). n! = n*(n-1)*(n-2)*(n-3)…3.2.1 and zero factorial is defined as one i.e. 0! = 1. Using While Loop #include<stdio.h> void main() { int a,f,i; printf(“Enter a number: “); scanf(“%d”,&a); f=1; i=1; while(i<=a) { f = f * i; i++; } […]

Read More

Find the GCD of two numbers

Using For Loop #include<stdio.h> int main(){ int x,y,m,i; do { printf(“Enter two number: “); scanf(“%d%d”,&x,&y); if(x==0 || y==0) printf(“Please check the input and try again…\n”); } while(x==0 || y==0); if(x>y) m=y; else m=x; for(i=m;i>=1;i–){ if(x%i==0&&y%i==0){ printf(“\nGCD of %d and %d is %d”,x,y,i) ; break; } } return 0; } Using Recursion #include <stdio.h> int gcd(int […]

Read More

TCP Echo Server – Echo Client Program using C

tcpechoServer.c #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <strings.h> #include <string.h> #define MAX_MSG 100 #define SERVER_ADDR “127.0.0.1” #define SERVER_PORT 3786 main ( ) { int sd, newSd, cliLen, n; struct sockaddr_in cliAddr, servAddr; char line[MAX_MSG], line_r[MAX_MSG]; /**********************************/ /* build server address structure */ /**********************************/ bzero((char *)&servAddr, sizeof(servAddr)); […]

Read More

UDP Client-Server Program using C

udpClient.c #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/time.h> #define MAX_MSG 100 #define SERVER_ADDR “127.0.0.1” #define SERVER_PORT 9227 main( ) { int sd, rc, tempLen, n; struct sockaddr_in cliAddr, remoteServAddr, tempAddr; char msg[MAX_MSG]; /**********************************/ /* build server address structure */ /**********************************/ bzero((char *)&remoteServAddr, sizeof(remoteServAddr)); remoteServAddr.sin_family […]

Read More

UDP Client-Server Program (Command Line) using C

udpClient.c #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include<stdlib.h> #include <unistd.h> #include <string.h> #include <strings.h> #include <sys/time.h> #define MAX_MSG 100 main(int argc,char *argv[] ) { int i, sd, rc, tempLen, n, portno; struct sockaddr_in remoteServAddr, tempAddr; struct sockaddr_in cliAddr; char server[20]; char msg[MAX_MSG]; if(argc<3) { fprintf(stderr,”Usage : %s hostname port\n […]

Read More

Concatenation of two strings sent from Client on the Server – [ TCP ] using C

tcpClient.c #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <strings.h> #define MAX_MSG 100 #define SERVER_ADDR “127.0.0.1” #define CLIENT_ADDR “127.0.0.1” #define SERVER_PORT 3786 #define CLIENT_PORT 8229 main () { int sd, rc, i,n; struct sockaddr_in clientAddr, servAddr; char line[MAX_MSG]; /**********************************/ /* build server address structure */ /**********************************/ […]

Read More