Open In App

AKTU 1st Year Sem 2 Solved Paper 2017-18 | COMP. SYSTEM & C PROGRAMMING | Sec C

Improve
Improve
Like Article
Like
Save
Share
Report

Paper download link: Paper | Sem 2 | 2017-18

B.Tech. (SEM-II) THEORY EXAMINATION 2017-18 COMPUTER SYSTEM & PROGRAMMING IN C

Time: 3hrs Total Marks: 70 Note:-

  • There are three sections. Section A carries 20 marks, Section B carries 30 marks and Section C carries 50 marks.
  • Attempt all questions. Marks are indicated against each question.
  • Assume suitable data wherever necessary.

Section – C

3. Attempt any one of the following: (7*1 = 7)

  1. Describe Compiler, interpreter, assembler? Write the names of compiler that are used in C programming. The language processors can be any of the following three types:
    1. Compiler – The language processor that reads the complete source program written in high level language as a whole in one go and translates it into an equivalent program in machine language is called as a Compiler. Example: C, C++, C#, Java In a compiler, the source code is translated to object code successfully if it is free of errors. The compiler specifies the errors at the end of compilation with line numbers when there are any errors in the source code. The errors must be removed before the compiler can successfully recompile the source code again.>
    2. Assembler – The Assembler is used to translate the program written in Assembly language into machine code. The source program is a input of assembler that contains assembly language instructions. The output generated by assembler is the object code or machine code understandable by the computer.
    3. Interpreter – The translation of single statement of source program into machine code is done by language processor and executes it immediately before moving on to the next line is called an interpreter. If there is an error in the statement, the interpreter terminates its translating process at that statement and displays an error message. The interpreter moves on to the next line for execution only after removal of the error. An Interpreter directly executes instructions written in a programming or scripting language without previously converting them to an object code or machine code. Example: Perl, Python and Matlab.
    4. Microsoft Visual Studio Community
    5. Xcode
    6. Tiny C Compiler (TCC)
    7. Clang
    8. GNU C Compiler
  2. Convert the following:
    1. (0110110.1100)2 = ()8 
      (0110110.1100)2 
      = (0 110 110.110 0)2 
      = (66.6)8
    2. (74.67)10 = ()16 
      (74.67)10 
      = (4A.AB851EB851EB851EB852)16
    3. (AB.CD)16 = ()8 
      (AB.CD)16 
      = (253.632)8
    4. (EFE.45)16 = ()2 
      (EFE.45)16 
      = (111011111110.01000101)2
    5. (576.4)10 = ()6 
      (576.4)10 
      = (2400.22222222222222222222)6
    6. (1234.7)8 = ()16 
      (1234.7)8 
      = (29C.E)16
    7. (334.43)8 = ()2 
      (334.43)8 
      = (11011100.100011)2

4. Attempt any one part of the following: (7 x 1 = 7)

  1. Explain different bitwise operators available in C with examples. 
    In C, following 6 operators are bitwise operators (work at bit-level) & (bitwise AND) Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. | (bitwise OR) Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1. ^ (bitwise XOR) Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. << (left shift) Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. >> (right shift) Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. ~ (bitwise NOT) Takes one number and inverts all bits of it Following is example C program. 

C




// C Program to demonstrate
// use of bitwise operators
 
#include <stdio.h>
int main()
{
 
    unsigned char a = 5, b = 9;
 
    // a = 5(00000101), b = 9(00001001)
    printf("a = %d, b = %d\n", a, b);
 
    // The result is 00000001
    printf("a&b = %d\n", a & b);
 
    // The result is 00001101
    printf("a|b = %d\n", a | b);
 
    // The result is 00001100
    printf("a^b = %d\n", a ^ b);
 
    // The result is 11111010
    printf("~a = %d\n", a = ~a);
 
    // The result is 00010010
    printf("b<<1 = %d\n", b << 1);
 
    // The result is 00000100
    printf("b>>1 = %d\n", b >> 1);
 
    return 0;
}


Output:

a = 5, b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18
b>>1 = 4
  1. What is meant by type conversion? Why is necessary? Explain about implicit and explicit type conversion with examples. 
    A type cast is basically a conversion from one type to another. There are two types of type conversion:
    1. Implicit Type Conversion Also known as ‘automatic type conversion’.
      • Done by the compiler on its own, without any external trigger from the user.
      • Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data.
      • All the data types of the variables are upgraded to the data type of the variable with largest data type.
    
       bool -> char -> short int -> int -> 
       unsigned int -> long -> unsigned -> 
       long long -> float -> double -> long double
  • It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).
  1. Explicit Type Conversion– This process is also called type casting and it is user defined. Here the user can type cast the result to make it of a particular data type. The syntax in C:
(type) expression
  1. Type indicated the data type to which the final result is converted. 

C




// C program to demonstrate explicit type casting
#include <stdio.h>
 
