Programming In C 2017-18 Previous Year Paper Solution
Long Questions

Q3. a) Define keyword and identifier in C. What is data type? Explain about different data types with examples. [Marks : 10]

Ans: Keywords: Keywords are predefined, reserved words that have special meanings to the compiler. They cannot be used as an identifier. For example:

int num;

Here, int is a keyword that represents integers type data. So num is a variable of type integer.

Identifier: Identifier refers to the name given to entities such as variables, functions, etc. They must be unique within a program context. Also, identifier names must be different from keywords. For example:

int num;
double balance;

Here, num and balance are identifiers.

Data Type: A data type specifies the type of data that a variable can store such as integer, floating, character, etc. Data types also determine the type and size of data associated with variables. For example:

int num;

Here, num is a variable of  integer type. The size of int is 4 bytes.

In C, Data types can be categorized as follow:

  1. Basic Data Type: int, char, float, double
  2. Derived Data Type: array, pointer, structure, union

Basic Data Types

These are also termed as primary or fundamental data types. The basic data types are integer-based and floating-point based. Example: int, char, float, double

int data type

Integer types can be signed (with negative values) or unsigned values (only positive). Int values are always signed unless specifically mentioned.

Integer types are further classified as:

Data typeRange
signed int−32,768 to 32,767
unsigned int0 to 65,535
signed short int-2,147,483,648 to 2,147,483,647 (4 bytes)
unsigned short int0 to 4,294,967,295 (4 bytes)
signed long int-2,147,483,648 to 2,147,483,647 (4 bytes)
unsigned long int0 to 4,294,967,295 (4 bytes)

For examples :

int num = 120;
long longnum= 12530221947;

Float

Float data type allows users to store decimal values. It takes 4 bytes of memory. For example:

 float balance = 1027.145;

Double

Double data type also allows to store decimal values with more precision than float. Double takes 8 bytes of memory. This is useful in scientific programs that require precision.  For Example:

double accuracy= 1089999999.216;

char

char stores a single character. Char takes only single byte of memory.For example:

char bloodGroup = 'O';

Derived data types

Array

Array in C stores multiple values of the same data type. For example:

double marks[6];

Pointers

A pointer is just a variable that stores the address of another variable. A pointer can store the address of variables of any data types. For example:

int num = 10;
int *ptr;
/* address of num is assigned to ptr*/
 ptr = &a;

Structs

Struct is a composite structure that can contain variables of different data types. For example:

typedef struct{
    char name[30];
    int id;
    char dept;
    double salary;
}Employee;

Union

Union is a special kind of data type in C. With union, you can store different data types in the same memory location. Union can have many members, but only one member can have a value at one time. For example:

union Employee{
 char name[25];
 int id;
 char dept;
 double salary;
 }emp1, emp2;

b) What are the variables and constants? What are the rules for declaring the variables? [Marks: 5]

Ans: variable: a variable is like a container to hold data. The value of a variable can be changed, hence the name variable. For example:

int num = 10;
num = 20;

Here, num is a variable of the type. Here, the variable is assigned an integer value 10.

Constants: If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant. For example,

const double PI = 3.14;
PI = 1.5; //It will throw an error

Here, PI is a symbolic constant; its value cannot be changed.

Rules for declaring the variables:

  1. A variable name can only have letters (both uppercase and lowercase letters), digits and underscore.
  2. The first letter of a variable should be either a letter or an underscore.
  3. There is no rule on how long a variable name can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters.

Q4. a) Explain different types of operators used in C, with suitable examples. [Marks: 10]

Ans: An operator is a symbol that tells the compiler to perform a certain mathematical or logical manipulation.

C language provides a wide range of operators to perform various operations:

  1. Arithmetic operators
  2. Relational operators
  3. Logical operators
  4. Bitwise operators
  5. Assignment operators

Arithmetic operators

An arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, division etc. C supports all the basic arithmetic operators as shown below:

OperatorDescriptionExample
+additionc = a + b
subtractionc = a – b
*multiplicationc = a * b
/divisionc = a / b
%remainder of divisionc = a % b
++Increment operatorc++
Decrement operatorc–

Relational operators

A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. C supports the below relational operators:

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example Code:

// relational operators Example Code
#include <stdio.h>
int main()
{
    int a = 10, b = 20;

    printf("%d == %d is %d \n", a, b, a == b);
    printf("%d > %d is %d \n", a, b, a > b);
    printf("%d < %d is %d \n", a, b, a < b);
    printf("%d != %d is %d \n", a, b, a != b);
    printf("%d >= %d is %d \n", a, b, a >= b);
    printf("%d >= %d is %d \n", a, c, a >= b);
    printf("%d <= %d is %d \n", a, b, a <= b);
    printf("%d <= %d is %d \n", a, c, a <= b);

    return 0;
}

Logical operators

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. C language supports following 3 logical operators.

OperatorDescriptionExample
&&Logical ANDif(a==1 && b==1)
||Logical ORif(a==1 || b==1)
!Logical NOTif(!(a==1))

Bitwise operators

Bitwise operators perform manipulations of data at bit level.  C language supports following 6 Bitwise operators.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
~Bitwise complement
<<Shift left
>>Shift right

Assignment Operators

An assignment operator is used for assigning a value to a variable. Assignment operators supported by C language are as follows.

OperatorDescription
=assigns values from right side operands to left side operand
+=adds right operand to the left operand and assign the result to left
-=subtracts right operand from the left operand and assign the result to left operand
*=mutiply left operand with the right operand and assign the result to left operand
/=divides left operand with the right operand and assign the result to left operand
%=calculate modulus using two operands and assign the result to left operand

b) Write a program in C to find the factorial of a given number. [Marks : 5]

Ans: Factorial C Program code:

#include <stdio.h>
 
int main()
{
  int c, n, f = 1;
 
  printf("Enter the number to calculate factorial\n");
  scanf("%d", &n);
  for (c = 1; c <= n; c++){
    f = f * c;
  }
  printf("Factorial of %d is %d\n", n, f);
 
  return 0;
}

Q5. a) Differentiate between Continue and Break statement. Write a C program to calculate the grade of a student by considering the following range of marks using a switch-case statement. Grades =

O, 90 ≤ Marks 100

E, 80 ≤ Marks < 90

A, 70 ≤ Marks < 80

B, 60 ≤ Marks <70

C, 50 ≤ Marks < 60

D, 40 ≤ Marks <50

F, Otherwise

[Marks : 10]

Ans: Difference between break and continue:

breakcontinue
1. break statement is used in switch and loops1. continue statement is used in loops only.
2. When break is encountered the switch or loop execution is immediately stopped.2. When continue is encountered, the statements after it are skipped and the loop control jump to next iteration.

C program code to calculate grade of a student:

#include<stdio.h>
 int main()
 {
     int score;

     printf("Enter score( 0-100 ): ");
     scanf("%d",&score);

     switch( score / 10 )
     {

     case 10:
     case 9:
         printf("Grade is : O\n");
         break;

     case 8:
         printf("Grade is : E\n");
         break;

     case 7:
         printf("Grade is : A\n");
         break;

     case 6:
         printf("Grade is : B\n");
         break;

     case 5:
         printf("Grade: C\n");
         break;
    
     case 4:
         printf("Grade is : D\n");
         break;

     default:
         printf("Grade is : F\n");
         break;

     }

     return 0;
 } 

b) Write a program in C to print count the odd numbers present in between 1 to 100. [Marks : 5]

Ans: C Program:

#include<stdio.h> 

int main(){

               int rem,i,c;
               c = 0;
               for(i=1; i<=100; ++i)
               {
                   rem = i % 2;
                   if(rem != 0)
                        c++;
               }
               printf("\n The odd numbers between 1 and 100 are : %d \n", c); 
               return 0; 

    } 

Q6 a) Explain about different parameter passing mechanisms in function with examples. [Marks : 10]

Ans: In C Language, there are two methods to pass parameters from calling function to called function and they are:

  1. Call by Value
  2. Call by Reference

Call by Value

In call by value method, the copy of actual parameter values are copied to formal parameters and these formal parameters are used in called function. The changes made on the formal parameters inside the function does not effect the values of actual parameters. Means, after the execution control comes back to the calling function, the actual parameter values remains same.

Call by Value Example Code:

#include <stdio.h>
int main(
{
    int num1 =10;
    int num2 = 20;
    int sum = doubleSumFunc(num1 , num2 );
    printf("Sum of num1 and num2 is %d", num1 + num2);
    printf("Sum of both number is %d", sum);

    return 0;
}
int doubleSumFunc(int a, int b)
{
     a = a + a;
     b = b + b;
     int c=a+b;
     return c;
}

In the above example program, the variables num1 and num2 are called actual parameters and the variables a and b are called formal parameters. The value of num1 is copied into a and the value of num2 is copied into b. The changes made on variables a and b does not effect the values of num1 and num2.

Call by Reference

In Call by Reference method, the memory location address of the actual parameters is copied to formal parameters. This address is used to access data of the actual parameters in called function. In this method of parameter passing, the formal parameters must be pointer variables.T the changes made on the formal parameters effects the values of actual parameters. 

Call by Reference Example Code:

#include<stdio.h>
#include<conio.h>
void swapNumber(int *,int *) ; // function declaration
void main(){
   int num1, num2 ;
   
   num1 = 10 ;
   num2 = 20 ;
   
   printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
   swapNumber(&num1, &num2) ; // calling function 
   
   printf("\nAfter swap: num1 = %d, num2 = %d", num1, num2);
   getch() ;
}
void swapNumber(int *a, int *b)  // called function
{
   int temp ;
   temp = *a ;
   *a = *b ;
   *b = temp ;
}

In the above example program, the addresses of the variables num1 and num2 are copied to pointer variables a and b. The changes made on the pointer variables a and b in called function effects the values of actual parameters num1 and num2 in calling function.

b) Write a program to find the greatest among 3 numbers using function. [Marks : 5]

Ans: C Program Code:

#include <stdio.h>
#include <stdlib.h>
greatestNumber(int,int,int);//function declaration

int main()
{

    int a,b,c;
    printf("Enter the three numbers\n");
    scanf("%d%d%d",&a,&b,&c);

    int result=greatestNumber(a,b,c);//function call
    printf("Biggest number is: %d\n",result);

    getch();
    return 0;
}
int greatestNumber(int a,int b,int c)
{
    if(a>b)
    {
        if(a>c)
          return a;
        else
          return c;
     }
     else
     {
         if(b>c)
            return b;
         else
           return c;
     }
}

Q7 a) Explain about different string manipulation functions with examples. [Marks : 10]

Ans: C supports a large number of string manipulation functions in the standard library "string.h". For example:

FunctionsDescription
strlen()evaluates string’s length
strcpy()copies a string to another string
strcat()concatenates two strings
strcmp()compares two strings
strlwr()converts string to lowercase
strupr()converts string to uppercase

String Manipulation Code Example:

#include <stdio.h>
#include <string.h>
int main()
{
     char str1[20] = "ExamAssure";
     char str2[20] = "C Programming";
     
     // String Length
     printf("Length of string str1 is : %d", strlen(str1));

     // String Copy
     strcpy(str1,str2);
     printf("String str1 is: %s", str1);

     // String Concatenation
     strcpy(str1,str2);
     printf("Output string after concatenation: %s", str1);

     // String Compare
     if (strcmp(str1, str2) ==0)
     {
        printf("str1 and str2 are equal");
     }else
      {
         printf("str1 and str2 are different");
      }
     return 0;
}

b) Write a program to find given string is palindrome or not. [Marks : 5]

Ans: Palindrome C Program Code:

#include <stdio.h>
#include <string.h>

int main(){
    char str[20];
    int i, length;
    int flag = 0;
    
    printf("Enter a string:");
    scanf("%s", str);
    
    for(i=0;i < strlen(str);i++){
        if(str[i] != str[length-i-1]){
            flag = 1;
            break;
       }
    }
    
    if (flag) {
        printf("%s is not a palindrome", str);
    }    
    else {
        printf("%s is a palindrome", str);
    }
    return 0;
}

Q8 a) What do you mean by Array? Explain with suitable diagram that, how the array variables are stored in memory? Write a program in C to add two matrices. [Marks : 10]

Ans: An array is a variable that can store multiple values of the same type. For example, if you want to store 50 integers, you can create an array of integer type.

int numbers[50]; 

C Program to add two matrices:

#include <stdio.h>
int main() {
    int r, c, a[100][100], b[100][100], sum[100][100], i, j;
    printf("Enter the number of rows (between 1 and 100): ");
    scanf("%d", &r);
    printf("Enter the number of columns (between 1 and 100): ");
    scanf("%d", &c);

    printf("\nEnter elements of 1st matrix:\n");
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            printf("Enter element a%d%d: ", i + 1, j + 1);
            scanf("%d", &a[i][j]);
        }

    printf("Enter elements of 2nd matrix:\n");
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            printf("Enter element a%d%d: ", i + 1, j + 1);
            scanf("%d", &b[i][j]);
        }

    // adding two matrices
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            sum[i][j] = a[i][j] + b[i][j];
        }

    // Displaying the result
    printf("\nSum of two matrices: \n");
    for (i = 0; i < r; ++i)
        for (j = 0; j < c; ++j) {
            printf("%d   ", sum[i][j]);
            if (j == c - 1) {
                printf("\n\n");
            }
        }

    return 0;
}

b) Write a program to swap two numbers using pointer concept. [Marks : 5]

Ans: Swapping of two numbers using pointer c program code:

#include <stdio.h>
 
int main()
{
   int num1, num2, *a, *b, temp;
 
   printf("Enter the value of num1 and num2 \n");
   scanf("%d%d", &num1 , &num2);
 
   printf("Before Swapping\n num1 = %d\n num2 = %d\n", num1, num2);
 
   a = &num1;
   b = &num2;
 
   temp = *b;
   *b = *a;
   *a = temp;
 
   printf("After Swapping\n num1 = %d\n num2 = %d\n", num1, num2);
 
   return 0;
}

Q9 a) Explain about different file operations that can be performed on files. How to read from and write to a file? Explain with examples. [Marks: 10]

Ans: File is a structure defined in ‘stdio.h’ to handle file operations. Different file operations can be performed on file like:

  1. Creating a new file
  2. Opening a file
  3. reading a file
  4. writing to a file
  5. appending to a file
  6. closing a file

When working with files, you need to declare a pointer of type file. 

FILE *fptr;

Opening a file

Opening a file is performed using the fopen() function defined in the stdio.h header file.

fptr= fopen("file_to_be_opened","mode");

mode above represents the operations like “Open for reading(r)”,”Open for reading in binary mode(rb)”,”Open for writing(w)”,”Open for append(a)” etc.

Read And Write to a file

For reading and writing to a text file, we use the functions fprintf() and fscanf().

Closing a File

Closing a file is performed using the fclose() function.

fclose(fptr);

Reading And Write C Code Example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   FILE *fr;
   char s[40];
   int n;
   fr = fopen("welcome.txt", "w");
   printf("Enter a string and a number");
   scanf("%s%d", s, &n); /*read from keyboard*/
   fprintf(fr, "%s %d", s, n); /*  write to file*/
   fclose(fr);
   fr = fopen("welcome.txt", "r");
   fscanf(fr, "%s%d", s, &n); /*read from file*/
   printf("%s %d", s, n); /*display on screen*/
   fclose(fr);
   return 0;
}

b) Differentiate between Structure and Union with suitable example. [Marks : 5]

Ans: Structures

Structure is used to stores the different types of elements i.e heterogeneous elements. The struct keyword is used to define structure. For example:

// Syntax
struct structure_name
{
    data_type member1;
    data_type member2;
    data_type memeberN;
};

C Program Structure Example Code:

#include <stdio.h>
struct employee {
    char name[60];
    int emp_code;
    float salary;
} emp;
int main() {
    printf("Enter the following information:\n");
    printf("Enter Employee name: ");
    fgets(sdt.name, sizeof(emp.name), stdin);
    printf("Enter employee code : ");
    scanf("%d", & emp.emp_code);
    printf("Enter employee salary: ");
    scanf("%f", & emp.salary);
    printf("The information you have entered is: \n");
    printf("Employee name: ");
    printf("%s", emp.name);
    printf("Employee code : %d\n", emp.emp_code);
    printf("Employee marks: %.1f\n", emp.salary);
    return 0;
}

Unions

Union also stores the different types of elements i.e heterogeneous elements. The union keyword is used to define structure. Union takes the memory of largest member only so occupies less memory than structures. For example:

// Syntax
union union_name
{
    data_type member1;
    data_type member2;
    data_type memeberN;
};

C Program Union Code Example:

#include <stdio.h>

union union_name
{
    int a;
    float b;
    char c;
};

int main( )
{
    union union_name it;
    it.a = 12;
    it.b = 20.2;
    it.c = 'a';
    
    printf("%d\n", it.a);
    printf("%f\n", it.b);
    printf("%c\n", it.c);
    
    return 0;
}