PROGRAM :
/* Program to calculate whether a given number is palindrome or not */
#include<stdio.h>
#include<conio.h>
int main()
{
int temp,rev=0,num,remainder ;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
temp=num;
while(num!=0) //Reversing the number
{
remainder = num%10;
num = num/10;
rev = rev*10+ remainder;
}
printf("The reverse number is %d",rev);
if(rev == temp)
printf("\n%d is a palindrome",temp);
else
printf("\n%d is not a palindrome", temp);
getch();
}
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
int rollno;
float sub1, sub2, sub3, sub4, , sum, score;
printf("Enter name of student: ");
scanf(“%s”,&name[]);
printf ("\n Enter Roll Number: ");
scanf("%d", &rollno);
printf ("\n Enter Marks in 4 Subjects:\n");
scanf("%f%f%f%f", &sub1, &sub2, &sub3, &sub4);
sum=sub1+sub2+sub3+sub4;
score = (sum/500)*100;
printf("\n Name of student: %s", name[]);
printf("\n Roll Number: %d", rollno);
printf ("\nPercentage score secured: %2.2f%c", score,'%');
getch();
}
PROGRAM :
/* Program to calculate all possible roots of a quadratic equation */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a, b, c, disc;
float root1,root2,real,imag;
clrscr();
printf("Enter a,b,c values\n");
scanf("%f%f%f",&a,&b,&c);
if( (a == 0) && (b == 0) &&(c==0))
{
printf("Invalid coefficients\n");
printf(" Try Again with valid inputs !!!!\n");
getch();
}
disc = b*b - 4*a*c;
if(disc == 0)
{
printf("The roots are real and equal\n");
root1 = root2 = -b/(2*a);
printf("Root1 = %.3f \nRoot2 = %.3f", root1,root2);
}
else if(disc>0)
{
printf("The roots are Real and Distinct\n");
root1 = (-b+sqrt(disc)) / (2*a);
root2 = (-b-sqrt(disc)) / (2*a);
printf("Root1 = %.3f \nRoot2 = %.3f",root1,root2);
}
else
{
printf("The roots are Real and Imaginary\n");
real = -b / (2*a);
imag = sqrt(fabs(disc)) / (2*a);//fabs() returns only numberignoring sign
printf("Root1 = %.3f + i %.3f \n",real,imag);
printf("Root2 = %.3f - i %.3f",real,imag);
} getch(); }
PROGRAM :
/* Evaluating the polynomial f(x) = a4x4+ a3x3+ a2x2+ a1x+ a0 using Horner’s
method (n, i,
sum, a[], x) */
#include <stdio.h>
int main()
{
float a[100],sum=0,x;
int n,i;
printf("\nEnter degree of the polynomial X :: ");
scanf("%d",&n);
printf("\nEnter coefficient's of the polynomial X :: \n");
for(i=n;i>=0;i--)
{
printf("\nEnter Coefficient of [ X^%d ] :: ",i);
scanf("%f",&a[i]);
}
printf("\nEnter the value of X :: ");
scanf("%f",&x);
for(i=n;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("\nValue of the polynomial is = [ %f ]\n",sum);
return 0;
}
PROGRAM:
/* Program to calculate sine value of given angle */
#include<stdio.h>
#include<math.h>
#define PI 3.142
int main()
{
int i, degree;
float x, sum=0,term,nume,deno;
clrscr();
printf("Enter the value of degree");
scanf("%d",°ree);
x = degree * (PI/180); //converting degree into radian
nume = x;
deno = 1;
i=2;
do { //calculating the sine value.
term = nume/deno;
nume = -nume*x*x;
deno = deno*i*(i+1);
sum=sum+term;
i=i+2; }
while (fabs(term) >= 0.00001); // Accurate to 4 digits
printf("The sine of %d is %.3f\n", degree, sum);
printf("The sine function of %d is %.3f", degree, sin(x));
getch();
}
PROGRAM:
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
Program:
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Example 2: Pass Arrays to Functions
#include <stdio.h>
float calculateSum(float num[]);
int main()
{
float result, num[] = {23.4, 55.8, 22.6, 333.8, 40.5, 18};
// num array is passed to calculateSum()
result = calculateSum(num);
printf("Result = %.2f", result);
return 0;
}
float calculateSum(float num[])
{
int i;
float sum = 0.0;
for (i = 0; i < 6; ++i)
{
sum += num[i];
}
return sum;
}
Program: Write a program to find biggest among three numbers using pointer.
#include <stdio.h>
int main()
{
int a,b,c;
int*ptra=&a,*ptrb=&b,*ptrc=&c;
printf("enter three values");
scanf("%d%d%d",ptra,ptrb,ptrc);
printf("a=%d\n b=%d\n c=%d\n",*ptra,*ptrb,*ptrc);
if((*ptra>*ptrb && *ptra>*ptrc))
printf("biggest number=%d",*ptra);
else if((*ptrb>*ptra && *ptrb>*ptrc))
printf("biggest number =%d",*ptrb);
else
printf("biggest number=%d",*ptrc);
return 0;
}
PROGRAM:
#include <stdio.h>
/*structure declaration*/
struct employee
{
char name[30];
int empId;
float salary;
};
int main()
{
/*declare and initialization of structure variable*/
struct employee emp={"Manickam",001,80000.00};
printf("\n Name: %s" ,[Link]);
printf("\n Id: %d" ,[Link]);
printf("\n Salary: %f\n",[Link]);
return 0;
}
#include <stdio.h>
#include <conio.h>
struct student
{
int rollno, marks;
char name[20], grade;
};
void main() {
int i,n,found=0;
struct student s[10];
char sname[20];
clrscr();
printf("Enter the number of student details n=");
scanf("%d",&n);
for(i=0;i<n;i++) {
printf("\nenter the %d student details \n",i+1);
printf("enter the roll number:");
scanf("%d",&s[i].rollno);
printf("enter the student name without white spaces:");
scanf("%s", s[i].name); printf("enter the marks : ");
scanf("%d", &s[i].marks);
printf("enter the grade : ");
fflush(stdin); scanf("%c",&s[i].grade);
}
printf("\nStudent details are \n");
printf("\nRollno\tName\t\t\tMarks\tGrade\n");
for(i=0;i<n;i++)
printf("%d\t%s\t\t%d\t%c\n", s[i].rollno, s[i].name, s[i].marks, s[i].grade); printf("\
nEnter the
student name to print the marks:");
scanf("%s", sname);
for(i=0;i<n;i++) {
if(strcmp(s[i].name,sname)==0)
{
Printf(“\Marks of the student is : %d”,s[i].marks);
found=1;
} }
if(found ==0)
printf(“ Given student name not found\n”);
getch(); }
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
// use appropriate location if you are using MacOS or Linux
fptr = fopen("F:\\[Link]","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
Program
/* Program on merge two files */
#include<stdio.h>
int main()
{
FILE *fp1,*fp2,*fp3;
char usn[20], name[20];
fp1=fopen("[Link]", "r");
if(fp1 == NULL)
printf(" File not found");
fp2=fopen("[Link]", "r");
if(fp2 == NULL)
printf(" File not found");
fp3=fopen("[Link]","w");
while( !feof(fp1) && !feof(fp2) )
{
fscanf(fp1,"%s",name);
fscanf(fp2,"%s",usn);
fprintf(fp3,"%15s %10s\n", name, usn);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("[Link]","r");
printf("\n----------------------------\n");
printf(" Name USN \n");
printf("----------------------------\n");
while(!feof(fp3))
{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s \n",usn);
printf("%-15s %10s \n", name,usn);
}
fclose(fp3);
getch();
}
PROGRAM
#include <stdio.h>
int main()
{
// declare local variables
char opt;
int n1, n2;
float res;
printf (" Choose an operator(+, -, *, /) to perform the operation in C Calculator \n ");
scanf ("%c", &opt); // take an operator
if (opt == '/' )
{
printf (" You have selected: Division");
}
else if (opt == '*')
{
printf (" You have selected: Multiplication");
}
else if (opt == '-')
{
printf (" You have selected: Subtraction"); }
else if (opt == '+') {
printf (" You have selected: Addition");
}
printf (" \n Enter the first number: ");
scanf(" %d", &n1); // take fist number
printf (" Enter the second number: ");
scanf (" %d", &n2); // take second number
switch(opt)
{
case '+':
res = n1 + n2; // add two numbers
printf (" Addition of %d and %d is: %.2f", n1, n2, res);
break;
case '-':
res = n1 - n2; // subtract two numbers
printf (" Subtraction of %d and %d is: %.2f", n1, n2, res);
break;
ase '*':
res = n1 * n2; // multiply two numbers
printf (" Multiplication of %d and %d is: %.2f", n1, n2, res);
break;
case '/':
if (n2 == 0) // if n2 == 0, take another number
{
printf (" \n Divisor cannot be zero. Please enter another value ");
scanf ("%d", &n2); }
res = n1 / n2; // divide two numbers
printf (" Division of %d and %d is: %.2f", n1, n2, res);
break; default: /* use default to print default message if any
condition is not satisfied */
printf (" Something is wrong!! Please check the options ");
}
return 0;
}
Example 2: Calculator Program in C using do while loop and switch statement
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
// declaration of local variable op;
int op, n1, n2;
float res;
char ch;
do
{
// displays the multiple operations of the C Calculator
printf (" Select an operation to perform the calculation in C Calculator: ");
printf (" \n 1 Addition \t \t 2 Subtraction \n 3 Multiplication \t 4 Division \n 5
Square \t \t 6
Square Root \n 7 Exit \n \n Please, Make a choice ");
scanf ("%d", &op); // accepts a numeric input to choose the operation
// use switch statement to call an operation
switch (op)
{
case 1:
// Add two numbers
printf (" You chose: Addition");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
res = n1 + n2; // Add two numbers
printf (" Addition of two numbers is: %.2f", res);
break; // break the function
case 2:
// Subtract two numbers
printf (" You chose: Subtraction");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
res = n1 - n2; // subtract two numbers
printf (" Subtraction of two numbers is: %.2f", res);
break; // break the function
case 3:
// Multiplication of the numbers
printf (" You chose: Multiplication");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
res = n1 * n2; // multiply two numbers
printf (" Multiplication of two numbers is: %.2f", res);
break; // break the function
case 4:
// Division of the numbers
printf (" You chose: Division");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
printf (" Enter Second Number: ");
scanf (" %d", &n2);
if (n2 == 0)
{
printf (" \n Divisor cannot be zero. Please enter another value ");
scanf ("%d", &n2);
}
res = n1 / n2; // divide two numbers
printf (" Division of two numbers is: %.2f", res);
break; // break the function
case 5:
// getting square of a number
printf (" You chose: Square");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
res = n1 * n1; // get square of a number
printf (" Square of %d number is: %.2f", n1, res);
break; // break the function
case 6:
// getting the square root of the number
printf (" You chose: Square Root");
printf ("\n Enter First Number: ");
scanf (" %d", &n1);
res = sqrt(n1); // use sqrt() function to find the Square Root
printf (" Square Root of %d numbers is: %.2f", n1, res);
break; // break the function
case 7:
printf (" You chose: Exit");
exit(0);
break; // break the function
default:
printf(" Something is wrong!! ");
break;
}
printf (" \n \n ********************************************** \n ");
} while (op != 7);
return 0;
}
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include <math.h>
#include <stdlib.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
void main()
{
//clrscr();
int ch;
char g='y';
do
{
printf("\n main Menu");
printf("\n [Link] \n [Link] \n [Link] \n [Link] \n [Link]\n [Link] \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
} n--; }
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
} }
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}
else
{
printf("Value %d is not in the list::", e);
continue;
} } }
void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n) {
printf("\n invalid Location::");
}
Else {
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p; n++; }
printf("\n The list after insertion::\n");
display(); }
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++) {
printf("\n\n%d", b[i]); } }
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAXSTK 100
int top=-1;
int items[MAXSTK];
int Isempty();
int Isfull();
void Push(int);
int Pop();
void Display();
void main()
{
int x;
char ch='1';
clrscr();
while(ch!='4')
{
printf("\n 1-PUSH");
printf("\n 2-POP");
printf("\n 3-DISPLAY");
printf("\n 4-QUIT");
printf("\n Enter your choice:");
fflush(stdin);
ch=getchar();
switch(ch)
{
case '1':
printf("\n Enter the element to be pushed:");
scanf("%d",&x);
Push(x);
break;
case '2':
x=Pop();
printf("\n Pop element is %d\n:",x);
break;
case '3':
Display();
break;
case '4':
break;
default:
printf("\n Wrong choice!Try again:");
} }}
int Isempty()
{
if(top==-1)
return 1;
else return 0;
}
int Isfull()
{ if(top==MAXSTK-1)
return 1;
else
return 0;
}
void Push(int x)
{ if(Isfull())
{ printf("\n Stack full");
return;
} top++;
items[top]=x;
}
int Pop()
{ int x;
if(Isempty())
{ printf("\n Stack empty");
exit(0);
} x=items[top];
top--;
return x; }
void Display()
{ int i;
if(Isempty())
{ printf("\n Stack empty");
return;
} printf("\n Elements in the Stack are :\n");
for(i=top;i>=0;i--)
printf("%d\n",items[i]); }
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>#define MAXQ 100
int front=0,rear=-1;
int items[MAXQ];
int Isempty();
int Isfull();
void Insert(int);
int Delete();
void Display();
void main()
{
int x;
char ch='1';
clrscr();
while(ch!='4')
{
printf("\n 1-INSERT");
printf("\n 2-DELETE");
printf("\n 3-DISPLAY");
printf("\n 4-QUIT");
printf("\n Enter your choice:");
fflush(stdin);
ch=getchar();
switch(ch)
{
case '1':
printf("\n Enter the element to be inserted:");
scanf("%d",&x);
Insert(x);
break;
case '2':
x=Delete();
printf("\n Delete element is %d\n:",x);
break;
case '3':
Display();
break;
case '4':
break;
default:
printf("\n Wrong choice!Try again:");
}
}
getch();
}
int Isempty()
{
if(rear<front)
return 1;
else return 0;
}
int Isfull()
{
if(rear==MAXQ-1)
return 1;
else
return 0;
}
void Insert(int x)
{
if(Isfull())
{
printf("\n Queue full");
return;
}
rear++;
items[rear]=x;
}
int Delete()
{
int x;
if(Isempty())
{
printf("\n Queue is empty");
exit(0);
}
x=items[front];
front++;
return x;
}
void Display()
{
int i;
if(Isempty())
{
printf("\n Queue is empty");
return;
}
printf("\n Elements in the Queue are :\n");
for(i=front;i<=rear;i++)
printf("%d\n",items[i]);
}
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int count=0;
struct node
{
int data;
struct node *next;
}*head,*newn,*trav;
//----------------------------------------------------------
void create_list()
{
int value;
struct node *temp;
temp=head;
newn=(struct node *)malloc(sizeof (struct node));
printf("\nenter the value to be inserted");
scanf("%d",&value);
newn->data=value;
if(head==NULL)
{
head=newn;
head->next=NULL;
count++;
}
Else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn;
newn->next=NULL;
count++;
}
}
//----------------------------------------------------
void insert_at_begning(int value)
{
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(head==NULL)
{
head=newn;
head->next=NULL;
count++;
}
Else
{
newn->next=head;
head=newn;
count++;
}
}
//----------------------------------------------------------
void insert_at_end(int value)
{
struct node *temp;
temp=head;
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
if(head==NULL)
{
head=newn;
head->next=NULL;
count++; }
else
{
while(temp->next!=NULL)
{
temp=temp->next; }
temp->next=newn;
newn->next=NULL;
count++; }
}
//------------------------------------------------------
int insert_at_middle()
{
if(count>=2)
{
struct node *var1,*temp;
int loc,value;
printf("\n after which value you want to insert : ");
scanf("%d",&loc);
printf("\nenter the value to be inserted");
scanf("%d",&value);
newn=(struct node *)malloc(sizeof (struct node));
newn->data=value;
temp=head;
/* if(head==NULL)
{
head=newn;
head->next=NULL;
count++;
return 0;
}
Else
{*/
while(temp->data!=loc)
{
temp=temp->next;
if(temp==NULL)
{
printf("\nSORRY...there is no %d element",loc);
return 0;
}
}
//var1=temp->next;
newn->next=temp->next;//var1;
temp->next=newn;
count++;
//} }
Else {
printf("\nthe no of nodes must be >=2");
}}
//----------------------------------------------------------------
int delete_from_middle()
{
if(count==0)
printf("\n List is Empty!!!! you can't delete elements\n");
else if(count>2)
{
struct node *temp,*var;
int value;
temp=head;
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&value);
while(temp->data!=value)
{
var=temp;
temp=temp->next;
if(temp==NULL)
{
printf("\nSORRY...there is no %d element",value);
return 0;
}
}
if(temp==head)
{
head=temp->next;
}
Else
{
var->next=temp->next;
temp->next=NULL;
}
count--;
if(temp==NULL)
printf("Element is not avilable in the list \n**enter only middle elements..**");
else
printf("\ndata deleted from list is %d",value);
free(temp);
}
Else
{
printf("\nthere no middle elemts..only %d elemts is avilable\n",count);
}}
//------------------------------------------------------------------
int delete_from_front()
{
struct node *temp;
temp=head;
if(head==NULL)
{
printf("\nno elements for deletion in the list\n");
return 0;
}
Else
{
printf("\ndeleted element is :%d",head->data);
if(temp->next==NULL)
{
head=NULL;
}
else{
head=temp->next;
temp->next=NULL;
}
count--;
free(temp);
}}
//--------------------------------------------------------
int delete_from_end(){
struct node *temp,*var;
temp=head;
if(head==NULL)
{
printf("\nno elemts in the list");
return 0;
}
else{
if(temp->next==NULL )
{
head=NULL;//temp->next;
}
else{
while(temp->next != NULL)
{
var=temp;
temp=temp->next;
}
var->next=NULL;
}
printf("\ndata deleted from list is %d",temp->data);
free(temp);
count--;
}
return 0;}
//------------------------------------------------------
int display()
{
trav=head;
if(trav==NULL)
{
printf("\nList is Empty\n");
return 0;}
else
{
printf("\n\nElements in the Single Linked List is %d:\n",count);
while(trav!=NULL)
{
printf(" -> %d ",trav->data);
trav=trav->next;
}
printf("\n");
}
}
//---------------------------------------------------------------
int main()
{
int ch=0;
char ch1;
head=NULL;
while(1)
{
printf("\[Link] linked list");
printf("\[Link] at begning of linked list");
printf("\[Link] at the end of linked list");
printf("\[Link] at the middle where you want");
printf("\[Link] from the front of linked list");
printf("\[Link] from the end of linked list ");
printf("\[Link] of the middle data that you want");
printf("\[Link] the linked list");
printf("\[Link]\n");
printf("\nenter the choice of operation to perform on linked list");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
do{
create_list();
display();
printf("do you want to create list ,y / n");
getchar();
scanf("%c",&ch1);
}while(ch1=='y');
break;
}
case 2:
{
int value;
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_begning(value);
display();
break;
}
case 3:
{
int value;
printf("\nenter value to be inserted");
scanf("%d",&value);
insert_at_end(value);
display();
break;
}
case 4:
{
insert_at_middle();
display();
break;
}
case 5:
{
delete_from_front();
display();
}break;
case 6:
{
delete_from_end();
display();
break;
}
case 7:
{
display();
delete_from_middle();
display();
break;
}
case 8:
{
display();
break;
}
case 9:
{
exit(1);
}
default:printf("\n****Please enter correct choice****\n");
}}
getch();
}
Program:
#include<stdio.h>
#include<conio.h>
#define max 5
int st[max],top=-1;
void push()
{
int x;
if(top==max-1)
{
printf("stack is full");
return;
}
printf("enter element");
scanf("%d",&x);
top++;
st[top]=x;
}
void pop()
{
int x;
if(top==-1)
{
printf("stack is empty");
return;
}
printf("enter element");
scanf("%d",&x);
top--;
printf("enter deleted element=%d",x);
}
void display()
{
int i;
if(top==-1)
{
printf("stack is empty");
return;}
for(i=top;i>=0;i--)
{ printf("%d",st[i]);}}
int main()
{
int ch;
while(1)
{
printf("enter \[Link]\[Link]\[Link]\[Link]");
scanf("%d",&ch);
switch(ch) {
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(1);break;
}}
return 1; }
Program:
#include<stdio.h>
#include<conio.h>
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}}
void main()
{
int value, choice;
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}}}
Program:
#define MAXLEN 100
typedef struct {
char element[MAXLEN];
int top;
} stack;
stack init ()
{
stack S;
[Link] = -1;
return S;
}
int isEmpty ( stack S )
{
return ([Link] == -1);
}
int isFull ( stack S )
{
return ([Link] == MAXLEN - 1);
}
char top ( stack S )
{
if (isEmpty(S)) {
fprintf(stderr, "top: Empty stack\n");
return '\0';
}
return [Link][[Link]];
}
stack push ( stack S , char ch )
{
if (isFull(S)) {
fprintf(stderr, "push: Full stack\n");
return S;
}
++[Link];
[Link][[Link]] = ch;
return S;
}
stack pop ( stack S )
{
if (isEmpty(S)) {
fprintf(stderr, "pop: Empty stack\n");
return S;
}
--[Link];
return S;
}
void print ( stack S )
{
int i;
for (i = [Link]; i >= 0; --i) printf("%c",[Link][i]);
}
Here is a possible main() function calling these routines:
int main ()
{
stack S;
S = init(); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S));
S = push(S,'a'); printf("Current stack : "); print(S); printf(" with top = %c.\n",
top(S));
S = push(S,'b'); printf("Current stack : "); print(S); printf(" with top = %c.\n",
top(S));
S = push(S,'c'); printf("Current stack : "); print(S); printf(" with top = %c.\n",
top(S));
S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S));
S = push(S,'x'); printf("Current stack : "); print(S); printf(" with top = %c.\n",
top(S));
S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S));
S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S));
S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S));
S = pop(S); printf("Current stack : "); print(S); printf(" with top = %c.\n", top(S));
}
Program:
#define MAXLEN 100
typedef struct {
char element[MAXLEN];
int front;
int back;
} queue;
queue init ()
{
queue Q;
[Link] = 0;
[Link] = MAXLEN - 1;
return Q;
}
int isEmpty ( queue Q )
{
return ([Link] == ([Link] + 1) % MAXLEN);
}
int isFull ( queue Q )
{
return ([Link] == ([Link] + 2) % MAXLEN);
}
char front ( queue Q )
{
if (isEmpty(Q)) {
fprintf(stderr,"front: Queue is empty\n");
return '\0';
}
return [Link][[Link]];
}
queue enqueue ( queue Q , char ch )
{
if (isFull(Q)) {
fprintf(stderr,"enqueue: Queue is full\n");
return Q;
}
++[Link];
if ([Link] == MAXLEN) [Link] = 0;
[Link][[Link]] = ch;
return Q;
}
queue dequeue ( queue Q )
{
if (isEmpty(Q)) {
fprintf(stderr,"dequeue: Queue is empty\n");
return Q;
}
++[Link];
if ([Link] == MAXLEN) [Link] = 0;
return Q;
}
void print ( queue Q )
{
int i;
if (isEmpty(Q)) return;
i = [Link];
while (1) {
printf("%c", [Link][i]);
if (i == [Link]) break;
if (++i == MAXLEN) i = 0;
}
}
int main ()
{
queue Q;
Q = init(); printf("Current queue : "); print(Q); printf("\n");
Q = enqueue(Q,'a'); printf("Current queue : "); print(Q); printf("\n");
Q = enqueue(Q,'b'); printf("Current queue : "); print(Q); printf("\n");
Q = enqueue(Q,'c'); printf("Current queue : "); print(Q); printf("\n");
Q = dequeue(Q); printf("Current queue : "); print(Q); printf("\n");
Q = dequeue(Q); printf("Current queue : "); print(Q); printf("\n");
Q = enqueue(Q,'c'); printf("Current queue : "); print(Q); printf("\n");
Q = dequeue(Q); printf("Current queue : "); print(Q); printf("\n");
Q = dequeue(Q); printf("Current queue : "); print(Q); printf("\n");
Q = dequeue(Q); printf("Current queue : "); print(Q); printf("\n");
}
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *left_child, *right_child;
};
struct node *new_node(int value)
{
struct node *tmp = (struct node *)malloc(sizeof(struct node));
tmp->value = value;
tmp->left_child = tmp->right_child = NULL;
return tmp;
}
void print(struct node *root_node) // displaying the nodes!
{
if (root_node != NULL)
{
print(root_node->left_child);
printf("%d \n", root_node->value);
print(root_node->right_child);
}
}
struct node* insert_node(struct node* node, int value) // inserting nodes!
{
if (node == NULL) return new_node(value);
if (value < node->value)
{
node->left_child = insert_node(node->left_child, value);
}
else if (value > node->value)
{
node->right_child = insert_node(node->right_child, value);
}
return node;
}
int main()
{
printf("Diaplay: Implementation of a Binary Tree in C!\n\n");
struct node *root_node = NULL;
root_node = insert_node(root_node, 10);
insert_node(root_node, 10);
insert_node(root_node, 30);
insert_node(root_node, 25);
insert_node(root_node, 36);
insert_node(root_node, 56);
insert_node(root_node, 78);
print(root_node);
return 0;
}
Program:
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
// Traverse to the right place and insert the node
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
// Find the inorder successor
struct node *minValueNode(struct node *node) {
struct node *current = node;
// Find the leftmost leaf
while (current && current->left != NULL)
current = current->left;
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
// Find the node to be deleted
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node *temp = minValueNode(root->right);
// Place the inorder successor in position of the node to be deleted
root->key = temp->key;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->key);
}
return root;
}
// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 9);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 11);
root = insert(root, 14);
root = insert(root, 4);
printf("Inorder traversal: ");
inorder(root);
printf("\nAfter deleting 10\n");
root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}
Program:
#include <stdio.h>
int LINEAR_SEARCH(int inp_arr[], int size, int val)
{
int i;
for (i = 0; i < size; i++)
if (inp_arr[i] == val)
return i;
return -1;
}
int main(void)
{
int arr[] = { 1000, 2000, 3000, 4000, 5000, 100, 0 };
int key = 1000;
int size = 10;
int res = LINEAR_SEARCH(arr, size, key);
if (res == -1)
printf("ELEMENT NOT FOUND!!");
else
printf("Item is present at index %d", res);
return 0;
}
Program:
/*Program to sort elements of an array using insertion sort method*/
#include<stdio.h>
#include<stdlib.h>
void main( )
{
int a[10],i,j,k,n;
clrscr( );
printf("How many elements you want to sort?\n");
scanf("%d",&n);
printf("\nEnter the Elements into an array:\n");
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
k=a[i];
for(j= i-1; j>=0 && k<a[j];j--)
a[j+1]=a[j];
a[j+1]=k;
}
printf("\n\n Elements after sorting: \n");
for(i=0;i<n;i++)
printf("%d\n", a[i]);
getch( );
}
Program:
/* program to sort elements of an array using Quick Sort */
#include<stdio.h>
void quicksort(int[ ],int,int);
void main( )
{
int low, high, pivot, t, n, i, j, a[10];
clrscr( );
printf("\nHow many elements you want to sort ? ");
scanf("%d",&n);
printf("\Enter elements for an array:");
for(i=0; i<n;i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
quicksort(a,low,high);
printf("\After Sorting the elements are:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch( );
}
void quicksort(int a[ ],int low,int high)
{
int pivot,t,i,j;
if(low<high)
{
pivot=a[low];
i=low+1;
j=high;
while(1)
{
while(pivot>a[i]&&i<=high)
i++;
while(pivot<a[j]&&j>=low)
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
else
break;
}
a[low]=a[j];
a[j]=pivot;
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}}
Program:
/* program to sort elements of an array using Merge Sort */
#include <stdio.h>
void disp( );
void mergesort(int,int,int);
void msortdiv(int,int);
int a[50],n;
void main( )
{
int i;
clrscr( );
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter elements for an array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nBefore Sorting the elements are:");
disp( );
msortdiv(0,n-1);
printf("\nAfter Sorting the elements are:");
disp( );
getch( );
}
void disp( )
{ int i;
for(i=0;i<n;i++)
printf("%d ",a[i]); }
void mergesort(int low,int mid,int high)
{
int t[50],i,j,k;
i=low;
j=mid+1;
k=low;
while((i<=mid) && (j<=high))
{
if(a[i]>=a[j])
t[k++]=a[j++];
else
t[k++]=a[i++];
}
while(i<=mid)
t[k++]=a[i++];
while(j<=high)
t[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=t[i]; }
void msortdiv(int low,int high)
{
int mid;
if(low!=high) {
mid=((low+high)/2);
msortdiv(low,mid);
msortdiv(mid+1,high);
mergesort(low,mid,high); } }
Program:
#include<stdio.h>
#define size 7
int arr[size];
void init()
{
int i;
for(i = 0; i < size; i++)
arr[i] = -1;
}
void insert(int value)
{
int key = value % size;
if(arr[key] == -1)
{
arr[key] = value;
printf("%d inserted at arr[%d]\n", value,key);
}
else
{
printf("Collision : arr[%d] has element %d already!\n",key,arr[key]);
printf("Unable to insert %d\n",value);
} }
void del(int value)
{
int key = value % size;
if(arr[key] == value)
arr[key] = -1;
else
printf("%d not present in the hash table\n",value);
}
void search(int value)
{
int key = value % size;
if(arr[key] == value)
printf("Search Found\n");
else
printf("Search Not Found\n");
}
void print()
{
int i;
for(i = 0; i < size; i++)
printf("arr[%d] = %d\n",i,arr[i]);
}
int main()
{
init();
insert(10); //key = 10 % 7 ==> 3
insert(4); //key = 4 % 7 ==> 4
insert(2); //key = 2 % 7 ==> 2
insert(3); //key = 3 % 7 ==> 3 (collision)
printf("Hash table\n");
print(); printf("\n");
printf("Deleting value 10..\n");
del(10);
printf("After the deletion hash table\n");
print(); printf("\n");
printf("Deleting value 5..\n");
del(5);
printf("After the deletion hash table\n");
print(); printf("\n");
printf("Searching value 4..\n");
search(4);
printf("Searching value 10..\n");
search(10); return 0; }