Friday, June 24, 2016

C program to understand the concept of initialization in 2D arrays

#include<stdio.h>
void main()
{
//here inner curly braced values will be assigned to 0th array and outer to 1th array
 int arr_new[3][4]={{1,2},3,4};
 arr_new[2][3]=0;
 printf("\n%d %d %d %d %d",arr_new[0][0],arr_new[0][1],arr_new[0][2],arr_new[1][0],arr_new[1][1]);
}

output-
1 2 0 3 4

Sunday, June 19, 2016

C program to make two matrix dynamically and show thier addition

#include <stdio.h>
#include<stdio.h>
int main()
{
   int row, colum, row_element, colum_element, first[50][50], second[50][50], sum[50][50];
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &row, &colum);
   if(row>0&&colum>0)
   {
   printf("Enter the elements of first matrix\n");
   for (row_element = 0; row_element < row; row_element++)
   {
     for (colum_element= 0; colum_element <colum; colum_element++)
      {
         scanf("%d", &first[row_element][colum_element]);
      }
   }
   printf("Enter the elements of second matrix\n");
   for (row_element = 0; row_element < row; row_element++)
   {
      for (colum_element = 0 ; colum_element< colum; colum_element++)
      {
            scanf("%d", &second[row_element][colum_element]);
      }
   }
   printf("Sum of entered matrices:-\n");
   for (row_element = 0; row_element< row; row_element++)
    {
      for (colum_element= 0 ; colum_element < colum; colum_element++)
  {
    sum[row_element][colum_element] = first[row_element][colum_element] +                                        second[row_element][colum_element];
    printf("%d\t", sum[row_element][colum_element]);
      }
 printf("\n");
   }
   }
   else
   printf("please enter positive number only");
}

output
Enter the number of rows and columns of matrix
2 2
Enter the elements of first matrix 1
2
3
2
Enter the elements of second matrix 2
3
2
1
Sum of entered matrices:-
3  5
5  3

Friday, June 17, 2016

C program to calculate GCD (greatest common divisors) of two numbers

Problem Statement:- GCD of two numbers is the greatest number which divides both the numbers
ex:- 20 and 10 have 10 as GCD because it divides both the numbers completely.

#include<stdio.h>
void function_gcd( int catch_firstnumber,int catch_secondnumber,int minimum_number1 )
{
  int counter_variable,gcd;
  for(counter_variable=1;counter_variable<=minimum_number1;counter_variable++)
  {
    if(catch_firstnumber%counter_variable==0 && catch_secondnumber % counter_variable == 0 )
gcd=counter_variable;
  }
 printf("the gcd of %d and %d is %d",catch_firstnumber,catch_secondnumber,gcd);
}
main()
{
float first_number,second_number;
int minimum_number,counter_variable,gcd;
int first_temp,second_temp;
clrscr();
printf("Enter the First integer number\n");
scanf("%f",&first_number);
first_temp=first_number;
        //processing for calculating GCD
while((first_number/first_temp!=1)||(first_temp<0))
{
if(first_number/first_temp!=1)
{
 printf("\nEnter a integer number only Enter first number again");
}
else
{
 printf("\nEnter a positive number only Enter first number again");
}
scanf("%f",&first_number);
first_temp=first_number;
}
printf("\nEnter the Second integer");
scanf("%f",&second_number);
second_temp=second_number;
while((second_number/second_temp!=1)||(second_temp<0))
{
if(second_number/second_temp!=1)
{
 printf("\nEnter a integer number only Enter Second number again");
}
else
{
 printf("\nEnter a positive number only Enter Second number again");
}
scanf("%f",&second_number);
second_temp=second_number;
}
    
if(first_number>second_number)
{
  minimum_number = second_number;
  function_gcd(first_number,second_number,minimum_number);
}
else
{
 minimum_number = first_number;
 function_gcd(first_number,second_number,minimum_number);
}
return 0;
}
output:
Enter the First integer number 20
Enter the Second integer number 10
the gcd of 20 and 10 is 10

