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