Answers To Exercises in C Book

Chapter 1 - Introduction To C
Chapter 2 - First C Program
Chapter 3 - Language Elements
Chapter 4 - Operators
Chapter 5 - Control Statements
Chapter 6 - Looping Structures
Chapter 7 - Character Handling
Chapter 8 - Arrays
Chapter 9 - Multidimensional Arrays
Chapter 10 - String Handling
Chapter 11 - User-defined functions
Chapter 12 - Storage Classes
Chapter 13 - Pointers
Chapter 14 - Pointer vs. array
Chapter 15 - Structures
Chapter 16 - Union, Typedef, and Enumeration
Chapter 17 - Pre-processor Commands
Chapter 18 - File Handling
Chapter 19 - Binary files and random access
Chapter 20 - Command line arguments
Chapter 21 - Dynamic Memory Allocation
Chapter 22 - Linked List

Chapter 1 - Introduction To C

I. Fill in the blanks.

  1. Compiler or interpreter
  2. Borland Inc.
  3. They use more operators, which make program very compact.
  4. Functions

Chapter 2 - First C Program

I. Fill in the blanks

  1. /* */
  2. \t
  3. main
  4. A

II. Write programs.

  1. Display your name and address in two different lines.
    main()
    {
          printf(“P.Srikanth”);
          printf(“\n304, Srikanth Technologies, Eswar Paradise, Dwarkanagar, Vizag-16”);
    }
    
    
  2. Display your name on the first row and address on the second line. Separate each part of the address using tab.
    main()
    {
          printf(“P.Srikanth”);
          printf(“\n304\tSrikanth Technologies\tEswar Paradise\tDwarkanagar\tVizag-16”);
    }
    
  3. Ring bell for five time and then display message Bell is over.
    main()
    {
          printf(“\a \a \a \a\a Bell is over”);
    }
    

Chapter 3 - Language Elements

I. Fill in the blanks.

  1. u%
  2. 8 bytes
  3. _ (underscore)
  4. Character

II. Identify invalid identifiers.

  1. Valid
  2. Valid
  3. Invalid - main is a keyword
  4. Valid
  5. Invalid – identifier must not start with a digit
  6. Valid

III. Write program.

  1. Accept two integers and display octal equivalent of first and hex equivalent of second.
    main()
    {
       int  n1,n2;
    
         /* Accept two numbers */
         printf("Enter two numbers :");
         scanf("%d%d",&n1,&n2);
      
         printf("Octal of %d = %o  Hex of %d = %x",n1,n1,n2,n2);
    }
    
  2. Accept amount and discount percentage and display net amount.
    main()
    {
       float  amt, disper,net;
    
         /* accept amount and discount percentage */
    
         printf("Enter amount and discount percentage : ");
         scanf("%f%f", &amt,&disper);
      
         net =  amt  - ( amt * disper / 100 );
    
         printf("Net amount : %f ", net);
    }
    
  3. Take two numbers and display them in the reverse order of accepted order.
    main()
    {
       int n1,n2;
    
         /* accept two numbers */
    
         printf("Enter two numbers  ");
         scanf("%d%d", &n1, &n2);
      
         /* print numbers in the reverse order */   
         printf("Numbers in reverse order : %d %d ", n2, n1);
    }
    

Chapter 4 - Operators

I. Fill in the blanks

  1. &&
  2. 10 and store the result back in A
  3. a = b is assigning value of b to a, a == b is comparing the value of a with b for equality
  4. pre-fix notaion
  5. An operator that takes only one operand, such as ++ and --.

II. Write Programs.

  1. Take two integers and display the following.
    Sum, difference, product, quotient, and remainder.
    main()
    {
       int n1,n2;
    
         /* accept two numbers */
    
         printf("Enter two numbers  ");
         scanf("%d%d", &n1, &n2);
      
         /* print sum, differnce, product and quotient and remainder */   
         printf("\nSum = %d ", n1 + n2);
         printf("\nDifference = %d ", n1 - n2);
         printf("\nProduct = %d ", n1 * n2);
         printf("\nQuotient = %d ", n1 / n2);
         printf("\nRemainder = %d ", n1 % n2);
    }
    
  2. Take a number and display its square.
    main()
    {
       int n;
    
         /* accept a number */
    
         printf("Enter a number ");
         scanf("%d",&n);
      
         printf("\nSqare of %d = %d ", n , n * n );
    
    }
    
    
  3. Take principal amount, rate of interest, no. of years and display simple interest.
    main()
    {
       float pamt, rate, interest;
       int  ny;
       
         printf("Enter principal amount : ");
         scanf("%f",&pamt);
         printf("Enter interest rate    : ");
         scanf("%f",&rate);
         printf("Enter number of years  : ");
         scanf("%d",&ny);
      
         interest =  pamt * rate * ny / 100;
    
         printf("Total Simple Interest = %f", interest );
    
    }
    
    
  4. Take value for ‘a’ and ‘b’ and then display the result for a2 + b2
    main()
    {
        int a,b;
       
         printf("Enter value for a and  b : ");
         scanf("%d%d",&a,&b);
      
         printf(" Result of  =  %d " ,  a * a + b * b);
    
    }
    