Wednesday, June 15, 2016

C program to save and retrieve records from external file

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 200
struct emp
{
    int id;
    char *name;
    int eng;
    int math;
    int hindi;
}*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:
   fp = fopen("file.txt", "a");
   create();
   break;
case 2:
   fp1 = fopen("file.txt","rb");
   display();
   break;
case 3:
   exit(0);
}
    }
}
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);
 printf("\nEnter English Marks");
 scanf("%d",&emp1->eng);
 printf("\nEnter Maths Marks");
 scanf("%d",&emp1->math);
 printf("\nEnter Hindi Marks");
 scanf("%d",&emp1->hindi);
    fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
    fwrite(emp1->name, size, 1, fp);
    fwrite(&emp1->eng, sizeof(emp1->eng), 1, fp);
    fwrite(&emp1->math, sizeof(emp1->math), 1, fp);
    fwrite(&emp1->hindi, sizeof(emp1->hindi), 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");
    printf("Employee ID Employee Name English Maths Hindi");
    while (i <= count)
    {
fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name, size, 1, fp1);
fread(&emp3->eng, sizeof(emp3->eng), 1, fp1);
fread(&emp3->math, sizeof(emp3->math), 1, fp1);
fread(&emp3->hindi, sizeof(emp3->hindi), 1, fp1);
printf("\n%d %s %d %d %d",emp3->id,emp3->name,emp3->eng,emp3->math,emp3->hindi);
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: sasas
eneter emp id: 1
Enter Englist Marks 70
Enter Maths Marks 75
Enter Hindi Marks 60
Enter your choice 1
Enter name of employee sasa
enter emp id: 2
Enter English Marks 65
Enter Maths Marks 82
Enter Hindi Marks 70
Enter your choice 2
Employee ID     Employee Name    English    Maths    Hindi
    1                        sasas                      70          75            60
    2                        sasa                        65         82            70

Monday, June 13, 2016

c program to find factorial of a number without using recursive technique

#include<stdio.h>
#include<conio.h>
void main()
 {
  clrscr();
  int n,m,sum=1,a;
  printf("Enter a no");
  scanf("%d",&n);
//it will loop through the number and multiply all its small integers
  for(int i=1;i<=n;i++)
   {
    sum=sum*i;
   }
  printf("Factorial = %d",sum);
 getch();
}

output
Enter a no 6
720

Wednesday, June 8, 2016

C program to select elements from three given array and make sum of zero

Problem Statement: you're given three arrays and you have to pick one element from each array to make sum of zero
Example
 int a[]={1,7,5,9};
 int b[]={-2,-1,1,2};
 int c[]={-8,-7,1,-8};
taking 1 from a,-2 from b and 1 from c is making a combination with sum as zero
1 + -2 + 1 = 0

now ignoring these values, and repeating the process
taking 7 from first array, taking -1 from b isn't suiting with any element of c, taking 1 from b and -8 from c making combination with sum as zero
7 + 1 + -8 = 0

repeat this procedure

#include<stdio.h>
void main()
{
 int a[]={1,7,5,9};
 int b[]={-2,-1,1,2};
 int c[]={-8,-7,1,-8};
 int i,j,k;
 int occupy[100];
 int count=0, flag=0,temp;
 for(i=0;i<4;i++)
  {
   for(j=0;j<4;j++)
    {
     for(k=0;k<4;k++)
      {
       if((a[i]+b[j]+c[k])==0)
{
flag=0;
temp=count;
while(temp>0)
{
 if((i==occupy[temp*3-3])||(j==occupy[3*temp-2])||(k==occupy[3*temp-1]))
  {
   flag=1;
   break;
  }
  temp--;
 }
 if(flag==0)
  {
   printf("%d %d %d \n",a[i],b[j],c[k]);
   count++;
   occupy[3*count-3]=i;
   occupy[3*count-2]=j;
   occupy[3*count-1]=k;
  }
}
      }
    }
  }
}

output
1 -2 1
7 1 -8
5 2 -7
9 -1 -8

Monday, June 6, 2016

c++ program to show operation on time, add seconds, add minutes, add hours, show time

#include<iostream.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);
 }
