Some Programs


Annexure:

  1. program to convert hexadecimal/octal number to decimal equivalent
  2. Program to find the length of the given string
  3. Program to convert Upper case letters to lower case letters and vice versa
  4. program to compare two strings
  5. Program to concatenate two strings
  6. Program to find the number of vowels in a given string
  7. Program to convert first character of a word to capital letter
  8. Program to reverse the given string and check whether it is a palindrome
  9. Program to add and multiply two complex numbers
  10. Program to add  and multiply two complex numbers using structure pointers
  11. Program to add and subtract matrices using functions and pointers
  12. Program to read the contents of the file & send it to the printer (parallel port)
  13. Program to copy the contents from one file to another using command line arguments
  14. Program to count the number of bit set and unset in an integer

/* C program to convert hexadecimal /octal number to decimal equivalent*/

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
void main( )
{
int i , len, deci, z, d, j,flg1,flg2;
char a[25], c;
clrscr( );
flg1=0;
flg2=0;
do {
printf("Enter Hexadecimal number: ");
scanf("%s",&a);
flushall();
i=0;
len=0;
while (a[i]!='\0')
{
len++;
i++;
}
len--;
deci=0;
d=0;
z=0;
for (i=len; i>=0; i--)
{
c=tolower(a[i]);
z=pow(16,d);
j=a[i]-48;
if((c >='a' && c <='f')|| (j <= 9))
{
switch (c)
{
case 'a':deci=deci+10*z; break;
case 'b':deci=deci+11*z; break;
case 'c':deci=deci+12*z; break;
case 'd':deci=deci+13*z; break;
case 'e':deci=deci+14*z; break;
case 'f':deci=deci+15*z; break;
}
switch(j)
{
case 1:deci=deci+1*z; break;
case 2:deci=deci+2*z; break;
case 3:deci=deci+3*z; break;
case 4:deci=deci+4*z; break;
case 5:deci=deci+5*z; break;
case 6:deci=deci+6*z; break;
case 7:deci=deci+7*z; break;
case 8:deci=deci+8*z; break;
case 9:deci=deci+9*z; break;
}
d++;
}
else
{
printf("\nInvalid Hexadecimal number\n");
flg1=1;
break;
}
}
if(flg1==0)
  {
printf("\nDecimal equivalent of Hexadecimal %s is %d ",a,deci);
  }
printf("\n\nEnter the Octal number: ");
scanf("%s",&a);
i=0;
len=0;
while(a[i]!='\0')
{
len++;
i++;
}
len--;
deci=0;
d=0;
z=0;
for (i=len; i>=0; i--)
{
z=pow(8,d);
j=a[i]-48;
if (j<=7)
{
switch (j)
{
case 1:deci=deci+1*z; break;
case 2:deci=deci+2*z; break;
case 3:deci=deci+3*z; break;
case 4:deci=deci+4*z; break;
case 5:deci=deci+5*z; break;
case 6:deci=deci+6*z; break;
case 7:deci=deci+7*z; break;
}
}
else
{
flg2=1;
printf("Invalid Octal Number\n");
break;
}
d++;
}
if(flg2==0)
{
printf("\nDecimal equivalent of Octal %s is %d ",a,deci);
printf("\n");
}
printf("\nPress 'Y' to continue any other key to terminate: ");
scanf ("%c",&c);
c=getchar( );
printf("\n\n");
}while (c == 'Y');

/* Program to find the length of the given string*/

#include <stdio.h>
#include <conio.h>
void main()
{
char strn[100];
int i, len;
printf("Enter the string\n");
scanf("%[^\n]",&strn);
i=0;
len=0;
/*It even counts the number of spaces included in the string*/
while(strn[i] != '\0')
           {
           len++;
           i++;
           }
printf("Length of the entered string is : %d ",len);
}



Output:




/* Program to convert Upper case letters to lower case letters and vice versa*/

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main ()
{
char strn [100];
int i;
i = 0;
printf ("Enter any string\n");
scanf("%[^\n]",strn);
while (strn[i]!= '\0')
              {
isupper(strn[i])?(strn[i]=tolower(strn[i])):(strn[i]=toupper(strn[i]));
             i++;
             }
printf ("Coverted string is:  %s\n",strn );
getch();
}




Output:



/* C program to compare two strings*/

#include <stdio.h>
#include <conio.h>
void main( )
{
int i,j,flg=0;
char string1[100], string2[25];
clrscr( );
printf("Enter the first string\n");
scanf("%[^\n]", &string1);
flushall( );
printf("Enter the second string\n");
scanf("%[^\n]", &string2);
flushall( );
i=0;
j=0;
while (string1[i]!='\0'||string2[j]!='\0')
{
if(string1[i]!= string2[j])
{
flg=1;
break;
}
i++;
j++;
}
if(flg==0)
{
printf("The strings are equal \n");
}
else
{
printf("The strings are not equal \n");
}
getch();
}



Output:
 



/* Program to concatenate two strings*/

#include <stdio.h>
#include <conio.h>
void main( )
{
int i,j;
char string1[100], string2[25];
clrscr( );
printf("Enter the first string\n");
scanf("%[^\n]", &string1);
flushall( );
printf("Enter the second string\n");
scanf("%[^\n]", &string2);
flushall( );
i=0;
j=0;
i=0;
while (string1[i] !='\0')
{
i++;
}
j=0;
string1[i]=' ';
i++;
while (string2[j] !='\0')
{
string1[i]= string2[j];
j++;
i++;
}
string1[i] ='\0';
printf("The resultant string after concatenation is: %s \n", string1);
getch( );
}




Output:


/* Program to find the number of vowels in a given string*/

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
char strn[100],ch;
int i, vowel;
i=0;
vowel=0;
clrscr();
printf("Enter the string\n");
scanf("%[^\n]",&strn);
while(strn[i] != '\0')
  {
  ch=tolower(strn[i]);
  switch(ch)
                    {
  case 'a':
                vowel++;
                  break;
  case 'e':
                    vowel++;
                     break;
  case 'i':   
vowel++;
             break;
  case 'o':
             vowel++;
              break;
  case 'u':
             vowel++;
             break;
      }
      i++;
      }
      printf("Number of vowels present in the entered string is : %d",vowel);
      getch();
      }


Output:



/* Program to convert first character of a word to capital letter*/

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main ()
{
char strn[100];
int i;

i = 0;
printf ("Enter any string\n");
scanf("%[^\n]",&strn);
strn[i]=toupper(strn[i]);
while (strn[i]!= '\0')
            {
if(strn[i]==' ' && strn[i+1]!=' ')
           {
strn[i+1]=toupper(strn[i+1]);
           }
           i++;
           }
printf ("Coverted string is:  %s\n",strn );
getch();
}


Output:




/*Program to reverse the given string and check whether it is a palindrome*/

#include <stdio.h>
#include <conio.h>
void main()
{
char strn[100],temp[100];
int i, j,flg;
flg=0;
printf("Enter the string\n");
scanf("%[^\n]",&strn);
i=0;
while(strn[i] != '\0')
           {
           i++;
           }
 i--;
           j=0;
while( i >=0)
           {
           temp[j]=strn[i];
           j++;
           i--;
           }
temp[j]='\0';
i=0;
printf("\nReversed string is --> %s \n",temp);
while(strn[i] != '\0')
            {
            if (strn[i] != temp[i])
                              {
                              flg=1;
                              break;
                              }
            i++;
            }

If(flg==0)
          printf("\nEntered string %s ---> is a palindrome\n",strn);
else
printf("\nEntered string %s ---> is not a palindrome\n",strn);
}


Output:





/* Program to add and multiply two complex numbers*/

#include <stdio.h>
#include <conio.h>

struct complex
                  {
                     int real;
                     int img;
                   };
void main()
{
struct complex c1,c2,c3;
struct complex complex_add();
struct complex complex_mult();
printf("Enter the real and imaginary parts of first complex number: ");
scanf("%d%d",&c1.real,&c1.img);
printf("Enter the real and imaginary parts of second complex number: ");
scanf("%d%d",&c2.real,&c2.img);
c3=complex_add(c1,c2);
printf("\nResult of addition of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
c3=complex_mult(c1,c2);
printf("\nResult of multiplication of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
getch();
}
struct complex complex_add(cmpl1, cmpl2)
struct complex cmpl1,cmpl2;
{
struct complex cmpl3;
cmpl3.real=cmpl1.real + cmpl2.real;
cmpl3.img=cmpl1.img + cmpl2.img;
return(cmpl3);
}
struct complex complex_mult(cmpl1, cmpl2)
struct complex cmpl1,cmpl2;
{
struct complex cmpl3;
cmpl3.real=cmpl1.real*cmpl2.real - cmpl1.img*cmpl2.img;
cmpl3.img=cmpl1.real*cmpl2.img + cmpl1.img*cmpl2.real;
return(cmpl3);
}


Output:





/* Program to add  and multiply two complex numbers using structure pointers */

#include <stdio.h>
#include <conio.h>

struct complex
            {
                int real;
                int img;
             };
void main()
{
struct complex c1,c2,c3;
void complex_add();
void complex_mult();
printf("Enter the real and imaginary parts of first complex number: ");
scanf("%d%d",&c1.real,&c1.img);
printf("Enter the real and imaginary parts of second complex number: ");
scanf("%d%d",&c2.real,&c2.img);
complex_add(&c1,&c2,&c3);
printf("\nResult of addition of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
complex_mult(&c1,&c2,&c3);
printf("\nResult of multiplication of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
getch();
}
void complex_add(cmpl1, cmpl2, cmpl3)
struct complex *cmpl1,*cmpl2, *cmpl3;
{
cmpl3->real=cmpl1->real + cmpl2->real;
cmpl3->img=cmpl1->img + cmpl2->img;
}
void complex_mult(cmpl1, cmpl2,cmpl3)
struct complex *cmpl1,*cmpl2, *cmpl3;
{
cmpl3->real=cmpl1->real*cmpl2->real - cmpl1->img*cmpl2->img;
cmpl3->img=cmpl1->real*cmpl2->img + cmpl1->img*cmpl2->real;
}




Output:




Standard file pointers:

stdin Standard Input device keyboard
stdout Standard Output device Monitor

stdaux Standard

Auxiliary device Serial Port
stdprn Standard Printing device Parallel Port
stderr Standard Error device Monitor





/*Read the contents of the file & send it to the printer (parallel port)*/
# include <stdio.h>
# include <conio.h>
void main ( )
          {
          FILE *ptvar;
          char c;
          ptvar = fopen ("test.dat", "r");
          do
          {
          c=fgetc(ptvar);
          fputc(c,stdprn);
          }while (c!= EOF);
          fclose(ptvar);
          getch();
          }
          Standard File pointers are defined in stdio.h.



/* Program to copy the contents from one file to another using command line arguments*/
#include <stdio.h>
#include <conio.h>
void main(int argc, char *argv[])
{
FILE *ptr1,*ptr2;
char ch;
clrscr();
ptr1=fopen(argv[1],"w");
printf("Enter the contents for the file: %s\n",argv[1]);
do
{
ch=getchar();
putc(ch,ptr1);
}while(ch !=EOF);

fclose(ptr1); /* argv[1] is opened in read mode and argv[2] is opened in the write mode*/
ptr1=fopen(argv[1],"r");
ptr2=fopen(argv[2],"w");
do
{
ch=getc(ptr1);
putc(ch,ptr2);
}while(ch !=EOF);
fclose(ptr1);
fclose(ptr2);
/*Open argv[2] in read mode to read the copied contents*/
printf("\nCopied contents of file: %s\n",argv[2]);
ptr1=fopen(argv[1],"r");
do
{
ch=getc(ptr1);
putchar(ch);
}while(ch !=EOF);
fclose(ptr1);
getch();
}


Output:


Note:

See the chapter Command Line Parameters to learn the way of executing the program with command line parameters.





/*C Program to count the number of bit set and unset in an integer*/
#include
#include
void main()
{
int n;
void dispbits();
clrscr( );
printf("Enter an integer \n");
scanf("%d",&n);
dispbits(n);

getch( );
}

void dispbits(m)
int m;
{
int mask, k, i, s, u;
u=0;
s=0;
printf("Bit pattern of %d is : ",m);
for (i=15;i>=0;i--)
{
mask=1;

mask=mask<<i;
k=mask&m;
k==0?u++:s++;
k==0?printf("0"):printf("1");
}
printf("\n\nNumber of bits set in %d is %d and unset is %d \n",m , s, u);
}




/* Program to add and subtract matrices using functions and pointers*/
#include <alloc.h>
#include <stdio.h>
#include <conio.h>
void main( )
{
int i, j, m, n, p,q, choice, flg=0, ch;
int *ptr1,*ptr2, *ptr3,*temp1,*temp2;
void sum(int *, int *,int *,int,int);
void sub(int *, int *,int *,int,int);
clrscr();
printf ("Enter the order of the first matrix\n");
scanf ("%d%d",&m,&n);
flushall( );
ptr1=(int *)malloc(m*n*sizeof(int));
temp1=ptr1;
printf ("Enter the order of the second matrix\n");
scanf ("%d%d",&p,&q);
flushall( );
ptr2=(int *)malloc(p*q*sizeof(int));
temp2=ptr2;
printf ("Enter the elements of the first matrix\n");
for (i=0; i<m;i++)
{
for (j=0; j<n;j++)
{
scanf("%d",ptr1);
ptr1++;
}
}
printf ("Enter the elements of the second matrix\n");
for (i=0; i<p;i++)
{
for (j=0; j<q;j++)
{
scanf("%d",ptr2);
ptr2++;
}
}
do
{
ptr1=temp1;
ptr2=temp2;
printf("1)Addition \t");
printf("2)Subtraction \t");
printf("Enter Your Choice-->");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Matrix Addition and ");
if((m==p)&&(n==q))
{
ptr3=(int *)malloc(m*n*sizeof(int));
sum(ptr1,ptr2,ptr3,m,n);
flg = 1;
}
else
printf("Order of matrix do not match so addition is not possible \n");
break;
case 2: printf("Matrix Subtraction and ");
if((m == p) && (n == q))
{
ptr3=(int *)malloc(m*n*sizeof(int));
sub(ptr1,ptr2,ptr3,m,n);
flg = 1;
}
else
printf("Order of matrix do not match so matrix subtraction is not possible \n");
break;
default: printf("Wrong Choice");
}
if (flg == 1)
{
printf("The resultant matrix is \n");
for (i=0; i<m;i++)
{
for (j=0; j<q;j++)
{
printf("%d\t",*ptr3);
ptr3++;
}
printf("\n");
}
}
printf("Press 'Y' to continue and any other key to terminate --->");
flushall();
ch=getchar();
}
while (ch == 'Y');
getch( );
}
/* Function for matrix addition */
void sum(pt1,pt2,pt3,row,col)
int *pt1,*pt2,*pt3,row,col;
{
int i,j;
for (i=0; i<row;i++)
{
for (j=0; j<col;j++)
{
*pt3 = *pt1 + *pt2;
pt1++;
pt2++;
pt3++;
}
}
}

/* Function for matrix subtraction */
void sub(pt1,pt2,pt3,row,col)
int *pt1,*pt2,*pt3,row,col;
{
int i, j;
for (i=0; i<row;i++)
{
for (j=0; j<col;j++)
{
*pt3 = *pt1 - *pt2;
pt1++;
pt2++;
pt3++;
}
}
}

Output:




Short hand assignment operator:

Instead of writing i=i+1 you can use a short hand assignment operator and write the same statement as i +=1; Similarly i=i-5 can be written as i-=5; This can be used for all the arithmetic operators.

The general syntax will be v op= exp; and it will be expanded as v= v op exp;


So if we have a statement i += j+2; it is equivalent to i = i+(j+2)


Increment and Decrement operators:

The most usefull operators of C are increment ( ++) and decrement ( -- ). The operator ++ adds 1 to the operand and -- subtracts 1 from the operand.


Prefix and Postfix increment and decrement operators:



Prefix operator first adds 1 to the operand and then assigns the result to the variable on left.
Example:
i=1;

m=++i;


In the above example the value of both i and m will be 2.



Postfix operator first assigns the value of the operand to the variable on the left and then increments the operand.


Example:
i=1;
m=i++;


In the above example the value of m will be 1 and i will be 2.


Character Test Functions:
The following functions tests the value present in the variable passed as argument :
1)isalnum(c)- is c an alphanumeric character.
2)isalpha(c)- is c an alphabet
3)isdigit(c)- is c a digit
4)islower(c)- is c a lower case letter
5)isupper(c)- is c an upper case letter
6)ispunct(c)- is c a punctuation mark
7)isspace(c)- is c a white space character
8)isprint(c)- is c a printable character

All the above character test functions are defined in the header file ctype.h

exit() :
This function terminates the program.
Syntax:
void exit(int);


This function is defined in the header file stdlib.h

Example:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
void main()
{
int i;
for(i=0; i< 15; i++)
{
if(i==7)
exit(0);
printf("i is %d\n",i);
}
getch();
}

Output:




When the value of i equals to 7 the program is terminated.

Table containing different data types, the data range, number of bytes and the format specification character is given below:
Dta type Range Bytes Format
signed char or char -128 to +127 1 %c
unsigned char 0 to255 1 %c
signed int or int -32768 to +32767 2 %d
unsigned int 0 to 65535 2 %u
signed long int or long int -2147483648 to +2147483647 4 %ld
unsigned long int 0 to 4294967295 4 %lu
Float 3.4E-38 to 3.4E+38 4 %f
Double 1.7E-308 to 1.7E+308 8 %lf
long double 3.4E-4932 to +1.1E+4932 10 %Lf



Command Line Parameters<< Previous

Our aim is to provide information to the knowledge seekers.


Support us generously

comments powered by Disqus








Footer1