Showing posts with label C rules. Show all posts
Showing posts with label C rules. Show all posts

Simple calculator

Program to perform imlpement simple calculator performing basic operations.

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

float add(float,float);
float sub(float,float);
float product(float,float);
float divide(float,float);

void main()
{
float n1,n2;
char sym,choice;
clrscr();
printf("Enter the operation you want to perform(Format eg :- 2+3) \n\n");
scanf("%f%c%f",&n1,&sym,&n2);
if(sym=='+')
printf("\n%f",add(n1,n2));
if(sym=='-')
printf("\n%f",sub(n1,n2));
if(sym=='*')
printf("\n%f",product(n1,n2));
if(sym=='/')
printf("%f",divide(n1,n2));
printf("\nDo you wish to continue[y/n]");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
main();
}

float add(float m1,float m2)
{
return(m1+m2);
}

float sub(float m1,float m2)
{
return(m1-m2);
}

float product(float m1,float m2)
{
return(m1*m2);
}

float divide(float m1,float m2)
{
return(m1/m2);
}

Binary search

Program to perform binary operation using linked list.. a simple method!

#include"stdio.h"
#include"conio.h"
#include"alloc.h"
struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
p->next=NULL;
return p;
}
void freenode(nodeptr p)
{
free(p);
}
main()
{
int i,n,l,h,mid,ele,flag=0,pos;
nodeptr p,q,list;
clrscr();
printf("\nEnter the no of nodes : ");scanf("%d",&n);
printf("\nEnter the elements");
p=getnode();
printf("\nEnter info : ");scanf("%d",&p->info);
list=p;
for(i=1;i 'lesser than' n;i++) /* Replace lesser than with symbols*/
{
q=getnode();
printf("\nEnter info : ");scanf("%d",&q->info);
p->next=q;
p=q;
}
q->next=NULL;
printf("\nEnter the element to be searched : ");scanf("%d",&ele);
l=1;
h=n;
while(l<=h)
{
mid=(l+h)/2;
p=list;
for(i=1;i
p=p->next;
if(p->info==ele)
{
pos=mid;
flag=1;
break;
}
else if(p->info
l=mid+1;
else
h=mid-1;
}
if(flag)
printf("\n%d found in the list @ position %d",ele,pos);
else
printf("\nSearch unsuccessful!! Element not found in the list");
getch();
return 0;
}

Circular linked list

Program to perform operation on circular linked lidt....
Operations:-
1 : Insert element from end
2 :Delete element from rear


#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
p->next=NULL;
return p;
}
void freenode(nodeptr p)
{
free(p);
}
nodeptr ins_end(nodeptr list,int ele)
{
nodeptr p,q;
p=getnode();
p->info=ele;
if(list==NULL)
{
list=p;
list->next=list;
}
else
{
q=list->next;
p->next=q;
list->next=p;
list=p;
}
clrscr();
printf("%d element inserted\n",ele);
return list;
}
void display(nodeptr list)
{
nodeptr p;
if(list==NULL)
{
printf("List empty..!");
return;
}
else
{
printf("\nCIRCULAR LIST ELEMENTS\n----------------------");
for(p=list->next;p!=list;p=p->next)
{
printf("\n%d",p->info);
}
printf("\n%d\n",list->info);
}
}
nodeptr del_f(nodeptr list)
{
nodeptr p,q;
int x;
if(list==NULL)
{
printf("\nInvalid operation..!");
return 0;
}
else
{
p=list->next;
q=p->next;
list->next=q;
x=p->info;
freenode(p);
printf("%d deleted form list..!\n",x);
}
return list;
}
main()
{
int ele,choice;
nodeptr list;
list=NULL;
clrscr();
while(1)
{
printf("\n1 : Insert from the end\n2 : Delete from the front\n3 : Display\n4 : Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to be inserted :");scanf("%d",&ele);
list=ins_end(list,ele);
break;
case 2:clrscr();
list=del_f(list);
break;
case 3:clrscr();
display(list);
break;
default:exit(0);
break;
}
}
getch();
return 0;
}

Feedback

Hi frns...
The programs posted here are all tested and workng..
So any prob regarding the programs can be posted here...
Also if u want me to post any particular program,  jus type in here..
I will try to post as quickly as possible..
Thank you!!

Tower of Hanoi


Program with recurrsion.. TOWER OF HANOI prob...


#include<stdio.h> 
#include<conio.h>
void towers(int,char,char,char);
int count=0;
main()
{
int n;
clrscr();
printf("\n Enter the no of disks:");
scanf("%d",&n); 
towers(n,'a','c','b');
printf("\nTotal no of moves required is %d",count);
getch();
return 0;
}
void towers(int n,char frompeg,char topeg,char temppeg)
{
if(n==1) 
{
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
count++;
return;
}
towers(n-1,frompeg,temppeg,topeg);
count++;
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
towers(n-1,temppeg,topeg,frompeg); 
}

Double linked list

A program on double linked list... To  perform various operations on DLL.

#include"stdio.h"      /*Note tags are to be used for pre-prcessor directives*/
#include"conio.h"
#include"stdlib.h"
struct node
 {
  int info;
  struct node *left;
  struct node *right;
 };
typedef struct node *nodeptr;
nodeptr getnode()
 {
  nodeptr p;
  p=(nodeptr)malloc(sizeof(struct node));
  p->left=NULL;
  p->right=NULL;
  return p;
  }
void freenode(nodeptr p)
 {
  free(p);
 }
nodeptr ins_f(nodeptr list,int ele)
 {
  nodeptr p;
  p=getnode();
  p->info=ele;
  if(list==NULL)
   {
    list=p;
   }
  else
   {
    p->right=list;
    list->left=p;
    list=p;
   }
  clrscr();
  printf("%d element inserted",ele);
  return list;
 }
nodeptr ins_end(nodeptr list,int ele)
 {
  nodeptr p,q;
  p=getnode();
  p->info=ele;
  if(list==NULL)
   {
    list=p;
   }
  else
   {
    for(q=list;q->right!=NULL;q=q->right);
    p->left=q;
    q->right=p;
   }
   clrscr();
   printf("%d element inserted",ele);
   return list;
  }
nodeptr ins_aftx(nodeptr list,int ele,int x)
 {
  nodeptr p,q;
  int flag=0;
  p=getnode();
  p->info=ele;
  if(list==NULL)
   {
    printf("\nNo element X found in the list...!");
    return 0;
   }
  else
   {
    for(q=list;q!=NULL;q=q->right)
      if(q->info==x)
{
flag=1;
break;
}
      if(flag)
       {
(q->right)->left=p;
p->right=q->right;
q->right=p;
p->left=q;
clrscr();
printf("%d inserted into the list after %d",ele,x);
       }
       else printf("%d not found in the list..!",x);
     }
   return list;
  }
void display(nodeptr list)
 {
  nodeptr p;
  if(list==NULL)
   {
    printf("List empty..!");
    return;
   }
  else
   {
   printf("\nLIST ELEMENTS\n-------------");
   for(p=list;p!=NULL;p=p->right)
    {
      printf("\n%d",p->info);
    }
   }
 }
nodeptr del_f(nodeptr list)
 {
  nodeptr p;
  int x;
  if(list==NULL)
   {
    printf("\nInvalid operation..!");
    return 0;
   }
  else
   {
    p=list;
    list=list->right;
    list->left=NULL;
    x=p->info;
    freenode(p);
    printf("%d deleted form list..!",x);
   }
  return list;
 }
nodeptr del_end(nodeptr list)
 {
  nodeptr p,q;
  int x;
  if(list==NULL)
   {
    printf("\nInvalid opertion...!");
    return 0;
   }
  else
   {
    q=NULL;
    for(p=list;p->right!=NULL;p=p->right)
     q=p;
    x=p->info;
    q->right=NULL;
    freenode(p);
   }
  printf("%d deleted from the list..!",x);
  return list;
 }
nodeptr del_b4x(nodeptr list,int x)
 {
  nodeptr p,q,r;
  int y,flag=0;
  if(list==NULL||list->info==x)
   {
    printf("Invalid opertaion..!");
    return 0;
   }
  else
   {
    q=NULL;
    for(p=list;p!=NULL;p=p->right)
     {
     if((list->right)->info==x)
       list=list->right;
     if(p->info==x)
      {
       flag=1;
       break;
      }
     q=p;
     }
     if(flag)
      {
       printf("\nElement %d found in the list!!",x);
r=q->left;
r->right=p;
p->left=r;
y=q->info;
freenode(q);
printf("\n%d deleted form the list..!",y);
      }
     else
      printf("\nElement not found in the list..!");
      return list;
     }
    }
main()
 {
  int ele,c1,c2,x;
  nodeptr list;
  clrscr();
  list=NULL;
  while(1)
   {
     printf("\n1 : Insert node\n2 : Delete node\n3 : Display list\n4 : Exit");
     printf("\nEnter choice : ");
     scanf("%d",&c1);
     switch(c1)
      {
       case 1:printf("\n1 : Insert from front\n2 : Insert from rear\n3 : Insert after element X\n");
     printf("Enter your option : ");
     scanf("%d",&c2);
     printf("Enter the element to be inserted : ");scanf("%d",&ele);
     switch(c2)
      {
case 1:list=ins_f(list,ele);
      break;
case 2:list=ins_end(list,ele);
      break;
case 3:printf("\nEnter the element X : ");scanf("%d",&x);
      list=ins_aftx(list,ele,x);
      break;
}
      break;
       case 2:printf("1 : Delete from front\n2 : Delete from rear\n3 : Delete before element X\n");
     printf("Enter your option : ");scanf("%d",&c2);
     switch(c2)
      {
case 1:list=del_f(list);
      break;
case 2:list=del_end(list);
      break;
case 3:printf("\nEnter the element x : ");
      scanf("%d",&x);
      list=del_b4x(list,x);
      break;
}
     break;
       case 3:display(list);
     break;
       default:exit(0);
      break;
      }
   }
    getch();
    return 0;
 }


Diamond string



This is a C program to print your entered string in DIAMOND shape..... Check it!
The above shown output is expected..


#include"stdio.h" /*Pre processor directives */
#include"conio.h"
main()
 {
  int i,j,l=0,k;
  char str[20];
  void fun(char *);
  clrscr();
  printf("Enter the string : ");
  gets(str);
  for(i=0;str[i]!='\0';i++)
   l++;
  for(i=0;i<l;i++)
   {
    k=l-i;
    for(j=0;j<k;j++)
     printf("  ");
    for(j=0;j<=i;j++)
     {
      fun(&str[j]);
     }
    printf("\n");
    }
   for(i=l-1;i>0;i--)
    {
     k=l-i;
     for(j=0;j<=k;j++)
      printf("  ");
     for(j=i;j>0;j--)
      fun(&str[l-j]);
     printf("\n");
     }
   getch();
   return 0;
  }
void fun(char *p)
 {
   printf("  %c ",*p);
 }