//function to get the value of hour
int Time::gethour()
 {
  return hours;
 }
//function to set the value of the hour
void Time::sethour(int h)
 {
  hours=h;
 }
//function to add the minutes in current minutes
void Time::addminute(int m)
 {
  m=getminute()+m;
  if(m>60)
   {
    m=m%60;
    addhour(1);
   }
  setminute(m);
 }
//function to get the value of current minutes
int Time::getminute()
 {
  return minutes;
 }
//function to set the value of current value
void Time::setminute(int m)
 {
  minutes=m;
 }
//function to add the seconds in current second value
void Time::addsecond(int s)
 {
  s=getsecond()+s;
  if(s>60)
   {
    s=s%60;
    addminute(1);
   }
  setsecond(s);
 }
//function to get the value of current second value
int Time::getsecond()
 {
  return seconds;
 }
//function to set the value of current second value
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 add5
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
2
How many minutes to add25
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
5
Time::05:25:00
Enter 1.Add hours
2.Add minutes
3.Add seconds
4.Add time
5.Show time
6.Exit
6

Saturday, June 4, 2016

C program to check given number in strong number or not

#include<stdio.h>
//function to calculate factorial
int fact(int i)
 {
  int k=1;
  if(i==1)
   {
    return 1;
   }
  else
   {
    k=i*fact(i-1);
   }
   return k;
 }
void main()
 {
  int n,m,sum=0,a;
  int i;
  printf("Enter a no");
  scanf("%d",&n);
  m=n;
  while(n>0)
   {
    a=n%10;
    sum=sum+fact(a);
    n=n/10;
   }
  if(m==sum)
   {
    printf("No is strong no");
   }
  else
   {
    printf("Not a strong no");
   }
}

output
Enter a no 32
Not a strong no

Thursday, June 2, 2016

c program to count number of 1 in binary representation of a number

Problem Statement: count the no of ones in binary repesentation of a decimal number
#include<stdio.h>
#include<math.h>
void main()
{
 int i,k,l,j=0;
 printf("\nEnter a no");
 scanf("%d",&i);
 l=i;
//it will give the size of any data type in bits
 k=8*sizeof(int);
 while(k!=0)
  {
   if(i&1==1)
    {
     j++;
    }
   i=i>>1;
   k--;
  }
 printf("\nNo of one set in number is %d",j);
}

output
Enter a no10
No of one set in number is 2

Tuesday, May 31, 2016

C program to print binary representation of number

#include<stdio.h>
#include<math.h>
//function to print binary number of a decimal
void binary(int i)
{
 if(i!=0)
  {
   binary(i>>1);
  }
 printf("%c",i&1==1?'1':'0');
}
void main()
{
 int i,k,l,j=0;
 printf("\nEnter a no");
 scanf("%d",&i);
 l=i;
 //To print binary equivalent
 printf("\nBinary representation of the no is ");
 binary(l);
}

output
Enter a no 54
Binary representation of the no is 110110

Sunday, May 29, 2016

C program to sort list of names

#include<stdio.h>
#include<string.h>
void main()
{
 int i,j;
//list of names to be sort, you can customize to take input from keyboard
 char *list[]={"aj","su","punit","pushp"},*s;
printf("Input List is :")
 for(i=0;i<4;i++)
 {
  printf("\n%s ",*(list+i));
 }
 for(i=0;i<4;i++)
  {
   for(j=0;j<4-i-1;j++)
    {
     if(strcmp(*(list+j),*(list+j+1))>0)
      {
       s=*(list+j);
       *(list+j)=*(list+j+1);
       *(list+j+1)=s;
      }
    }
   }
 printf("\nSorted List is:\t");
 for(i=0;i<4;i++)
  {
   printf(" %s",*(list+i));
  }
}

