Friday, 19 December 2014

C Programs

Programs :    functions with arguments .

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

long int factorial(int x); /* function declaration or prototype*/


 /* main function*/

void main()
 {
   int n;
   long int res;
   clrscr();
   printf("enter an integer value");
   scanf("%d",&n);
   res = factorial(n);  /* function call*/
   printf("the factorial of %d is %ld",n,res);
   getch();
 }

/* function definition*/

long int factorial(int x)
 {
   int i;
   long int fact=1;
   for(i=1;i<=x;i++)
     {
       fact=fact*i;
     }
   return fact;
 }


Programs :    functions with arguments without return value


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

/* function definition*/

 void factorial(int x)
 {
   int i;
   long int fact=1;
   for(i=1;i<=x;i++)
     {
       fact=fact*i;
     }
   printf("the factorial of %d is %ld",x,fact);
 }

 /* main function*/

void main()
 {
   int n;
   clrscr();
   printf("enter an integer value");
   scanf("%d",&n);
   factorial(n);  /* function call*/
  
   getch();
 }

Programs :    functions without arguments


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

/* function definition*/

 void factorial()
 {
   int i,x;
   long int fact=1;
   printf("enter an integer value");
   scanf("%d",&x);
   for(i=1;i<=x;i++)
     {
       fact=fact*i;
     }
   printf("the factorial of %d is %ld",x,fact);
 }

 /* main function*/

void main()
 {
   clrscr();
   factorial();  /* function call*/
   getch();
 }

Programs :   Recursive Functions

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

long factorial(int n)  /* function definition*/
{
 long fact=1;
 if(n<=1)
   return 1;
 else
  {
   fact=n*factorial(n-1); /* recursively calling function itself */
   return fact;
  }
 }

void main()
{
int a;
long x;
clrscr();
printf("enter an integer value : ");
scanf("%d",&a);
x=factorial(a);  /* function call statement from main */
printf("the factorial is %ld",x);
getch();
}


Programs :   Matrix Addition

#include<stdio.h>
#include<conio.h>
void main ()
{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
                    /* reading values for first matrix */
printf("enter the values for matrix a");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
                                   /* reading values for second matrix */

printf("enter the values for matrix b");
{
for (i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)                       /*  matrix multiplication process */
{
for(j=0;j<2;j++)
   {  
     c[i][j]= a[i][k]+b[k][j];      /* Adding corresponding values */
    }
 }
printf("the result of addition of two matrices\n\n");

for(i=0;i<2;i++)                       /*  printing resultant matrix row wise */
 {
  for(j=0;j<2;j++)
    {
      printf("  %d",c[i][j]);   
   }
   printf("\n");
 }
getch();
}

Programs :   Matrix Subtraction

#include<stdio.h>
#include<conio.h>
void main ()
{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
                    /* reading values for first matrix */
printf("enter the values for matrix a");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
                                   /* reading values for second matrix */

printf("enter the values for matrix b");
{
for (i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)                       /*  matrix subtraction process */
{
for(j=0;j<2;j++)
   {  
     c[i][j]= a[i][k]-b[k][j];      /* subtracting corresponding values */
    }
 }
printf("the result of subtraction of two matrices\n\n");

for(i=0;i<2;i++)                       /*  printing resultant matrix row wise */
 {
  for(j=0;j<2;j++)
    {
      printf("  %d",c[i][j]);   
   }
   printf("\n");
 }
getch();
}


Programs :   Matrix Multiplication

#include<stdio.h>
#include<conio.h>
void main ()
{
int a[2][2],b[2][2],c[2][2],i,j,k;
clrscr();
                    /* reading values for first matrix */
printf("enter the values for matrix a");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
                                   /* reading values for second matrix */

printf("enter the values for matrix b");
{
for (i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("the result of multiplication of two matrices\n\n");
for(i=0;i<2;i++)                       /*  matrix multiplication process */
{
for(j=0;j<2;j++)
 {
  c[i][j]=0;                 /* initializing resultant matrix */
  for(k=0;k<2;k++)
    {
     c[i][j]=c[i][j]+a[i][k]*b[k][j];      /* multiplying row & column values  for a resultant cell value */
    }
 }
}
for(i=0;i<2;i++)                       /*  printing resultant matrix row wise */
 {
  for(j=0;j<2;j++)
    {
      printf("  %d",c[i][j]);   
   }
   printf("\n");
 }
getch();
}



Program : Perfect number

#include<stdio.h>
#include<conio.h>
void main()
{
 int x,i,sum=0;
 clrscr();
 printf("enter a number");
 scanf("%d",&x);
 for(i=1;i<x;i++)
  {
   if(x%i==0)
    {
      sum=sum+i;
    }
  }
if(sum==x)
 {
  printf("perfect number");
 }
else
 {
  printf("not a perfect number");
 }
getch();
}

Program : Reverse of the digits of a number

#include<stdio.h>
#include<conio.h>
void main()
{
int n,rem,rev=0;
clrscr();
printf("enter an integer value:  ");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("\n the reverse of the digits of a number is %d",rev);
getch();
}

Program :  Sorting numbers

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[10],i,j,temp,n;
  clrscr();
  printf("enter the values of n\t");
  scanf("%d", &n);
  printf("\n\n enter %d values \n",n);
  for(i=0;i<n; i++)
   {
    scanf("%d", &a[i]);
   }
  for(i=0;i<n-1;i++)
   {
    for(j=i+1;j<n; j++)
      if(a[i]>a[j])
       {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
       }
   }
  printf("\n\n values after sorting are:\n\n");
  for(i=0;i<n;i++)
  printf("\t%d", a[i]);
  getch();
 }


Program :  to find the largest of an array

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[10],i,j,temp,n;
  clrscr();
  printf("enter the values of n\t");
  scanf("%d", &n);
  printf("\n\n enter %d values \n",n);
  for(i=0;i<n; i++)
   {
    scanf("%d", &a[i]);
   }
  for(i=0;i<n-1;i++)
   {
    for(j=i+1;j<n; j++)
      if(a[i]>a[j])
       {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
       }
   }
  
  printf("\n Largest value of array is :%d", a[n-1]);
  getch();
 }



Program : Structures
struct personal    /* Structure definition */
 {
    char name[20]
    int day;
    char month[10];
    int year;
    float salary;
 };

main()
{
struct  personal  person;       /* Structure variable declaration */
printf(“Input Values\n”);
                                                  /*  Reading data into structure variable elements */
scanf(“%s %d %s %d %f”, person.name, &person.day, person.month, &person.year, &person.salary);
                                                  /* display data from structure variable elements */
printf(“%s %d %s %d %f\n”, person.name, person.day, person.month, person.year, person.salary);
}

Output
Input values
M.L.Goel 10 January 1945 4500
M.L.Goel 10 January 1945 4500.00



















PROGRAM :  SORTING STRINGS:

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
    /*variable declaration */
     char str[80],*ptr[5];
     int i,j;
     clrscr();
    /*  input strings */
     printf("enter 5 strings\n");
     for(i=0;i<5;i++)
     {
        scanf("%s",ptr[i]);
     }
    /* sorting strings */
     for(i=0;i<4;i++)
     {
        for(j=i+1;j<5;j++)
          {
             if((strcmp(ptr[i],ptr[j]))>0)
               {
                 strcpy(str,ptr[i]);
                 strcpy(ptr[i],ptr[j]);
                 strcpy(ptr[j],str);   
                }
         }
     }
   /* printing sorted strings */
    printf("\n the stored are");
    for(i=0;i<5;i++)
            printf("\n%s",ptr[i]);
    return 0;
}

Program to count characters,words,lines

#include<stdio.h>
#include<conio.h>
void main()
{
char line[81], ctr;
int i,c,end = 0, characters = 0, words = 0, lines = 0;
clrscr();
printf("\nKEY IN THE TEXT" );
printf("GIVE ONE SPACE AFTER EACH WORD.\n");
printf("WHEN COMPLETED, PRESS - ENTER\n\n");
while (end == 0)
{
c = 0;
while ((ctr = getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
if (line[0] == '\0')
break;
else
{
words++;
for (i = 0; line[i] != '\0'; i++)
if (line[i] == ' ' || line[i] == '\t')
words++;
}
lines = lines + 1;
characters = characters + strlen(line);
}
printf("\n");
printf("\nNumber of lines = %d", lines);
printf("\nNumber of words = %d", words);
printf("\nNumber of characters = %d", characters);

}

No comments:

Post a Comment