Search This Blog

Thursday, October 13, 2011

Write a program to read an array of numbers and evaluate and display its mean, variant and deviant

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:  void main()
   5:  {
   6:  float x[50];
   7:  int n,i;
   8:  float sumx=0,sumx2=0;
   9:  float var,mean,sd;
  10:  printf("Number of elements in an array:");
  11:  scanf("%d",&n);
  12:  printf("\nEnter the elements:\n");
  13:  for(i=0;i<n;i++)
  14:  {
  15:  scanf("%f",&x[i]);
  16:  sumx=sumx+x[i];
  17:  sumx2=sumx2=x[i]*x[i];
  18:  }
  19:  mean=sumx/n;
  20:  var=1.0/n*(sumx2-(mean*mean));
  21:  sd=sqrt(var);
  22:  printf("\nThe array is:\n");
  23:  for(i=0;i<n;i++)
  24:  printf("%.2f ",x[i]);
  25:  printf("\nIts mean = %.2f \nvariant = %.2f\nStd deviation =%.2f\n",mean,var,sd);
  26:  getch();
  27:  }

Write a Program For Sorting An Array by Bubble Sort.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,j,n;
   6:  float x[50];
   7:  printf("Enter the number of elements in array:");
   8:  scanf("%d",&n);
   9:  for(i=0;i<n;i++)
  10:  {
  11:  printf("\nEnter the element no %d:",i);
  12:  scanf("%f",&x[i]);
  13:  }
  14:  printf("\nOriginal array is:\n");
  15:  for(i=0;i<n;i++)
  16:  printf("%.2f ",x[i]);
  17:  printf("\n");
  18:  for(j=0;j<n-1;j++)
  19:      for(i=0;i<n-1;i++)
  20:      {
  21:      if(
  22:      x[i]>x[i+1])
  23:          {
  24:          float p,q;
  25:          p=x[i];
  26:          q=x[i+1];
  27:          x[i]=q;
  28:          x[i+1]=p;
  29:          }
  30:      }
  31:  printf("\nThe sorted array is:\n");
  32:  for(i=0;i<n;i++)
  33:  printf("%.2f ",x[i]);
  34:  printf("\n");
  35:  getch();
  36:  }

Write a Program to find max element in an Array.

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

Write a program to read any char and display its ASCII value

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  char ch;
   6:  printf("Enter the character:\n");
   7:  ch=getche();
   8:  printf("\nASCII value of %c is %d\n",ch,ch);
   9:  getch();
  10:  }

Write a program to read real num from a array and find max

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int i,n;
   6:  char ch;
   7:  float x[20];
   8:  float max=-1.0E38;
   9:  printf("\nEnter the array:\n");
  10:  for(i=1;i<20;i++)
  11:  {
  12:  scanf("%f%c",&x[i],&ch);
  13:  if(ch=='\n')
  14:  break;
  15:  }
  16:  n=i+1;
  17:  for(i=0;i<n;i++)
  18:  {
  19:  if(x[i]>max)max=x[i];
  20:  }
  21:  printf("\nThe array is:");
  22:  for(i=0;i<n;i++)
  23:  printf("%7.2f",x[i]);
  24:  printf("\nmaximum num is %7.2f\n",max);
  25:  getch();
  26:  }

Write a program to read two sides of a right triangle and find its hypotenuse.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:  void main()
   5:  {
   6:  float a,b;
   7:  printf("Enter the two sides of right triangle:");
   8:  scanf("%f%f",&a,&b);
   9:  printf("\hypotenuse of right triangle is %f\n",hypot(a,b));
  10:  getch();
  11:  }

Write a program to evaluate the three angels of a triangle given its sides

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<math.h>
   4:   
   5:  float getdegree(float radians)
   6:  {
   7:  float degree;
   8:  float pie=3.14159;
   9:  degree=(radians*180)/pie;
  10:  return(degree);
  11:  }
  12:   
  13:  float getangle(float x,float y, float z)
  14:  {
  15:  float rads;
  16:  float degs;
  17:  rads=(y*y+z*z-x*x)/(2.0*y*z);
  18:  rads=acos(rads);
  19:  degs=getdegree(rads);
  20:  return(degs);
  21:  }
  22:  void main()
  23:  {
  24:  float a,b,c,A,B,C;
  25:  printf("Enter the sides of the triangel: ");
  26:  scanf("%f%f%f",&a,&b,&c);
  27:  A=getangle(a,b,c);
  28:  printf("Angle A=%f degrees\n",A);
  29:  B=getangle(b,a,c);
  30:  printf("Angle B=%f degrees\n",B);
  31:  C=getangle(c,a,b);
  32:  printf("Angle C=%f degrees\n",C);
  33:  getch();
  34:  }