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