output
Input List is:
aj
su
punit
pushp
Sorted List is: aj punit pushp su

Friday, May 27, 2016

C program to make a binary tree and print the tree in inorder, preorder and postorder form

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node *left,*right;
}*root;
//function to create a new node in tree
node *create(struct node *ptr,int n)
 {
  struct node *curr;
  curr=(node *)malloc(sizeof(node));
  if(ptr==NULL)
   {
    curr->info=n;
    curr->left=NULL;
    curr->right=NULL;
    ptr=curr;
   }
  else
   {
    if(ptr->info>n)
     ptr->left=create(ptr->left,n);
    else
     ptr->right=create(ptr->right,n);
   }
   return ptr;
 }
void preorder(struct node *ptr)
 {
  if(ptr!=NULL)
   {
    printf(" %d",ptr->info);
    preorder(ptr->left);
    preorder(ptr->right);
   }
 }
void inorder(struct node *ptr)
{
 if(ptr!=NULL)
  {
   inorder(ptr->left);
   printf(" %d",ptr->info);
   inorder(ptr->right);
  }
}
void postorder(struct node *ptr)
 {
  if(ptr!=NULL)
   {
    postorder(ptr->left);
    postorder(ptr->right);
    printf(" %d",ptr->info);
   }
 }
void main()
{
//initially root will be null as there is no node
 root=NULL;
 int n;
 int i;
 do
  {
   printf("\n\n1.insert\n\n2.preorder.\n\n3.inorder\n\n4.postorder\n\n5.exit");
   scanf("%d",&n);
   if(n==1)
    {
     printf("Enter value to insert");
     scanf("%d",&i);
     root=create(root,i);
    }
   else if(n==2)
    {
     preorder(root);
    }
   else if(n==3)
    {
     inorder(root);
    }
   else if(n==4)
    {
     postorder(root);
    }
   }while(n!=5);
 }

output

1.insert
2.preorder
3.inorder
4.postorder
5.exit
1
Enter value to insert 4

1.insert
2.preorder
3.inorder
4.postorder
5.exit
1
Enter value to insert 3
1.insert
2.preorder
3.inorder
4.postorder
5.exit
2
3 4
1.insert
2.preorder
3.inorder
4.postorder
5.exit
5

Tuesday, May 24, 2016

C program to find whether given string or sentence is armstrong or not

#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
//str1 is input string here you can input here using your choice
 char str1[50]="hey its its hey",str2[50]="",strtemp[50];
 printf("Input string is %s",str1)
 strlwr(str1);
 int s[50],l[50],c=0;
 for(int i=0;i<=strlen(str1);i++)
  {
   if(str1[i]>=97&&str1[i]<=122)
    continue;
   else
    {
     if(c==0)
      {
       s[c]=0;
       l[c]=i-1;
       c++;
      }
     else
      {
       s[c]=l[c-1]+2;
       l[c]=i-1;
       c++;
      }
    }
  }
 c=c-1;
 for(i=c;i>=0;i--)
  {
   int temp=0;
   if((i!=c))
    {
     strtemp[temp]=' ';
     temp++;
    }
   for(int j=s[i];j<=l[i];j++)
    {
     strtemp[temp]=str1[j];
     temp++;
    }
   strtemp[temp]='\0';
   strcat(str2,strtemp);
  }
 printf("%s",str2);
 if((strcmp(str1,str2)==0))
  {
   printf("\nString are armstrong");
  }
 else
  {
   printf("\nString is not armstrong");
  }
}

output
Input string is hey it's it's hey
String are armstrong

Friday, May 20, 2016

C program to find whether given string is palindrome or not

Problem Statement:- you are given a string and you need to check whether the given string is palindrome or not, if yes you need to produce output yes
Palindrome:- if the string is equal to its reverse of string then it is called palindrome numbers