int main()
{
    double x = 1.2;
 
    // Explicit conversion from double to int
    int sum = (int)x + 1;
 
    printf("sum = %d", sum);
 
    return 0;
}


Output:

sum = 2
  1. Output:
sum = 2
  1. Advantages of Type Conversion
    • This is done to take advantage of certain features of type hierarchies or type representations.
    • It helps us to compute expressions containing variables of different data types.

5. Attempt any one of the following: (7*1 = 7)

  1. Write a program to find the Armstrong number from 1 to 100. 

C




// C program to find Armstrong number
// from 1 to 100
 
#include <stdio.h>
 
/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    return x * power(x, y / 2) * power(x, y / 2);
}
 
/* Function to calculate order of the number */
int order(int x)
{
    int n = 0;
    while (x) {
        n++;
        x = x / 10;
    }
    return n;
}
 
// Function to check whether the given number is
// Armstrong number or not
int isArmstrong(int x)
{
    // Calling order function
    int n = order(x);
    int temp = x, sum = 0;
    while (temp) {
        int r = temp % 10;
        sum += power(r, n);
        temp = temp / 10;
    }
 
    // If satisfies Armstrong condition
    if (sum == x)
        return 1;
    else
        return 0;
}
 
// Driver Program
int main()
{
    int i = 1;
 
    for (i = 1; i <= 100; i++)
        if (isArmstrong(i) == 1)
            printf("%d is an ArmStrong Number\n", i);
        else
            printf("%d is not an ArmStrong Number\n", i);
 
    return 0;
}


Output:

1 is an ArmStrong Number
2 is an ArmStrong Number
3 is an ArmStrong Number
4 is an ArmStrong Number
5 is an ArmStrong Number
6 is an ArmStrong Number
7 is an ArmStrong Number
8 is an ArmStrong Number
9 is an ArmStrong Number
10 is not an ArmStrong Number
11 is not an ArmStrong Number
12 is not an ArmStrong Number
13 is not an ArmStrong Number
14 is not an ArmStrong Number
15 is not an ArmStrong Number
16 is not an ArmStrong Number
17 is not an ArmStrong Number
18 is not an ArmStrong Number
19 is not an ArmStrong Number
20 is not an ArmStrong Number
21 is not an ArmStrong Number
22 is not an ArmStrong Number
23 is not an ArmStrong Number
24 is not an ArmStrong Number
25 is not an ArmStrong Number
26 is not an ArmStrong Number
27 is not an ArmStrong Number
28 is not an ArmStrong Number
29 is not an ArmStrong Number
30 is not an ArmStrong Number
31 is not an ArmStrong Number
32 is not an ArmStrong Number
33 is not an ArmStrong Number
34 is not an ArmStrong Number
35 is not an ArmStrong Number
36 is not an ArmStrong Number
37 is not an ArmStrong Number
38 is not an ArmStrong Number
39 is not an ArmStrong Number
40 is not an ArmStrong Number
41 is not an ArmStrong Number
42 is not an ArmStrong Number
43 is not an ArmStrong Number
44 is not an ArmStrong Number
45 is not an ArmStrong Number
46 is not an ArmStrong Number
47 is not an ArmStrong Number
48 is not an ArmStrong Number
49 is not an ArmStrong Number
50 is not an ArmStrong Number
51 is not an ArmStrong Number
52 is not an ArmStrong Number
53 is not an ArmStrong Number
54 is not an ArmStrong Number
55 is not an ArmStrong Number
56 is not an ArmStrong Number
57 is not an ArmStrong Number
58 is not an ArmStrong Number
59 is not an ArmStrong Number
60 is not an ArmStrong Number
61 is not an ArmStrong Number
62 is not an ArmStrong Number
63 is not an ArmStrong Number
64 is not an ArmStrong Number
65 is not an ArmStrong Number
66 is not an ArmStrong Number
67 is not an ArmStrong Number
68 is not an ArmStrong Number
69 is not an ArmStrong Number
70 is not an ArmStrong Number
71 is not an ArmStrong Number
72 is not an ArmStrong Number
73 is not an ArmStrong Number
74 is not an ArmStrong Number
75 is not an ArmStrong Number
76 is not an ArmStrong Number
77 is not an ArmStrong Number
78 is not an ArmStrong Number
79 is not an ArmStrong Number
80 is not an ArmStrong Number
81 is not an ArmStrong Number
82 is not an ArmStrong Number
83 is not an ArmStrong Number
84 is not an ArmStrong Number
85 is not an ArmStrong Number
86 is not an ArmStrong Number
87 is not an ArmStrong Number
88 is not an ArmStrong Number
89 is not an ArmStrong Number
90 is not an ArmStrong Number
91 is not an ArmStrong Number
92 is not an ArmStrong Number
93 is not an ArmStrong Number
94 is not an ArmStrong Number
95 is not an ArmStrong Number
96 is not an ArmStrong Number
97 is not an ArmStrong Number
98 is not an ArmStrong Number
99 is not an ArmStrong Number
100 is not an ArmStrong Number
  1. Write a program to generate a following numbers structure:
12345
1234
123
12

C




#include <stdio.h>
 
int main()
{
 
    int i = 0, j = 0;
 
    for (i = 1; i < 5; i++) {
        for (j = 1; j <= 6 - i; j++)
            printf("%d", j);
        printf("\n");
    }
 
    return 0;
}


Output:

12345
1234
123
12

6. Attempt any one of the following: (7*1 = 7)

  1. Write a program to add two matrices of dimension 3*3 and store the result in another matrix. 

C




#include <stdio.h>
#define N 3
 
// This function adds A[][] and B[][], and stores
// the result in C[][]
void add(int A[][N], int B[][N], int C[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            C[i][j] = A[i][j] + B[i][j];
}
 
int main()
{
    int A[N][N] = { { 1, 1, 1 },
                    { 2, 2, 2 },
                    { 3, 3, 3 } };
 
    int B[N][N] = { { 1, 1, 1 },
                    { 2, 2, 2 },
                    { 3, 3, 3 } };
 
    int C[N][N]; // To store result
    int i, j;
    add(A, B, C);
 
    printf("Result matrix is \n");
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++)
            printf("%d ", C[i][j]);
        printf("\n");
    }
 
    return 0;
}


Output:

Result matrix is 
2 2 2 
4 4 4 
6 6 6
  1. Write a program in C to create a database of fifty students to store personal details such as roll no, name and marks. Print all the details of student whose name is entered by user. 

C




#include <stdio.h>
#include <string.h>
 
struct Student {
    int roll_no;
    char name[100];
    float marks;
};
 
int main()
{
    int i = 0;
    char n[100];
    struct Student student[50];
 
    for (i = 0; i < 50; i++) {
        printf("\nEnter details for Student %d", i + 1);
 
        printf("\nRoll Number: ");
        scanf("%d", &student[i].roll_no);
 
        printf("\nName: ");
        scanf("%s", student[i].name);
 
        printf("\nMarks: ");
        scanf("%f", &student[i].marks);
    }
 
    printf("\nEnter the name of the student whose details you need: ");
    scanf("%s", n);
 
    for (i = 0; i < 50; i++) {
        if (strcmp(n, student[i].name) == 0) {
 
            printf("\nRoll Number: %d", student[i].roll_no);
 
            printf("\nName: %s", student[i].name);
 
            printf("\nMarks: %f", student[i].marks);
 
            break;
        }
    }
 
    if (i == 50)
        printf("No student found with this name");
}


7. Attempt any one of the following: (7*1 = 7)

  1. Write a program in C to reverse a string by using pointer. 

C




#include <stdio.h>
#include <string.h>
 
// Function to reverse the string
// using pointers
void reverseString(char* str)
{
    int l, i;
    char *begin_ptr, *end_ptr, ch;
 
    // Get the length of the string
    l = strlen(str);
 
    // Set the begin_ptr and end_ptr
    // initially to start of string
    begin_ptr = str;
    end_ptr = str;
 
    // Move the end_ptr to the last character
    for (i = 0; i < l - 1; i++)
        end_ptr++;
 
    // Swap the char from start and end
    // index using begin_ptr and end_ptr
    for (i = 0; i < l / 2; i++) {
 
        // swap character
        ch = *end_ptr;
        *end_ptr = *begin_ptr;
        *begin_ptr = ch;
 
        // update pointers positions
        begin_ptr++;
        end_ptr--;
    }
}
 
// Driver code
int main()
{
 
    // Get the string
    char str[100] = "GeeksForGeeks";
    printf("Enter a string: %s\n", str);
 
    // Reverse the string
    reverseString(str);
 
    // Print the result
    printf("Reverse of the string: %s\n", str);
 
    return 0;
}


  1. Explain the following functions in file operations
    • getw( ): The getw() function is used to read an integer value from a file. This file is pointed by the pointer passed as the parameter. Syntax:
int getw(FILE *fp);
  • putw(): The putw() function is used to write an integer value from a file. This file is pointed by the pointer passed as the parameter. And the integer value is also specified as the parameter. Syntax:
 int putw(int number, FILE *fp);
  • fscanf( ): fscanf reads from a file pointed by the FILE pointer (ptr), instead of reading from the input stream. Syntax:
int fscanf(FILE *ptr, const char *format, ...) 
  • Consider the following text file abc.txt
NAME    AGE   CITY
abc     12    hyderabad
bef     25    delhi
cce     65    bangalore  
  • Now, we want to read only the city field of the above text file, ignoring all the other fields. A combination of fscanf and the trick mentioned above does this with ease.
  • fprintf( ): The printf is used to print content in file instead of stdout console.
int fprintf(FILE *fptr, const char *str, ...);


Last Updated : 28 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads