c lab programs

46
C Lab Programs By Tapasya Gutha in Gitam cse b · Edit Doc 1. Write a program, to read X,Y coordinates of three points and then calculate the area of the triangle formed by them and print the coordinates of the three points and the area of the triangle. What will be the output from your program if the three given points are in a straight line? VARIABLES USED: Variable name datatype purpose x1,x2,x3,y1,y2,y3 float coordinates of three points area float to store area of the triangle PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> main() { float x1,x2,x3,y1,y2,y3; float area;

Upload: rajasekhar-praneeth

Post on 23-Oct-2014

72 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: C Lab Programs

C Lab Programs

By Tapasya Gutha in Gitam cse b · Edit Doc

1. Write a program, to read X,Y coordinates of three points and then calculate the area of the triangle formed by them and print the coordinates of the three points and the area of the triangle. What will be the output from your program if the three given points are in a straight line? 

VARIABLES USED:

 

             Variable name                          datatype                         purpose

          x1,x2,x3,y1,y2,y3                     float                         coordinates of three points             

                        area                                float            to store area of the triangle

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<math.h>

main()

{

float x1,x2,x3,y1,y2,y3;

float area;

printf("Enter the coordinates x1,y1\n");

scanf("%f%f",&x1,&y1);

printf("Enter the coordinates x2,y2\n");

scanf("%f%f",&x2,&y2);

Page 2: C Lab Programs

printf("Enter the coordinates x3,y3\n");

scanf("%f%f",&x3,&y3);

area=0.5*(fabs(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)));

printf("the coordinates (x1,y1) are %f,%f\n",x1,y1);

printf("the coordinates (x2,y2) are %f,%f\n",x2,y2);

printf("the coordinates (x3,y3) are %f,%f\n",x3,y3);

printf("area of the triangle is %f",area);

if(area= =0)

printf("\nthree given points are in the straight line");

getch();

}

 

OUTPUT:

Enter the coordinates x1,y1

9 6

Enter the coordinates x2,y2

3 5

Enter the coordinates x3,y3

1 2

the coordinates (x1,y1) are 9.000000,6.000000

the coordinates (x2,y2) are 3.000000,5.000000

the coordinates (x3,y3) are 1.000000,2.000000

area=abs(0.5*((x1*y2+x2*y3+x3*y1)-(y1*x2+y2*x3+y3*x1)));                               

Page 3: C Lab Programs

2. Write a program to find the roots of a quadratic equation using if else and switch statements.

VARIABLES USED:

 

      Variable name                        datatype                         purpose

            a,b,c                                       float      coefficients of Quadratic equation

              D                                           float            to store determinant

            r1,r2                                        float                to store real roots

            a1,a2                                      float                to store imaginary roots

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<math.h>

main( )

