WELCOME

Welcome to KoDes4U

Pages

Sunday, February 27, 2011

decimal to binary conversion



/*Write a C program to convert given decimal into its euivalent binary.
Coded by KoDes4u.blogspot.com*/


#include<stdio.h>
#include<conio.h>
#define MAX 10
void convert(int dec_num,int bin_num[]);
void main()
{
 int dec_num,bin_num[MAX];
 clrscr();
 printf("\nEnter a decimal number:");
 scanf("%d",&dec_num);
 convert(dec_num,bin_num);
 getch();
}
void convert(int dec_num,int bin_num[])
{
int i=0,j;
 while(dec_num!=0)
 {
  bin_num[i]=dec_num%2;
  dec_num=dec_num/2;
  i++;
 }
 printf("\nThe Binary equivalent is:");
 for(j=i-1;j>=0;j--)
 printf("%d",bin_num[j]);
}

Friday, February 25, 2011

string operation without using library function(c programs)



/*Write a program on string operations without using library functions
   coded by KoDes4U.blogspot.com*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
int length(char str[]);
void copy(char str[]);
int compare(char str1[],char str2[]);
void reverse(char str[]);
int pallindrme(char str[]);
void concat(char str1[],char str2[]);
int search(char str1[],char str2[]);
void count(char str[]);


void main()
{
 char str1[50],str2[50];
 int res,op;
 clrscr();
  do
 {
  printf("\n-------------------------------MENU-----------------------------");
  printf("\n1)Length\n2)Copy\n3)Compare\n4)Reverse\n5)Pallindrome");
  printf("\n6)Concatenate\n7)Search\n8)Count\n9)Exit");
  printf("\nEnter your choice:");
  scanf("%d",&op);
  flushall();
  switch(op)
  {
   case 1:printf("\nEnter any string:");
 gets(str1);
 res=length(str1);
 printf("\nLength of the string is: %d",res);break;
   case 2:printf("\nEnter any string:");
 gets(str1);
 copy(str1);
 break;
   case 3:printf("\nEnter 1st string:");
 gets(str1);
 printf("\nEnter 2nd string:");
 gets(str2);
 res=compare(str1,str2);
 if(res>0)
 printf("\n1st string is greater.");
 else if(res<0)
 printf("\n2nd string is greater.");
 else
 printf("\nBoth are same.");
 break;
   case 4:printf("\nEntre any string:");
 gets(str2);
 reverse(str1);
 break;
   case 5:printf("\nEnter any string:");
 gets(str1);
 res=pallindrome(str1);
 if(res==0)
 printf("\nPallindrome.");
 else
 printf("\nNot a pallindrome.");
 break;
   case 6:printf("\nEnter main string:");
 gets(str1);
 printf("\nEnter substring:");
 gets(str2);
 concat(str1,str2);break;
   case 7:printf("\nEnter the main string:");
 gets(str1);
 printf("\nEnter substrring:");
 gets(str2);
 res=search(str1,str2);
 printf("\nFound at location:%d",res);
 break;
   case 8:count(str1);
 break;
   case 9:break;
  }
 }              while(op!=9);
}


int length(char str[])
{
 int i=0;
 while(str[i]!='\0')
 i++;
 return(i);
}
void copy(char str[])
{
 char a[50];
 int i=0;
  printf("\nOriginal string is.%s",str);
 while(str[i]!='\0')
 {
 a[i]=str[i];
 i++;
 }
 printf("\nCopied string is:%s",a);
}
int compare(char str1[],char str2[])
{
 int i=0;
 while(str1[i]!='\0')
 {
  if(str1[i]>str2[i])
  return(1);
  if(str1[i]<str2[i])
  return(-1);
  i++;
 }
 return(0);
}


void reverse(char str[])
{
 int i=0,j=0;
 char temp[50];
 while(str[i]!='\0')
 i++;
 i--;
// n=i;
 //i=0;
 printf("\nOriginal string is:%s",str);
 //i=0;
 while(i>=0)
 {
  temp[j]=str[i];
  i--;
  j++;
 }
  printf("\nReversed string is:%s",temp);
}


void concat(char str1[],char str2[])
{
 int i=0,j=0;
 while(str1[i]!='\0')
 i++;
 printf("\nBefore concatenation:");
 printf("\n1st string is:%s",str1);
 printf("\n2nd string is:%s",str2);
 while(str2[j]!='\0')
 {
  str1[i]=str2[j];
  i++;j++;
 }
 printf("\nConcatenated string is:%s",str1);
}


int pallindrome(char str[])
{
 int i=0,j=0;
 while(str[j]!='\0')
 j++;
 j--;
 while(j>i)
 {
  if(str[i]!=str[j])
  return(1);
  i++;
  j--;
 }
  return(0);
}
int search(char str1[],char str2[])
{
 int i,j,lstr1,lstr2;
 lstr1=length(str1);
 lstr2=length(str2);
 for(i=0;i<=lstr1-lstr2+1;i++)
  for(j=0;str1[i+j]==str2[j]&&str2[j]!='\0';j++)
   if(str2[j]=='\0')
   return(i+1);
   return(0);
}
void count(char str[])
{
 int spcnt=0,chcnt=0,i;
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]==' ')
  spcnt++;
  chcnt++;
 }
 printf("\nNo. of spaces=%d",spcnt);
 printf("\nNo. of words=%d",spcnt+1);
 printf("\nNo. of characters=%s",chcnt);
}

matrix operation using array(c program)



/*write a program on matrix operation using array
 coded by KoDes4U.com*/




// list of include files
#include<stdio.h>
#include<conio.h>
#define SIZE 10


//Function definition for reading the matrix


void ReadMatrix(int mat[SIZE][SIZE],int *row,int *col)
{
 int i,j;
  for(i=0;i<*row;i++)
for(j=0;j<*col;j++)
 {
printf("\nEnter the element no:[%d][%]:",i,j);
  scanf("%d",&mat[i][j]);
 }
}
//Function definition for printing the matrix


void PrintMatrix(int mat[SIZE][SIZE],int row,int col)
{
 int i,j;
   for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
  printf("  %d",mat[i][j]);
  printf("\n\a");
}
}


//Function definition for addition of matrix


int AddMatrix(int mat1[SIZE][SIZE],int mat2[SIZE][SIZE],int mat3[SIZE][SIZE],int row1,int row2,int col1,int col2,int *row3,int *col3)
{
  int i,j;
  if(row1==row2&&col1==col2)   //size should be same
   {
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
 mat3[i][j]=mat1[i][j]+mat2[i][j];
*row3=row1;
  *col3=col1;
return(1);
   }
else
 {
printf("\nAddition is no possible");
return(-1);
 }
}


//Function of multiplication


int MulMatrix(int mat1[SIZE][SIZE],int mat2[SIZE][SIZE],int mat3[SIZE][SIZE],int row1,int row2,int col1,int col2,int *row3,int *col3)
{
 int i,j,k;
  if(col1==row2)
{
  for(i=0;i<row1;i++)
for(j=0;j<col2;j++)
{
  mat3[i][j]=0;
  for(k=0;k<col1;k++)
mat3[i][j]=mat3[i][i]+mat1[i][k]*mat2[k][j];
}
*row3=row1;
*col3=col2;
return(1);
}
  else
{
 printf("\nMultiplication is not possible:");
 return(-1);
}
}


//Function definition for transpose of the matrix
void Transpose(int mat1[SIZE][SIZE],int row1,int col1,int mat3[SIZE][SIZE],int *row3,int *col3)
{
  int i,j;
   for(i=0;i<col1;i++)
{
 for(j=0;j<row1;j++)
  {
mat3[i][j]=mat1[j][i];
  }
}
*row3=col1;
*col3=row1;
}


//Main Function definition
 void main()
 {
  int mat1[SIZE][SIZE],mat2[SIZE][SIZE],mat3[SIZE][SIZE];
int row1,row2,row3,col1,col2,col3;
  int choice,val;
clrscr();
  printf("\nEnter the rows and columns of 1st matrix:");
scanf("%d %d",&row1,&col1);
printf("\nRead matrix 1");
ReadMatrix(mat1,&row1,&col1);
printf("\nPrint matrix 1");
  PrintMatrix(mat1,row1,col1);
 printf("\nEnter the rows and columns of 2nd matrix:");
   scanf("%d %d",&row2,&col2);
 printf("\nRead matrix 2:");
ReadMatrix(mat2,&row2,&col2);
printf("\nprint matrix 2:");
PrintMatrix(mat2,row2,col2);
do
 {
  printf("\n1)ADD\n2)Multiplication\n3)Transpose\n4)Exit");
  printf("\nEnter your choice:");
  scanf("%d",&choice);
switch(choice)
{
 case 1:val=AddMatrix(mat1,mat2,mat3,row1,row2,col1,col2,&row3,&col3);
if(val!=-1)
 {
printf("Addition matrix is:");
PrintMatrix(mat3,row3,col3);
 }
break;
 case 2:val=MulMatrix(mat1,mat2,mat3,row1,row2,col1,col2,&row3,&col3);
  if(val!=-1)
  {
printf("\nMultiplication of matrices is:");
PrintMatrix(mat3,row3,col3);
  }
break;
 case 3:Transpose(mat1,row1,col1,mat3,&row3,&col3);
printf("\ntranspose matrix is:");
 PrintMatrix(mat3,row3,col3);
 break;
 case 4:break;
}


 }
while(choice!=4);
 }

swapping numbers(c programs)



/*Write a C program to swap two numbers without using third variable
  Coded by koDes4U,blogspot.com*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int a=4,b=6;
 clrscr();
 printf("\nBefore swaping a=%d\tb=%d",a,b);
 a=a+b;
 b=a-b;
 a=a-b;
 printf("\nAfter swaping a=%d\tb=%d",a,b);
 getch();
}

alphabetical pattern printing( c-programs)









/*Write a C program to print the following pattern
ABCDEFGFEDCBA
ABCDEF FEDCBA                                                                
ABCDE   EDCBA                                                                
ABCD     DCBA                                                                
ABC       CBA                                                                
AB         BA                                                                
A           A


coded by KoDes4U.blogspot.com */


#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,k;
  clrscr();
  for(i=71;i>=65;i--)
    {
      for(j=65;j<=i;j++)
        {
           printf("%c",j);
        }
      for(k=71;k>j-1;k--)
        {
          printf(" ");
        }
      for(k=i;k<70;k++)
        printf(" ");
        for(j=j-1;j>=65;j--)
          {
            if(j==71)
              {
       j=70;
       printf("%c",j);
              }
            else
              printf("%c",j);
          }
        printf("\n");
    }
  getch();
}

pattern printing( c-programs)





/*Write a C program to print the following pattern
                          *
                      * *
* * *
                     * * * *         coded by KoDes4u.Blogspot.com*/

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