12 Essential C Programming Projects for Beginners and Intermediates
Program 1 calender
#include <stdio.H>
#include <stdlib.H>
#include <string.H>
struct Day
{
char *name;
int date;
char *activity;
};
struct Day create()
{
struct Day day;
day.Name = (char *)malloc(20 * sizeof(char));
day.Activity = (char *)malloc(100 * sizeof(char));
printf(“Enter the day name: “);
scanf(“%s”, day.Name);
printf(“Enter the date: “);
scanf(“%d”, &day.Date);
printf(“Enter the activity for the day: “);
scanf(” %[^\n]”, day.Activity);
return day;
}
void read(struct Day calendar[], int size)
{
for (int i = 0; i < size; i++)
{
calendar[i] = create();
}
}
void display(struct Day calendar[], int size)
{
printf(“\nWeekly Activity Details:\n”);
for (int i = 0; i < size; i++)
{
printf(“Day %d: %s\n”, i + 1, calendar[i].Name);
printf(“Date: %d\n”, calendar[i].Date);
printf(“Activity: %s\n”, calendar[i].Activity);
printf(“\n”);
}
}
int main()
{
int weekSize = 7;
struct Day calendar[weekSize];
read(calendar, weekSize);
display(calendar, weekSize);
for (int i = 0; i < weekSize; i++)
{
free(calendar[i].Name);
free(calendar[i].Activity);
}
return 0;
}
Sample Output
Enter the day name: Monday
Enter the date: 161023
Enter the activity for the day: Reading
Enter the day name: Tuesday
Enter the date: 171023
Enter the activity for the day: Coding
Enter the day name: Wednesday
Enter the date: 181023
Enter the activity for the day: Painting
Enter the day name: Thursday Enter the date: 191023
Program 2 operations on strings
#include<stdio.H>
#include<conio.H>
char str[100], pat[50], rep[50],
ans[100];int i, j, c, m, k, flag=0;
void stringmatch()
{
i = m = c = j = 0;
while(str[c] ! = ‘\0’)
{
if(str[m] = = pat[i])
{
i++; m++;
if(pat[i] = = ‘\0’)
{
flag = 1;
for(k = 0; rep[k] != ‘\0’; k++, j++)
ans[j] = rep[k];
i = 0;
c = m;
}
}
else
{
ans[j] = str[c];
j++; c++;
m = c; i = 0;
}
}
}
void main()
{
clrscr();
printf(“\nEnter a main string \n”); ]
gets(str);
printf(“\nEnter a pattern string \n”);
flushall();
gets(pat);
printf(“\nEnter a replace string \n”);
flushall();
gets(rep);
stringmatch();
if(flag = = 1)
printf(“\nThe resultant string is\n %s” , ans);
else
printf(“\nPattern string NOT found\n”);
getch();
}
RUN 1: Enter a main string
Test
Enter a pattern string
Te
Enter a replace string
Re
The resultant string is
Rest
Program 3 stack operations
#include<stdio.H>
#include<string.H>
#include<conio.H>
#define max 5
int st[max],top=-1;
void push(),disp(),palin();
int pop();
void main()
{
int ch,k,item;clrscr();
while(1)
{
printf(“MAIN MENU\n”);
printf(” 1:Push\n 2:Pop\n 3:Display\n 4:Palindrome\n 5:Exit\n”);
printf(“Enter your choice\n”);
scanf(“%d”,&ch);
switch(ch)
{
case
1:printf(“Enter an item to push\n”);
scanf(“%d”,&item);
push(item);
break;
case 2: k=pop();
if(k)
printf(“popped element is %d\n”,k);
break;
case 3:disp();
break;
case 4:palin();
break;
case 5:exit(0);
}
}
}
void push(int item)
{
if(top==max-1)
{
printf(“Stack overflow\n”);
return ;
}
st[++top]=item;
}
int pop()
{
if(top==-1)
{
printf(“Stack underflow\n”);return 0;
}
return(st[top–]);
}
void palin()
{
int i;
char p[100];top=-1;
printf(“Enter a number to check for palindrome\n”);
fflush(stdin);
gets(p);
for(i=0;i<strlen(p);i++)
push(p[i]-‘0’);
for(i=0;i<strlen(p)/2;i++)
if((p[i]-‘0’)!=pop())
{
printf(“the number is not palindrome\n”);
return;
}
printf(“the number is palindrome\n”);
}
void disp()
{
int i;
if(top==-1)
{
printf(“Stack Empty\n”);
return;
}
printf(“the stack contents are”);
for(i=top;i>=0;i–)
printf(“|%d|\n”,st[i]);
}
}
program 4 infix to postfix
#include<stdio.H>
#include<string.H>
#include<conio.H>
int F(char symbol)
{
switch(symbol)
{
case ‘+’ :
case ‘-‘: return 2;
case ‘*’:
case ‘/’: return 4;
case ‘^’:
case ‘$’: return 5;
case ‘(‘: return 0;
case ‘#’: return -1;
default: return 8;
}
int G(char symbol)
{
switch(symbol)
{
case ‘+’:
case ‘-‘: return 1;
case ‘*’:
case ‘/’: return 3;
case ‘^’:
case ‘$’: return 6;
case ‘(‘: return 9;
case ‘)’: return 0;
default: return 7;
}
}
void infix_postfix(char infix[], char postfix[])
{
int top, j, i;
char s[30], symbol;top = -1;
s[++top] = ‘#’;
j = 0;
for(i=0; i < strlen(infix); i++)
{
symbol = infix[i];
while(F(s[top]) > G(symbol))
{
postfix[j] = s[top–];j++;
}
if(F(s[top]) != G(symbol))
s[++top] = symbol;
else
top–;
}
while(s[top] != ‘#’)
{
postfix[j++] = s[top–];
}
postfix[j] = ‘\0’;
}
void main()
{
char infix[20], postfix[20];
clrscr();
printf(“\nEnter a valid infix expression\n”);
flushall();
gets(infix); i
nfix_postfix(infix,postfix);
printf(“\nThe infix expression is:\n”);
printf (“%s”,infix);
printf(“\nThe postfix expression is:\n”);
printf (“%s”,postfix);
getch();
}
SAMPLE OUTPUT:
Enter a valid infix expression
(a+(b-c)*d)
The infix expression is:
(a+(b-c)*d)
The postfix expression is:
abc-d*+
PROGRAM 5A: suffix expression
#include<stdio.H>
#include<conio.H>
#include<math.H>
#include<string.H>
double compute(char symbol, double op1, double op2)
{
switch(symbol)
{
case ‘+’: return op1 + op2;
case’ -‘: return op1 – op2;
case ‘*’: return op1 * op2;
case ‘/’: return op1 / op2;
case ‘$’:
case ‘^’: return pow(op1,op2);
default: return 0;
}
}
void main()
{
double s[20], res, op1, op2;
int top, i;
char postfix[20], symbol;
clrscr();
printf(“\nEnter the postfix expression:\n”);
flushall();
gets(postfix);
top=-1;
for(i=0; <strlen(postfix); i++)
{
symbol = postfix[i];
if(isdigit(symbol))
s[++top] = symbol – ‘0’;
else
{
op2 = s[top–];
op1 = s[top–];
res = compute(symbol, op1, op2);
s[++top] = res;
}}
res = s[top–];
printf(“\nThe result is : %f\n”, res);
getch();
}
SAMPLE OUTPUT:
RUN1:
Enter the postfix expression:23+
The result is: 5.000000
RUN2:
Enter the postfix expression:23+7*
The result is: 35.000000
PROGRAM 5B: tower of hanoi
#include<stdio.H>
#include<conio.H>
void tower(int n, int source, int temp, int destination)
{
if(n == 0)
return;
tower(n-1, source, destination, temp);
printf(“\nMove disc %d from %c to %c”, n, source, destination);
tower(n-1, temp, source, destination);
}
void main()
{
int n; clrscr();
printf(“\nEnter the number of discs: \n”);
scanf(“%d”, &n);
tower(n, ‘A’, ‘B’, ‘C’);
printf(“\n\nTotal Number of moves are: %d”, (int)pow(2,n)-1);
getch();
}
SAMPLE OUTPUT:
Enter the number of discs:3
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C
Total Number of moves are: 7
program 6 circular queue
#include<stdio.H>
#include<conio.H>
#define MAX 4
int ch, front = 0, rear = -1, count=0;
char q[MAX], item;
void insert(char item)
{
if(count == MAX)
printf(“\nQueue is Full”);
return;
else
{
rear = (rear + 1) % MAX;
q[rear]=item;
count++;
}
}
void del()
{
if(count == 0)
printf(“\nQueue is Empty”);
return;
else
{
if(front > rear && rear==MAX-1)
{
front=0; rear=-1;count=0;
}
else
{
item=q[front];
printf(“\nDeleted item is: %c”,item);
front = (front + 1) % MAX;
count–;
}
}
}
void display()
{
int i, f=front, r=rear;
if(count == 0)
printf(“\nQueue is Empty”);
else
{
printf(“\nContents of Queue is:\n”);
for(i=0; i<=count; i++)
{
printf(“%c\t”, q[f]);
f = (f + 1) % MAX;
}
}
}
void main()
{
clrscr();
do
{
printf(“\n1. Insert\n2. Delete\n3. Display\n4. Exit”);
printf(“\nEnter the choice: “);
scanf(“%d”, &ch);
flushall();
switch(ch)
{
case 1: printf(“\nEnter the character / item to be inserted: “);
scanf(“%c”, &item);
insert(item);
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
break;
}
}while(ch!=4);
getch();
}
program 7 SLL student data
#include<stdio.H>
#include<conio.H>
#include<string.H>
#define null 0 struct
student
{
char usn[15],name[20],branch[10];
int sem;
char phno[20];
struct student *link;
};
typedef struct student node;
node *start;
void main()
{
void create(),insert_end(),del_front(),disp();
int ch;
clrscr();
while(1)
{
printf(“Main Menu\n”);
printf(“1:Create\n2:Display\n3:Insert Endt\n4:Delete Front\n5:Exit\n”);
printf(“Enter your choice\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:create();
break;
case 2:disp();
break;
case 3:insert_end();
break;
case 4:del_front();
break;
case 5:exit(0);
}}
}
void create()
{
int i,n; node *p;
printf(“Enter the number of students\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
p=(node*)malloc(sizeof(node));
printf(“Enter the student USN , NAME ,BRANCH,SEM,PHNO\n”);
scanf(“%s%s%s%d%s”,
p->usn,p->name,p->branch,&p->sem,p->phno);
p->link=start;
start=p;
}
}
void disp()
{
int cnt=0;
node *t;
t=start;
while(t)
{
cnt++;
printf(“%s\t%s\t%s\t%d\t%s->\n”,t->usn,t->name,t->branch,t->sem,t->phno);
t=t->link;
}
printf(“Total number of nodes=%d\n”,cnt);
}
void insert_end()
{
node *p,*r; p=(node*)malloc(sizeof(node));
printf(“Enter the student USN , NAME ,BRANCH,SEM,PHNO\n”);
scanf(“%s%s%s%d%s”,p->usn,p->name,p->branch,&p->sem,p->phno);
r=start;
while(r->link!=null)
r=r->link;
r->link=p;
p->link=null;
}
void del_front()
{
node *q;
if(start==null)
{
printf(“List empty\n”);
return;
}
q=start;
printf(“Deleted node is %s”,q->usn);
start=start->link;
free(q);
}
Program Code 8 DLL employee data
#include<stdio.H>
#include<stdlib.H>
#define null 0
struct emp
{
char name[40],dept[40],desig[40];
int ssn;
long int sal;
char phno[20];
struct emp *llink;
struct emp *rlink;
};
typedef struct emp node;
node *start;
void create(),insert_front(),del_front(),disp();
void main()
{
int ch;
clrscr();
while(1)
{
printf(“\nMain Menu\n”);
printf(“1:Create\n2:Display\n3:Insert_Front\n4:Del_Front\n5:Exit\n”);
printf(“Enter your choice\n”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:create();
break;
case 2:disp();
break;
case 3:insert_front();
break;
case 4:del_front();
break;
case 5:exit(0);
}
}
}
void create()
{
node *p,*t;
int i,n;
printf(“Enter the number of employees \n”);
scanf(“%d”,&n);
printf(“Enter the employee details[SSN,NAME,DEPT,DESIG,SAL AND PH.NO.]\n”);
for(i=0;i<n;i++)
{
printf(“enter the Employee %d details\n”,i+1);
p=(node*)malloc(sizeof(node));
p->rlink=null;
scanf(“%d%s%s%s%ld%s”,&p->ssn,p->name,p->dept,p->desig,&p->sal,p->phno);
if(start==null)
{
start=p;
start->llink=null;
}
else
{
t=start;
while(t->rlink!=null)
t=t->rlink;
t->rlink=p;
p->llink=t;
}
}
}
}
void disp()
{
node *r;
r=start;
while(r)
{
printf(“|%d|%s|%s|%s|%ld|%s|\n<->”,r->ssn,r->name,r->dept,r->desig,r->sal,r->phno);
r=r->rlink;
}
}
void insert_front()
{
node *p; p=(node*)malloc(sizeof(node));
printf(“Enter emp details\n”);
scanf(“%d%s%s%s%ld%%s”,&p->ssn,p->name,p->dept,p->desig,&p->sal,p->phno);
p->rlink=start;
start=p;
start->llink=null;
}
void del_front()
{
node *q;
if(start==null)
{
printf(“list empty\n”);
return;
}
q=start;
printf(“Deleted node is %d”,q->ssn);
start=start->rlink;
free(q);
}
PROGRAM CODE:10 binary search tree
#include <stdio.H>
#include <stdlib.H>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST NODE;
NODE *node;
NODE* createtree(NODE *node, int data)
{
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
if (data < (node->data))
{
node->left = createtree(node->left, data);
}
else if (data > node->data)
{
node -> right = createtree(node->right, data);
}
return node;
}
NODE* search(NODE *node, int data)
{
if(node == NULL)
printf(“
\nElement not found”);
else if(data < node
->data)
{
node
->left=search(node
->left, data);
}
else if(data > node
->data)
{
node
->right=search(node
->right, data);
}
else
printf(“
\nElement found is: %d”, node
->data);
return node;
}void inorder(NODE *node) {
if(node != NULL) {
inorder(node
->left);
printf(“%d
\t”, node
->data);
inorder(node
->right);
}
}
void preorder(NODE *node) {
if(node != NULL) {
printf(“%d
\t”, node
->data);
preorder(node
->left);
preorder(node
->right);
}
}
void postorder(NODE *node) {
if(node != NULL) {
postorder(node
->left);
postorder(node
->right);
printf(“%d
\t”, node
->data);
}
}
void main() {
int data, ch, i, n;
NODE *root=NULL;
clrscr();
while (1
)
{
printf(“
\n1.Insertion in Binary Search Tree”);
printf(“\n2.Inorder\n3.Preorder\n4.Postorder\n5.Exit”);
printf(“\nEnter your choice: “);
scanf(“%d”, &ch);
switch (ch)
{
case 1: printf(“\nEnter N value: ” );
scanf(“%d”, &n);
printf(“\nEnter the values to create BST like(6,9,5,2,8,15,24,14,7,8,5,2)\n”);
for(i=0; i<n; i++)
{
scanf(“%d”, &data);
root=createtree(root, data);
}
break;
case 2: printf(“\nInorder Traversal: \n”);
inorder(root);
break;
case 3: printf(“\nPreorder Traversal: \n”);
preorder(root);
break;
case 4: printf(“\nPostorder Traversal: \n”);
postorder(root);
break;
case 5: exit(0);
default:printf(“\nWrong option”);
break;
}
}
}
PROGRAM 11 operations on graphs
#include<stdio.H>
#include<conio.H>
int a[10][10], n, m, i, j, source, s[10], b[10];
int visited[10];
void create()
{
printf(“\nEnter the number of vertices of the digraph: “);
scanf(“%d”, &n);
printf(“\nEnter the adjacency matrix of the graph:\n”);
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf(“%d”, &a[i][j]);
}
void bfs()
{
int q[10], u, front=0, rear=-1;
printf(“\nEnter the source vertex to find other nodes reachable or not: “);
scanf(“%d”, &source);
q[++rear] = source;
visited[source] = 1;
printf(“\nThe reachable vertices are: “);
while(front<=rear)
{
u = q[front++];
for(i=1; i<=n; i++)
{
if(a[u][i] == 1 && visited[i] == 0)
{
q[++rear] = i;
visited[i] = 1;
printf(“\n%d”, i);
}
}}}
void dfs(int source)
{
int v, top = -1;
s[++top] = 1;
b[source] = 1;
for(v=1; v<=n; v++)
{ if(a[source][v] == 1 && b[v] == 0)
{
printf(“\n%d -> %d”, source, v);
dfs(v);
}}}
void main()
{
int ch;
clrscr();
while(1)
{
printf(“\n1.Create Graph\n2.BFS\n3.Check graph connected or not(DFS)\n4.Exit”);printf(“\nEnter your
choice: “);
scanf(“%d”, &ch);
switch(ch)
{
case 1: create();
break;
case 2: bfs();
for(i=1;i<=n;i++)
if(visited[i]==0)
printf(“\the vertex that is not reachable %d” ,i);
break;
case 3: printf(“\nEnter the source vertex to find the connectivity: “);
scanf(“%d”, &source);
m=1;
dfs(source);
for(i=1;i<=n;i++)
{
if(b[i]==0)
m=0;
}
if(m==1)
printf(“\n Graph is Connected”);
else
printf(“\n Graph is not Connected”);
break;
default: exit(0);
}}}
PROGRAM CODE:12 hash function hashing technique
#include <stdio.H>
#include <stdlib.H>
#define MAX 10
struct employee
{
int id;
char name[15];
};
typedef struct employee EMP;
EMP emp[MAX];
int a[MAX];
int create(int num)
{
int key;
key = num % 100;
return key;
}
int getemp(EMP emp[],int key)
{
printf(“\nEnter emp id: “);
scanf(“%d”,&emp[key].Id);
printf(“\nEnter emp name: “);
flushall();
gets(emp[key].Name);
return key;
}
void display()
{
int i, ch;
printf(“\n1.Display ALL\n2.Filtered Display”);
printf(“\nEnter the choice: “);
scanf(“%d”,&ch);
if(ch == 1)
{
printf(“\nThe hash table is:\n”);
printf(“\nHTKey\tEmpID\tEmpName”);
for(i=0; i<MAX; i++)
printf(“\n%d\t%d\t%s”, i, emp[i].Id, emp[i].Name);
}
else
{
printf(“\nThe hash table is:\n”);
printf(“\nHTKey\tEmpID\tEmpName”);
for(i=0; i<MAX; i++)
if(a[i] != -1)
{
printf(“\n%d\t%d\t%s”, i, emp[i].Id, emp[i].Name);
continue;
}
}
}
void linear_prob(int key, int num)
{
int flag, i, count = 0;flag = 0;
if(a[key] == -1)
{
a[key]=getemp(emp, key);
}
else
{
printf(“\nCollision Detected…!!!\n”);
i = 0;
while(i < MAX)
{
if (a[i] != -1)
count++;
else
i++;
printf(“\nCollision avoided successfully using LINEAR PROBING\n”);
if(count == MAX)
{
printf(“\n Hash table is full”);
display(emp);
exit(1);
}
for(i=key; i<MAX; i++)
if(a[i] == -1)
{
a[i] = num;
flag = 1; break; }
i = 0;