{

float a,b,c,D,r1,r2,a1,a2;

int value;

printf("Enter the values of a,b,c");

scanf("%f%f%f",&a,&b,&c);

if(a==0)

{

 printf("It is not a quadratic equation");

}

Page 4: C Lab Programs

else

 {

   D=(b*b)-(4*a*c);

 if(D>0)

       value=1;

else     if(D<0)

                                    value=2;

else

       value=3;

switch(value)

            {

      case 1: printf("\nThe roots are real and distinct:");

                                                        r1=(-b+sqrt(D))/(2*a);

                                                        r2=(-b-sqrt(D))/(2*a);

                                                        printf("\nThe roots are:r1=%f\t r2=%f",r1,r2);

                                                        break;

      case 2:printf("\nThe roots are imaginary and distinct:");

                                                       a1=(-b)/(2*a);

                                                       a2= (sqrt(fabs(D)))/(2*a);

        printf("\nThe roots are r1=%f+i%f \t r2=%f-   

         i%f",a1,a2,a1,a2);

                                                         break;

        case 3:printf("\nThe roots are real and same:");

Page 5: C Lab Programs

                                                r1=r2=(-b)/(2*a);

                                                printf("\nThe roots are r1=%f \t r2=%f",r1,r2);

                                                break;

                                    }

 }

 getch();

 }

 

OUTPUT:

Enter the values of a,b,c1 5 6

The roots are real and distinct

The roots are:r1=-2.000000           r2=-3.000000

 

3. Write a program which generates one hundred random integers in the range of 1 to 100, store them in an array and then prints the average. Write three versions of the program using different loop constructs(e.g., for, while and do while)

 VARIABLES USED:

 

              Variable name                   data                           purpose

                     i                                  int                       to repeat loops                  

                   a[100]                               int              to store random numbers

                     total                               int        to find total of 100 random numbers

 

PROGRAM:  

Page 6: C Lab Programs

//Using for loop:

#include<stdio.h>

#include<stdlib.h>

#include <time.h>

void main()

{

int i,a[100],total=0;

randomize();

printf("\n Hundred random numbers between 1 and 100 are \n");

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

{

a[i]=random(100)+1;

printf("%d\t",a[i]);

total=total+a[i];

}

printf("the average of 100 random numbers is %d ",total/100);

}

 

//Using while loop:

#include<stdio.h>

#include<stdlib.h>

#include <time.h>

void main()

Page 7: C Lab Programs

{

int i=0,a[100],total=0;

randomize();

printf("\n Hundred random numbers between 1 and 100 are \n");

while(i<100)

{

a[i]=random(100)+1;

printf("%d\t",a[i]);

total=total+a[i];

i++;

}

printf("the average of 100 random numbers is %d ",total/100);

}

 

//Using do-while loop:

#include<stdio.h>

#include<stdlib.h>

#include <time.h>

void main()

{

int i=0,a[100],total=0;

randomize();

printf("\n Hundred random numbers between 1 and 100 are \n");

Page 8: C Lab Programs

do

{

a[i]=random(100)+1;

printf("%d\t",a[i]);

total=total+a[i];

i++;

}while(i<100);

printf("the average of 100 random numbers is %d ",total/100);

}

 

OUTPUT:

Hundred random numbers between 1 and 100 are

95        82        73        12        91        75        100     11        2          45

76        13        1          81        9          69        9          6          9          51

42        50        87        32        57        26        86        20        96        76

45        61        85        36        85        47        76        83        30        80

73        33        38        100     57        29        57        42        42        97

93        20        61        22        96        14        32        5          39        56       

5          55        26        74        25        76        82        90        50        33

53        14        39        20        199     45        3          52        82        60       

91        56        43        68        96        40        64        10        6          80

16        12        45        52        91        72        5          55        2          94

the average of 100 random numbers is 49

Page 9: C Lab Programs

 

4. Write a program for multiplication of square matrices

 

VARIABLES USED:

            Variable name                     datatype                  purpose

              x[ ],y[ ]                                   int                   to store given matrices elements

               z[ ]                                        int                   to store result matrix elements

               i,j,k                                      int                   to repeat loops

 

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main( )

{           void multi(int x[2][2],int y[2][2]);

            int x[2][2], y[2][2], i, j;

            printf("enter 4 elements in x :");

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

            for(j=0; j<2; j++)

                        scanf("%d",&x[i][j]);

            printf("enter 4 elements in y :");

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

Page 10: C Lab Programs

            for(j=0; j<2; j++)

                        scanf("%d",&y[i][j]);

            multi(x,y);

            getch();

}

void multi(int x[2][2],int y[2][2])

{           int i,j,z[2][2],k;

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

            {

                        for(j=0;j<2;j++)

                        {

                                    z[i][j] =0;

                                    for(k=0; k<2;k++)

            z[i][j] = z[i][j] + x[i][k] * y[k][j];

                        }

            }

            printf("\n Elements in x matrix : \n");

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

            {           for(j=0;j<2;j++)

                                    printf("%d\t",x[i][j]);

                        printf("\n");

            }

            printf("\n Elements in y matrix : \n");

Page 11: C Lab Programs

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

            {           for(j=0;j<2;j++)

                                    printf("%d\t",y[i][j]);

                        printf("\n");

            }

            printf("\n Elements in z matrix : \n");

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

            {           for(j=0;j<2;j++)

                                    printf("%d\t",z[i][j]);

                        printf("\n");

            }

}

 

 

OUTPUT:

Enter the 4 elements in x: 1 1 1 1

Enter the 4 elements in x: 1 1 1 1

 

Elements in x matrix:

1               1

1               1

Elements in y matrix:

1               1

Page 12: C Lab Programs

1               1

Elements in z matrix:

2               2

2         2

 

5. Write a program to find maximum and minimum elements with their positions in a given array and then sort the above array.

VARIABLES USED:

         

            Variable name                     datatype                          purpose

                        x[ ]                                 int                to store result matrix elements

                        i,j                                   int                 to repeat loops

                         t                                    int                 temporary variable            

                max,min                               int                 to store max and min values

            maxpos,minpos                                  int                to store max and min positions

 

 

 

 

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

Page 13: C Lab Programs

main()

{           int x[5],i,j,t,max,min,maxpos,minpos;

printf("enter 5 elements");

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

scanf("%d",&x[i]);

max=min=x[0];

for(i=1;i<=4;i++)

{

if(x[i]>max)

{

max=x[i];

maxpos=i;

}

if(x[i]<min)

{

min=x[i];

minpos=i;

}

 }

printf("max=%d is found at %d\n min=%d is found at %d",max,maxpos,min,minpos);

printf("\n before sorting elements:");

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

printf("%d\t",x[i]);

Page 14: C Lab Programs

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

{

for(j=i+1;j<=4;j++)

{

if(x[i]>x[j])

{

t=x[i];

x[i]=x[j];

x[j]=t;

}

}

}

printf("\n after sorting elements:");

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

printf("%d\t",x[i]);

getch();

}

OUTPUT:

Enter 5 elements:3 1 8 9 4

Max=9 is found at 3

Min=1 is found at 1

Before sorting elements:3  1  8  9  4

After sorting elements:1  3  4  8  9

Page 15: C Lab Programs

 

6.Write a program to insert an element into an array

VARIABLES USED:

 

                     Variable name                         datatype                        purpose

                                    a[10]                           int                                to store the array

                                    n                                  int                                size of the array

                                    i                                   int                             to repeat the loop

                                    item                            int                    new item to be inserted

                                    pos                              int                    to store the position

 

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],i,n,pos,item;

printf("Enter how many values u want:");

scanf("%d",&n);

printf("Enter %d elements into array a:\n",n);

Page 16: C Lab Programs

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

{

      scanf("%d",&a[i]);

}

printf("Enter which element u want to insert:");

scanf("%d",&item);

printf("Enter position for that element:");

scanf("%d",&pos);

for(i=n;i>=pos;i--)

{

        a[i]=a[i-1];

            }

a[pos-1]=item;

printf("\nAfter insertion the array elements are:");

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

    printf("  %d  ",a[i]);

getch();

}

 

OUTPUT:

Enter how many values u want:5

Enter 5 elements into array a:12 4 8 45 89

Enter which element u want to insert:43

Page 17: C Lab Programs

Enter position for that element:3

 

 

After insertion the array elements are: 12           4          43        8          45        89

 

7.Write a function for transposing a square matrix in place.(in place means that you are not allowed to have full temporary matrix)

VARIABLES USED:

                   

               Variable name                         datatype                        purpose

                        a[10][10]                                int                    to store the matrix   

                        m,n                                         int        to store the order of the matrix

                        i,j                                             int                   to repeat the loops

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

main()

{  int a[10][10],m,n,i,j;

  void trans(int a[10][10],int,int);

  printf("Enter Order of Matrix A:\n");

  scanf("%d %d",&m,&n);

  if(m= =n)

Page 18: C Lab Programs

  {

    printf("Enter The Elements into Matrix A:\n");

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

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

      scanf("%d",&a[i][j]);

    trans(a,m,n);

  }

  else

    printf("Not a Square Matrix");

  getch();

}

void trans(int a[10][10],int m,int n)

{

  int i,j;

  printf("The Transpose of Matrix A is:\n");

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

   {

   for(j=0;j<m;j++)

      printf("%3d",a[j][i]);

   printf("\n");

   }

getch();

}

Page 19: C Lab Programs

 

OUTPUT:

Enter Order of Matrix A:3 3

Enter The Elements into Matrix A:1 2 3 4 5 6 7 8 9

The Transpose of Matrix A is:

1 4 7

2 5 8

3 6 9

 

8.Write a program to print Fibonacci series using functions. 

 

VARIABLES USED:

 

                 Variable name               datatype                                      purpose

                          n                                int                       to store given terms

                         fib1,fib2                     int    to store first,second  temporary numbers

                         fib                               int                      to store fibonacci number                                                                                                                                                                                                   

                         i                                  int                       to repeat while loop

 

                             

PROGRAM:

#include<stdio.h>

Page 20: C Lab Programs

#include<conio.h>

void fibo(int, int, int);

void main ( )

{

int fib1=0, fib2=1, n;

printf("\n\t Enter number of terms - ") ;

scanf("%d",&n);

fibo(fib1,fib2,n);

getch () ;

}

void fibo(int fib1, int fib2,int n)

            { int i=1,fib;

/* generation of Fibonacci series */

do

{

printf("\t %d",fib1);

fib=fib1+fib2;

fib1=fib2 ;

fib2=fib;

i++;

} while (i<=n) ;

}

OUTPUT:

Page 21: C Lab Programs

enter n:5

 

0   1   1   2   3

 

9.Write a program to find the factorial of a given number using recursion.

VARIABLES USED:

 

                     Variable name                         datatype                        purpose

                                    n                                    int                              given number

                                    f                                     int                              to store factorial

 

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

void  main( )

{

            int fact(int n);

int  n, f;

            printf(“enter n :”);

            scanf(“%d”,&n);

            f = fact(n);

            printf(“factorial of the given no. is :%d”,f);

Page 22: C Lab Programs

getch( );

}

 

int fact(int n)

{

            int f;

            if(n = =0)

                        return(1);

else

            if(n = =1)

                        return(1);

            else

                        f = n * fact(n-1);

            return(f);

}

 

OUTPUT:

            enter n : 5

            factorial of the given no. is : 120

 

10.Write a program to find ncr " using non recursive function while finding the factorial value using recursion.

 VARIABLES USED:

                     Variable name                         datatype                        purpose

Page 23: C Lab Programs

                                    n,r                               int                           to find the ncr

                                    result                          int                    to store the result

 

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

int fact(int);

main()

{

  float ncr(int,int);

  int n,r;

  printf("Enter n and r values for ncr:");

  scanf("%d%d",&n,&r);

  if(n<r)

   printf("ncr is not possible");

  else

   printf("%dC%d is %.2f",n,r,ncr(n,r));

  getch();

}

float ncr(int n,int r)

{

  float result;

Page 24: C Lab Programs

  result=(float)fact(n)/(fact(n-r)*fact(r));

  return (result);

}

int fact(int n)

{

  int f;

  if((n==0)||(n==1))

    return 1;

  else

    f=n*fact(n-1);

  return f;

}

 

OUTPUT:

Enter n and r values for ncr:5 3

5C3 is 10.00

 

11.Write a program to find whether given string is palindrome or not without using string functions.

VARIABLES USED:

                     Variable name                         datatype                        purpose

                        a[50],b[50]                               char              to store the strings

                           length                                  int                    string length

                           i,j                                          int                    to repeat the loops

Page 25: C Lab Programs

                        flag                                         int                    to check for true or false

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

main()

{

 char a[50],b[50];

 int length=0,i=0,j=0,flag;

printf("Enter The String:");

 scanf("%s",a);

 while(a[i]!='\0')

 {

   i++;

 }

 length=i;

 printf("String Length is:%d",length);

 for(i=length-1;i>=0;i--)

  {

    b[j]=a[i];

    j++;

  }

 b[j]='\0';

Page 26: C Lab Programs

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

  {

    if(b[i]==a[i])

     flag=0;

    else

     {

      flag=1;

      break;

     }

  }

 if(flag==0)

   printf("\nThe String is Palindrome");

 else

   printf("\nThe String is Not Palindrome");

 printf("\nThe Reverse String of %s is %s",a,b);

 getch();

}

 

OUTPUT:

Enter The String:madam

String Length is:5

The String is Palindrome

The Reverse String of madam is madam

Page 27: C Lab Programs

 

 

12.Given an array of strings write a program to sort the string in dictionary order.

VARIABLES USED:

                     Variable name                         datatype                        purpose

          s[ 5][20],t[15]                       char array            to store strings

                           i,j,n                                          int                     to repeat loop                     

 

 

PROGRAM:

#include<stdio.h>

#include<string.h>

void main()

{

char s[5][20],t[15];

int i,j,n;

printf("how many strings do you want to sort?");

scanf("%d",&n);

printf("\nenter the strings");

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

scanf("%s",s[i]);

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

Page 28: C Lab Programs

{

for(j=1;j<n;j++)

{

if(strcmp(s[j-1],s[j])>0)

{

strcpy(t,s[j-1]);

strcpy(s[j-1],s[j]);

strcpy(s[j],t);

}

}

}

printf("the sorted strings are:\n");

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

printf("\n %s",s[i]);

}

OUTPUT:

how many strings do you want to sort?5

enter the strings  Visakhapatnam

Vijayawada

Hyderabad

Guntur

Tirupati

 

Page 29: C Lab Programs

The sorted strings are:

Guntur

Hyderabad

Tirupati

Vijayawada

Visakhapatnam

 

13.Develop a program to implement a structure to read and display the Name, Birthdate and salary of ten employers.

 

VARIABLES USED:

                     Variable name                         datatype                        purpose

                        name[20]                               char                            to store the name

                        day,month,year,salary                    int        to store the employee details

                                    i                                   int                    to repeat the loop

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

struct employee

{

char name[20];

int day,month,year,salary;

Page 30: C Lab Programs

};

main()

{

             int i;

struct employee e[10];

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

{

printf("enter %d employee name:",i+1);

scanf("%s",e[i].name);

printf("enter %d employee birthdate(dd-mm-yyyy):",i+1);

scanf("%d-%d-%d",&e[i].day,&e[i].month,&e[i].year);

printf("enter %d employee salary:",i+1);

scanf("%d",&e[i].salary);

}

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

printf("\n%s employee birthdate is %d-%d-%d and his or her salary is %d",e[i].name,e[i].day,e[i].month,e[i].year,e[i].salary);

getch();

}

 

OUTPUT:

enter 1 employee name: aaaa

enter 1 employee birthdate(dd-mm-yyyy): 1-1-2000

enter 1 employee salary: 1000

Page 31: C Lab Programs

enter 2 employee name: bbbb

enter 2 employee birthdate(dd-mm-yyyy): 1-2-2000

enter 2 employee salary: 2000

enter 3 employee name: cccc

enter 3 employee birthdate(dd-mm-yyyy): 1-3-2000

enter 3 employee salary: 3000

enter 4 employee name: dddd

enter 4 employee birthdate(dd-mm-yyyy): 1-4-2000

enter 4 employee salary: 4000

enter 5 employee name: eeee

enter 5 employee birthdate(dd-mm-yyyy): 1-5-2000

enter 5 employee salary: 5000

enter 6 employee name: ffff

enter 6 employee birthdate(dd-mm-yyyy): 1-6-2000

enter 6 employee salary: 6000

enter 7 employee name: gggg

enter 7 employee birthdate(dd-mm-yyyy): 1-7-2000

enter 7 employee salary: 7000

enter 8 employee name: hhhh

enter 8 employee birthdate(dd-mm-yyyy): 1-8-2000

enter 8 employee salary: 8000

enter 9 employee name: iiii

enter 9 employee birthdate(dd-mm-yyyy): 1-9-2000

Page 32: C Lab Programs

enter 9 employee salary: 9000

enter 10 employee name: jjjj

enter 10 employee birthdate(dd-mm-yyyy): 1-10-2000

enter 10 employee salary: 10000

aaaa employee birthdate is 1-1-2000 and his or her salary is 1000

bbbb employee birthdate is 1-2-2000 and his or her salary is 2000

cccc employee birthdate is 1-3-2000 and his or her salary is 3000

dddd employee birthdate is 1-4-2000 and his or her salary is 4000

eeee employee birthdate is 1-5-2000 and his or her salary is 5000

ffff employee birthdate is 1-6-2000 and his or her salary is 6000

gggg employee birthdate is 1-7-2000 and his or her salary is 7000

hhhh employee birthdate is 1-8-2000 and his or her salary is 8000

iiii employee birthdate is 1-9-2000 and his or her salary is 9000

jjjj employee birthdate is 1-10-2000 and his or her salary is 10000

 

14.Develop a program to display the Name, Marks in five subjects and total marks of ten students. (Using array of structures)

VARIABLES USED:

           Variable name                         datatype                        purpose

             name[20]                            char                            student name

            i                                               int                               to repeat loop

Sub1,sub2,sub3,sub3,sub4,sub5  int                              five subject marks

 

PROGRAM:

Page 33: C Lab Programs

#include<stdio.h>

main()

{

struct student

{

char name[20];

int sub1,sub2,sub3,sub4,sub5,total;

}student[10];

int i;

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

{

printf("enter %d student name and marks in 5 subjects:\n",i+1);

scanf("%s%d%d%d%d%d",student[i].name,&student[i].sub1,&student[i].sub2,&student[i].sub3,&student[i].sub4,&student[i].sub5);

student[i].total=student[i].sub1+student[i].sub2+student[i].sub3+student[i].sub4+student[i].sub5;

}

printf("\n\t\tThe Student Details");

            printf("\n\t\t-------------------------");

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

{

printf("\n\t\tname     ---   sub1         sub2   sub3   sub4   sub5   total\n");

printf("\t\t%s       ---    %d     %d      %d      %d      %d %d\n",student[i].name,student[i].sub1,student[i].sub2,student[i].sub3,student[i].sub4,student[i].sub5,student[i].total);

Page 34: C Lab Programs

}

getch();

}

OUTPUT:

enter 1 student name and marks in 5 subjects:aaa 10 10 10 10 10

enter 2 student name and marks in 5 subjects:bbb 20 20 20 20 20

enter 3 student name and marks in 5 subjects:ccc 30 30 30 30 30

enter 4 student name and marks in 5 subjects:ddd 40 40 40 40 40

enter 5 student name and marks in 5 subjects:eee 50 50 50 50 50

enter 6 student name and marks in 5 subjects:fff 60 60 60 60 60

enter 7 student name and marks in 5 subjects:ggg 70 70 70 70 70

enter 8 student name and marks in 5 subjects:hhh 80 80 80 80 80

enter 9 student name and marks in 5 subjects:iii 90 90 90 90 90

enter 10 student name and marks in 5 subjects:jjj 100 100 100 100 100

                        The Student Details

                        -------------------------

                        name     ---   sub1     sub2   sub3   sub4   sub5   total

                        aaa     ---       10        10        10        10        10        10

                        bbb     ---        20        20        20        20        20        20

                        ccc      ---        30        30        30        30        30        30

                        ddd     ---        40        40        40        40        40        40

                        eee     ---        50        50        50        50        50        50

                        fff        ---        60        60        60        60        60        60

Page 35: C Lab Programs

                        ggg     ---        70        70        70        70        70        70

                        hhh     ---        80        80        80        80        80        80

                        iii         ---        90        90        90        90        90        90

                        jjj         ---        100     100     100     100     100     100

 

15.Develop a program to read and write to a file

VARIABLES USED:

                     Variable name                         datatype                        purpose

                       *f1                                        FILE                          file pointer

                             c                                           char        to read and write character

PROGRAM:

#include<stdio.h>

main()

{

FILE *f1;

char c;

printf("Data Input\n");

f1=fopen("INPUT","w");

while((c=getchar())!=EOF)

putc(c,f1);

fclose(f1);

printf("\n Data Output\n\n");

f1=fopen("INPUT","r");

Page 36: C Lab Programs

while((c=getc(f1))!=EOF)

printf("%c",c);

fclose(f1);

}

 

 

OUTPUT:

Data Input

Good morning ctrl+z

Data Output

Good morning

 

16.Develop a program to create and count number of characters in a file

VARIABLES USED:

                     Variable name                         datatype                        purpose

                                    c                                  int                    to store the character

                                    co                                int                    to count the characters

                                    fp                                 FILE                file pointer

 

 

PROGRAM:

#include<stdio.h>

#include<conio.h>

Page 37: C Lab Programs

void main()

{

FILE *fp;

int c,co=0;

printf("Enter the chars:\n");

printf("Press ctrl+Z to stop entry\n");

fp=fopen("enter.txt","w");

while((c=getchar())!=EOF)

fputc(c,fp);

fclose(fp);

printf("\n");

fp=fopen("enter.txt","r");

while((c=fgetc(fp))!=EOF)

{

printf("%c",(char)c);

++co;

}

fclose(fp);

printf("\nThe no of chars in the file are %d",co);

getch();

}

 

OUTPUT:

Page 38: C Lab Programs

Enter the chars:

Press ctrl+Z to stop entry

Gitam University

Gitam University

The no of chars in the file are 17

Like · Follow Post · Report · November 12 at 10:28pm

o

o Sumanth Saggurthi , Abhinand Turlapati and Vinay Vithala like this.

o