C LAB SOLUTIONS

PROGRAMMING FOR PROBLEM SOLVING USING C LAB

Exercise 1:
  1. Write a C program to print a block F using hash (#), where the F has a height of six characters and width of five and four characters.
  2. #include<stdio.h>
    #include<conio.h>
     int main() 
     {
    clrscr();
    printf("######\n");
    printf("#\n");
    printf("#\n");
    printf("#####\n");
    printf("#\n");
    printf("#\n");
    printf("#\n");
    return(0);
    }
  3. Write a C program to compute the perimeter and area of a rectangle with a height of 7 inches and width of 5 inches.
  4. #include<stdio.h>
    #include<conio.h>
    int main() 
    {
     int width,height,area,perimeter;  
     clrscr();
     height = 7;
     width = 5;
     perimeter = 2*(height + width);
     printf("Perimeter of the rectangle = %d inches\n", perimeter);
    
     area = height * width;
     printf("Area of the rectangle = %d square inches\n", area);
    
    return(0);
    }
  5. Write a C program to display multiple variables.
  6. #include<stdio.h>
    #include<conio.h>
    int main() {
       char ch = 'B';
       printf("%c\n", ch); //printing character data
       //print decimal or integer data with d and i
       int x = 45, y = 90;
       printf("%d\n", x);
       printf("%i\n", y);
       long ax = 1234567890;
       printf("%ld\n", ax);//print long value
       float f = 12.67;
       printf("%f\n", f); //print float value
       printf("%e\n", f); //print in scientific notation
       int a = 67;
       printf("%o\n", a); //print in octal format
       printf("%x\n", a); //print in hex format
     return(0);
     }
Exercise 2:
  1. Write a C program to calculate the distance between the two points.
  2. #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    int main() {
    	float x1, y1, x2, y2, dist;
        clrscr();
    	printf("Enter x1 and X2: ");
    	scanf("%f %f", &x1,&x2);
    	printf("Input y1 and y2: ");
    	scanf("%f %f", &y1,&y2);
    	dist = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)); // Alternative sqrt(pow((x2-x1),2)+pow((y2-y1),2)))
    	printf("Distance between the said points: %.4f", sqrt(dist));
    	printf("\n");
    	return 0;
    }
    
  3. Write a C program that accepts 4 integers p, q, r, s from the user where r and s are positive and p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater than the sum of p and q print "Correct values", otherwise print "Wrong values".
  4. #include<stdio.h>
    #include<conio.h>
    int main() {
    	int p, q, r, s;
    		    
        printf("\nEnter p, q, r ,s values: "); 
        scanf("%d %d %d %d ", &p,&q,&r,&s);
    
    	if((q > r) && (s > p) && ((r+s) > (p+q)) && (r > 0) && (s > 0) && (p%2 == 0))
    	 {
    		printf("\nCorrect values\n");
    	} 
    	else {
    		printf("\nWrong values\n");
    	}
    	return 0;
    }
    
Exercise 3:
  1. Write a C program to convert a string to a long integer.
  2. #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
    {
    char str[20]="ll wor2ld s32andeep";
    clrscr();
    printf("%ld",strtol(str));
    }
      
  3. Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shape.
  4. #include<stdio.h>
    #include<conio.h>
    void main ()
    {
          int choice,r,l,w,b,h;
          float area;
          printf("Input 1 for area of circle\n");
          printf("Input 2 for area of rectangle\n");
          printf("Input 3 for area of triangle\n");
          printf("Input your choice : ");
          scanf("%d",&choice);
          switch(choice)
          {
               case 1:
                     printf("Input radious of the circle : ");
                     scanf("%d",&r);
                     area=3.14*r*r;
                     break;
                case 2:
                      printf("Input length and width of the rectangle : ");
                      scanf("%d%d",&l,&w);
                      area=l*w;
                      break;
                case 3:
                      printf("Input the base and hight of the triangle :");
                      scanf("%d%d",&b,&h);
                      area=.5*b*h;
                      break;
                 deafult:
                	 printf("Please choose Appropriate menu :");
                     break;
              }
              printf("The area is : %f\n",area);
    }
    
  5. Write a C program to calculate the factorial of a given number.
  6. #include<stdio.h>
    #include<conio.h>
    void main(){
      int i,f=1,num;
    
      printf("Enter the number : ");
      scanf("%d",&num);
    
      for(i=1;i<=num;i++)
      {
          f=f*i;
      }
      printf("The Factorial of %d is: %d\n",num,f);
    }
    
