C

Thursday, 27 July 2017

PROGRAM TO FIND GEOMETRIC MEAN

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int a[10],n,i;
    double sum=0, gm;
    clrscr();
    printf("Enter the total no. of elements");
    scanf("%d",&n);
    printf("Enter the elements\n");
    for(i=0;i<n;i++)
       scanf("%d",&a[i]);
    for(i=0;i<n;i++)
       sum =sum+(log10(a[i]));
    gm=exp(sum/n*log(10));
    printf("Geometric mean is %f",gm);
    getch();
}

Sunday, 16 July 2017

PROGRAM TO FIND ARITHMETIC MEAN

#include<stdio.h>
#include<conio.h>
void main()
{
   int a[50],n,i,s;
   float mean;
   clrscr();
   printf("enter how many terms u want to enter\n");
   scanf("%d",&n);
   printf("enter %d terms\n",n);
   for(i=0;i<n;i++)
     scanf("%d",&a[i]);
     s=0;
     for(i=0;i<n;i++)
     s+=a[i];
     mean=s/n;
     printf("mean is %f",mean);
     getch();

}

Saturday, 8 July 2017

Program to read name of a employee, employee number and salary from the keyboard and writes same to binary file

#include<stdio.h>
struct employee
{
          int eno,salary;
          char name[20];
}        
main()
{

          struct employee e;
          char ans;
          FILE *fp;
          fp=fopen("file9.out","wb");
          if(fp==NULL)
          {
                   printf("Error in file opening");
                   exit(0);
          }
          do
          {
                   printf("\nEmployee No.");
                   scanf("%d",&e.eno);
                   printf("\nName :");
                   scanf("%s",&e.name);
                   printf("\nSalary");
                   scanf("%d",&e.salary);
                   fwrite(&e,sizeof(e),1,fp);
                   printf("\nDo you want to add more records y/n");
                   ans=getch();
          }while(ans !='n');
          fclose(fp);

}

Program to demonstrate pointer to structure

#include<stdio.h>
main()
{
          struct student
          {
                   int roll;
                   char name[20];
                   int age;
                   char class[8];
          };        
          struct student s,*ptr;
          ptr=&s;
          printf("Enter the student record\n");
          printf("Roll no. :");
          scanf("%d",&ptr->roll);
          printf("Name :");
          scanf("%s",&ptr->name);
          printf("Age:");
          scanf("%d",&ptr->age);
          printf("Class :");
          scanf("%s",&ptr->class);
          printf("\n Record details are\n");
          printf("Roll no: %d\n",ptr->roll);
          printf("Name: %s\n",ptr->name);
          printf("Age: %d\n",ptr->age);
          printf("Class : %s\n",ptr->class);

}

Program to show nesting of macros

#include<stdio.h>
#define SQU(x)((x)*x)
#define CUBE(x)(SQU(x)*x)
int main()
{         
          int x,y;
          clrscr();
          x=SQU(3);
          y=CUBE(3);
          printf("\nSquare of 3 : %d",x);
          printf("\nCube of 3 : %d",y);
          return(0);

}

Program to demonstrate pointer to function

#include<stdio.h>
main()
{
          void mess();
          void (*ptr)();
          ptr=mess;
          clrscr();
          printf("Address of the function is %d\n",ptr);
          (*ptr)();
}             
      void mess()
      {
          printf("I am in the subprogram\n");

      }

Program to demonstrate enumerated data types.

#include<stdio.h>
enum month {Jan,Feb,Mar,Apr,May,June,July,Aug,Sep,Oct,Nov,Dec,Next};
main()
{
          int m;
          float pay,sum=0;
          printf("\n");
          for(m=Jan;m<=Dec;m++)
          {
                   printf("Enter the pay of month %d :",m+1);
                   scanf("%f",&pay);
                   sum+=pay;
          }
          printf("Total Salary =%f",sum);

}

Program to demonstrate dynamic memory allocation

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
          char *mem_allocation;
          clrscr();
          mem_allocation= malloc( 20* sizeof(char));
          if(mem_allocation==NULL)
          {
           printf("couldn't able to allocate requested memory\n");
          }
          else
          {
                   strcpy(mem_allocation,"Besties");
      }
      printf("Dynamically allocated memory                                  content:""%s",mem_allocation);
           free(mem_allocation);

}

Program to demonstrate use of #define

 #include<stdio.h>
#define NAME "vokytoky"
#define AGE 15

int main()
{       clrscr();
          printf("%s is over %d years old.\n",NAME,AGE);
          return 0;

}

Program to demonstrate conditional compilation.

#include<stdio.h>
int main()
{              
          int i=2;
          clrscr();
          #ifdef DEF
          {
                   i*=i;
          }
          #else
          {
                   printf("The answer is %d\n",i);
          }
          #endif
          return 0;
}

Program to demonstrate array of strings

#include<stdio.h>
#include<string.h>
void main()
{                  
          char name[35][21];
          int n,i;
          clrscr();
          printf("Enter numbers of students");
          scanf("%d",&n);
          printf("Enter the %d students name\n",n);
          for(i=0;i<=n;++i)
          {
                   gets(name[i]);
          }
     printf("\n List of students is");
          for(i=0;i<=n;++i)
          puts(name[i]);
}

Write a program to draw a circle using mid-point circle drawing algorithm.

#include<iostream.h> #include<graphics.h> #include<conio.h> void drawcircle(int x0, int y0, int r) {         ...