Skip to main content

Posts

A Palindrome program in c language

 c code to check if a string is a palindrome or not and for palindrome number. The first step we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise  not palindrome.Some palindrome strings examples are  “ dad “, “ radar “, “ madam ” etc. #include<stdio.h> #include<conio.h> #include<string.h> void main() { int i=0,j=0,k=0; char *str; char *s; clrscr(); printf("Program to find whether the given string and it's reverse are same\n\n"); printf("Enter the length of the string\n"); scanf("%d",&j); str=(char*)malloc((j+1)*sizeof(char)); s=(char*)malloc((j+1)*sizeof(char)); fflush(stdin); printf("Enter the string\n"); gets(str); for(i=0;i<j;i++) { *(s+i)=*(str+(j-1-i)); //printf("Character %d of Original strin...

Mutex variable using pthread in C programming

#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *functionC(); pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0; main() { int rc1, rc2; pthread_t thread1, thread2; /* Create independent threads each of which will execute functionC */ if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) ) { printf("Thread creation failed: %d\n", rc1); } if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) ) { printf("Thread creation failed: %d\n", rc2); } /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); pthread_join( thread2, NULL); exit(0); } void *functionC() { pthread_mutex_lock( &mutex1 ); counter++; pri...

Write a program of greeting using pthread in C programming

#include <stdio.h> #include <string.h> #include <sys/types.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> #define MAX_THREAD 1000 typedef struct { int id; int nproc; }parm; char message[100]; /* storage for message */ pthread_mutex_t msg_mutex = PTHREAD_MUTEX_INITIALIZER; int token = 0; void* greeting(void *arg) { parm *p = (parm *) arg; int id = p->id; int i; if (id != 0) { /* Create message */ while (1) { pthread_mutex_lock(&msg_mutex); if (token == 0) { sprintf(message, "Greetings from process %d!", id); token++; pthread_mutex_unlock(&msg_mutex); break; } pthread_mutex_unlock(&msg_mutex); sleep(1); } /* Use strlen+1 so that '\0' gets transmitted */ } else { /* my_rank == 0 */ for (i = 1; i nproc; i++) { while (1) { pthread_mutex_lock(&msg_mutex); if (token == 1) { printf("%s\n", message); ...

Write a program of Hello World using pthread in C programming

#include #include #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, thread #%ld!\n", tid); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0;t OUTPUT Compile & run(linux terminal): gcc -o Pthread_Hello_world pthread_hello_world.c -lpthread ./Pthread_Hello_world OUTPUT: In main: creating thread 0 In main: creating thread 1 Hello World! It's me, thread #0! In main: creating thread 2 Hello World! It's me, thread #1! In main: creating thread 3 Hello World! It's me, thread #2! In main: creating thread 4 Hello World! It's me, thread #3! Hello World! It's me, thread #4!

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}:...

Matrix Multiplication in C Programming

#include int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for (c=0;c OUTPUT Enter the number of rows and columns of first matrix 3 3 Enter the elements of first matrix 1 2 3 4 5 6 7 8 9 Enter the number of rows and columns of second matrix 3 3 Enter the elements of second matrix 9 8 7 6 5 4 3 2 1 Product of entered matrices:- 30 24 18 84 69 54 138 114 90

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("\...