Program No 41-45

 

41. Write a C program to store data of employees. The data to be stored is --- EmpId, Name, Basic Salary and Gross Salary. To get the gross salary, TA is 10% and DA is 30%. Assume maximum of 50 employees. Write a function to print details of each employee with gross salary above 15000.

42. Write a C program to implement pointer to arrays, pointer to functions.

A) Pointer to Arrays:

#include<stdio.h>

void main()

{

int *q,n,i,se=0,so=0;

clrscr();

q=(int *)malloc(sizeof(int));

printf("enter the size of pointer array\n");

scanf("%d",&n);

printf("Enter the element into pointer array\n");

for(i=0;i<n;i++)

{

scanf("%d",(q+i));

if(*(q+i)%2==0)

se=se+*(q+i);

}

printf("sum of the even elements is %d\n",se);

printf("sum of the odd elements is %d\n",so);

getch();

}

B) Pointer to Functions:

#include<stdio.h>

void main()

{

int a=10,b=20;

clrscr();

printf("Before Swapping the values of a=%d b=%d\n",a,b);

swap(&a,&b);

printf("After swapping the values of a=%d b=%d\n",a,b);

getch();

}

swap(int *p,int *q)

{

int t;

p=(int *)malloc(sizeof (int));

q=(int *)malloc(sizeof (int));

t=*p;

*p=*q;

*q=t;

printf("In swap function a=%d b=%d\n",*p,*q);

}

43. Write a C program for finding the number of characters,words & lines of a given text file.

#include<stdio.h>

void main()

{

   char str[100];

   int i = 0, nl=0, nc = 0, nw = 0 ;

printf("Enter text ( press @ in new line to terminate)\n");

scanf("%[^@]s",str);

for(i = 0; str[i] !='\0'; i++)

nc = nc + 1;

printf("The number of characters in the text are %d\n", nc-1);

for(i = 0; str[i] != '\0'; i++)

   {

      if(str[i] == ' '|| str[i] == '\t' || str[i] == '\n')

nw = nw + 1;

   }

printf("The number of words in the text are %d\n", nw);

for(i = 0; str[i]!='\0'; i++)

   {

      if(str[i] == '\n')

nl = nl + 1;

   }

printf("The number of lines in the text  are %d\n", nl);

}

 44. Write a C program to implement file handling.

#include<stdio.h>

#include<string.h>

int main()

{

    char *str;

    FILE *fp;//we declare a file pointer fp

    fp=fopen("add1.c","r");

    //This tries to open the file ReadExample in reading mode present at location

    //(we have to specify the location of the file before reading it)

    if(fp!=NULL)//if fp==NULL then there exists no such file

    {

    char c; //by using getc we read the file from character by character and then dipslay its contents

   while(c!=EOF)

    {

    c=getc(fp);

    printf("%c",c);

    }

    }

    else

    printf("No such file exists");

    fscanf(stdin,"%c",str);//The I/O devices can also be treated as files and we use file pointers stdin and stdout for them

    //So we use fscanf to read a string from the standard input device and store it in str

    fprintf(stdout,"%c",str);////Similarly we use fprintf to write a string to the standard output device

    fp=fopen("add1.c","w");

    //Now we open the file pointer in write mode and try to open(if exists) or create(if doesnot exist) file WriteExample

    fprintf(fp,"The Learning Point");

    putc('t',fp);//We use fprintf and putc to write contents in the file

    close(fp);

    getch();

    return 0;

}

45. Write a C program to add two numbers using pointers?

#include <stdio.h>

int main()
{
   int first, second, *p, *q, sum;

   printf("Enter two integers to add\n");
   scanf("%d%d", &first, &second);

   p = &first;
   q = &second;

   sum = *p + *q;

   printf("Sum of the numbers = %d\n", sum);

   return 0;
}

 

Comments

Popular posts from this blog