#include<stdio.h>
#include<string.h>
void check(char *t)
 {
  char *i;
//strlen() is used to calculate length of the string defined in string.h
  int l=strlen(t);
  while(*t!=NULL)
   {
    *i=*t;
    i++;
    t++;
   }
   *i='\0';
   t=t-l;
   i--;
   int count=0;
   while(*t!=NULL)
    {
     if(*t!=*i)
      {
       count++;
       break;
      }
      t++;
      i--;
    }
  if((count==0)&&(*t==NULL))
   {
    printf("String is palindrome");
   }
 else
  {
   printf("String is not palindrome");
  }
}

void main()
{
 char s[20];
 printf("Enter a string");
 scanf("%s",s);
 check(s);
}

output
Enter a string
swswwsws
String is palindrome

Wednesday, May 18, 2016

c program to sort a given string alphabetically

Problem Statement:- you are given a string and you need to sort that string into their alphabetical order. Like in "enter" the first alphabet also comes first alphabetically so it will stay there and after that the secode 'e' must come.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
 //random string as input
 char s[15]="enter";
 int i;
 char temp;
 printf("String entered is %s ",s);
//applying logic as bubble sort for string sorting
 for(i=0;i<strlen(s);i++)
  {
   for(int k=0;k<strlen(s)-i-1;k++)
    {
     //this  will use ASCII codes for characters
     if(s[k]>s[k+1])
     {
      temp=s[k];
      s[k]=s[k+1];
      s[k+1]=temp;
     }
    }
   }
 printf("\nString is %s",s);
}

output
String entered is  enter
String is eenrt

Monday, May 16, 2016

c program to print box using stars

Problem Statement:- print square box using stars in c, you can use nested loops to solve this problem.

#include<stdio.h>
void main()
 {
  int a,n=6,i,j,k;
  for(i=1;i<=n-2;i++)
   {
    if(i==1||i==(n-2))
     {
      for(j=1;j<=n;j++)
       printf("*");
      printf("\n");
     }
    else
     {
      printf("*");
      for(j=1;j<=n-2;j++)
       printf(" ");
      printf("*");
      printf("\n");
     }
   }
}

output
******
*        *
*        *
******

Saturday, May 14, 2016

C program to print stars in diamond form

#include<stdio.h>
#include<conio.h>
void main()
 {
  clrscr();
  int a,n=4,i,j,k;
  for(i=1;i<=n;i++)
   {
    for(k=1;k<=n-i;k++)
     printf(" ");
    for(j=1;j<=2*i-1;j++)
     printf("*");
     printf("\n");
   }
   for(i=n;i>=1;i--)
   {
    for(j=1;j<=n-i;j++)
     printf(" ");
    for(k=1;k<=2*i-1;k++)
     printf("*");
     printf("\n");
   }
 getch();
}

output
      *
    ***
  *****
*******
*******
  *****
   ***
     *

Thursday, May 12, 2016

C program to find factorial of a numbe using recursive technique

Problem Statement: factorial of any number is equal to multiplication of all the numbers from 1 to that number. Factorial of 5 will be equal to multiplication of numbers from 1 to 5 which will be 1*2*3*4*5 = 120
Recursive Technique: If a function calls itself then it is called recursive function.

#include<stdio.h>
//recursive function for calculating factorial
int fact(int i)
 {
  int k=1;
  if(i<=1)
   {
    return 1;
   }
  else
   {
    k=i*fact(i-1);
   }
   return k;
 }
void main()
 {
  int n,factorial=1;
  printf("Enter a no");
  scanf("%d",&n);
  factorial=fact(n);
  printf("Factorial = %d",factorial);
}

output
Enter a no 5
Factorial = 120

Tuesday, May 10, 2016

C++ program to explain default, parameterized and copy constructor

