Thursday, March 31, 2016

C program to check enter number is perfect number or not

#include<stdio.h>

void main()
 {
   int n,m,sum=0,a;
  int i;
  printf("Enter a no");
  scanf("%d",&n);
  m=n;

  for(i=1;i<n;i++)
   {
    if(n%i==0)
     {
      sum=sum+i;
     }
   }
   if(sum==m)
    {
     printf("Perfect no");
    }
   else
    {
     printf("Not a perfect no");
    }

}
output
Enter a no 6
Perfect no

C program to implement link list using malloc

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct node{
int info;
struct node *link;
}*start;
void addfirst()
 {
  struct node *ptr;
  int n;
  ptr=start;
  struct node *nw;
  nw=(node *)malloc(sizeof(node));
  printf("Enter item to insert");
  scanf("%d",&nw->info);
  if(nw==NULL)
   {
    printf("Overflow");
    exit(0);
   }
  if(start==NULL)
   {
    nw->link=NULL;
    start=nw;
   }
  else
   {
    nw->link=start;
    start=nw;
   }
 }
void addlast()
 {
  int n;
  struct node *nw,*ptr;
  nw=(node *)malloc(sizeof(node));
  if(nw==NULL)
   {
    printf("Overflow");
    exit(0);
   }
  printf("Enter data to enter");
  scanf("%d",&nw->info);
  ptr=start;
  while(ptr->link!=NULL)
   ptr=ptr->link;
  nw->link=NULL;
  ptr->link=nw;
 }
void addafter()
 {
  int n,pos;
  struct node *nw,*ptr;
  nw=(node *)malloc(sizeof(node));
  if(nw==NULL)
   {
    printf("overflow");
    exit(0);
   }
  printf("Enter data to insert");
  scanf("%d",&nw->info);
  printf("\n\nEnter node no afterwhich you want to insert node");
  scanf("%d",&pos);
  ptr=start;
  for(n=1;n<pos;n++)
   ptr=ptr->link;
  nw->link=ptr->link;
  ptr->link=nw;
 }
void deletenode()
 {
  int n;
  printf("Enter position of the node you want to delete\n\n");
  scanf("%d",&n);
  struct node *ptr;
  ptr=start;
  for(int i=1;i<n-1;i++)
   ptr=ptr->link;
  if(i==1)
   {
    start=start->link;
   }
  else
   {
    ptr->link=ptr->link->link;
   }
 }
void display()
 {
  struct node *ptr;
  ptr=start;
  while(ptr!=NULL)
   {
    printf(" ->%d",ptr->info);
    ptr=ptr->link;
   }
 }
void main()
{
 int n;
 start=0;
 do
 {
  printf("\n\n1.ADDFIRST\n\n2.ADDAFTER\n\n3.ADDLAST\n\n4.DISPLAY\n\n5.DELETE\n\n6.EXIT\n\n");
  scanf("%d",&n);
  if(n==1)
   {
    addfirst();
   }
  if(n==2)
   {
    addafter();
   }
  if(n==3)
   {
    addlast();
   }
  if(n==4)
   {
    display();
   }
  if(n==5)
   {
    deletenode();
   }
 }while(n!=6);
}
output
1. ADDFIRST
2.ADDAFTER
2.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
1
Enter item to insert 5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
3
Enter data to enter
5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
4
->3->5

Wednesday, March 30, 2016

C program to print a 2D matrix in a linear view

#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int i,j;
 int a[5][5]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
 for(i=0;i<5;i++)
  {
   for(j=0;j<5;j++)
    {
     printf("  %d",a[i][j]);
    }
   printf("\n\n");
  }
 for(i=0;i<5;i++)
  {
   for(j=0;j<5;j++)
    {
     printf("  %d",a[j][i]);
    }
   i++;
   printf("\n\n");
   if(i<5)
    {
     for(j=4;j>=0;j--)
      {
       printf("  %d",a[j][i]);
      }
    }
   printf("\n\n");
  }
 getch();
}
output

C program to print all the posstion permutations of the string

#include<stdio.h>
#include<conio.h>
#include<string.h>
int factorial(int x)
 {
  if(x==1||x==0)
   {
    return 1;
   }
  else
   {
    return x*factorial(x-1);
   }
 }
void main()
{
 clrscr();
 char str[]="ABCD";
 int i,j,k,l,count=0;
 char temp;
 /*for(i=0;i<4;i++)
  {
   for(j=0;j<3;j++)
    {
     for(k=0;k<2;k++)
      {
       for(l=0;l<1;l++)
{
printf("%d %d \n",i,j);
count=count+1;
}
      }
    }
  }*/
  l=strlen(str);
  j=0;
  k=1;
  for(i=0;i<factorial(l);i++)
   {
    if(j==l)
     {
      j=0;
     }
    if(k==l)
     {
      k=0;
     }
    temp=str[j];
    str[j]=str[k];
    str[k]=temp;
    printf("%s\n",str);
    j++;
    k++;
   }
 //printf("count %d",count);
 getch();
}

output

Tuesday, March 29, 2016

C program to implement multiplication between two 2D matrices

#include<stdio.h>
#include<conio.h>
void main()
{
int p,q,m,n,a[20][20],b[20][20],c[20][20],i,j,k,sum;
printf("enter dimension of first matrix");
scanf("%d%d",&m,&n);
printf("enter values of first matrix");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter dimension of second matrix");
scanf("%d%d",&p,&q);
printf("enter elements of 2nd matrix");
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=q;j++)
{
sum=0;
for(k=1;k<=n;k++)
{
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("multiplication is ");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=q;j++)
{
printf("%d ",c[i][j]);
}
}
getch();
}
output
Enter dimension of first matrix 2 3
enter values of first matrix 1 2 3 4 5 6
Enter dimension of second matrix 3 2
enter values of 2nd matix 1 2 3 4 5 6
multiplication is
22 28
49 64

C program to implement merge sort using recursive technique

#include<stdio.h>
#include<conio.h>
void merge_sequence(int [],int,int,int);
void merge(int arr_new[],int p, int q)
{
 int mid;
 if(p<q)
  {
   mid=(q+p)/2;
   merge(arr_new,p,mid);
   merge(arr_new,mid+1,q);
   merge_sequence(arr_new,p,mid,q);
  }
}
void merge_sequence(int arr_temp[],int l, int m,int r)
{
 int n1,n2;
 int i,j,k;
 int l_arr[100],r_arr[100];
 n1=m-l+1;
 n2=r-m;
 for(i=0;i<n1;i++)
  {
   l_arr[i]=arr_temp[l+i];
  }
 for(j=0;j<n2;j++)
  {
   r_arr[j]=arr_temp[m+j+1];
  }
 i=0;
 j=0;
 k=l;
 while(i<n1&&j<n2)
  {
   if(l_arr[i]<=r_arr[j])
    {
     arr_temp[k]=l_arr[i++];
     k=k+1;
    }
   else
    {
     arr_temp[k]=r_arr[j++];
     k=k+1;
    }
  }
 while(i<n1)
  {
   arr_temp[k]=l_arr[i++];
   k=k+1;
  }
 while(j<n2)
  {
   arr_temp[k]=r_arr[j++];
   k=k+1;
  }
}
void main()
{
 clrscr();
 int arr[10]={5,9,7,0,3,8,9,66,90,2};
 int i,j;
 printf("input array :");
 for(i=0;i<10;i++)
  {
   printf(" %d",arr[i]);
  }
 merge(arr,0,9);
 printf("\n");
printf("sorted array:");
 for(i=0;i<10;i++)
  {
   printf(" %d",arr[i]);
  }
 getch();
}

output
input array :5 9 7 0 3 8 9 66 90 2
sorted array :0 2 3 5 7 8 9 9 66 90

Monday, March 28, 2016

C program to find longest palindrome string from a string

#include<stdio.h>
#include<string.h>
int check_pal(char str_check[],int i,int j)
{
 int k,flag=0;
 for(k=0;k<=(j-i)/2;k++)
  {
   if(str_check[i+k]!=str_check[j-k])
    {
     flag=1;
     break;
    }
  }
 if(flag==1)
  {
   return -1;
  }
 else
  {
   return j-i+1;
  }
}
void main()
{
 char *str="vhgfhjggjjggknkjnk";
 int i,j,count=0,max=0,start,end,length;
 for(i=0;str[i]!='\0';i++)
  {
   for(j=i+1;str[j]!='\0';j++)
    {
     if(str[i]==str[j])
      {
       length=check_pal(str,i,j);
       if(length>max)
{
start=i;
end=j;
max=length;
}
      }
    }
   }
 printf("Length =%d\n",max);
 printf("Longest substring is as follows ");
 for(i=start;i<=end;i++)
  {
   printf("%c",str[i]);
  }
}

output
Longest substring is as follows ggjjgg

C program to convert prefix expression into postfix notations

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
char operator_stack[50];
int operator_top=-1;
int isoperator(char s)
 {
  if(s=='^'||s=='*'||s=='/'||s=='%'||s=='+'||s=='-')
   {
    return 1;
   }
  else
   {
    return 0;
   }
 }
int isparenthesis(char pa)
 {
  if(pa=='(')
   return (1);
  else if(pa==')')
   return(2);
  else
   return(0);
 }
void operator_push(char n)
{
 if(operator_top==49)
  {
   printf("\Overflow condition");
   exit(0);
  }
 operator_stack[++operator_top]=n;
}
int priority(char p)
 {
  if(p=='^')
   return 3;
  else if(p=='*'||p=='/'||p=='%')
   return 2;
  else if(p=='+'||p=='-')
   return 1;
  else
   return 0;
 }
char operator_pop()
 {
  if(operator_top==-1)
   {
    printf("\nUnderflow condition");
    exit(0);
   }
  return(operator_stack[operator_top--]);
 }
void main()
{
 int x;
 char *exp_infix;
 char postfix[50][50];
 int digit;
 char dig[50];
 int y;
 printf("\nEnter the infix notation");
 scanf("%[^\n]s",exp_infix);
 for(x=0,y=0;exp_infix[x]!='\0';)
  {
   if(isdigit(exp_infix[x]))
    {
     digit=0;
     while((exp_infix[x]>=48)&&(exp_infix[x]<=57))
      {
       dig[digit]=exp_infix[x];
       x++;
       digit=digit+1;
      }
     dig[digit]='\0';
     //puts(dig);
     strcpy(postfix[y],dig);
   //  puts(postfix[y]);
     //postfix[y]=exp_infix[x];
     y++;
    }
    else if(isoperator(exp_infix[x])||isparenthesis(exp_infix[x]))
    {
     if(operator_top==-1||isparenthesis(exp_infix[x])==1)
      {
       operator_push(exp_infix[x]);
       x++;
      }
     else
      {
       if(isparenthesis(exp_infix[x])==2)
{
while(operator_stack[operator_top]!='(')
 {
  digit=0;
  dig[digit]=operator_pop();
  strcpy(postfix[y],dig);
  y++;
 }
operator_pop();
x++;
continue;
}
       if(priority(exp_infix[x])<priority(operator_stack[operator_top]))
{
while((priority(exp_infix[x])<priority(operator_stack[operator_top])&&(operator_top>-1)))
 {
  digit=0;
  dig[digit]=operator_pop();
  digit=digit+1;
  dig[digit]='\0';
  strcpy(postfix[y],dig);
  y++;
 }
operator_push(exp_infix[x]);
x++;
}
else if(priority(exp_infix[x])==priority(operator_stack[operator_top]))
{
  digit=0;
  dig[digit]=operator_pop();
  digit=digit+1;
  dig[digit]='\0';
  strcpy(postfix[y],dig);
  y++;
  operator_push(exp_infix[x]);
  x++;
}
       else if(priority(exp_infix[x])>priority(operator_stack[operator_top]))
{
operator_push(exp_infix[x]);
x++;
}
      }
    }
  }
while(operator_top!=-1)
   {
    digit=0;
    dig[digit]=operator_pop();
    digit++;
    dig[digit]='\0';
    strcpy(postfix[y],dig);
    y++;
   }
  //postfix[y]='\0';
  for(x=0;x<y;x++)
   {
    printf("%s",postfix[x]);
   }
getch();
}

output:
Enter the infix notation 5*2+6
652*+

Wednesday, March 23, 2016

C program to print a flag.

#include<graphics.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
//5 is the color code
setcolor(5);
line(20,50,20,180);
line(20,50,100,50);
line(20,70,100,70);
line(20,90,100,90);
line(20,110,100,110);
line(100,50,100,110);
circle(60,80,10);
line(52,80,68,80);
line(60,72,60,88);
setfillstyle(SOLID_FILL,22);
floodfill(21,51,5);
setfillstyle(SOLID_FILL,7);
floodfill(21,75,5);
setfillstyle(SOLID_FILL,2);
floodfill(21,95,5);
setfillstyle(SOLID_FILL,7);
floodfill(81,75,5);
setfillstyle(SOLID_FILL,7);
floodfill(61,81,5);
closegraph();
}

output

Wednesday, March 16, 2016

C program to print n terms of fabonacci series using recursion

#include<stdio.h>
int fab(int i) //calculate fabonacci term for perticular index
 {
  if(i==0)
   {
    return 0;
   }
  else if(i==1)
   {
    return 1;
   }
  else
   return fab(i-1)+fab(i-2);
 }
void main()
 {
  int n;
  printf("Enter no of terms");
  scanf("%d",&n);
  int i;
  for(i=0;i<n;i++)
   {
    printf(" %d ",fab(i));
   }
 }

output
Enter no of terms 6
0 1 1 2 3 5

c program to print the names of all the files in current directory

#include<stdio.h>
#include<dirent.h>
void main()
{
  DIR *d;
 struct dirent *dir;
 d=opendir("."); //pointing to current directory
 if(d)
  {
   while((dir=readdir(d))!=NULL)
   {
    printf("\n%s",dir->d_name);
   }
   closedir(d);
  }
 }

output
abc.txt
hello.txt
program1.c
newhello.txt

Tuesday, March 15, 2016

C program to convert a digit into words

#include<stdio.h>
#include<conio.h>
#include<string.h>
char *single_digit[]={"","one","two","three","four","five","six","seven","eight","nine"};
char *two_digit[]={"ten","eleven","twelve","thirteen","fourteen","fiveteen","sixteen","seventeen","eighteen","nineteen"};
char *tens_multiple[]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
char *tens_power[]={"hundred","thousand"};
void one(char *num1)
{
 printf("%s",single_digit[*num1-'0']);
}
void two(char *num2)
{
 if(*num2=='0')
 {
  num2=num2+1;
  one(num2);
 }
 else if(*(num2)=='1')
      {
       printf("%s",two_digit[*(num2+1)-'0']);
      }
     else
      {
       printf("%s ",tens_multiple[*num2-'0']);
       num2=num2+1;
       one(num2);
      }
}
void three(char *num3)
{
 if(*num3=='0')
  {
   num3=num3+1;
   two(num3);
  }
 else
  {
   one(num3);
   printf("%s "," hundred");
   num3=num3+1;
   two(num3);
  }
}
void four(char *num4)
{
 if(*num4=='0')
  {
   num4=num4+1;
   three(num4);
  }
 else
  {
   one(num4);
   printf("%s "," thousand");
   num4=num4+1;
   three(num4);
  }
}
void convert(char *number)
{
 //printf("\nNumber is %s length is %d",number,strlen(number));
 while(*number=='0')
  {
   number=number+1;
  }
  //printf("\nnumber is %s length is %d",number,strlen(number));
  int l=strlen(number);
  //printf("\n first number is %s ",single_digit[0]);
  switch(l)
   {
    case 1:
     one(number);
     break;
    case 2:
     two(number);
     break;
    case 3:
     three(number);
     break;
    case 4:
     four(number);
     break;
    default:
     printf("\nOnly four digit numbers are allowed");
    }
}
void main()
 {
  char *num;
  printf("\n Enter a number ");
  scanf("%s",num);
  //printf("String is %s",num);
  convert(num);
  getch();
 }

output
Enter a number 1230
one thousand two hundred thirty

C program to check whether a matrix exist in another matrix or not

#include<stdio.h>
void main()
{
 int const size =5;
 int i,j;
 int n=5;
 int a[size][size];
 int b[3][3]={{1,2,3},{6,7,8},{11,12,13}};
 int count;
 for(i=0;i<n;i++)
  {
   for(j=0;j<n;j++)
    {
     a[i][j]=n*i+j;
    }
  }
 int k,l;
 int o,p;
 for(i=0;i<n;i++)
  {
   for(j=0;j<n;j++)
    {
     count=0;
     for(o=0;o<3;o++)
      {
       for(p=0;p<3;p++)
{
if(((i+o)<n)&&((j+p)<n))
 {
 if(a[i+o][j+p]==b[o][p])
  {
   count++;
  }
 else
  {
   break;
  }
 }
else
 {
  break;
 }
}
      }
     if(count==9)
      {
       printf("Same pattern in the array found");
      }
    }
  }
}
output
Same pattern in the array found

Sunday, March 13, 2016

C program to implement bubble sort using recursion

#include<stdio.h>
#include<conio.h>
void sort(int arr_new[],int i,int n)
{
 int temp;
 if(i<n-1)
  {
   if(arr_new[i]>arr_new[i+1])
    {
     temp=arr_new[i];
     arr_new[i]=arr_new[i+1];
     arr_new[i+1]=temp;
    }
   sort(arr_new,++i,n);
   sort(arr_new,0,n-i);
  }
}
void main()
{
 clrscr();
 int arr[10]={3,8,7,6,0,77,3,90,3,13};
 int i;
 printf("Array elements before sorting");
 for(i=0;i<10;i++){
  printf(" %d",arr[i]);
 }
 sort(arr,0,10);
 printf("Array elements after sorting");
 for(i=0;i<10;i++)
  {
   printf(" %d",arr[i]);
  }
 getch();
}

output
Array elements before sorting
3 8 7 6 0 77 3 90 3 13
Array elements after sorting
0 3 3 3 6 7 8 13 77 90

C program to implement binary search on array

#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[5][5];
 int i,j;
 int n=5,m=5;
 int mid,low,high,item;
 printf("Enter a number to search");
 scanf("%d",&item);
 printf("\nArray elements");
 for(i=1;i<=n;i++)
  for(j=1;j<=m;j++)
   {
    a[i-1][j-1]=(i-1)*5+j;
    printf("%d ",a[i-1][j-1]);
   }
 low=0;
 high=n*m-1;
 mid=(low+high)/2;
 i=mid/5;
 j=mid%5;
 while((low<high)&&(a[i][j]!=item))
  {
   if(a[i][j]<item)
    {
     low=i*5+j+1;
     mid=(low+high)/2;
     i=mid/5;
     j=mid%5;
    }
   else
    {
     high=i*5+j-1;
     mid=(low+high)/2;
     i=mid/5;
     j=mid%5;
    }
  }
 if(a[i][j]==item)
  {
   printf("\nitem found at %d",i*5+j+1);
  }
 else
  {
   printf("\nitem not fount");
  }
 getch();
}

output

Saturday, March 12, 2016

C program to dyncamically allocate and deallocate memory in an array.

#include<stdio.h>
#include<stdlib.h>
void main()
{
  int *a,*b;
 int x,y,i;
 printf("Please enter the size of array");
 scanf("%d",&x);
 a=(int*)malloc(x*sizeof(int));
 printf("\n Enter %d values in array",x);
 for(i=0;i<x;i++)
  {
   scanf("%d",(a+i));
  }
 printf("\nValue in the array is");
 for(i=0;i<x;i++)
  {
   printf("%d ",*(a+i));
  }
 free(a);
 }

output

C program to check the value is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
 {
  clrscr();
  int n,m,sum=0,a;
  printf("Enter a no");
  scanf("%d",&n);
//a integer number is palindrome if original number and number formed by reversing the sequence of //digits are equal
  m=n;
  while(n>0)
   {
    a=n%10;
    sum=sum*10+a;
    n=n/10;
   }
  if(sum==m)
   {
    printf("No is palindrome");
   }
  else
   {
    printf("No is not palindrome");
   }
 getch();
}

output
Enter a no 242
No is palindrome

C program to find first non-repeating alphabet in striing of alphabets.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
  clrscr();
  char name[50]="abaaccsbvv";
  int count[200]={0};
  int i=0;
  int flag=0;
  for(i=0;i<strlen(name);i++)
   {
    count[name[i]]=count[name[i]]+1;
   }
  //printf("%d",strlen(name));
  for(i=0;i<strlen(name);i++)
   {
    if(count[name[i]]==1)
     {
      printf("first non repeated alphabet is %c ",name[i]);
      flag=1;
      break;
     }
    //printf("%d",count[name[i]]);
   }
  if(flag==0)
   {
    printf("all alphabets are repeating");
   }
  getch();
 }

output
first non repeated alphabet is s

C ++ program for bank account management

#include<iostream.h>
#include<conio.h>
 class account
{
char c_name[20];
int acc_no;
char acc_type;
public:
float balance;
void get_balance(int a)
{
   balance=a;
   penalty();
}
void put_balance()
{
cout<<"\nyour current balance is: "<<balance;
}
void debit()
{
int deb;
cout<<"\nenter ammount to be withdrawl: ";
cin>>deb;
balance=balance-deb;
put_balance();
penalty();
}

void credit()
 {
 int cre;
   cout<<"\nenter ammount to be credited: ";
  cin>>cre;
   balance=balance+cre;
   put_balance();
 }
/*void intrest()
{
 int intr;
 intr=balnce
 balance=balance+intr;
 cout<<"\ntotal intrest added:"<<intr;
 put_balance();
} */
void penalty()
{
 float pen=0;
 float f=0.05;
 if(balance<500)
 {
 cout<<"\nyour balance is less then 500";
 pen=balance*f;
 balance=balance-pen;

 cout<<"\nyour penalty amount is: "<<pen;
 put_balance();
 }
 else
 cout<<"\nno penalty";
}
};
void main()
{
clrscr();
account acc;
acc.get_balance(5000);
acc.put_balance();
acc.credit();
acc.debit();
getch();
}

output

Friday, March 11, 2016

C program to show add, append, sort, delete, display, search operation on array

#include<iostream.h>
#include<conio.h>
class List
 {
  int a[50];
  int c;
  public:
   void add(int x);
   void deleteall();
   int delete_index(int index);
   void delete_number(int x);
   int search(int x);
   void insert(int x,int index);
   void sort();
   void append(int x);
   void display();
   int get(int index);
   List();
 };
void List::List()
 {
  c=-1;
 }
void List::add(int x)
 {
  c++;
  a[c]=x;
 }
void List::deleteall()
 {
  int i;
  for(i=0;i<=c;i++)
   {
    a[i]=NULL;
   }
  c=-1;
 }
int List::delete_index(int index)
 {
  if(index>c)
   {
    cout<<"\nIndex is out of bound";
    return -1;
   }
  else
   {
    int r=a[index];
    int i;
    for(i=index;i<c;i++)
     {
      a[i]=a[i+1];
     }
    c--;
    return r;
   }
 }
void List::delete_number(int x)
 {
  int i;
  for(i=0;i<=c;i++)
   {
    if(a[i]==x)
     {
      break;
     }
   }
  if(i<=c)
   {
    for(int k=i;k<c;k++)
     {
      a[k]=a[k+1];
     }
     c--;
     cout<<"\nNumber deleted at index "<<i;
   }
  else
   {
    cout<<"\nEntered number not present";
   }
 }
int List::search(int x)
 {
  int i;
  for(i=0;i<=c;i++)
   {
    if(a[i]==x)
     {
      break;
     }
   }
  if(i<=c)
   {
    return i;
   }
  else
   {
    return -1;
   }
 }
void List::insert(int x,int index)
 {
  int i;
  if(index>(c+1))
   {
    cout<<"\nList is shorter";
   }
  else
   {
    for(i=c;i>=index;i--)
     {
      a[i+1]=a[i];
     }
    a[index]=x;
    c++;
   }
 }
void List::sort()
 {
  int i;
  int j;
  int temp;
  for(i=0;i<c;i++)
   {
    for(j=0;j<c-i;j++)
     {
      if(a[j]>a[j+1])
       {
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
       }
     }
   }
 }
void List::append(int x)
 {
  c++;
  a[c]=x;
 }
void List::display()
 {
  int i;
  for(i=0;i<=c;i++)
   {
    cout<<"\t"<<a[i];
   }
 }
int List::get(int index)
 {
  return (a[index]);
 }
void main()
 {
  int ch;
  int no,in,x;
  List List_object;
  do
   {
    cout<<endl<<"1.Add an item\n 2.Delete All \n3.Delete at perticular index\n4.Delete perticular number \n5.Search a number \n6.Insert a number at perticular index \n7.Sort List \n8.Append an item \n9.Display list \n10.Get item at perticular index";
    cin>>ch;
    switch(ch)
     {
      case 1:
       cout<<"\nEnter a number";
       cin>>no;
       List_object.add(no);
       break;
      case 2:
       List_object.deleteall();
       break;
      case 3:
       cout<<"\nEnter Index to delete";
       cin>>in;
       x=List_object.delete_index(in);
       cout<<"\nDeleted number="<<x;
       break;
      case 4:
       cout<<"\nEnter number to delete";
       cin>>no;
       List_object.delete_number(no);//Bool
       break;
      case 5:
       cout<<"\nEnter number to search";
       cin>>no;
       x=List_object.search(no);
       if(x==-1)
{
cout<<"\nNumber not present";
}
       else
{
cout<<"\nNumber present at "<<x;
}
       break;
      case 6:
       cout<<"\nEnter no and index";
       cin>>no>>in;
       List_object.insert(no,in);
      break;
     case 7:
       List_object.sort();
       List_object.display();
       break;
     case 8:
       cout<<"\nEnter a no to append";
       cin>>no;
       List_object.append(no);
       break;
     case 9:
       List_object.display();
       break;
     case 10:
       cout<<"\nEnter index to find number";
       cin>>in;
       x=List_object.get(in);
       cout<<"\nNumber is "<<x;
     }
   }while(ch!=11);
  getch();
 }

output
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 1
 Enter a number 6
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 8
 Enter a no to append 2
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index
 9
 6 2
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index 
 5 
 Enter number to search 8
 Number not present
 1.Add an item
 2.Delete All 
 3.Delete at perticular index
 4.Delete perticular number 
 5.Search a number 
 6.Insert a number at perticular index 
 7.Sort List 
 8.Append an item 
 9.Display list 
 10.Get item at perticular index 
 11

Thursday, March 10, 2016

C ++ program to implement time operations

#include<iostream.h>
#include<conio.h>
#include<process.h>
class Time
{
 private:
  int hours;
  int minutes;
  int seconds;
 public:
  Time();
  void addhour(int);
  void addminute(int);
  void addsecond(int);
  void displaytime();
  void addtime();
  int gethour();
  int getminute();
  int getsecond();
  void sethour(int);
  void setminute(int);
  void setsecond(int);
};
Time::Time()
 {
  sethour(0);
  setminute(0);
  setsecond(0);
 }
void Time::addhour(int h)
 {
  h=gethour()+h;
  if(h>24)
   {
    h=h%24;
   }
  sethour(h);
 }
int Time::gethour()
 {
  return hours;
 }
void Time::sethour(int h)
 {
  hours=h;
 }
void Time::addminute(int m)
 {
  m=getminute()+m;
  if(m>60)
   {
    m=m%60;
    addhour(m/60);
   }
  setminute(m);
 }
int Time::getminute()
 {
  return minutes;
 }
void Time::setminute(int m)
 {
  minutes=m;
 }
void Time::addsecond(int s)
 {
  s=getsecond()+s;
  if(s>60)
   {
    s=s%60;
    addminute(s/60);
   }
  setsecond(s);
 }
int Time::getsecond()
 {
  return seconds;
 }
void Time::setsecond(int s)
 {
  seconds=s;
 }
void Time::displaytime()
 {
  cout<<"Time:: ";
  cout.width(2);
  cout.fill('0');
  cout<<gethour()<<":";
  cout.width(2);
  cout.fill(2);
  cout<<getminute()<<":";
  cout.width(2);
  cout.fill('0');
  cout<<getsecond();
 }
void Time::addtime()
 {
  int h,m,s;
  cout<<"Enter hours, minutes and seconds to add in time";
  cin>>h>>m>>s;
  addsecond(s);
  addminute(m);
  addhour(h);
 }
void main()
 {
  int i;
  Time t;
  int h;
  int m;
  int s;
  do
   {
    cout<<endl<<"Enter 1.Add hours \n2.Add minutes \n3.Add seconds \n4.Add time \n5.Show time \n6.Exit";
    cout<<endl<<"Enter your choice";
    cin>>i;
    switch(i)
     {
      case 1:
       cout<<endl<<"How many hours to add";
       cin>>h;
       t.addhour(h);
       break;
      case 2:
       cout<<endl<<"How many minutes to add";
       cin>>m;
       t.addminute(m);
       break;
      case 3:
       cout<<endl<<"How many seconds to add";
       cin>>s;
       t.addsecond(s);
       break;
      case 4:
       t.addtime();
       break;
      case 5:
       t.displaytime();
       break;
      case 6:
       exit(0);
      default:
       cout<<endl<<"Please enter a correct choice";
     }
   }while(i!=6);
 }

 output
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 1
 How many hours to add 3
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 5
 Time::04:00:00
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 2
 How many minutes to add 5
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 Time::04:05:00
 Enter 1.Add hours
 2.Add minutes
 3.Add seconds
 4.Add time
 5.Show time
 6.Exit
 6



Wednesday, March 9, 2016

C program for queue implementation using link list

#include <stdio.h>
#include <stdlib.h>
//structure for the link list
struct node
{
    int info;
    struct node *ptr;
}*front,*rear,*temp,*front1;
//enq for insertion in queue
void enq(int data);
//deq for deletion from queue
void deq();
//for displaying queue data
void display();
//called once for the initialization
void create();
int count = 0;
void main()
{
    int no, ch, e;
    printf("\n 1 - Enque");
    printf("\n 2 - Deque");
    printf("\n 3 - Display");
    printf("\n 4 - Exit");
    create();
    while (1)
    {
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
   printf("Enter data : ");
   scanf("%d", &no);
   enq(no);
   break;
case 2:
   deq();
   break;
case 3:
   display();
   break;
case 4:
   exit(0);
   break;
default:
   printf("Wrong choice, Please enter correct choice  ");
   break;
}
    }
}
void create()
{
    front = rear = NULL;
}
void enq(int data)
{
    if (rear == NULL)
    {
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
    }
    else
    {
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
    }
    count++;
}
void display()
{
    front1 = front;
    if ((front1 == NULL) && (rear == NULL))
    {
printf("Queue is empty");
return;
    }
    while (front1 != rear)
    {
printf("%d ->", front1->info);
front1 = front1->ptr;
    }
    if (front1 == rear)
printf("%d->", front1->info);
}
void deq()
{
    front1 = front;
    //check if the queue is empty
    if (front1 == NULL)
    {
printf("\n Error: Trying to display elements from empty queue");
return;
    }
    else
if (front1->ptr != NULL)
{
   front1 = front1->ptr;
   printf("\n Dequed value : %d", front->info);
   free(front);
   front = front1;
}
else
{
   printf("\n Dequed value : %d", front->info);
   free(front);
   front = NULL;
   rear = NULL;
}
count--;
}

output
1 - Enque
2 - Deque
3 - Display
4 - Exit
Enter choice : 1
Enter data : 4
Enter choice :1
Enter data  8
Enter choice :3
4->3->
Enter choice :2
Dequed value : 4
Enter choice :1
Enter data : 5
Enter choice :3
3->5->
Enter choice :4

C program for creating, deleting and display records of an employee dynamically

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 200
struct emp
{
    int id;
    char *name;
}*emp1, *emp3;
void display();
void create();
FILE *fp, *fp1;
int count = 0;
void main()
{
    int i, n, ch;
    printf("1 Create a Record\n");
    printf("2 Display Records\n");
    printf("3 Exit");
    while (1)
    {
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
           //opening file into append mode
   fp = fopen("emp.txt", "a");
   create();
   break;
case 2:
           //opening file into read binary mode
   fp1 = fopen("emp.txt","rb");
   display();
   break;
case 3:
          //passing 0 to exit means success-convention
           exit(0);
       default:
           printf("please enter a correct choice");
}
    }
}
void create()
{
    int i;
    char *p;
    emp1 = (struct emp *)malloc(sizeof(struct emp));
    emp1->name = (char *)malloc((size)*(sizeof(char)));
    printf("Enter name of employee : ");
    scanf(" %[^\n]s", emp1->name);
    printf("Enter emp id : ");
    scanf(" %d", &emp1->id);
    fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
    fwrite(emp1->name, size, 1, fp);
    count++;
    fclose(fp);
}
void display()
{
    int i=1;
    emp3=(struct emp *)malloc(1*sizeof(struct emp));
    emp3->name=(char *)malloc(size*sizeof(char));
    if (fp1 == NULL)
    printf("\nFile not opened for reading"); //if there is some error in opening file
    while (i <= count)
    {
fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name, size, 1, fp1);
printf("\n%d %s",emp3->id,emp3->name);
i++;
    }
    fclose(fp1);
    free(emp3->name);
    free(emp3);
}


output:
 1 Create a Record
 2 Display Records
 3 Exit
Enter your choice :1
Enter name of employee :emp1
Enter emp id :1

Enter your choice :1
Enter name of employee :emp2
Enter emp id :2

Enter your choice :2
1 emp1
2 emp2

Enter your choice :3