C Program to search the linked list.

Data structures using C,
Write c program to search the linked list.
Linked list is a data structure in which the objects are arranged in a linear order. In this program, we sort the list elements in ascending order. Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct l_list
{
int info;
struct link *next;
}
start, *node;

int search(int);
void main()
{
int no,i,item,pos;
clrscr();
start.next=NULL;
node=&start;
printf("How many nodes, you want in linked list? ");
scanf("%d",&no);
printf("");
for(i=0;i<no;i++)
{
node->next=(struct l_list *)malloc(sizeof(struct l_list));
printf("Enter element in node %d: ",i+1);
scanf("%d",&node->info);
node=node->next;
}
node->next=NULL;
printf("Linked list(only with info field) is:");

node=&start;
while(node->next!=NULL)
{
printf("%d ",node->info);
node=node->next;
}
printf("

Enter item to be searched : ");
scanf("%d",&item);
pos=search(item);
if(pos<=no)
printf("Your item is at node %d",pos);
else
printf("Sorry! item is no in linked list.a");
getch();
}

int search(int item)
{
int n=1;
node=&start;
while(node->next!=NULL)
{
if(node->info==item)
break;
else
n++;
node=node->next;
}
return n;
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to find day of birth from DOB

Problem Statement

Write a C Program to find the day of birth when date of birth is given. For example, if we give 02/04/2107  (02nd April 2017) as input, the program should output that it was a Sunday.

Solution

  1. Pick a year as base year where January 1st falls on a Monday. We are using 1900 as base year in our program.
  2. Then we find the number of years after base year. Day of the week in the beginning of the year shifts by 1 in every non leap year. 365 days -> 52 * 7 + 1. 
  3. Once we know on what day a given year started, we can calculate each month’s starting day.
  4. From there it’s as simple as counting the days and finding which day it is.

In this program we find the day of birth like Monday, Sunday.. when date of birth is given. For a more detailed explanation of the method can be found here Converting a Month-Date-Year to the Day of the Week

The Program

#include<stdio.h>
#include<stdlib.h>
int main() {
int d, m, y, year, month, day, i;
printf("Enter date of birth (DD MM YYYY) :");
scanf("%d %d %d", &d, &m, &y);
if( (d > 31) || (m > 12) || (y < 1900 || y >= 2100) )
{
printf("INVALID INPUT. Please enter a valid date between 1900 and 2100");
exit(0);
}
year = y-1900;
year = year/4;
year = year+y-1900;
switch(m)
{
case 1:
case 10:
month = 1;
break;
case 2:
case 3:
case 11:
month = 4;
break;
case 7:
case 4:
month = 0;
break;
case 5:
month = 2;
break;
case 6:
month = 5;
break;
case 8:
month = 3;
break;
case 9:
case 12:
month = 6;
break;
}
year = year + month;
year = year + d;
/* Need to make sure extra day is not needed in leap year for dates before March */
if(( y > 1900 ) && ( y % 4 == 0 ) && ( m < 2 ) )
year--;
day = year % 7;
switch(day)
{
case 0:
printf("Day is SATURDAY\n");
break;
case 1:
printf("Day is SUNDAY\n");
break;
case 2:
printf("Day is MONDAY\n");
break;
case 3:
printf("Day is TUESDAY\n");
break;
case 4:
printf("Day is WEDNESDAY\n");
break;
case 5:
printf("Day is THURSDAY\n");
break;
case 6:
printf("Day is FRIDAY\n");
break;
}
return 0;
}
view raw day_of_birth.c hosted with ❤ by GitHub

Sample Output

Date of birth to day of birth
Finding day of birth from DOB

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this List of C Programs

(c) www.c-program-example.com

C program to print all the possible permutations of given digits

Write a C program to print all the possible permutations of given digits.
Permutations means possible way of rearranging in the group or set in the particular order.
Example:
Input:1, 2, 3
Output:1 2 3, 1 3 2, 2 1 3, 3 1 2, 2 3 1, 3 2 1
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int lev=-1,n,val[50],a[50];
void main()
{
int i,j;
clrscr();
printf("Enter how many numbers?n");
scanf("%d",&n);
printf("nEnter %d numbers:nn",n);
for(i=0;i<n;i++)
{
val[i]=0;
j=i+1;
scanf("%dnn",&a[j]);
}
visit(0);
getch();
}
visit(int k)
{
int i;
val[k]=++lev;
if(lev==n)
{
for(i=0;i<n;i++)
printf("%2d",a[val[i]]);
printf(" ");
}
for(i=0;i<n;i++)
if(val[i]==0)
visit(i);
lev--;
val[k]=0;

}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to toss a coin using random function.

Write a c program to toss a coin using random function.
In this Program,We use the rand()%2 function that will compute random integers 0 or 1.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
int toss = 0;
int call = 0;
srand(time(NULL));

toss = rand() % 2;

printf("Say head or tail! press 0 for head and 1 for tail:nn");
scanf("%d", &call);
if(call==0 || call==1)
{
if(toss == call)
{
if(toss==1)
printf("You called it correctly ... it is tailn");
else
printf("You called it correctly ... it is headn");
}
else
{
if(toss==1)
printf("No way ...it is head !n");
else
printf("No way ... it is tail!n");
}
}
else
printf("Invalid call!!!!!nn");
return 0;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to demonstrate assert macro.

Write a C program to demonstrate assert macro.
assert macro defined in assert.h library. assert is mainly used for debugging. assert function checks the conditions at run time, and program executes only if the assert is true otherwise program aborts and shows the error message.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <assert.h>
#include<conio.h>
int main()
{
int num1, num2;
clrscr();
printf("Enter two numbers:nn");
scanf("%d %d", &num1, &num2);
assert(num2 != 0);
printf("%d/%d = %.2fn", num1, num2, num1/(float)num2);
return 0;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to implement SJF algorithm.

Write a C program to implement SJF algorithm.
Shortest Job First(SJF) is the CPU scheduling algorithm. In SJF, if the CPU is available it allocates the process which has smallest burst time, if the two process are same burst time it uses FCFS algorithm.Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,n,brust_time[10],start_time[10],end_time[10],wait_time[10],temp,tot;
float avg;
clrscr();
printf("Enter the No. of jobs:nn");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("n n Enter %d process burst time:n",i);
scanf("%d",&brust_time[i]);
}

for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(brust_time[i]>brust_time[j])
{
temp=brust_time[i];
brust_time[i]=brust_time[j];
brust_time[j]=temp;
}
}

if(i==1)
{
start_time[1]=0;
end_time[1]=brust_time[1];
wait_time[1]=0;
}

else
{
start_time[i]=end_time[i-1];
end_time[i]=start_time[i]+brust_time[i];
wait_time[i]=start_time[i];
}
}
printf("nn BURST TIME t STARTING TIME t END TIME t WAIT TIMEn");
printf("n ********************************************************n");
for(i=1;i<=n;i++)
{
printf("n %5d %15d %15d %15d",brust_time[i],start_time[i],end_time[i],wait_time[i]);
}
printf("n ********************************************************n");
for(i=1,tot=0;i<=n;i++)
tot+=wait_time[i];
avg=(float)tot/n;
printf("nnn AVERAGE WAITING TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=end_time[i];
avg=(float)tot/n;
printf("nn AVERAGE TURNAROUND TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=start_time[i];
avg=(float)tot/n;
printf("nn AVERAGE RESPONSE TIME=%fnn",avg);
getch();
}
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to implement FCFS algorithm.

Write a C Program to implement FCFS algorithm.
First Come First Served(FCFS) algorithm is the CPU scheduling algorithm. In FCFS process is served when they are arrived in order, i.e First In First Out(FIFO). FCFS is the simple scheduling algorithm, but it takes typically long/varying waiting time.
Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,sum=0;
int arrv[10],ser[10],start[10];
int finish[10],wait[10],turn[10];
float avgturn=0.0,avgwait=0.0;
start[0]=0;
clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the arriwal and service time of %d process:",i+1);
scanf("%d%d",&arrv[i],&ser[i]);
}
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<i;j++)
sum=sum+ser[j];
start[i]=sum;
}
for(i=0;i<n;i++)
{
finish[i]=ser[i]+start[i];
wait[i]=start[i];
turn[i]=ser[i]+wait[i];
}
for(i=0;i<n;i++)
{
avgwait+=wait[i];
avgturn+=turn[i];
}
avgwait/=n;
avgturn/=n;
printf("narraival service Start Finish Wait Turnn");
for(i=0;i<n;i++)
printf("%dt%dt%dt%dt%dt%dn",arrv[i],ser[i],start[i],
finish[i],wait[i],turn[i]);
printf("nAverage waiting time=%f",avgwait);
printf("nAverage turn around time=%f",avgturn);
getch();
}
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Chapter 4 Exercise Solutions.

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 4 of the book for easy reference.