Exercise 4:
  1. Write a program in C to display the n terms of even natural number and their sum.
  2. #include<stdio.h>
    #include<conio.h>
    void main()
    {
       int i,n,sum=0;
       clrscr();
       printf("Input number of terms : ");
       scanf("%d",&n);
       printf("\nThe even numbers are :");
       for(i=1;i<=n;i++)
       {
         printf("%d ",2*i);
         sum+=2*i;
       }
       printf("\nThe Sum of even Natural Number upto %d terms : %d \n",n,sum);
    } 
    
  3. Write a program in C to display the n terms of harmonic series and their sum. 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.
  4. #include<stdio.h>
    #include<conio.h>
    void main()
    {
       int i,n;
       float s=0.0;
       printf("Input the number of terms : ");
       scanf("%d",&n);
       printf("\n\n");
       for(i=1;i<=n;i++)
       {
         s+=1/(float)i;
       }
         
       printf("\nSum of Series upto %d terms : %f \n",n,s);
    }  
    
  5. Write a C program to check whether a given number is an Armstrong number or not.
  6. #include<stdio.h>
    #include<conio.h>
    void main(){
        int num,r,sum=0,temp;
    
        printf("Input  a number: ");
        scanf("%d",&num);
    	temp=num;
        while(num>0)
        {
             r=num % 10;
             sum=sum+(r*r*r);
             num=num/10
        }
        if(sum==temp)
             printf("%d is an Armstrong number.\n",temp);
        else
             printf("%d is not an Armstrong number.\n",temp);
    
    }
    
Exercise 5:
  1. Write a program in C to print all unique elements in an array.
  2. #include<stdio.h>
    #include<conio.h>
    int main()
    {
        int arr1[100], n,ctr=0;
        int i, j;
           printf("Enter no of elements to be stored in the array: ");
           scanf("%d",&n);
           printf("Input %d elements in the array :\n",n);
           for(i=0;i<n;i++)
           {
    	      printf("element - %d : ",i);
    	      scanf("%d",&arr1[i]);
    	    }
        printf("\nThe unique elements found in the array are: \n");
        for(i=0; i<n; i++)
        {
            ctr=0;
            for(j=0; j<n; j++)
            {
                    if (i!=j)
                {
    		       if(arr1[i]==arr1[j])
                  {
                     ctr++;
                   }
                 }
            }
           if(ctr==0)
            {
              printf("%d ",arr1[i]);
            }
        }
           printf("\n\n");
    }
    
  3. Write a program in C to separate odd and even integers in separate arrays.
  4. #include<stdio.h>
    #include<conio.h>
    
    void main()
     {
        int arr1[10], even[10], odd[10];
        int i,j=0,k=0,n;
    
           printf("Input the number of elements to be stored in the array :");
           scanf("%d",&n);
       
           printf("Input %d elements in the array :\n",n);
           for(i=0;i<n;i++)
                {
    	      printf("element - %d : ",i);
    	      scanf("%d",&arr1[i]);
    	    }
    
        for(i=0;i<n;i++)
        {
    	if (arr1[i]%2 == 0)
    	{
    	   even[j] = arr1[i];
    	   j++;
    	}
    	else
    	{
    	   odd[k] = arr1[i];
    	   k++;
    	}
        }
    
        printf("\nThe Even elements are : \n");
        for(i=0;i<j;i++)
        {
    	printf("%d ",even[i]);
        }
    
        printf("\nThe Odd elements are :\n");
        for(i=0;i<k;i++)
        {
    	printf("%d ", odd[i]);
        }
        printf("\n\n");	
     }
    
  5. Write a program in C to sort elements of array in ascending order.
  6. #include<stdio.h>
    #include<conio.h>
    
    void main()
    {
        int arr1[100];
        int n, i, j, tmp;
    	
    	
           printf("\n\nsort elements of array in ascending order :\n ");
           printf("----------------------------------------------\n");	
    
        printf("Input the size of array : ");
        scanf("%d", &n);
    
           printf("Input %d elements in the array :\n",n);
           for(i=0;i<n;i++)
                {
    	      printf("element - %d : ",i);
    	      scanf("%d",&arr1[i]);
    	    }
    
        for(i=0; i<n; i++)
        {
            for(j=i+1; j<n; j++)
            {
                if(arr1[j] <arr1[i])
                {
                    tmp = arr1[i];
                    arr1[i] = arr1[j];
                    arr1[j] = tmp;
                }
            }
        }
        printf("\nElements of array in sorted ascending order:\n");
        for(i=0; i<n; i++)
        {
            printf("%d  ", arr1[i]);
        }
    	        printf("\n\n");
    }
    