#include<iostream.h>
class vector
{
 public:
  int *a;
  int b;
//default constructor
 vector()
  {
   a = new int;
   *a=10;
  }
//parameterized constructor
 vector(int k)
  {
   a = new int;
   *a=k;
  }
 void getvalue()
  {
   cout<<"a="<<*a<<"\n";
  }
//copy constructor
  vector(vector &v)
  {
  a = new int;
  *a=*v.a;
  }
};
void main()
{
 vector vector1; //invoke default constructor
 vector1.getvalue();
 vector vector2(25); //invoke parameterized constructor
 vector.getvalue();
 vector vector3;
 vector vector4(vector3); //invoke copy constructor
 vector4.getvalue();
}
output
a=10
a=25
a=10

Monday, May 9, 2016

c++ program to swap two numbers using third variable

problem statement:- swapping of two numbers means you are given two numbers in variables a and b respectively and with the use program you have to swap value of a to b and value of b to a

#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
13 18
before swapping
13 18
after swapping
18 13

Saturday, May 7, 2016

c++ program to find second max element from an array using malloc

#include<iostream.h>
class A
{
 public:
 int *a,i,j,smax,max,n;
 A();
 A(int);
 ~A();
 void input_func();
 void output_func();
 void find_smax();
};
A::A()
{
}
A::A(int n)
{
 a=new int(n);
 this->n=n;
 input_func();
}
A::~A()
{
 cout<<"\nA's destructor is called";
}
void A::input_func()
 {
  cout<<"Enter "<<n<<" nos";
  //int *k=i;
  for(i=0;i<n;i++)
   {
    cin>>*(a+i);
    //a++;
   }
  // a=k;
  output_func();
 }
void A::output_func()
 {
  cout<<"\nNumber's are";
  for(i=0;i<n;i++)
   {
    cout<<" "<<*(a+i);
   }
   find_smax();
 }
void A::find_smax()
 {
  int flag=0;
  max=*a;
  for(i=1;i<n;i++)
   {
    if(flag==0)
     {
       if(*(a+i)==*(a))
{
continue;
}
       else
{
flag=1;
if(*(a+i)>max)
 {
  smax=max;
  max=*(a+i);
 }
else
 {
  smax=*(a+i);
 }
}
     }
    else if(*(a+i)>smax)
     {
      if(*(a+i)>max)
       {
smax=max;
max=*(a+i);
       }
      else if(*(a+i)!=max)
       {
smax=*(a+i);
       }
     }
   }
   if(flag==1)
    cout<<"Smax ="<<smax;
   else
    cout<<"All numbers are equal";
 }
int main()
{
 int n;
 cout<<"\nHow many values";
 cin>>n;
 A a_object(n);
 return 0;
}
output
How many values 10
Enter 10 nos 5 8 5 0 5 4 8 4 6 2
Number's are 5 8 5 0 5 4 8 4 6 2
Smax=6

Friday, May 6, 2016

C program to sort an array using insertion sort

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

output
sorted array
0 1 6 6 8 38 44 45 66 68

Sunday, May 1, 2016

C program to sort an array using selection sort

#include<stdio.h>
void main()
{
 int a[10]={1,8,6,0,6,68,44,66,45,38};
 int i,temp,k,n=10,pos;
 printf("Array before sort");
 for(i=0;i<n;i++){
  printf("%d ",a[i]);
 }
 for(i=0;i<n;i++)
  {
   pos=i;
   for(k=i+1;k<n;k++)
    {
     if(a[pos]>a[k])
      {
       pos=k;
      }
    }
   if(pos!=i)
    {
     temp=a[pos];
     a[pos]=a[i];
     a[i]=temp;
    }
  }
 printf("Array after sorting\n");
 for(i=0;i<n;i++)
  {
   printf("%d ",a[i]);
  }
}
output
Array before sort
1 8 6 0 6 68 44 66 45 38
Array after sort
0 1 6 6 8 38 44 45 66 68

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

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*+