Friday, April 29, 2016

C program to sort array using bubble sort

#include<stdio.h>
#include<string.h>
void main()
{
 int a[10]={1,8,6,0,6,68,44,66,45,38};
 int i,temp,k,n=10;
 for(i=0;i<n;i++)
  {
   for(k=0;k<n-i-1;k++)
    {
     if(a[k]>a[k+1])
      {
       temp=a[k];
       a[k]=a[k+1];
       a[k+1]=temp;
      }
    }
  }
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
}

output
0 1 6 6 8 38 44 45 66 68

Wednesday, April 27, 2016

c program to print matrix after rotation

#include<stdio.h>
void main()
{
 int arr[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
 int arr_new[4][4];
 int i,j;
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     printf(" %d ",arr[i][j]);
    }
   printf("\n");
  }
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     arr_new[j][4-i-1]=arr[i][j];
    }
  }
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     printf(" %d ",arr_new[i][j]);
    }
   printf("\n");
  }
}
output

Sunday, April 24, 2016

c program to count number of alphabets, digits and spaces in a given file

#include<stdio.h>
//included process.h for using the function exit(0)
#include<process.h>
void main()
{
 FILE *fp;
 char c;
//a will stand for number of alphabets
//d for digits
//s for spaces
 int a=0,d=0,s=0;
//this is the name of file to read
 fp=fopen("emp1.txt","r");
 if(fp==NULL)
  {
   printf("Not open");
//it will exit the program
   exit(0);
  }
 while((c=fgetc(fp))!=EOF)
  {
//we are recognizing alphabets based on thier ascii codes
   if((c>=65&&c<=90)||(c>=97&&c<=122))
    a++;
   else if(c>=48&&c<=58)
    d++;
   else
    s++;
  }
 printf("alphabets=%d,digits= %d ,spaces=%d",a,d,s);
}

output
alphabets=2, digits =5, spaces=8

Thursday, April 21, 2016

C program to convert an infix expression into postfix and calculate result using postfix notations

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
#include<math.h>
#define maxsize 50
int top=-1;
int stack[maxsize];
int y;
char postfix[50][50];
void push(int digit)
 {
  if(top==maxsize-1)
   {
    printf("\nOverflow condition");
    exit(0);
   }
  top++;
  stack[top]=digit;
 }
int pop()
{
 if(top==-1)
  {
   printf("\nUnderflow Condition");
   exit(0);
  }
 return(stack[top--]);
}
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 calculate_postfix()
{
 int i,j,k;
 int res;
 for(i=0;i<y;i++)
  {
   if(isdigit(postfix[i][0]))
    {
     int digit;
     digit=0;
     for(int c=0;postfix[i][c]!='\0';c++)
      {
       digit=digit*10+(postfix[i][c]-48);
      }
     push(digit);
    }
   else if(isoperator(postfix[i][0]))
    {
     k=pop();
     j=pop();
     switch(postfix[i][0])
     {
      case '^':
       res=pow(j,k);
       break;
      case '*':
       res=j*k;
       break;
      case '/':
       res=j/k;
       break;
      case '%':
       res=j%k;
       break;
      case '+':
       res=j+k;
       break;
      case '-':
       res=j-k;
       break;
     }
     push(res);
     }
  }
 printf("\nResult=%d",stack[top]);
}

void infix_to_postfix1()
{
 int x;
 char *exp_infix;
 //char postfix[50][50];
 int digit;
 char dig[50];
 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';
     strcpy(postfix[y],dig);
     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();
  digit=digit+1;
  dig[digit]='\0';
  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++;
   }
  for(x=0;x<y;x++)
   {
      printf("%s",postfix[x]);
   }
  calculate_postfix();
}
void main()
{
 infix_to_postfix1();
 getch();
}
output
Enter the infix notation 5+7
infix notation 57+
result 12

Friday, April 15, 2016

C program to sort an array using bubble sort using recursive technique

Problem Statement:- we can an array of some integers/float datatype and we need to sort that
array using bubble sort recursively.
Bubble Sort- in bubble what we do is compare first two elements and sort them in sequence and then compare element number two and three and sort them we countinue this upto last element and we get largest element in last position in our first pass in next pass what we do is continue to do above process for the n-1 elements and we get second largest element in its place as we continue out sort and in nth pass we get our list sorted.
Recursive - the function which calls itself

#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 813 77 90

Monday, April 11, 2016

C program to find fabonacci series upto n using recursive technique