Exercise 6:
  1. Write a program in C for multiplication of two square Matrices.
  2. #include<stdio.h>
    #include<conio.h>
    void main()
    {
      int A[50][50],B[50][50],C[50][50],i,j,k,r1,c1,r2,c2,sum=0;
      
      printf("\nInput the rows and columns of first matrix : ");
      scanf("%d %d",&r1,&c1);
      printf("\nInput the rows and columns of second matrix : ");
      scanf("%d %d",&r2,&c2);
      if(c1!=r2){
          printf("Mutiplication of Matrix is not possible.");
          printf("\nColumn of first matrix and row of second matrix must be same.");
      }
      else
          {
           printf("Input elements in the first matrix :\n");
           for(i=0;i<r1;i++)
            {
                for(j=0;j<c1;j++)
                {
    	           printf("element - [%d],[%d] : ",i,j);
    	           scanf("%d",&A[i][j]);
                }
            }   
           printf("Input elements in the second matrix :\n");
           for(i=0;i<r2;i++)
            {
                for(j=0;j<c2;j++)
                {
    	           printf("element - [%d],[%d] : ",i,j);
    	           scanf("%d",&B[i][j]);
                }
            }    
     	 printf("\nThe First matrix is :\n");
      		for(i=0;i<r1;i++)
        		{
          		printf("\n");
          		for(j=0;j<c1;j++)
              	printf("%d\t",A[i][j]);
        		}
      
      	printf("\nThe Second matrix is :\n");
      		for(i=0;i<r2;i++)
        		{
          		printf("\n");
          		for(j=0;j<c2;j++)
          		printf("%d\t",B[i][j]);
        		}
    //multiplication of matrix
          for(i=0;i<r1;i++)
          {
              for(j=0;j<c2;j++)
              {
               C[i][j]=0;
                  for(k=0;k<c1;k++)    
                         {  
                           C[i][j] +=A[i][k]*B[k][j];
                               
                         }
               }
           }
      printf("\nThe multiplication of two matrices is : \n");
      for(i=0;i<r1;i++)
         {
            printf("\n");
            for(j=0;j<c2;j++)
             {
               printf("%d\t",C[i][j]);
             }
         }
      }
    printf("\n\n");
    }
    
  3. Write a program in C to find transpose of a given matrix.
  4. #include<stdio.h>
    #include<conio.h>
    void main()
      
      {
      int A[50][50],B[50][50],i,j,r,c;
      
          printf("\nInput the rows and columns of the matrix : ");
           scanf("%d %d",&r,&c);
    
           printf("Input elements in the first matrix :\n");
           for(i=0;i<r;i++)
            {
                for(j=0;j<c;j++)
                {
    	           printf("element - [%d],[%d] : ",i,j);
    	           scanf("%d",&A[i][j]);
                }
            } 
      
      for(i=0;i<r;i++)
         {
          for(j=0;j<c;j++)
                {
                       B[j][i]=A[i][j];
                }
          }
    
          printf("\n\nThe transpose of a matrix is : ");
          for(i=0;i<c;i++)
          {
          printf("\n");
          for(j=0;j<r;j++){
               printf("%d\t",B[i][j]);
          }
      }
          printf("\n\n");
    }
    
Exercise 7:
  1. Write a program in C to search an element in a row wise and column wise sorted matrix.
  2. #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[10][10],i,j,m,n,k,temp=0;
    clrscr();
    printf("Enter no of rows and columns");
    scanf("%d %d",&m,&n);
    printf("Enter Array Elements");
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
    scanf("%d",&a[i][j]);
    }
    }
    printf("search n element in row wise and column wise");
    scanf("%d",&k);
    for(i=0;i<m;i++)
    {
    for(j=0;j<n;j++)
    {
    if(a[i][j]==k)
    {
    printf(" Element found at[%d][%d] index",i,j);
    temp++;
    }
    }
    }
    if(temp==0)
    printf("Element not foubd");
    getch();
    }
    
  3. Write a program in C to print individual characters of string in reverse order.
  4. #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    void main()
    {
        char str[100]; 
        int l,i;
    
           printf("Input the string : ");
           gets(str);
    	   l=strlen(str);
    	   printf("The characters of the string in reverse are : \n");
           for(i=l;i>=0;i--)
            {
              printf("%c  ", str[i]);
            }
        printf("\n");
    }
    
Exercise 8:
  1. Write a program in C to compare two strings without using string library functions.
  2. #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main() {
       char str1[30], str2[30];
       int i=0,l;
       clrscr();
       printf("\nEnter two strings :");
       gets(str1);
       gets(str2);
    
       l = strlen(str1);
       while (str1[i] == str2[i] && str1[i] != '\0')
          i++;
       if (l==i)
    	printf("strings are equal");
       else if (str1[i] > str2[i])
          printf("str1 > str2");
       else if (str1[i] < str2[i])
          printf("str1 < str2");
       return (0);
    }
  3. Write a program in C to copy one string to another string.
  4. #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    void main()
    {
        char str1[100], str2[100];
        int  i;
    
    
           printf("Input the string : ");
           gets(str1);
    	   
        i=0;
        while(str1[i]!='\0')
        {
            str2[i] = str1[i];
            i++;
        }
    
        //Makes sure that the string is NULL terminated
        str2[i] = '\0';
    
        printf("\nThe First string is : %s\n", str1);
        printf("The Second string is : %s\n", str2);
        
    }
    
Exercise 9:
  1. Write a C Program to Store Information Using Structures with Dynamically Memory Allocation
  2. #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
    struct course
    {
    int marks;
    char sub[30];
    };
    struct course *ptr;
    int i, no;
    printf("Enter number of records: ");
    scanf("%d", &no);
    ptr = (struct course*) malloc (no * sizeof(struct course));
    for(i = 0; i < no; i++)
    {
    printf("Enter name of the subject and marks respectively:\n");
    scanf("%s %d", &(ptr+i)->sub, &(ptr+i)->marks);
    }
    printf("Displaying Information:\n");
    for(i = 0; i < no ; i++)
    printf("%s\t%d\n", (ptr+i)->sub, (ptr+i)->marks);
    return 0;
    }
    
  3. Write a program in C to demonstrate how to handle the pointers in the program.
  4. #include<stdio.h>
    #include<conio.h>
    void main()
    {
       int *p;
       int a=10;
       p=&a;
       printf("\n\n Pointer : How to  handle the pointers in the program :\n"); 
       printf("------------------------------------------------------------\n");
       printf(" Here in the declaration p = int pointer, int a= 10\n\n");
        
       printf(" Address of a : %p\n",&a);
       printf(" Value of a : %d\n\n",a);
      
       printf(" Now p is assigned with the address of a.\n");
       printf(" Address of pointer p : %p\n",p);
       printf(" value of pointer p : %d\n\n",*p);
     }
    
