Search This Blog

Thursday, October 13, 2011

Write a function to find squre and cube of a num

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void squrecube(float x,float *s,float *c)
   4:  {
   5:  *s=x*x;
   6:  *c=x*x*x;
   7:  }
   8:  void main()
   9:  {
  10:  float x,s,c;
  11:  printf("enter the num:");
  12:  scanf("%f",&x);
  13:  squrecube(x,&s,&c);
  14:  printf("squre=%f cube=%f",s,c);
  15:  getch();
  16:  }

Write a Program to Give output

1
1 2
1 2 3


   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,n,s;
   6:  printf("Enter the num of rows:\n");
   7:  scanf("%d",&n);
   8:  for(i=1;i<=n;i++)
   9:      {
  10:      for(s=n-i;s>=1;s--)
  11:          {
  12:          printf(" ");
  13:          }
  14:      for(j=1;j<=i;j++)
  15:          {
  16:          printf("%d ",j);
  17:          }
  18:      printf("\n");
  19:      }
  20:  getch();
  21:  }

Write a program that will provide a character string from command line which translates it into a number and evaluate and display its cube root

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:  #include<stdlib.h>
   5:  void main(int argc,char *argv[])
   6:  {
   7:  float x;
   8:  if(argc<2)
   9:      {
  10:      printf("Insufficient number of arguments supplied:\n");
  11:      getch();
  12:      exit(0);
  13:      }
  14:  x=atof(argv[1]);
  15:  printf("cube root of %f is %f\n",x,pow(x,1.0/3.0));
  16:  getch();
  17:  }

Write a program to read two strings and display them sorted alphabetically

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<string.h>
   4:  void main()
   5:  {
   6:  char str1[40];
   7:  char str2[40];
   8:  int n;
   9:  printf("Enter the first string:\n");
  10:  gets(str1);
  11:  printf("Enter the second string:\n");
  12:  gets(str2);
  13:  n=strcmp(str1,str2);
  14:      if(n==0)
  15:      {
  16:      printf("the strings are identical\n");
  17:      }
  18:      else
  19:      {
  20:      printf("if sorted alphabetically the strings will come in following order\n");
  21:      }
  22:      if(n<0)
  23:      {
  24:      puts(str1);
  25:      puts(str2);
  26:      }
  27:      if(n>0)
  28:      {
  29:      puts(str2);
  30:      puts(str1);
  31:      }
  32:  getch();
  33:  }

Write a program to read two strings and append second to first

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<string.h>
   4:  void main()
   5:  {
   6:  char str1[40];
   7:  char str2[40];
   8:  printf("Enter the first string:\n");
   9:  gets(str1);
  10:  printf("Enter the second string:\n");
  11:  gets(str2);
  12:  strcat(str1,str2);
  13:  printf("String 1 is now:\n");
  14:  puts(str1);
  15:  getch();
  16:  }

Write a program that will read names of three students from the keyboard store them as array of strings

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<string.h>
   4:  void main()
   5:  {
   6:  int i;
   7:  char sname[3][40];
   8:  char yourname[40];
   9:  for(i=0;i<3;i++)
  10:      {
  11:      printf("Enter the name of the student:\n");
  12:      gets(yourname);
  13:      strcpy(sname[i],yourname);
  14:      }
  15:  for(i=0;i<3;i++)
  16:  puts(sname[i]);
  17:  getch();
  18:  }

Write a program to read my number x and to evaluate x*x-2. use a function to evaluate x*x-2 and call this function through its pointer

#include<stdio.h>
#include<conio.h>
#include<math.h>
float expr(float);
void main()
{
float x,value;
printf("Enter the value of x ");
scanf("%f",&x);
value=(*expr)(x);
printf("value =%f\n",value);
getch();
}
float expr(float x)
{
return(x*x-2);
}

The trace of a square is given by the sum of the diagonal elements. Write a program to evaluate the trace of a matrix. Read the elements of matrix from keyboard and pass on its address to a function to evaluate and return the traces

   1:   #include<stdio.h>
   2:   #include<conio.h>
   3:   float trace(int n,float **x);
   4:   void main()
   5:   {
   6:   int i,j;
   7:   float *xx[10];
   8:   float t,x[10][10],num;
   9:   int n;
  10:   printf("Enter the size of matrix:\n");
  11:   scanf("%d",&n);
  12:   printf("Enter the elements:\n");
  13:   for(i=0;i<n;i++)
  14:   {
  15:      for(j=0;j<n;j++)
  16:      {
  17:      scanf("%f",&num);
  18:      x[i][j]=num;
  19:      }
  20:   }
  21:   for(i=0;i<n;i++)
  22:   xx[i]=x[i];
  23:   t=trace(n,xx);
  24:   printf("trace of matrix is: %f",t);
  25:   getch();
  26:   }
  27:   
  28:   float trace(int n,float **x)
  29:   {
  30:   int i;
  31:   float t=0.0;
  32:   for(i=0;i<n;i++)
  33:   t=t+*(*(x+i)+i);
  34:   return(t);
  35:   }

Illustrate working of increment operator(++)

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int a,b,x,y;
   6:  a=b=2;
   7:  x=a++;
   8:  y=++b;
   9:  printf("At this point \n");
  10:  printf("Value of x= %d\n",x);
  11:  printf("Value of y= %d\n",y);
  12:  getch();
  13:  }

Write a program to read elements of matrix and display them through pointers

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  float x[10][10];
   6:  int i,j,nr,nc;
   7:  printf("Enter the num of rows:\n");
   8:  scanf("%d",&nr);
   9:  printf("Enter the num of columns:\n");
  10:  scanf("%d",&nc);
  11:  printf("Enter the elements of matrix:\n");
  12:  for(i=0;i<nr;i++)
  13:  {
  14:      for(j=0;j<nc;j++)
  15:      {
  16:          scanf("%f",&x[i][j]);
  17:      }
  18:  printf("\n");
  19:  }
  20:  printf("The matrix is:\n\n");
  21:  for(i=0;i<nr;i++)
  22:  {
  23:      for(j=0;j<nc;j++)
  24:      {
  25:          printf("%.2f ",*(*(x+i)+j));
  26:      }
  27:  printf("\n");
  28:  }
  29:  getch();
  30:  }

Write a program to sort an array of real Num using separate functions. Access array through pointers

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void readdata(int *size,float *addx)
   4:  {
   5:  int i;
   6:  float n;
   7:  char ch;
   8:  printf("Enter the elements of array:\n");
   9:  i=0;
  10:  do
  11:  {
  12:  scanf("%f%c",&n,&ch);
  13:  *(addx+i)=n;
  14:  i++;
  15:  }while(ch!='\n');
  16:  *size=i;
  17:  }
  18:   
  19:  void displayarray(int size,float *addx)
  20:  {
  21:  int i;
  22:  for(i=0;i<size;i++)
  23:  printf("%.2f ",*(addx+i));
  24:  printf("\n");
  25:  }
  26:   
  27:  void sort(int size,float *addx)
  28:  {
  29:  int i,j;
  30:  for(j=0;j<size-1;j++)
  31:  for(i=0;i<size-1;i++)
  32:  if(*(addx+i)>*(addx+i+1))
  33:      {
  34:      int p,q;
  35:      p=*(addx+i);
  36:      q=*(addx+i+1);
  37:      *(addx+i)=q;
  38:      *(addx+i+1)=p;
  39:      }
  40:  }
  41:   
  42:  void main()
  43:  {
  44:  int i,size;
  45:  float x[50];
  46:  readdata(&size,x);
  47:  printf("Array is:\n");
  48:  displayarray(size,x);
  49:  sort(size,x);
  50:  printf("sorted array is:\n");
  51:  displayarray(size,x);
  52:  getch();
  53:  }

Write a program to find the maximum element of an array of real num. Read the element of the array in function readdata() and find maximum in another function. Access the array elements through pointers.

   1:  #include<conio.h>
   2:  #include<stdio.h>
   3:  void readdata(int *size,float *addx)
   4:  {
   5:  int i;
   6:  float n;
   7:  char ch;
   8:  printf("\nEnter the elements of the array:\n");
   9:  i=0;
  10:  do
  11:  {
  12:  scanf("%f%c",&n,&ch);
  13:  *(addx+i)=n;
  14:  i++;
  15:  }while(ch!='\n');
  16:  *size=i;
  17:  }
  18:  float maxno(int size,float *addx)
  19:  {
  20:  int i;
  21:  float max=-1.0E38;
  22:  for(i=0;i<size;i++)
  23:      {
  24:      if((*addx+i)>max)
  25:      {
  26:      max=*(addx+i);
  27:      }
  28:      }
  29:  return(max);
  30:  }
  31:  void main()
  32:  {
  33:  int size;
  34:  float max;
  35:  int i;
  36:  float x[50];
  37:  readdata(&size,x);
  38:  max=maxno(size,x);
  39:  printf("\nThe array is: ");
  40:  for(i=0;i<size;i++)
  41:      printf("%.2f ",x[i]);
  42:  printf("\nThe maximum No is: %f",max);
  43:  getch();
  44:  }

write a program to read a set of real nums in one line of the keyboard and using the function maxno(), display the largest num entered.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  float maxno(int size,float *addx);
   4:  void main()
   5:  {
   6:  int i,size;
   7:  float x[50],max;
   8:  char ch;
   9:  printf("\nEnter the elements of array:\n");
  10:  i=0;
  11:  do
  12:  {
  13:  scanf("%f%c",&x[i],&ch);
  14:  i++;
  15:  }while(ch!='\n');
  16:  size=i;
  17:  max=maxno(size,x);
  18:  printf("\nThe maximum No is: %.2f",max);
  19:  getch();
  20:  }
  21:  float maxno(int size,float *addx)
  22:  {
  23:  int i;
  24:  float max=-1.0E38;
  25:  for(i=0;i<size;i++)
  26:  if(*(addx+i)>max)
  27:  max=*(addx+i);
  28:  return(max);
  29:  }

Write a program to read the elements of an array in a function named readdata() and display its elements in function main()

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void readdata(int *size,float *x);
   4:  void main()
   5:  {
   6:  int size;
   7:  int i;
   8:  float x[50];
   9:  readdata(&size,x);     //for X[i] instead of &x[0] one can take x as address of array
  10:  printf("\nThe array is ");
  11:  for(i=0;i<size;i++)
  12:  printf("%.2f ",x[i]);
  13:  getch();
  14:  }
  15:  void readdata(int *size,float *x)
  16:  {
  17:  int i;
  18:  float n;
  19:  char ch;
  20:  printf("\nEnter the elements of array:\n");
  21:  i=0;
  22:  do
  23:  {
  24:  scanf("%f%c",&n,&ch);
  25:  *(x+i)=n;
  26:  i++;
  27:  }while(ch!='\n');
  28:  *size=i;
  29:  }

Write a program to book space in the memory for array and display the address of each element

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  float x[]={1.0,2.0,3.0,4.0,5.0,6.0};
   6:  int i;
   7:  float *add;
   8:  for(i=0;i<6;i++)
   9:  {
  10:  add=&x[i];
  11:  printf("Address of x[%d] is: %ld\n",i,add);
  12:  }
  13:  getch();
  14:  }

Write a function to evaluate all the three angels of triangle ABC and PQR in degrees given its sides.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  #include<math.h>
   5:  #define PIE 3.14
   6:   
   7:  void angles(float x,float y,float z, float *a1,float *a2,float *a3);
   8:  void main()
   9:  {
  10:  float a,b,c,A,B,C;
  11:  float p,q,r,P,Q,R;
  12:  printf("Enter the sides of ABC:\n");
  13:  scanf("%f%f%f",&a,&b,&c);
  14:  printf("Enter the sides of PQR:\n");
  15:  scanf("%f%f%f",&p,&q,&r);
  16:  angles(a,b,c,&A,&B,&C);
  17:  angles(p,q,r,&P,&Q,&R);
  18:  printf("Angle A= %.2f\n",A);
  19:  printf("Angle B= %.2f\n",B);
  20:  printf("Angle C= %.2f\n",C);
  21:  printf("Angle P= %.2f\n",P);
  22:  printf("Angle Q= %.2f\n",Q);
  23:  printf("Angle R= %.2f\n",R);
  24:  getch();
  25:  }
  26:  void angles(float x,float y,float z, float *a1,float *a2,float *a3)
  27:  {
  28:  *a1=(180.0/PIE)*acos((y*y+z*z-x*x)/(2.0*y*z));
  29:  *a2=(180.0/PIE)*acos((x*x+z*z-y*y)/(2.0*z*x));
  30:  *a3=(180.0/PIE)*acos((y*y+x*x-z*z)/(2.0*y*x));
  31:  }

Write a program to, as arguments, the co-efficient of a,b,c of the quadratic equation ax2+bx+c=0 and evaluates its roots such that both the roots will be available in the calling function.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:  #include<stdlib.h>
   5:  void roots(float a,float b,float c,float *r1,float *r2);
   6:  void main()
   7:  {
   8:  float a,b,c,root1,root2;
   9:  printf("Enter the coefficient of a,b,c:\n");
  10:  scanf("%f%f%f",&a,&b,&c);
  11:  roots(a,b,c,&root1,&root2);
  12:  printf("Roots of the equation are %.2f and %.2f\n",root1,root2);
  13:  getch();
  14:  }
  15:  void roots(float a,float b,float c,float *r1,float *r2)
  16:  {
  17:  float d;
  18:  d=b*b-4.0*a*c;
  19:  if(d<0)
  20:  {
  21:  printf("no real root\n");
  22:  getch();
  23:  exit(0);
  24:  }
  25:  else
  26:  *r1=(-b+sqrt(d))/(2.0*a);
  27:  *r2=(-b-sqrt(d))/(2.0*a);
  28:  }

Write a program to generate a random int num and assign the same to a variable x through pointer. Display the value of x

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  void main()
   5:  {
   6:  int x,i;
   7:  int *xptr=&x;
   8:  srand(12345);
   9:  *xptr=rand();
  10:  printf("x=%d\n",x);
  11:  getch();
  12:  }

Write a program to assign any value to any num and display the same through pointer

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  void main()
   5:  {
   6:  int x,i;
   7:  int *a_x;
   8:  x=rand();
   9:  a_x=&x;
  10:  printf("x=%d\n",*a_x);
  11:  getch();
  12:  }

Write a program to declare a pointer variable x and display its address

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  float x;
   6:  float *address_of_x;
   7:  address_of_x=&x;
   8:  printf("Address of x= %ld\n",address_of_x);
   9:  getch();
  10:  }

Write a program to sort an array in descending order

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  #define developing 0
   5:  #define debug
   6:  void main()
   7:  {
   8:  int i,j,k;
   9:  char cd;
  10:  clrscr();
  11:  #if developing
  12:  //To be compiled if developing =1
  13:  int n=10;
  14:  float x[]={20,25,32,50,17,12,28,30,15};
  15:  #else
  16:  //To be compiled if developing =0
  17:  int n;
  18:  float x[50];
  19:  printf("\nNo of elements in array:");
  20:  scanf("%d",&n);
  21:  for(i=0;i<n;i++)
  22:      {
  23:      printf("Enter the element no %d: ",i);
  24:      scanf("%f",&x[i]);
  25:      }
  26:  #endif
  27:   
  28:  printf("\n\nThe original array is:\n");
  29:  for(i=0;i<n;i++)
  30:  printf("%.2f ",x[i]);
  31:  printf("\n");
  32:  for(j=0;j<n-1;j++)
  33:      for(i=0;i<n-1;i++)
  34:      {
  35:      if(x[i]<x[i+1])
  36:          {
  37:          float p,q;
  38:          p=x[i];
  39:          q=x[i+1];
  40:          x[i]=q;
  41:          x[i+1]=p;
  42:          #if defined debug
  43:          //To be compiled if degub is defined
  44:          printf("\nThe interchangeing element %d and %d:\n",i,i+1);
  45:              for(k=0;k<n;k++)
  46:              printf("%.2f ",x[k]);
  47:              printf("\n");
  48:              printf("\ncontinue debugging N/Y: ");
  49:              cd=getche();
  50:              if(cd=='N'||cd=='n')
  51:              exit(0);
  52:              #endif
  53:          }
  54:      }
  55:  printf("\nThe sorted array is:\n");
  56:  for(i=0;i<n;i++)
  57:  printf("%.2f ",x[i]);
  58:  printf("\n");
  59:  getch();
  60:  }

Write a program that will give square of 3 and 5

   

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #define square(x) x*x
   4:  void main()
   5:  {
   6:  printf("square of 3 is:%d\n",square(3));
   7:  printf("square of 5 is:%d\n",square(5));
   8:  getch();
   9:  }

Write a program to read three real Num x,y,z and evaluates their avg. the Num read in a function Readdata() and Getavarage() evaluates the data and returns the value to main()

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void readdata();
   4:  float getaverage();
   5:  float x,y,z;
   6:  void main()
   7:  {
   8:  float average;
   9:  readdata();
  10:  average=getaverage();
  11:  printf("Average = %f\n",average);
  12:  getch();
  13:  }
  14:  void readdata()
  15:  {
  16:  printf("Enter x,y and z:\n");
  17:  scanf("%f%f%f",&x,&y,&z);
  18:  }
  19:  float getaverage()
  20:  {
  21:  float av;
  22:  av=(x+y+z)/3;
  23:  return(av);
  24:  }

Write a program to declaring operand as a block variable defined in if block

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:  void main()
   5:  {
   6:  int x;
   7:  printf("Enter an interger num:\n");
   8:  scanf("%d",&x);
   9:  if(x>0)
  10:  {
  11:  float r;
  12:  r=1/float(x);
  13:  printf("\nReciprocal of %d is: %f\n",x,r);
  14:  }
  15:  else
  16:  printf("X is too small to evaluate reciprocal:");
  17:  getch();
  18:  }

Write a program that will read an integer number and evaluate and display its square root as a float variable

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:  void main()
   5:  {
   6:  int n;
   7:  floar r;
   8:  printf("Enter an interger no\n");
   9:  scanf("%d",&n);
  10:  r=sqrt(n);
  11:  printf("Square root of %d is: %f\n",n,r);
  12:  getch();
  13:  }

Write a program to display ASCII values of all lowercase letters

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i;
   6:  pritf("Character ASCII NO\n");
   7:  pritf("--------  -------\n");
   8:  for(i=97;i<=122;i++)
   9:  printf("%c %3d",i,i);
  10:  getch();
  11:  }

Write a program to display decimal numbers 0 through 16 in decimal, octal and hexadecimal

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int k;
   6:  pritf("\nDecimal Octal Hexadecimal");
   7:  for(k=0;k<=16;k++)
   8:  printf("%5d %5o %5x",k,k,k);
   9:  getch();
  10:  }

Write a program to display transpose of a given matrix

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,u,v,k,m,x[10][10],y[10][10];
   6:  printf("Enter the no of row of matrix:");
   7:  scanf("%d",&k);
   8:  printf("\nEnter the no of column of matrix:");
   9:  scanf("%d",&m);
  10:  printf("Enter the elements:\n");
  11:  for(i=0;i<k;i++)
  12:  for(j=0;j<m;j++)
  13:  {
  14:  scanf("%d",&x[i][j]);
  15:  }
  16:  for(u=0;u<m;u++)
  17:  for(v=0;v<k;v++)
  18:  {
  19:  y[u][v]=x[v][u];
  20:  }
  21:  printf("Original Matrix:\n\n");
  22:  for(i=0;i<k;i++)
  23:  {
  24:  for(j=0;j<m;j++)
  25:  {
  26:  printf("%3d ",x[i][j]);
  27:  }
  28:  printf("\n");
  29:  }
  30:  printf("Traspose Matrix:\n\n");
  31:  for(u=0;u<m;u++)
  32:  {
  33:  for(v=0;v<k;v++)
  34:  {
  35:  printf("%3d ",y[u][v]);
  36:  }
  37:  printf("\n");
  38:  }
  39:  getch();
  40:  }

Write a pogram to display a matrix whose elements are x(i,j)=i+j when i!=j and x(i,j)=0 when i=j

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,x[3][4];
   6:  printf("Desired 3x4 matrix is:\n\n");
   7:  for(i=0;i<3;i++)
   8:  {
   9:  for(j=0;j<4;j++)
  10:  {
  11:  if(i!=j)
  12:  x[i][j]=i+j;
  13:  else if(i==j)
  14:  x[i][j]=0;
  15:  printf("%d ",x[i][j]);
  16:  }
  17:  printf("\n");
  18:  }
  19:  getch();
  20:  }

Write a program read three real numbers a,b,c and evaluate t1,t2 and t1-t2 when t1 and t2 are expressed as follows

t1=a*(b+c)+b*(c+a)+c*(a+b)

t2=a*a*(b*b+c*c)+b*b*(a*a+c*c)+c*c*(b*b+a*a)

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:   
   5:  void main()
   6:  {
   7:  float a,b,c,diff;
   8:  float t1=0.0,t2=0.0;
   9:  printf("Enter the value of a b c:\n");
  10:  scanf("%f%f%f",&a,&b,&c);
  11:   
  12:  /*Evalution of t1*/
  13:  t1+=a*(b+c);
  14:  t1+=b*(c+a);
  15:  t1+=c*(a+b);
  16:   
  17:  /*Evalution of t2*/
  18:  t2+=a*a*(b*b+c*c);
  19:  t2+=b*b*(c*c+a*a);
  20:  t2+=c*c*(a*a+b*b);
  21:  t2=sqrt(t2);
  22:   
  23:  diff=t1-t2;
  24:   
  25:  /*Output of results*/
  26:  printf("t1= %10.3f\n",t1);
  27:  printf("t2= %10.3f\n",t2);
  28:  printf("diff= %10.3f\n",diff);
  29:  getch();
  30:  }

Write a program to read a line of text and to convert it into a coded text by changing its characters to integer and adding k to each character

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  char line[80];
   6:  int i,l,k;
   7:  clrscr();
   8:  printf("Enter the line\n");
   9:  for(i=0;i<80;i++)
  10:  {
  11:  scanf("%c",&line[i]);
  12:  if(line[i]=='\n')
  13:  break;
  14:  }
  15:  l=i;
  16:  printf("Enter the num for coding: ");
  17:  scanf("%d",&k);
  18:  printf("\nCoded text is:\n");
  19:  for(i=0;i<l;i++)
  20:  printf("%4d",line[i]+k);
  21:  getch();
  22:  }

Program to read a line of text and display characters vertically.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  char line[80];
   6:  int i,l;
   7:  printf("Enter the Line:\n");
   8:  for(i=0;i<80;i++)
   9:  {
  10:  scanf("%c",&line[i]);
  11:  if(line[i]=='\n')
  12:  break;
  13:  }
  14:  l=i;
  15:  printf("the characters in the line are\n");
  16:  for(i=0;i<l;i++)
  17:  printf("%3d %c\n",i,line[i]);
  18:  getch();
  19:  }

Write a program to read two matrices from the keyboard and find its product

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  void getproduct(int,int,int,float[10][10],float[10][10]);
   5:  void main()
   6:  {
   7:  int i,j,k,m1,n1,m2,n2;
   8:  float x[10][10],y[10][10];
   9:  printf("\nEnter the rows of matrix X: ");
  10:  scanf("%d",&m1);
  11:  printf("\nEnter the clomuns of matrix X: ");
  12:  scanf("%d",&n1);
  13:  printf("\nEnter the rows of matrix Y: ");
  14:  scanf("%d",&m2);
  15:  printf("\nEnter the columns of matrix y: ");
  16:  scanf("%d",&n2);
  17:  if(n1==m2)
  18:  k=n1;
  19:  else
  20:  {
  21:  printf("\nMultiplication of X and Y is not possible:");
  22:  getch();
  23:  exit(0);
  24:  }
  25:  printf("\nEnter the elements of first matrix:\n");
  26:  for(i=0;i<m1;i++)
  27:  for(j=0;j<n1;j++)
  28:  scanf("%f",&x[i][j]);
  29:  printf("\nEnter the elements of second matrix:\n");
  30:  for(i=0;i<m2;i++)
  31:  for(j=0;j<n2;j++)
  32:  scanf("%f",&y[i][j]);
  33:  getproduct(m1,k,n2,x,y);
  34:  getch();
  35:  }
  36:  void getproduct(int m1,int k,int n2,float x[10][10],float y[10][10])
  37:  {
  38:  int u,v,i;
  39:  float z[10][10];
  40:  for(u=0;u<m1;u++)
  41:  for(v=0;v<n2;v++)
  42:  {
  43:  z[u][v]=0.0;
  44:  for(i=0;i<k;i++)
  45:  z[u][v]+=x[u][i]*y[i][v];
  46:  }
  47:  printf("MATRIX Z=X*Y\n\n");
  48:  for(u=0;u<m1;u++)
  49:  {
  50:  for(v=0;v<n2;v++)
  51:  {
  52:  printf("%8.3f",z[u][v]);
  53:  }
  54:  printf("\n");
  55:  }
  56:  getch();
  57:  }

Program to pass a matrix as an argument to a function and display the matrix in the function. x[i][j]=2xi+3xj

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void displaymatrix(int nr,int nc,int x[10][10])
   4:  {
   5:  int i,j;
   6:  printf("The matrix is given below:\n\n");
   7:  for(i=0;i<nr;i++)
   8:  {
   9:  for(j=0;j<nc;j++)
  10:  {
  11:  printf("%d ",x[i][j]);
  12:  }
  13:  printf("\n");
  14:  }
  15:  }
  16:  void main()
  17:  {
  18:  int i,j,nr,nc,x[10][10];
  19:  printf("\nNo of rows:");
  20:  scanf("%d",&nr);
  21:  printf("\nNo of columns:");
  22:  scanf("%d",&nc);
  23:  for(i=0;i<nr;i++)
  24:  for(j=0;j<nc;j++)
  25:  x[i][j]=2*i+3*j;
  26:  displaymatrix(nr,nc,x);
  27:  getch();
  28:  }

Check equality of two matrices

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  void main()
   5:  {
   6:  int x[10][10],y[10][10],xr,xc,yr,yc,i,j,equal=1;
   7:  printf("Matrix X:\n\n");
   8:  printf("Number of rows: ");
   9:  scanf("%d",&xr);
  10:  printf("\nNumber of clomuns: ");
  11:  scanf("%d",&xc);
  12:  for(i=0;i<xr;i++)
  13:  {
  14:  for(j=0;j<xc;j++)
  15:  {
  16:  printf("\nEnter the element x[%d][%d]",i,j);
  17:  scanf("%d",&x[i][j]);
  18:  }
  19:  }
  20:  printf("Matrix Y\n\n");
  21:  printf("Number of rows: ");
  22:  scanf("%d",&yr);
  23:  printf("\nNumber of columns: ");
  24:  scanf("%d",&yc);
  25:  for(i=0;i<yr;i++)
  26:  {
  27:  for(j=0;j<yc;j++)
  28:  {
  29:  printf("\nEnter the element y[%d][%d]",i,j);
  30:  scanf("%d",&y[i][j]);
  31:  }
  32:  }
  33:  if(xr!=yr||xc!=yc)
  34:  {
  35:  printf("\nMatrices cant be campare No of rows or columns are diffrent:\n");
  36:  getch();
  37:  exit(1);
  38:  }
  39:  else
  40:  {
  41:  for(i=0;i<xr;i++)
  42:  for(j=0;j<xc;j++)
  43:  if(x[i][j]!=y[i][j])
  44:  {
  45:  equal=0;
  46:  break;
  47:  }
  48:  if(equal)
  49:  printf("\nMatrices are identical");
  50:  else
  51:  printf("\nmatrice are not identical");
  52:  }
  53:  getch();
  54:  }

The trace of the matrix is given by the sum of its elements of its major diagonal. Write a program to evaluate the trace of the above matrix

       



   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,sum=0;
   6:  int x[4][4]={
   7:          {2,4,6,8},
   8:          {3,5,7,9},
   9:          {4,2,6,8},
  10:          {1,3,5,7},
  11:          };
  12:  for(i=0;i<4;i++)
  13:      {
  14:      for(j=0;j<4;j++)
  15:      {
  16:      if(i==j)
  17:      sum+=x[i][j];
  18:      printf("%5d",x[i][j]);
  19:      }
  20:      printf("\n");
  21:      }
  22:  printf("the trace of the matrix is: %d\n",sum);
  23:  getch();
  24:  }

X is a matrix whose elements x(i,j) is given by x(i,j)=2*i+3*j. Write a program generate the matrix and display the same

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,r,c;
   6:  int x[10][10];
   7:  printf("Enter the number of rows: ");
   8:  scanf("%d",&r);
   9:  printf("Enter the number of columns: ");
  10:  scanf("%d",&c);
  11:  for(i=0;i<r;i++)
  12:      {
  13:      for(j=0;j<c;j++)
  14:      {
  15:      x[i][j]=2*i+3*j;
  16:      }
  17:      }
  18:  printf("The matrix is given below\n\n");
  19:  for(i=0;i<r;i++)
  20:  {
  21:  for(j=0;j<c;j++)
  22:  {
  23:  printf("%5d",x[i][j]);
  24:  }
  25:  printf("\n");
  26:  }
  27:  getch();
  28:  }