Student Marklist Program

#include
main()
{
int m[5],i,p,total,sno;
char sname[20],g[5];
float avg;
char res[20],grade;
char subname[5][30]={"Computer Organisation","Business Process","Problem Solving","Accountancy","Data Structures"};
p=0;total=0;
printf("Enter the student details...\n\n");
printf("Enter the Student Name : ");
scanf("%s",sname);
printf("Enter the Student Number : ");
scanf("%d",&sno);
for(i=0;i<5;i++)
{
printf("Enter the %s mark : ",subname[i]);
scanf("%d",&m[i]);
if (m[i]>=80)
{
if (p<1)
{
grade='O';
p=1;
}
g[i]='O';
}
else if(m[i]>=70)
{
if (p<2)
{
grade='A';
p=2;
}
g[i]='A';
}
else if(m[i]>=60)
{
if (p<3)
{
grade='B';
p=3;
}
g[i]='B';
}
else if(m[i]>=50)
{
if (p<4)
{
grade='C';
p=4;
}
g[i]='C';
}
else
{
grade='D';
p=5;
g[i]='D';
}
total=total+m[i];
}
avg=(float)total/5;
printf("\n\n\t\t\tSTUDENT MARKLIST\n");
printf("----------------------------------------------------------------------------");
printf("\n\nSTUDENT NAME : %s",sname);
printf("\nROLL NUMBER : %d",sno);
printf("\n\n");
printf("----------------------------------------------------------------------------");
printf("\n\t\tSUBJECT\t\tMARKS\t GRADE\n");
printf("----------------------------------------------------------------------------");
printf("\n");
for(i=0;i<5;i++)
printf("%25s \t%d\t %c\n",subname[i],m[i],g[i]);
printf("\n");
if (grade=='O')
strcpy(res,"OUTSTANDING");
else if (grade=='A')
strcpy(res,"FIRST CLASS");
else if (grade=='B')
strcpy(res,"SECOND CLASS");
else if (grade=='C')
strcpy(res,"THIRD CLASS");
else
strcpy(res,"FAIL");
printf("----------------------------------------------------------------------------");
printf("\nTOTAL : %d",total);
printf("\nAVERAGE : %f",avg);
printf("\nGRADE : %c",grade);
printf("\nRESULT : %s\n\n",res);
printf("----------------------------------------------------------------------------\n\n");
}

Library Book search program

#include
struct book
{
int accno;
char tit[20],au[20];
float price;
int av;
char ava[5];
};
struct ent
{
int sid;
int a;
};
int ok;
struct book b[20];
struct ent e[20];
int c,j,i,k=0,l;
void addbook();
void display();
void avail();
void bookdue();
void sauthor();
void stitle();
void saccno();
main()
{
int i=-1;
system("clear");
printf("\t\t\tLIBRARY MANAGEMENT SYSTEM\n");
do
{
printf("\nOperations :\n1.ADD BOOKS TO LIBRARY\n2.DISPLAY BOOKS IN THE LIBRARY\n3.AVAILABILITY\n\ti.SEARCH BOOKS BY ACCESSION NUMBER\n\tii.SEARCH BOOKS BY AUTHOR\n\tiii.SEARCH BOOKS BY TITLE\n4.BOOK DUE(ENTRY)\n5.EXITING THE SYSTEM\n");
printf("\nEnter Your choice : ");
scanf("%d",&c);
switch(c)
{
case 1: addbook(); break;
case 2: display(); break;
case 3: avail(); break;
case 4: bookdue(); break;
}
}while(c!=5);
}
void addbook()
{
i++;
printf("\nEnter the Accession Number(4 digit Number) : ");
scanf("%d",&b[i].accno);
for(j=i-1;j>=0;j--)
{
if (b[j].accno==b[i].accno)
{
i--;
printf("\nThe Accession Number Alreary Exists!\n");
return;
}
}
printf("\nEnter the Book Title(max 20 char) : ");
scanf("%s",b[i].tit);
printf("Enter the Author Name : ");
scanf("%s",b[i].au);
printf("Enter the Price of book : ");
scanf("%f",&b[i].price);
b[i].av=1;
}
void display()
{
printf("\n\t\t\tBOOKS IN THE LIBRARY\n");
printf("ACCNO\tTITLE OF THE BOOK\tAUTHOR OF THE BOOK\tPRICE\tAVAIL\n");
for(j=1;j<=i;j++)
{
if (b[j].av==1)
strcpy(b[j].ava,"YES");
else
strcpy(b[j].ava,"NO");
printf("%d\t%18s\t%18s\t%6.2f\t %s\n",b[j].accno,b[j].tit,b[j].au,b[j].price,b[j].ava);
}
}
void avail()
{
int ch;
do
{
printf("\nChecking books availability by searching in means of \n1.ACCESSION NUMBER\n2.TITLE\n3.AUTHOR\n4.EXIT SEARCHING\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: saccno(); break;
case 2: stitle(); break;
case 3: sauthor(); break;
}
}while(ch!=4);
}
void bookdue()
{
int acc,id;
printf("\nEnter the Accession Number To get Book(4 digit number) : ");
scanf("%d",&acc);
for(j=0;j<=i;j++)
if (b[j].accno==acc)
{
if (b[j].av==1)
{
printf("\nEnter the Student ID(3 digit number) : ");
scanf("%d",&id);
//k++;
for(l=0;l<=k;l++)
if (e[l].sid==id)
{
if (e[l].a<2)
{
printf("\n Book Due Success!");
b[j].av=0;
e[l].a=e[l].a+1;
return;
}
else
{
printf("\n Sorry! You have 3 books on due!");
return;
}
}
printf("\nBook Due Success!");
e[k].sid=id;
e[k].a=0;
b[j].av=0;
k++;
return;
}
printf("\nThis Book on Due!");
return;
}
printf("\nBook Not Available!");
}
void saccno()
{
int ac;
printf("\nEnter the accession to find Book : ");
scanf("%d",&ac);
for(j=0;j<=i;j++)
if(b[j].accno==ac)
{
printf("\nBook Name : %s\nAuthor : %s\nPrice : %f\nAvailability : %s\n",b[j].tit,b[j].au,b[j].price,b[j].ava);
return;
}
printf("\n No such Accession Number available!");
}
void sauthor()
{
char author[20];
int ok=0;
printf("\nEnter the author name to find Books : ");
scanf("%s",author);
for(j=0;j<=i;j++)
if (strcmp(author,b[j].au)==0)
{
printf("\nAcc. No : %d\nBook Name : %s\nPrice : %f\nAvailability : %s\n",b[j].accno,b[j].tit,b[j].price,b[j].ava);
ok=1;
}
if (ok!=1)
printf("\n No books available!");
}
void stitle()
{
char title[20];
int ok1=0;
printf("\nEnter the Title to find the book : ");
scanf("%s",title);
for(j=0;j<=i;j++)
if (strcmp(title,b[j].tit)==0)
{
printf("\nAcc No : %d\nAuthor : %s\nPrice : %f\nAvailability : %s\n",b[j].accno,b[j].au,b[j].price,b[j].ava);
ok1=1;
}
if (ok!=1)
printf("\nNo Such book available!");
}

Insertion sort program