Exercise 10:
  1. Write a program in C to demonstrate the use of & (address of) and *(value at address) operator.
  2. #include<stdio.h>
    #include<conio.h>
    void main()
    {
      int a=10;
      float b = 21.68;
      char ch = 'g';
      
      int *pt1;
      float *pt2;
      char *pt3;
      pt1= &a;
      pt2=&b;
      pt3=&ch;
      printf ( " a = %d\n",a);
      printf ( " b = %f\n",b);
      printf ( " ch = %c\n",ch);
      printf("\n Using & operator :\n"); 
      printf("-----------------------\n");  
      printf ( " address of a = %p\n",&a);
      printf ( " address of b = %p\n",&b);
      printf ( " address of ch = %p\n",&ch);
      printf("\n Using & and * operator :\n"); 
      printf("-----------------------------\n"); 
      printf ( " value at address of a = %d\n",*(&a));
      printf ( " value at address of b = %f\n",*(&b));
      printf ( " value at address of ch = %c\n",*(&ch));
      printf("\n Using only pointer variable :\n"); 
      printf("----------------------------------\n"); 
      printf ( " address of a = %p\n",pt1);
      printf ( " address of b = %p\n",pt2);
      printf ( " address of ch = %p\n",pt3);
      printf("\n Using only pointer operator :\n"); 
      printf("----------------------------------\n"); 
      printf ( " value at address of a = %d\n",*pt1);
      printf ( " value at address of b= %f\n",*pt2);
      printf ( " value at address of ch= %c\n\n",*pt3);
    }
    
  3. Write a program in C to add two numbers using pointers.
  4. #include<stdio.h>
    #include<conio.h>
    void main()
    {
       int m,n,*p,*q,sum;
       
       printf("\n\n Pointer : Add two numbers :\n"); 
       printf(" Input the first number : ");
       scanf("%d", &m);
       printf(" Input the second  number : ");
       scanf("%d", &n);   
     
       p = &m;
       q = &n;
     
       sum = *p + *q;
     
       printf(" The sum of the entered numbers is : %d\n\n",sum);
     
    }
    
    
Exercise 11:
  1. Write a program in C to add numbers using call by reference.
  2. #include<stdio.h>
    #include<conio.h>
    int addNum(int *, int *);
     
    int main()
    {
       int n1, n2, sum;
       
       printf(" Add two numbers using call by reference:\n"); 
         
     
       printf(" Input the first number : ");
       scanf("%d", &n1);
       printf(" Input the second  number : ");
       scanf("%d", &n2);   
       sum = addNum(&n1, &n2);
       printf(" The sum of %d and %d  is %d\n\n", n1, n2, sum);
       return 0;
    }
    int addNum(int *n1, int *n2) 
    {
       int sum;
       sum = *n1 + *n2;
       return sum;
    }
    
  3. Write a program in C to find the largest element using Dynamic Memory Allocation.
  4. #include<stdio.h>
    #include<conio.h>
    int main()
    {
        int i,n;
        int *p;
    	printf("\nFind the largest element using Dynamic Memory Allocation :\n"); 
        scanf("%d",&n);
        
        p=(int*)malloc(n*sizeof(int));  
        
        if(p==NULL)
        {
            printf(" No memory is allocated.");
            exit(0);
        }
        printf("\n");
        for(i=0;i<n;++i)  
        {
           printf(" Element %d: ",i+1);
           scanf("%d",p+i);
        }
        for(i=1;i<n;++i)  
        {
           if(*p < *(p+i)) 
               *p=*(p+i);
        }
        free(p);
        printf(" The Largest element is :  %d \n\n",*p);
        return 0;
    }
    
Exercise 12:
  1. Write a program in C to swap elements using call by reference.
  2. #include<stdio.h>
    void swap(int *x,int *y);
    int main()
    {
        int n1,n2;
        printf("\n\n Pointer : Swap elements using call by reference :\n"); 
    	
        printf(" Input the value of 1st element : ");
        scanf("%d",&n1);
        printf(" \n Input the value of 2nd element : ");
        scanf("%d",&n2);
    	
        printf("\n The value before swapping are :\n");
        printf(" element 1 = %d\n element 2 = %d\n",n1,n2);
        swap(&n1,&n2);
        printf("\n The value after swapping are :\n");
        printf(" element 1 = %d\n element 2 = %d\n ",n1,n2);
        return 0;
    }
    void swap(int *x,int *y)
    {
        int tmp;
        tmp=*y;
        *y=*x;
        *x=tmp;
    
    }
    
    
  3. Write a program in C to count the number of vowels and consonants in a string using a pointer.
  4. 
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        char str1[50];
        char *p;
        int  vow=0,const=0;
    	    
        printf(" Input a string: ");
        gets(str1);   
        p=str1;
         
        while(*p!='\0')
        {
            if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U' ||*p=='a' 
                  ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
                vow++;
            else
                const++;
            p++; 
        }
         
        printf(" Number of vowels : %d\n Number of consonants : %d\n",vow,const);        
        return 0;
    }
    
    
Exercise 13:
  1. Write a program in C to show how a function returning pointer.
  2. #include<stdio.h>
    #include<conio.h>
    int* Largest(int*, int*);
    void main()
    {
     int a,b;
     int *res;
     printf(" Enter the first number : ");
     scanf("%d", &a);
     printf(" Enter the second  number : ");
     scanf("%d", &b); 	
    
     res=Largest(&a, &b);
     printf(" The number %d is larger.  \n\n",*res);
    }
    
    int* Largest(int *n1, int *n2)
    {
     if(*n1 > *n2)
      return n1;
     else
      return n2;
    }
    
    
    
  3. Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using malloc( ) function.
  4. #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main()
    {
        
        int i, n, sum = 0;
        int *p;
    
        printf("Enter number of elements : ");
        scanf("%d", &n);
        p = (int *)malloc(n * sizeof(int));
        for (i = 0; i < n; i++)
        {
            printf("Enter element %d : ", (i + 1));
            scanf("%d", p + i);
            sum += *(p + i);
    
     }
        printf("sum is %d \n", sum);
        free(p);
        return 0;
    }
    
Exercise 14:
  1. Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using calloc( ) function. Understand the difference between the above two programs
  2. #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    int main()
    {
        
        int i, n, sum = 0;
        int *p;
    
        printf("Enter number of elements : ");
        scanf("%d", &n);
        p = (int *)calloc(n, sizeof(int));
        for (i = 0; i < n; i++)
        {
            printf("Enter element %d : ", (i + 1));
            scanf("%d", p + i);
            sum += *(p + i);
    
     }
        printf("sum is %d \n", sum);
        free(p);
        return 0;
    }
    
  3. Write a program in C to convert decimal number to binary number using the function.
  4. #include<stdio.h>
    #include<stdlib.h>
    void DtoB(int n);    
        void main(){  
        int n;    
        system ("cls");  
        printf("Enter the number to convert: ");    
        scanf("%d",&n);   
        DtoB(n);
        }
        
     void DtoB(int n)
     {
     int a[10],i;
        for(i=0;n>0;i++)    
        {    
        a[i]=n%2;    
        n=n/2;    
        }    
        printf("\nBinary of Given Number is=");    
        for(i=i-1;i>=0;i--)    
        {    
        printf("%d",a[i]);    
        }    
          
      }  
        
Exercise 15:
  1. Write a program in C to check whether a number is a prime number or not using the function.
  2. #include<stdio.h>
    #include<conio.h>
    int PrimeOrNot(int);
    int main()
    {
        int n,prime;
        printf(" Enter a number : ");
        scanf("%d",&n);
        prime = PrimeOrNot(n);
       if(prime==1)
            printf(" The number %d is a prime number.\n",n1);
       else
          printf(" The number %d is not a prime number.\n",n1);
       return 0;
    }
    int PrimeOrNot(int n)
    {
        int i=2;
        while(i<=n/2)
        {
             if(n%i==0)
                 return 0;
             else
                 i++;
        }
        return 1;
    }
    
    
    
  3. Write a program in C to get the largest element of an array using the function.
  4. #include<stdio.h>
    #include<conio.h>
    
    int Large(int []);
    int n;
    
    int main()
    {
        int arr[10],big,i;
        printf(" Input the number of elements to be stored in the array :");
        scanf("%d",&n);
       
        printf(" enter elements in the array :\n");
           for(i=0;i<n;i++)
            {
    	      printf(" element - %d : ",i);
    	      scanf("%d",&arr[i]);
    	    }
    
        printf(" The largest element in the array is : %d\n\n",Large(arr));
        return 0;
    }
    int Large(int arr[])
    {
        int i=1,big;
        big=arr1[0];
        while(i < n)
    	{
          if(big<arr[i])
               big=arr[i];
          i++;
        }
        return big;
    }
    
Exercise 16:
  1. Write a program in C to append multiple lines at the end of a text file.
  2. #include<stdio.h>
    #include<conio.h>
    
    int main ()
    {
      FILE * fptr;
      int i,n;
      char str[100];
      char fname[20];
      char str1;
    
      printf(" Input the file name to be opened : ");
      scanf("%s",fname);
      fptr = fopen(fname, "a");
        printf(" Input the number of lines to be written : ");
        scanf("%d", &n);
        printf(" The lines are : \n");
        for(i = 0; i<n+1;i++)
        {
        fgets(str, sizeof str, stdin);
        fputs(str, fptr);
      }
      fclose (fptr);
    
    	fptr = fopen (fname, "r");
    	printf("\n The content of the file %s is  :\n",fname);
    	str1 = fgetc(fptr);
    	while (str1 != EOF)
    		{
    			printf ("%c", str1);
    			str1 = fgetc(fptr);
    		}
        printf("\n\n");
        fclose (fptr);
    
      return 0;
    }
    
  3. Write a program in C to copy a file in another name.
  4. #include<stdio.h>
    #include<conio.h>
    #include <stdlib.h>
    int main(){
       FILE *fptr1, *fptr2;
       char filename[100], c;
       printf("Enter the filename to open for reading \n");
       scanf("%s",filename);
       // Open one file for reading
       fptr1 = fopen(filename, "r");
       if (fptr1 == NULL){
          printf("Cannot open file %s \n", filename);
          exit(0);
       }
       printf("Enter the filename to open for writing \n");
       scanf("%s", filename);
       // Open another file for writing
       fptr2 = fopen(filename, "w");
       if (fptr2 == NULL){
          printf("Cannot open file %s \n", filename);
          exit(0);
       }
       // Read contents from file
       c = fgetc(fptr1);
       while (c != EOF){
          fputc(c, fptr2);
          c = fgetc(fptr1);
       }
       printf("\nContents copied to %s", filename);
       fclose(fptr1);
       fclose(fptr2);
       return 0;
       }
    
  5. Write a program in C to remove a file from the disk.
  6. #include<stdio.h>
    #include<conio.h>
    #include <stdlib.h>
    void main()
    {
    	int status;
    	char fname[20];
    	printf(" Input the name of file to delete : ");
    	scanf("%s",fname);
    	status=remove(fname);
    	if(status==0)
    	{
    printf(" The file %s is deleted successfully..!!\n\n",fname);
    	}
    	else
    	{
    		printf(" Unable to delete file %s\n\n",fname);
    	}
    }
    

1 comment: