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