#include
main()
{
int a[20],i,j,n,t;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the numbers to be sorted : \n");
for(i=0;iscanf("%d",&a[i]);
for(i=0;i{
t=a[i];
j=i+1;
while(t{
a[j+1]=a[j];
j=j-1;
if (j<=0)
break;
}
a[j+1]=t;
}
printf("\nSorted elements : \n");
for(i=0;iprintf("%d\n",a[i]);
}

Fibonacci using recursion program


#include
int fibo(int);
main()
{
int i,n,s,sum=0;
printf("\nEnter the limit : ");
scanf("%d",&n);
for(i=0;i{
s=fibo(i);
printf("%d\n",s);
}
//printf("%d",sum);
}
int fibo(int i)
{
if (i==0)
return (0);
if ((i==1)(i==2))
return (1);
else
return fibo(i-1)+fibo(i-2);
}

Hybrid Inheritance - sample program

/* $ Hybird Inheritance with Employee details $ */
#include
using namespace std;
class emp
{
int eno;
char ename[20];
public:
void getdata()
{
cout<<"\nEnter the Employee number : ";
cin>>eno;
cout<<"\nEnter the employee Name : ";
cin>>ename;
}
void display()
{
cout<<"\nEmployee Number : "<cout<<"\nEmployee Name : "<}
};
class ebasic:public emp
{
public:
float bpay;
char degn[20];
void getdata1()
{
cout<<"\nEnter the designation : ";
cin>>degn;
cout<<"Enter the Basic pay : ";
cin>>bpay;
}
void display1()
{
cout<<"\nEmployee Designation : "<cout<<"Employee Basic Pay : "<}
};
class epers
{
char addr[50],cname[20];
//int age;
public:
void getdata2()
{
cout<<"\nEnter the Name of the company : ";
cin>>cname;
cout<<"\nEnter the address of the company..."<cin>>addr;
}
void display2()
{
cout<<"\nCompany Name : "<cout<<"Company address : "<}
};
class edetail:public epers,public ebasic
{
float sal,hra,da;
public:
void calc()
{
hra=(bpay*0.4);
da=(bpay*0.75);
sal=bpay+hra+da;
}
void display3()
{
cout<<"Basic Pay : "<cout<<"H.R.A : "<cout<<" D.A : "<cout<<" Salary : "<}
};
main()
{
edetail ed;
ed.getdata();
ed.getdata1();
ed.getdata2();
ed.calc();
ed.display();
ed.display1();
ed.display2();
ed.display3();
}

Heap sort Program

#include
int n,a[10],k;
void heapsort(int a[],int n);
void swap(int b,int c);
void pushdown(int x,int y);
main()
{
system("clear");
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements one by one...\n");
for(k=0;kscanf("%d",&a[k]);
for(k=0;kheapsort(a,n);
printf("\nElements after the Heap sort...\n");
for(k=0;kprintf("%d\n",a[k]);
}
void heapsort(int a[],int n)
{
int i;
for(i=n/2;i>=1;i--)
pushdown(i,n);
for(i=n;i>=2;i--)
{
swap(1,i);
pushdown(1,i-1);
}
}
void pushdown(int first,int last)
{
int r;
r=first;
while(r<=last/2)
{
if(last==2*r)
{
if (a[r]>a[2*r])
swap(r,2*r);
r=last;
}
else
{
if (a[r]>a[2*r]&&a[2*r]<=a[2*r+1])
{
swap(r,2*r);
r=2*r;
}
else if(a[r]>a[2*r+1]&&a[2*r+1]{
swap(r,2*r+1);
r=2*r+1;
}
else
r=last;
}
}
}
void swap(int m,int x)
{
int t;
t=a[m];
a[m]=a[x];
a[x]=t;
}

Floyds Triangle program

#include
main()
{
int i,j,k,n;
k=0;
printf("Enter the limit : ");
scanf("%d",&n);
printf("\n\n\t\tFLOYDS TRIANGLE\n\n");
for(i=1;i<=n;i++)
{
for(j=i;j>=1;j--)
printf("%d\t",++k);
printf("\n");
}
}

sample output:

1

2 3

4 5 6

6 7 8 9

Fibonacci series program

#include
main()
{
int i,n,f1,f2,f3;
system("clear");
f1=-1;
f2=1;
printf("Enter the limit : ");
scanf("%d",&n);
printf("The Fibonacci series are....\n");
for(i=1;i<=n;i++)
{
f3=f1+f2;
printf("%d\n",f3);
f1=f2;
f2=f3;
}
}

Factorial program

#include
main()
{
int n,i;
int fact(int n);
printf("Enter the number : ");
scanf("%d",&n);
printf("\nThe factorial of the given number is %d\n",fact(n));
}
int fact(int f)
{
if ((f==0)(f==1))
return 1;
else
return (f*(fact(f-1)));
}

E-series calculation



#include
#include "math.h"
int fact(int i);
main()
{
float x;
int n,i=1;
float term=0.0,sum=0.0;
printf("\nEnter the limit : ");
scanf("%d",&n);
printf("\nEnter the value of x : ");
scanf("%f",&x);
x=x*(3.14/180.0);
for(i=0;i{
term=(pow(x,i))/fact(i);
sum=sum+term;
}
printf("\nValue of sum is (i.e e(x)) : %f\n",sum);
}
int fact(int n)
{
int i,s=1;
for(i=1;i<=n;i++)
s=s*i;
return s;
}

EBBILL generation sample program


#include
using namespace std;
class ebbill
{
public:
int lread,cread,cmode,tread,diff;
float amt;
char cname[20],mode[];
//public:
void getdata();
float bill(int lread,int cread,int cmode);
void display();
/* ebbill()
{
lread=0;
cread=0;
cmode=0;
}
~ebbill()
{
lread=0;
cread=0;
cmode=0;
}*/
};
void ebbill::getdata()
{
//float amt;
//int diff;
cout<<"Enter the name of the customer : ";
cin>>cname;
cout<<"Enter the customer mode : "<cin>>cmode;
get:
cout<<"Enter the last month reading : ";
cin>>lread;
cout<<"Enter the current month reading : ";
cin>>cread;
tread=cread-lread;
if (tread<0)
{
cout<<"Enter the correct reading : ";
goto get;
}
diff=tread;
amt=bill(lread,cread,cmode);
display();
}
float ebbill::bill(int lread,int cread,int cmode)
{
float read=0.0;
switch(cmode)
{
case 1:
{
strcpy(mode,"Home");
if ((tread==0)(tread<=100))
read=tread*0.4;
else if (tread<=200)
{
tread=tread-100;
read=(tread*0.75)+40.00;
}
else if (tread<=500)
{
tread=tread-200;
read=(tread*1.5)+(100.00*0.75)+40.00;
}
else
{
tread=tread-500;
read=(tread*2.0)+(300.0*1.5)+75.0+40.0;
}
break;
}
case 2:
{
strcpy(mode,"Business");
if ((tread==0)(tread<=100))
read=tread*1.0;
else if (tread<=500)
{
tread=tread-100;
read=(tread*2.0)+100.00;
}
else
{
tread=tread-500;
read=(tread*5.0)+(400.0*2.0)+100.0;
}
break;
}
case 3:
{
strcpy(mode,"Industry");
if ((tread==0)(tread<=200))
read=tread*5.0;
else if (tread<=1000)
{
tread=tread-200;
read=(tread*5.0)+500.00;
}
else
{
tread=tread-1000;
read=(tread*7.5)+(800.0*5.0)+500.0;
}
break;
}
}
return read;
}
void ebbill::display()
{
cout<<"Customer Name : "<cout<<"Customer Mode : "<cout<<"Last month reading : "<cout<<"Current month reading : "<cout<<"Calculated Reading : "<cout<<"Calculated Bill : "<}
main()
{
system("clear");
ebbill eb;
eb.getdata();
return 0;
}


Cos series calculation


#include
#include "math.h"
int fact(int i);
main()
{
float x;
int n,i=1;
float term=0.0,sum=0.0,sign=-1;
printf("\nEnter the limit : ");
scanf("%d",&n);
printf("\nEnter the value of x : ");
scanf("%f",&x);
x=x*(3.14/180.0);
for(i=0;i{
sign=-1*sign;
term=(sign*pow(x,i))/fact(i);
sum=sum+term;
}
printf("\nValue of sum is (i.e cos(x)) : %f\n",sum);
}
int fact(int n)
{
int i,s=1;
for(i=1;i<=n;i++)
s=s*i;
return s;
}

Circular queue implementation

#include
#define max 5
struct queue
{
int data[max],front,rear;
};
struct queue q;
int isfull()
{
if((q.rear+2)%max==q.front)
return 1;
else
return 0;
}
int isempty()
{
if ((q.rear+1)%max==q.front)
return 1;
else
return 0;
}
void enqueue()
{
int val;
if (isfull())
printf("\nQueue is full!");
else
{
printf("\nEnter the value : ");
scanf("%d",&val);
q.rear=(q.rear+1)%max;
q.data[q.rear]=val;
}
}
int dequeue()
{
int x;
if (isempty())
printf("\nQueue is empty!");
else
{
x=q.data[q.front];
q.front=(q.front+1)%max;
return x;
}
}
void display()
{
int i;
if (isempty())
printf("\nQueue is empty!");
else
{
printf("\nThe elements in the queue are...");
for(i=q.front;i!=q.rear;i=(i+1)%max)
printf("\n%d",q.data[i]);
printf("\n%d",q.data[q.rear]);
}
}
main()
{
q.rear=-1;
q.front=0;
int choice;
int isfull();
int isempty();
int dequeue();
void enqueue();
void display();
printf("\n1-Insert 2-Delete 3-Display 4-Exit");
do
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: enqueue(); break;
case 2: printf("The deleted element is %d",dequeue()); break;
case 3: display(); break;
}
}while(choice!=4);
}

Bubble sort Program



#include
main()
{
int a[10],n,i,pass;
system("clear");
printf("\nEnter the number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements one by one...\n");
for(i=0;iscanf("%d",&a[i]);
printf("\nMaking the sorting of elements in Bubble Sort...\n");
for(pass=0;passfor(i=0;iif (a[i]>a[i+1])
{
a[i]=a[i]+a[i+1];
a[i+1]=a[i]-a[i+1];
a[i]=a[i]-a[i+1];
}
printf("\nElements in the order after Bubble sort..\n");
for(i=0;iprintf("%d\n",a[i]);
}


Data structures : single linked list program

#include
struct node
{
int data;
struct node *next;
}*head,*temp1,*curr;
void insert();
void delete();
void count();
void display();
void insert()
{
struct node *temp;
int val;
temp=(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
{
printf("\nMemory Full!");
return;
}
printf("Enter the number : ");
scanf("%d",&val);
temp->data=val;
temp->next=NULL;
if (head==NULL)
{
head=temp;
return;
}
if (head->next==NULL (head->next!=NULL && temp->data <>data))
{
if (temp->data <>data)
{
temp->next=head;
head=temp;
}
else
head->next=temp;
return;
}
for(curr=head->next,temp1=head;curr!=NULL;curr=curr->next,temp1-temp1->next)
{
if (curr->data > temp->data)
{
temp->next=curr;
temp1->next=temp;
return;
}
}
temp1->next=temp;
return;
}
void delete()
{
int x,found=0;
struct node *temp;
printf("Enter the node to delete : ");
scanf("%d",&x);
if (x==head->data)
{
temp=head;
head=head->next;
free(temp);
found=1;
return;
}
for(temp=head,curr=head->next;curr!=NULL;curr=curr->next,temp=temp->next)
{
if (curr->data==x && found==0)
{
found=1;
temp1=curr;
if (curr->next==NULL)
temp->next=NULL;
else
{
curr=curr->next;
temp->next=curr;
}
free(temp1);
}
}
if (!found)
printf("\n Element not found!");
}
void count()
{
int c=0;
struct node *temp;
for(temp=head;temp!=NULL;temp=temp->next,c++);
printf("\nNumber of nodes : %d",c);
}
void display()
{
struct node *temp;
for(temp=head;temp!=NULL;temp=temp->next)
printf("%d-->",temp->data);
}
main()
{
int wish=1,choice;
head=NULL;
do
{
printf("\n1.Insert\n2.Delete\n3.Count\n4.Display\n5.Exit\n");
printf("Enter Your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(); break;
case 2: delete(); break;
case 3: count(); break;
case 4: display(); break;
}
}while(choice!=5);
}

Sample employee payslip program

#include
struct emp
{
double bp,hra,da,cca,ded,gp,np;
char empname[20];
};
struct emp e[50];
int i=0;
main()
{
int ch;
// struct emp e;
int addemp();
void display();
int delemp();
do
{
printf("\n1.Add 2.Delete 3.Display 4.Exit \nEnter the choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: i=addemp(); break;
case 2: i=delemp(); break;
case 3: display(); break;
}
}while(ch!=4);
}
int addemp()
{
i=i+1;
e[i].ded=1000.00;
printf("\nEnter the Employee Name : ");
scanf("%s",e[i].empname);
printf("\nEnter the Basic Pay : ");
scanf("%lf",&e[i].bp);
e[i].da=0.9*e[i].bp;
e[i].hra=0.2*e[i].bp;
e[i].cca=0.1*e[i].bp;
e[i].gp=e[i].bp+e[i].da+e[i].hra+e[i].cca;
e[i].np=e[i].gp-e[i].ded;
return i;
}
int delemp()
{
printf("The deleted employee name is %s",e[i].empname);
i=i-1;
return i;
}
void display()
{
int j,k=0;
printf("\t\t\t\tEMPLOYEE PAYSLIP\n\n");
printf("\nSNO EMPLOYEE NAME BASIC DA HRA CCA GP DED NP\n");
printf("--------------------------------------------------------------------------\n");
for(j=1;j<=i;j++)
{
k=k+1;
printf("%3d %13s %7.2f %7.2f %7.2f %7.2f %8.2f %7.2f %8.2f",k,e[j].empname,e[j].bp,e[j].da,e[j].hra,e[j].cca,e[j].gp,e[j].ded,e[j].np);
printf("\n");
}

}

Data structures : Stack using pointers program

#include
int pop();
void push();
int isempty();
void display();
struct node
{
int data;
struct node *next;
};
struct node *temp;
struct node *top=NULL;
void push()
{
int value;
printf("\nEnter the value : ");
scanf("%d",&value);
temp=(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
printf("\nMemory is not available!");
else
{
temp->data=value;
if (top==NULL)
temp->next=NULL;
else
temp->next=top;
top=temp;
}
}
int pop()
{
struct node *temp;
int value;
if (isempty())
printf("\nStack is empty!");
else
{
temp=top;
value=temp->data;
top=top->next;
free(temp);
return value;
}
}
int isempty()
{
if (top==NULL)
return 1;
else
return 0;
}
void display()
{
struct node *temp;
if (isempty())
printf("\nStack is empty!");
else
{
printf("The elements in the stack are...\n");
for(temp=top;temp!=NULL;temp=temp->next)
printf("%d\n",temp->data);
}
}
main()
{
int ch;
printf("\n1.Push 2.Pop 3.Display 4.Exit\n");
do
{
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(); break;
case 2: printf("\nDeleted element is %d",pop()); break;
case 3: display(); break;
}
}while(ch!=4);
}

Data structures : Stack using array program



#include
#define max 10
struct stack
{
int data[max];
int top;
};
struct stack s;
main()
{
s.top=-1;
int choice;
int isfull();
int isempty();
void push();
int pop();
void display();
do
{
printf("\n1-Add element 2-Delete element 3-Display element 4-Exit\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: push(); break;
case 2: printf("\nThe deleted element is %d",pop()); break;
case 3: display();
}
}while(choice!=4);
}
int isempty()
{
if(s.top==-1)
return 1;
else
return 0;
}
int isfull()
{
if(s.top==max-1)
return 1;
else
return 0;
}
void push()
{
int value;
printf("\nEnter the value... ");
scanf("%d",&value);
if(s.top{
s.top+=1;
s.data[s.top]=value;
printf("\n# Value Inserted #");
}
else
printf("\nStack Overflow!");
}
int pop()
{
int x;
if (isempty())
{
printf("\nStack empty!");
return -1;
}
else
{
x=s.data[s.top];
s.top-=1;
return x;
}
}
void display()
{
int i;
if (isempty())
printf("\nStack is empty!");
else
{
printf("The elements in the stack are...\n");
for(i=0;i<=s.top;i++)
printf("%d\n",s.data[i]);
printf("\nThe number of elements in the stack are %d",i);
}
}


Data structures : Queue using array program



#include
#define max 10

struct queue
{
int front,rear,data[max];
};
struct queue q;

main()
{
int ch;
q.front=0;
q.rear=-1;
int isempty();
void display();
void enqueue();
void count();
int isfull();
int dequeue();
do
{
printf("\n1-Add 2-Delete 3-Display 4-Count 5-Exit\nEnter ur choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: enqueue(); break;
case 2: printf("\nThe deleted element is %d",dequeue()); break;
case 3: display(); break;
case 4: count(); break;
}
}while(ch!=5);
}

int isfull()
{
if (q.rear==max-1)
return 1;
else
return 0;
}

int isempty()
{
if (q.rear return 1;
else
return 0;
}

void count()
{
printf("\nThe number of elements in the queue are %d",q.rear-q.front+1);
}

void enqueue()
{
int val;
if (isfull())
printf("\nQueue is full!");
else
{
printf("\nEnter the element... ");
scanf("%d",&val);
q.rear+=1;
q.data[q.rear]=val;
}
}

int dequeue()
{
int x;
if (isempty())
printf("\nQueue is empty!");
else
{
x=q.data[q.front];
q.front+=1;
return x;
}
}

void display()
{
int i;
printf("\nThe elements in the queue are...\n");
for(i=q.front;i<=q.rear;i++)
printf("%d\n",q.data[i]);
}

Data structures : Queue using pointers program

#include
struct node
{
int data;
struct node *next;
};
struct node *front=NULL,*rear=NULL;

void enqueue()
{
struct node *temp;
int val;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the value : ");
scanf("%d",&val);
temp->data=val;
temp->next=NULL;
if (rear==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
}

void dequeue()
{
struct node *temp;
int x;
if (front==NULL)
printf("\nQueue is Empty!");
else
{
if (front==rear)
{
printf("\n%d deleted",front->data);
front=rear=NULL;
}
else
{
temp=front;
front=front->next;
printf("\n%d deleted!",temp->data);
free(temp);
}
}
}

void display()
{
struct node *temp;
printf("\nThe elements in the queue are...\n");
for(temp=front;temp!=NULL;temp=temp->next)
printf("%d\n",temp->data);
}

main()
{
int choice;
void enqueue();
void dequeue();
void display();
printf("\n1-Insert 2-Delete 3-Display 4-Exit ");
do
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: enqueue(); break;
case 2: dequeue(); break;
case 3: display(); break;
}
}while(choice!=4);
}

Quadratic equation program

#include
main()
{
float a,b,c,d;
double i1,i2;
printf("Enter the values of a,b,c : ");
scanf("%f %f %f",&a,&b,&c);
d=pow(b,2)-4.0*a*c;
if (d>=0)
{
i1=(-b+sqrt(d))/(2*a);
i2=(-b-sqrt(d))/(2*a);
if (d==0)
printf("\nThe roots are real and equal.");
else
printf("\nThe roots are real and unequal\n");
printf("\nThe roots of the quadratic equation are %lf and %lf\n",i1,i2);
}
else
printf("\nThe roots are imaginary!");
}

Finding prime number with less time complexity

This is a program to find the number is prime or not with less time complexity.

step 1: Find the modulus of the number by 2 [To find its even or not]

step 2: Find the square root of that number(given)

step 3: Check the number with loop until the square root of that number.

Normal Time complexity : N power(n-1)

But using this program : the complexity is less that half of (N/2 power(n-1)/2)

#include
main()
{
int i,n,m,s=0;
system("clear");
printf("Enter the number : ");
scanf("%d",&n);
m=n;
if (n%2==0)
{
printf("The Number(%d) is not Prime",m);
s=1;
}
else
{
n=sqrt(n);
if ((n*n)==m)
{
printf("The number(%d) is not prime!",m);
s=1;
}
else
{
for(i=3;i<=n;i++)
{
if (n%i==0)
s=1;
}
}
}
if (s==1)
printf("The number(%d) is not prime!",m);
else
printf("The number is Prime!");
}

Program to calculate simple and compound interest

#include
main()
{
float p,r,si,ci;
int n;
printf("Enter the principle amount : ");
scanf("%f",&p);
printf("Enter the number of years : ");
scanf("%d",&n);
printf("Enter the rate of interest per year : ");
scanf("%f",&r);
si=(p*n*r)/100.0;
ci=p*(pow(1.0+(r/100),n))-p;
printf("\nSimple Interest = %.2f\n",si);
printf("\nCompound Interest = %.2f\n",ci);
}

Pascal program using C

#include
main()
{
int a[10][10],b[10][10],n,i,j,k;
printf("Enter the limit : ");
scanf("%d",&n);
a[0][0]=0;
a[0][1]=1;
for(i=1;i<=n;i++)
{
for(j=1;j {
a[i][j]=a[i-1][j-1]+a[i-1][j];
//a[j]=a[j-1]+a[j];
//b[j]=a[j];
//printf("%d\t",b[j]);
}
a[i][j]=1;
for(k=1;k<=i;k++)
{
for(j=1;j<=k;i++)
{
printf("%d\t",a[k][j]);
}
printf("\n");
}
}
}

Euclid Algorithm Implementation

#include
#include

class euclid
{
int n,m,r;
public:
void getdata();
int eu_alg(int m,int n);
};

void main()
{
clrscr();
euclid e;
e.getdata();
getch();
}

void euclid::getdata()
{
cout<<"Enter the two numbers : ";
cin>>m>>n;
r=eu_alg(m,n);
cout<<"The GCD of the given numbers : "<}

int euclid::eu_alg(int m,int n)
{
while(n>1)
{
r=m%n;
m=n;
n=r;
}
return m;
}

Consecutive Integer Checking Program

#include
#include

class cons
{
int n,m,t,r;
public:
void getdata();
int cic(int m,int n,int t);
int min(int m,int n);
};

void cons::getdata()
{
cout<<"Enter the two numbers : ";
cin>>m>>n;
t=min(m,n);
t=cic(m,n,t);
if (t!=1)
cout<<"The GCD of two numbers : "<else
cout<<"The given two numbers doesn't have GCD value!"<}

int cons::min(int m,int n)
{
if (m t=m;
else
t=n;
return t;
}

int cons::cic(int m,int n,int t)
{
l10:
r=m%t;
if (r==0)
{
r=n%t;
if (r==0)
return t;
}
t--;
goto l10;
}

void main()
{
clrscr();
cons c;
c.getdata();
getch();
}

Optimal Binary Search Tree


#include
#include
#define infinity 1000
class obst
{
int kmin,k,n,i,d,s,j,root[10][10];
float sum,table[10][10],prob[10],min;
public:
obst()
{
for(i=1;i<=n;i++)
for(j=0;j<=n;j++)
{
table[i][j]=float(0);
root[i][i]=float(0);
}
}
void getdata();
void obst1();
void display();
};
void obst::getdata()
{
cout<<"\nEnter the number of objects : ";
cin>>n;
cout<<"\nEnter the probability : ";
for(i=1;i<=n;i++)
cin>>prob[i];
}

void obst::obst1()
{
for(i=1;i<=n;i++)
{
table[i][i]=prob[i];
root[i][i]=float(i);
}
table[n+1][n]=float(0);
for(d=1;d{
for(i=1;i<=n-d;i++)
{
sum=float(0);
j=i+d;
min=infinity;
for(k=i;k<=j;k++)
{
if ((table[i][k-1]+table[k+1][j]){
min=table[i][k-1]+table[k+1][j];
kmin=k;
}
}
root[i][j]=kmin;
for(s=i;s<=j;s++)
sum=sum+prob[s];
table[i][j]=sum+min;
}
}
}
void obst::display()
{
cout<<"\n\tMAIN TABLE\n";
for(i=1;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(j!=0)
cout<<"\t"<else
cout<<"0.0";
}
cout<}
cout<<"\n\t\tROOT TABLE\n";
for(i=1;i<=n;i++)
{
for(j=0;j<=n;j++)
{
if(j!=0)
cout<<"\t"<else
cout<<"0";
}
cout<}
}
void main()
{
clrscr();
obst ob;
ob.getdata();
ob.obst1();
ob.display();
getch();
}