Chapter 5 - Control Structure

Write Programs

  1. Accept a number display whether it is an even number or odd number. Use conditional expression and normal if statement.
    
    main()
    {
        int n;
       
         printf("Enter a number  : ");
         scanf("%d",&n);
      
         if ( n % 2 == 0 )
             printf(" %d is even number");
         else
             printf(" %d is odd number");
    
    }
    
    
    /* using conditional expression */
    
    main()
    {
        int n;
       
         printf("Enter a number  : ");
         scanf("%d",&n);
      
         printf(" %d is %s ", n, n % 2 == 0 ? "Even" : "Odd");
    }
    
    
  2. Accept marks in subject and subject code (1 to 4) and display whether student passed or failed. Pass mark is based on subject code as follows. 1 - 40, 2 - 50 , 3 - 45 , 4 - 55.
    main()
    {
        int marks,pm,subject;
       
         printf("Enter subject code [1-4]: ");
         scanf("%d",&subject);
    
         printf("Enter marks : ");
         scanf("%d",&marks);
      
         switch(subject)
         {
           case 1: 
                   pm = 40; break;
           case 2: 
                   pm = 50; break;
           case 3: 
                   pm = 45; break;
           case 4: 
                   pm = 55; break;
         }
    
         if (marks >= pm )
            printf(" Passed");
         else
            printf(" Failed");
    
    }
    
    
  3. Accept two numbers and display the relationship between these two numbers. The output should display one of the messages; "Both number are equal", "First number is bigger" or "Second number is bigger".
    main()
    {
        int n1,n2;
       
         printf("Enter two numbers : ");
         scanf("%d%d",&n1,&n2);
    
    
         if ( n1 == n2 )
             printf("Both the numbers are equal");
         else
           if ( n1 > n2 )
             printf("First number is bigger");
           else
             printf("Second number is bigger");
    
    
    }
    
  4. Accept three numbers from user and display the biggest of three numbers.
    
    main()
    {
        int n1,n2,n3,big;
       
         printf("Enter three numbers : ");
         scanf("%d%d%d",&n1,&n2,&n3);
    
         big = n1 > n2 ? n1 : n2;
         big = n3 > big ? n3: big;
    
         printf(" The biggest of %d %d %d is %d ", n1,n2,n3,big);
    
    }
    

Chapter 6 - Looping Structures

I. Fill in the blanks

  1. do..while
  2. break
  3. initialization, condition and updation

II.Write programs

  1. Display numbers from 100 to 1.
    
    main()
    {
      int i;
    
         for ( i = 100 ; i >= 0 ; i --)
            printf("%d\n", i);
    }
    
  2. Accept a number and display sum of numbers from 1 to that number.
    main()
    {
      int i,n,sum=0;
    
         printf("Enter a number: ");
         scanf("%d", &n);
    
         for ( i = 1 ; i <= n ; i ++)
             sum += i;
    
         printf(" Sum of 1 to %d = %d ", n,sum);
    }
    
  3. Display all even numbers in the range 100 to 200.
    main()
    {
      int i;
    
         for ( i = 100 ; i <= 200 ; i += 2)
           printf("%d\n",i);
    }
    
  4. Take numbers from user until user enters 0 and display sum and average of accepted numbers.
    main()
    {
      int i=0,n;
      float sum=0;
    
     
         while (1)  /* always true loop */
         {
           printf("Enter a number [0 to stop] : ");
           scanf("%d", &n);
    
           if ( n == 0 )
               break;    /* terminate loop */
    
           sum += n;
           i++;  
    
         }
    
         printf(" Sum = %f Average = %f ",sum, sum / i);      
    
    }
    
  5. Take 10 numbers and display the biggest and smallest of the given numbers.
    main()
    {
      int i,n,big,small;
    
    
         big = -32768;   /* set big to smallest possible number */
         small = 32767;  /* set small to biggest possible number */
        
        
         for ( i=1; i <= 10 ; i++)
         {
             printf("Enter a number : ");
             scanf("%d", &n);
    
             if(n > big )
                 big = n;
    
             if ( n < small )
                  small = n;
         }
    
         printf(" The smallest = %d  and biggest = %d ", small, big);
    
    }
    
  6. Display table for 7 up to 20.
    Sample output:
    7 * 1 = 7
    7 * 2 = 14
    ...
    7 * 20  = 140
    
    
    main()
    {
      int i;
    
       for ( i = 1; i <= 20 ; i++)
         printf(" 7 * %d  = %d\n", i, 7 * i); 
    
     }
    
  7. Display numbers in the following format.
    1  2   3  4   5
    1  2   3  4   5  
    1  2   3  4   5 
    1  2   3  4   5
    1  2   3  4   5
    
    main()
    {
      int i,j;
    
       for ( i = 1; i <= 5 ; i++) 
       {
         printf("\n");
    
         for ( j = 1 ; j <= 5 ; j ++)
           printf("%5d", j);
       }
    
    }
    

Chapter 7 - Character Handling

I. Fill in the blanks.

  1. 50
  2. iscntrl()
  3. Standard input device.
  4. Echo.
  5. The character will be read from the file on the disk.
  6. printf(). For example, printf(“%d”, ‘A’);

II. Write programs.

  1. Accept 10 characters and display number of capital, small letters and digits.
    #include <ctype.h>
    #include <stdio.h>
    
    main()
    {
      char ch;
      int  i,nu,nd,nl;
    
        nu = nd = nl = 0;
        for ( i = 1 ; i <= 10 ; i ++)
        {
             ch = getchar();
             if (isupper(ch))
                  nu ++;
             else
               if (islower(ch))
                    nl ++;
               else
                if (isdigit(ch))
                      nd ++;
        }
    
    }
    
  2. Accept a character and convert that character to uppercase if it in lowercase or convert it to lowercase if it is in uppercase.
    #include <ctype.h>
    #include <stdio.h>
    
    
    main()
    {
      char ch;
        
         printf("Enter a character : ");   
         ch = getchar();
    
         if ( isupper(ch))
              putchar( tolower(ch));
         else
            if (islower(ch))
              putchar(toupper(ch));
    
    }
    
  3. Accept characters till the user inputs '*' and convert characters to lowercase.
    #include <ctype.h>
    #include <stdio.h>
    
    
    main()
    {
      char ch;
        
    
        while (1)
        {
         printf("Enter a character : ");   
         ch = getchar();
    
         fflush(stdin);   /* clear keyboard buffer */
    
         if ( ch == '*' )
               break;
    
         putch( tolower(ch));
        }
    
    }
    
    

Chapter 8 - Arrays

Write Programs

  1. Accept 20 numbers into array and display the sum and average along with numbers.
    main()
    {
       int ar[20]; 
       int i, sum = 0;
    
          for ( i = 0 ; i < 20 ; i++)
          {
             printf("Enter number for [%d] element : ",i);
             scanf("%d", &ar[i]);
    
             sum += ar[i];
          }
    
          printf("Entered Numbers \n");
       
          for ( i = 0 ; i < 20 ; i++)
          {
             printf("%d\n", ar[i]);
          }    
    
          printf("Sum = %d  Average = %d ", sum, sum  / 20);
    
    }
    
  2. Take an array of 10 integers and accept values into it. Sort the array in descending order.
    
    main()
    {
       int ar[10]; 
       int i, j, temp;
    
          for (i = 0 ; i < 10 ; i++)
          {
             printf("Enter number for [%d] element : ",i);
             scanf("%d", &ar[i]);
          }
    
          /* sort array in descending order */
    
          for ( i = 0 ; i < 9 ; i++)
          {
               for ( j = i+1; j < 10 ; j ++)
               {
                 if ( ar[j] > ar[i])
                 {
                    /* interchange */
    
                    temp  =  ar[j];
                    ar[j] = ar[i];
                    ar[i] = temp;
                 }
    
               } /* end of  j loop */
    
           } /* end of i loop */
    
    
           /* display sorted array */
    
          printf("Entered Numbers \n");
       
          for ( i = 0 ; i < 10 ; i++)
          {
             printf("%d\n", ar[i]);
          }    
    }
    
    
  3. Accept 20 numbers into an array. And display how many numbers are below the average of the array.
    main()
    {
       int ar[20]; 
       int i,avg,sum=0;
    
          for (i = 0 ; i < 20 ; i++)
          {
             printf("Enter number for [%d] element : ",i);
             scanf("%d", &ar[i]);
             sum += ar[i];
          }
    
          avg =  sum / 10;
    
          /* Display values that are below average of the array */
    
          for ( i = 0 ; i < 20 ; i++)
          {
               if ( ar[i]  <  avg )
                   printf("%d \n", ar[i]);
          }
    
    }
    
  4. Create an array of 10 elements and fill 9 elements of the array. Accept a number and position and insert the number at the given position in the array.
    
    main()
    {
       int ar[10]; 
       int i,num, pos;
    
          for (i = 0 ; i < 9 ; i++)
          {
             printf("Enter number for [%d] element : ",i);
             scanf("%d", &ar[i]);
          }
    
          /* take number and position */
     
          printf("Enter position and number : " );
          scanf("%d%d", &pos, &num);
      
          /* push all number from position to right */
    
          for ( i = 9 ; i > pos ; i-- )
                 ar[i] = ar [i - 1];
    
           /* insert new value at position */
    
           ar[pos] = num;
                  
           /* Display array after insertion */
    
            for ( i = 0 ; i < 10 ; i++)
            {
                printf("%d \n", ar[i]);
            }
    
     }
    
    
    
  5. Create an array of 10 elements and fill it. Interchange first 5 elements with last 5 elements.
    
    main()
    {
       int ar[10]; 
       int i,temp;
    
          for (i = 0 ; i < 10 ; i++)
          {
             printf("Enter number for [%d] element : ",i);
             scanf("%d", &ar[i]);
          }
    
          /* interchange first 5 elements with last 5 elements */
    
      
          for ( i = 0 ; i < 5 ; i-- )
          {
             temp = ar [i];
             ar[i] = ar [9-i];
             ar[9-i] = temp;
          }
    
    
          /* Display array after interchange */
    
           for ( i = 0 ; i < 10 ; i++)
           {
               printf("%d \n", ar[i]);
           }
    
     }
    
    

Chapter 9 - Multidimensional Array

  1. Take an array of 5 * 5 ( 5 rows and 5 columns ) and accept values into it. Display the array in the form of a matrix.
    main()
    {
       int ar[5][5]; 
       int i,j;
    
          for (i = 0 ; i < 5 ; i++)
          {
            for( j = 0 ; j < 5 ; j ++)
            {
               printf("Enter number for [%d][%d] element : ",i,j);
               scanf("%d", &ar[i][j]);
            }
          }
    
          /* display array as a matrix */
    
      
          for (i = 0 ; i < 5 ; i++)
          {
            /* move to next line before each row */
            printf("\n");
    
            for( j = 0 ; j < 5 ; j ++)
            {
               printf("%5d",ar[i][j]);
            }
    
          }
    
     }
    
    
  2. Take an array of 4 * 4. Place 1 in diagonal cells and 0 in remaining and display the array in matrix format.
    
    main()
    {
       int ar[4][4]; 
       int i,j;
    
          for (i = 0 ; i < 4 ; i++)
          {
            for( j = 0 ; j < 4 ; j ++)
            {
              if ( i == j )
                  ar[i][j] = 1;
              else
                  ar[i][j] = 0;
            }
          }
    
          /* display array as a matrix */
    
          for (i = 0 ; i < 4 ; i++)
          {
            /* move to next line before each row */
            printf("\n");
    
            for( j = 0 ; j < 4 ; j ++)
            {
               printf("%5d",ar[i][j]);
            }
    
          }
    
     }
    
    
  3. Accept numbers into an array of 5 * 5. Accept a number. Display all the elements of the array that are greater than the accepted number.
    main()
    {
       int ar[5][5]; 
       int i,j,num;
    
          for (i = 0 ; i < 5 ; i++)
          {
            for( j = 0 ; j < 5 ; j ++)
            {
               printf("Enter number for [%d][%d] element : ",i,j);
               scanf("%d", &ar[i][j]);
            }
          }
    
          printf("Enter a number : ");
          scanf("%d", &num);
    
    
          /* display all elements that are greater than the
             given number  */
    
      
          for (i = 0 ; i < 5 ; i++)
          {
           
            for( j = 0 ; j < 5 ; j ++)
            {
               if ( ar[i][j] > num )
                     printf("%d \n",ar[i][j]);
            }
    
         }
    
     }
    
  4. Store random numbers into an array of 6 * 6 and interchange first 3 rows with last three rows.
    #include <stdlib.h>
    
    main()
    {
       int ar[6][6]; 
       int i,j,temp;
    
          for (i = 0 ; i < 6 ; i++)
          {
            /* move to next line before each row */
            printf("\n");
    
            for( j = 0 ; j < 6 ; j ++)
            { 
               ar[i][j] = random(100);
               printf("%5d",ar[i][j]);
            }
    
          }
    
    
           
    
          /* interchange */
      
    
          for (i = 0 ; i < 3 ; i++)
          {
            for( j = 0 ; j < 6 ; j ++)
            {
               temp = ar[i][j];
               ar[i][j] =  ar[5-i][j];
               ar[5-i][j] = temp;
            }
          }    
          
          printf("\n\n");
    
          for (i = 0 ; i < 6 ; i++)
          {
            /* move to next line before each row */
            printf("\n");
    
            for( j = 0 ; j < 6 ; j ++)
            {
               printf("%5d",ar[i][j]);
            }
    
          } /* end of i loop */
    
     } /* end of main */