#include<stdio.h>
int fab(int i)
 {
  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 reverse the given array in minimum possible complexity

#include<stdio.h>
void main()
{
 int a[10]={7,9,6,8,3,9,6,1,9,5};
 int i;
 int n=10;
 int temp;
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
 for(i=0;i<n/2;i++)
  {
   temp=a[n-i-1];
   a[n-i-1]=a[i];
   a[i]=temp;
  }
 printf("\n");
 printf("Reverse array: ");
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
}

output
7 9 6 8 3 9 6 1 9 5
Reverse array: 5 9 1 6 9 3 8 6 9 7

Friday, April 8, 2016

C program to print an array from 45 degree view

#include<stdio.h>
void main()
{
 int arr[5][4];
 int i,j,k,l;
 for(i=0;i<5;i++)
  for(j=0;j<4;j++)
   arr[i][j]=i*4+j+1;
 for(i=0;i<5;i++)
 {
  for(j=0;j<4;j++)
   {
    printf(" %d ",arr[i][j]);
   }
  printf("\n");
 }
 for(i=0;i<5;i++)
  {
   for(j=0,k=i;j<4&&k>=0;j++,k=k-1)
    {
     printf(" %d ",arr[k][j]);
    }
   printf("\n");
  }
 for(k=1;i<5+4-1;i++,k++)
  {
   for(j=k,l=0;j<4;j++,l++)
    {
     printf(" %d ",arr[i-k-l][j]);
    }
   printf("\n");
  }
}
output

C program to calculate square of a number using recursive function

#include<stdio.h>
int square(int f)
 {
  static int count=0;
  count++;
  printf("%d ",count);
  if(f==1||(count==f))
   return f;
  else
   return (f+square(f));
 }
void main()
{
 int n=4;
 int j=square(n);
 printf("square=%d",j);
}

output
square=16

Wednesday, April 6, 2016

C program to implement link list with add first, add after, delete , reverse and display features

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *start,*newptr;
struct node *create_new_node(int i);
void insertend(struct node *n);
void insertmid(struct node *o);
void insertbeg(struct node *m);
void reverse(int c);
void del(int d);
void display(struct node *x);
int count(struct node *c);
void main()
{
int info,a,c;
struct node *np;
char ch;
start=NULL;
clrscr();
do
{
printf("ENTER YOUR CHOICE:\n");
printf("Enter 1 to insert at begining\n");
printf("Enter 2 to insert after a given element\n");
printf("Enter 3 to insert at end\n");
printf("Enter 4 to delete a particular element\n");
printf("Enter 5 to reverse the current linked list\n");
scanf("%d",&a);
if(a==1)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertbeg(np);
}
else if(a==2)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertmid(np);
}
else if(a==3)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
insertend(np);
}
else if(a==4)
{
printf("Enter the element\n");
scanf("%d",&info);
np=create_new_node(info);
del(info);
}
else if(a==5)
{
reverse(c);
}
display(start);
c=count(start);
printf("The number of nodes are %d\n",c);
printf("\nDo you want to proceed? y/n\n");
scanf(" %c",&ch);
}
while(ch=='y');
getch();
}
/*CREATION OF NEW NODE*/
struct node *create_new_node(int i)
{
newptr=(struct node *)malloc(sizeof(struct node));
newptr->info=i;
newptr->link=NULL;
return newptr;
}
/*INSERTION AT BEGINING*/
void insertbeg(struct node *m)
{
if(start==NULL)
{
start=m;
}
else
{
m->link=start;
start=m;
}
}
/*INSERTION AT END*/
void insertend(struct node *n)
{
struct node *temp;
if(start==NULL)
{
start=n;
}
else
{
temp=start;
while(temp->link!=NULL)
temp=temp->link;
temp->link=n;
}
}
/*INSERTION AFTER A GIVEN ELEMENT*/
void insertmid(struct node *o)
{
int value;
struct node *temp;
temp=start;
printf("Enter the number after which ");
printf("new element has to be inserted:\n");
scanf("%d",&value);
while(temp!=NULL)
{
if(temp->info==value)
{
o->link=temp->link;
temp->link=o;
}
temp=temp->link;
}}
/*DELETE*/
void del(int d)
{
struct node *temp,*ptr;
temp=start;
while(temp!=NULL)
{
if(temp->link->info==d)
{
ptr=temp->link;
temp->link=ptr->link;
free(ptr);
}
temp=temp->link;
}
}
/*REVERSE*/
void reverse(int c)
{
struct node *p1,*p2,*p3;
if(start->link==NULL)
return;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}
/*DISPLAY*/
void display(struct node *x)
{
printf("\nCurrent linked list is:\t");
do
{
printf("%d\t",x->info);
x=x->link;
}
while(x!=NULL);
printf("\n");
}
/*COUNT THE NO. OF NODES*/
int count(struct node *c)
{
int co=0;
do
{
co++;
c=c->link;
}
while(c!=NULL);
return co;
}

output
ENTER YOUR CHOICE:
Enter 1 to insert at begining
Enter 2 to insert after a given element
Enter 3 to insert at end
Enter 4 to delete a particular element
Enter 5 to reverse the current linked list
1
Enter the element 2
Current linked list is:2
Do you want to proceed? y/n y
ENTER YOUR CHOICE:
Enter 1 to insert at begining
Enter 2 to insert after a given element
Enter 3 to insert at end
Enter 4 to delete a particular element
Enter 5 to reverse the current linked list
1
Enter the element 5
Current linked list is:5 2
Do you want to proceed? y/n y
ENTER YOUR CHOICE:
Enter 1 to insert at begining
Enter 2 to insert after a given element
Enter 3 to insert at end
Enter 4 to delete a particular element
Enter 5 to reverse the current linked list
4
Enter the element 2
Current linked list is:5
Do you want to proceed? y/n n

Tuesday, April 5, 2016

C++ program to implement swapping of two numbers

#include<iostream.h>
class a
{
public:
int a,b,c;
void swap();
};
void a::swap()
{

 c=a;a=b;b=c;
cout<<"\n after swapping\n";
cout<<a<<" "<<b;
}
void main()
{
 a obj;
 cout<<"enter two nos.\n";
 cin>>obj.a>>obj.b;
 cout<<"before swapping\n";
 cout<<obj.a<<" "<<obj.b;
 obj.swap();
 }
output
enter two nos 5 8
before swapping
5 8
after swapping 5 8

C program to shift all the negative values in left in array

#include<stdio.h>
void main()
{
 clrscr();
 int n;
 int a[10]={4,8,6,-9,0,6,11,-4,3,7};
 int i,j;
 int count=0;
 int temp;
 for(i=0;i<10;i++)
  {
   printf("%d ",a[i]);
  }
 if(a[0]<0)
  {
   count=1;
  }
 for(i=1;i<10;i++)
  {
   if(a[i]<0)
    {
     for(j=i;j>count;j--)
      {
       temp=a[j];
       a[j]=a[j-1];
       a[j-1]=temp;
      }
     count=count+1;
    }
  }
 printf("\n");
 for(i=0;i<10;i++)
  {
   printf("%d ",a[i]);
  }
}

output
-9 -4 4 8 6 0 6 11 3 7

Monday, April 4, 2016

C program to find prime factors of the given number

#include<stdio.h>

void main()
 {
  int n,m,sum=0,a;
  int i;
  int count;
  int j;
  printf("Enter a no");
  scanf("%d",&n);
  m=n;
  for(i=2;i<=n/2;i++)
   {
    a=0;
    if(n%i==0)
     {
      count=0;
      for(j=2;j<=i/2;j++)
       {
if(i%j==0)
{
 count++;
 break;
}
}
if(count==0)
{
 printf("Prime factor is %d \n",i);
}
}
      }
}

output
Enter a no 50
Prime factor is 2
Prime factor is 5  

C program to implement sorting in link list

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node* ptr;
}*START;
void create()
{
 int n;
 struct node *temp;
 temp=(struct node*)malloc(sizeof(node));
 if(temp==NULL)
  {
   printf("Memory not available");
  }
 printf("Enter data to enter");
 scanf("%d",&n);
 temp->info=n;
 temp->ptr=NULL;
 if(START==NULL)
  {
   START=temp;
  }
 else
  {
   struct node *t =START;
   while(t->ptr!=NULL)
    {
     t=t->ptr;
    }
   t->ptr=temp;
  }
}
void display()
{
 struct node *t=START;
 printf("\n");
 while(t!=NULL)
  {
   printf("%d ",t->info);
   t=t->ptr;
  }
}
void sort()
{
 struct node *t1=START;
 struct node *t2;
 int count=0;
 int i=0,j;
 while(t1!=NULL)
 {
  count++;
  t1=t1->ptr;
 }
 for(i=0;i<count;i++)
  {
   t1=START;
   for(j=0;j<count-i-1;j++)
    {
      if(t1->info>(t1->ptr)->info)
       {
int temp=t1->info;
t1->info=(t1->ptr)->info;
(t1->ptr)->info=temp;
       }
       t1=t1->ptr;
    }
  }
}
void main()
{
 START=NULL;
 int ch;
 do
 {
  printf("\nEnter choice 1.Insert 2.display 3.Sort 4.Exit");
  scanf("%d",&ch);
  switch(ch)
   {
    case 1:
     create();
     break;
    case 2:
     display();
     break;
    case 3:
     sort();
     break;
    case 4:
     exit(0);
     break;
   }
 }while(ch!=4);
}
output

Friday, April 1, 2016

C program to find the digit in a number from msb



#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
 {
   int n,m,sum=0,a;
  int i;
  printf("Enter a no");
  scanf("%d",&n);
  printf("Enter position on which you want to find digit from left");
  scanf("%d",&a);
  m=n;

   while(n>0)
   {
    sum++;
    n=n/10;
   }
   if(a!=sum)
   {
    n=m/(pow(10,sum-a));
    n=n%10;
   }
   else
   {
    n=m%10;
   }
   printf("Digit that you want is %d",n);
   getch();
 }

output
Enter a no 4353
Enter position on which you want to find digit from left 4
Digit that you want is 3