Chapter 4: Functions and Program Structure
    1. Exercise 4-1. Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or -1 if there is none.
      Solution to Exercise 4-1.
    1. Exercise 4-2.Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.
      Solution to Exercise 4-2.
    1. Exercise 4-3.Given the basic framework, it’s straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.
      Solution to Exercise 4-3.
    1. Exercise 4-4. Add commands to print the top element of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack.
      Solution to Exercise 4-4.
    1. Exercise 4-5. Add access to library functions like sin , exp , and pow . See <math.h> in Appendix B, Section 4.
      Solution to Exercise 4-5.
    1. Exercise 4-6. Add commands for handling variables. (It’s easy to provide twenty-six variables with single-letter names.) Add a variable for the most recently printed value.
      Solution to Exercise 4-6.
    2. Exercise 4-7. Write a routine ungets(s) that will push back an entire string onto the input. Should ungets know about buf and bufp , or should it just use ungetch ?
      Solution to Exercise 4-7.
    1. Exercise 4-8.Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.
      Solution to Exercise 4-8.
    1. Exercise 4-9.Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement your design
      Solution to Exercise 4-9.
    1. Exercise 4-10. An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. Revise the calculator to use this approach.
      Solution to Exercise 4-10.
    1. Exercise 4-11. Modify getop so that it doesn’t need to use ungetch. Hint: use an internal static variable.
      Solution to Exercise 4-11.
    1. Exercise 4-12. Adapt the ideas of printd to write a recursive version of itoa ; that is, convert an integer into a string by calling a recursive routine.
      Solution to Exercise 4-12.
    1. Exercise 4-13. Write a recursive version of the function reverse(s) , which reverses the string s in place.
      Solution to Exercise 4-13.
  1. Exercise 4-14. Define a macro swap(t,x,y) that interchanges two arguments of type t . (Block structure will help.)
    Solution to Exercise 4-14.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

(c) www.c-program-example.com

C Program to solve equations using Jordan elimination method.

Write a C Program to solve equations using Jordan elimination method.
Gauss-Jordan elimination method is used to solve the linear equations. In this method, We find the inverse matrix of the coefficients of equations by elementary row operations. Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
void solution( int a[][], int var );
int main()
{

int a[ 20 ][ 20 ], var, i, j, k, l;
clrcsr();
printf( "nEnter the number of variables:n" );
scanf( "%d", &var );

for ( i = 0;i < var;i++ )
{
printf( "nEnter the equation%d:n", i + 1 );

for ( j = 0;j < var;j++ )
{
printf( "Enter the coefficient of x%d:n", j + 1 );
scanf( "%d", &a[ i ][ j ] );
}

printf( "nEnter the constant:n" );
scanf( "%d", &a[ i ][ n ] );
}

solution( a, var );
return 0;
}



void solution( int a[ 20 ][ 20 ], int var )
{
int k, i, l, j;

for ( k = 0;k < var;k++ )
{
for ( i = 0;i <= var;i++ )
{
l = a[ i ][ k ];

for ( j = 0;j <= var;j++ )
{
if ( i != k )
a[ i ][ j ] = a[ k ][ k ] * a[ i ][ j ] – l * a[ k ][ j ];
}
}
}

printf( "nSolutions:" );

for ( i = 0;i < n;i++ )
{
printf( "nTHE VALUE OF x%d IS %fn", i + 1, ( float ) a[ i ][ n ] / ( float ) a[ i ][ i ] );
}

}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to lock file using semaphores.

Write a C Program to lock file using semaphores.
Using semaphores, We can control access to files, shared memory and other things. The basic functionality of a semaphore is that you can either set it, check it, or wait until it clears then set it (“test-n-set”). In C semaphores functions defined in the sys/sem library. Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>
#include <sys/file.h>
#include <error.h>
#include <sys/sem.h>
#define MAXBUF 100
#define KEY 1216
#define SEQFILE "seq_file"
int semid,fd;
void my_lock(int);
void my_unlock(int);
union semnum
{
int val;
struct semid_ds *buf;
short *array;
}arg;
int main()
{
int child, i,n, pid, seqno;
char buff[MAXBUF+1];
pid=getpid();
if((semid=semget(KEY, 1, IPC_CREAT | 0666))= = -1)
{
perror("semget");
exit(1);
}
arg.val=1;
if(semctl(semid,0,SETVAL,arg)<0)
perror("semctl");
if((fd=open(SEQFILE,2))<0)
{
perror("open");
exit(1);
}
pid=getpid();
for(i=0;i<2;i++)
{
my_lock(fd);
lseek(fd,01,0);
if((n=read(fd,buff,MAXBUF))<0)
{
perror("read");
exit(1);
}
printf("pid:%d, Seq no:%dn", pid, seqno);
seqno++;
sprintf(buff,"%dn", seqno);
n=strlen(buff);
lseek(fd,01,0);
if(write(fd,buff,n)!=n)
{
perror("write");
exit(1);
}
sleep(1);
my_unlock(fd);
}
}
void my_lock(int fd)
{
struct sembuff sbuf=(0, -1, 0);
if(semop(semid, &sbuf, 1)= =0)
printf("Locking: Resource…n");
else
printf("Error in Lockn");
}
void my_unlock(int fd)
{
struct sembuff sbuf=(0, 1, 0);
if(semop(semid, &sbuf, 1)= =0)
printf("UnLocking: Resource…n");
else
printf("Error in Unlockn");
}
Read more c programs
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com