Saturday, March 8, 2025

4. Programming in C [Most Important Questions and Solutions]

 IMPORTANT OLD QUESTION OF Programming in C

[Functions]

2080 Q. No. 16 Write a C program to enter the radius of a football and find the area of the football by using a user-defined function.  [5]

Ans:

#include<stdio.h>

#include<conio.h>

float area(int);

void main()

{

    int r;

    float a;

    clrscr();

    printf("Enter radius:");

    scanf("%d", &r);

    a = area(r);

    printf("The area of a ball is %f", a);

    getch();

}

float area(int x)

{

    float ar;

    ar = 4 * 3.14 * x * x;

    return ar;

}

 

Example Output:

Enter radius: 7

The area of a ball is 615.44

 

2079 GIE Set B Q. No. 16 Define function. Write a program to generate the factorial of a given number using a recursive function in C programming. [3+5] 

Ans:

A function in C is a self-contained block of statements that performs a specific task. A function is executed when it is called in the program. Some advantages of functions are as follows:

  1. Reusability: Use the same code multiple times.
  2. Modularity: Break the program into smaller parts.
  3. Easy Maintenance: Fix or update one function without affecting others.
  4. Clear and Readable: Makes the program easy to understand.

Different types of functions are as follows:

  1. Predefined (Library) Functions: Functions provided by C standard libraries (e.g., printf, scanf, sqrt).
  2. User-Defined Functions: Functions created by the programmer to perform specific tasks (e.g., int add(int a, int b)). The different types of user-defined functions are as follows:

a)      Functions with No Arguments and No Return Value

b)      Functions with No Arguments but Return a Value

c)       Functions with Arguments but No Return Value

d)      Functions with Arguments and Return Value

A program to generate the factorial of a given number using a recursive function in C programming is as follows:

#include <stdio.h>

#include <conio.h>

int factorial(int n);

void main()

{

    int n, fact;

    clrscr();

    printf("Enter a number:");

    scanf("%d", &n);

    fact = factorial(n);

    printf("Factorial of %d is %d", n, fact);

    getch();

}

int factorial(int n)

{

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

    {

        return 1;

    }

    else

    {

        return n * factorial(n - 1);

    }

}

 

Example Output:

Enter a number: 5

Factorial of 5 is 120

 

[Structure]

2081 GIE Set A Q. No. 16 Write a C program to store 10 student records with fields for roll numbers, names, and marks in computer science. Process and display the roll numbers, names, and marks of students.    [8]

Ans:

#include <stdio.h>

#include <conio.h>

struct student

{

    int rn, marks;

    char name[20];

};

void main()

{

    struct student s[10];

    int i;

    clrscr();

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

    {

        printf("Enter Roll Number: ");

        scanf("%d", &s[i].rn);

        printf("Enter Name: ");

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

        printf("Enter Marks in Computer Science: ");

        scanf("%d", &s[i].marks);

    }

    printf("\nROLL NO\tNAME\tMARKS");

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

    {

        printf("\n%d\t%s\t%d", s[i].rn, s[i].name, s[i].marks);

    }

    getch();

}

 

Example Output:

Enter Roll Number: 101

Enter Name: Madhav

Enter Marks in Computer Science: 99

........

Enter Roll Number: 110

Enter Name: Sandesh

Enter Marks in Computer Science: 90

 

ROLL NO     NAME        MARKS

101              Madhav     99

........

110              Jack            90

 

2081 GIE Set B Q. No. 16 Create a structure named "player" to store details pname, game, age, and salary. Write a C program to input details for five players and display their information in proper format.

Ans:

#include <stdio.h>

#include <conio.h>

struct player

{

    char pname[20], game[20];

    int age, salary;

};

void main()

{

    struct player p[5];

    int i;

    clrscr();

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

    {

        printf("Enter Name: ");

        scanf("%s", p[i].pname);

        printf("Enter Game: ");

        scanf("%s", p[i].game);

        printf("Enter Age: ");

        scanf("%d", &p[i].age);

        printf("Enter Salary: ");

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

    }  

    printf("\nNAME\tGAME\tAGE\tSALARY");

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

    {

        printf("\n%s\t%s\t%d\t%d", p[i].pname, p[i].game, p[i].age, p[i].salary);

    }

    getch();

}

 

Example Output:

Enter Name: John

Enter Game: Cricket

Enter Age: 25

Enter Salary: 50000

………

Enter Name: David

Enter Game: Football

Enter Age: 24

Enter Salary: 55000

 

NAME    GAME          AGE     SALARY

John       Cricket         25        50000

……..

David     Football       24        55000

2081 Q. No. 16 Write a C program that uses structures to represent details of five books (title, author, publisher, and price) and prints them out. [8]

Ans:

#include<stdio.h>

#include<conio.h>

struct book

{

    char title[20],auth[20],pub[20];

    int price;

}

void main()

{

    struct book b[5];

    int i;

    clrscr();

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

    {

    printf("Enter Book's Title:");

    scanf("%s",b[i].title);

    printf("Enter Author:");

    scanf("%s",b[i].auth);

    printf("Enter Publisher:");

    scanf("%s",b[i].pub);

    printf("Enter Price:");

    scanf("%d",&b[i].price);

    }

    printf("\nTITILE\tAUTHOR\tPUBLISHER\tPRICE");

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

    {

    printf("\n%s\t%s\t%s\t%d",b[i].title,b[i].auth,b[i].pub,b[i].price);

    }

    getch();

    }

}

 

2080 GIE Set A Q. No. 16 What is a structure? Write a program to store five students' information (ID, Name, DOB, and phone) and display them using structure. [2+6]

Ans:

structure in C is a user-defined data type that allows grouping variables of different types under a single name. This helps to logically organize and manage related data. It is defined and declared using the ‘struct’ keyword followed by the structure name and a set of curly braces.

For example:

struct Student

{

    int id;

    float marks;

} s1;

 

A program to store five students' information (ID, Name, DOB, and phone) and display them using structure is as follows:

#include<stdio.h>

#include<conio.h>

struct student

{

    int id;

    char name[50], dob[20], phone[15];

};

void main()

{

    struct student s[5];

    int i;

    clrscr();

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

    {

        printf("Enter ID: ");

        scanf("%d", &s[i].id);

        printf("Enter Name: ");

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

        printf("DOB (DD/MM/YYYY): ");

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

        printf("Phone: ");

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

    }

    printf("\nID\tNAME\tDOB\tPHONE");

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

    {

        printf("\n%d\t%s\t%s\t%s", s[i].id, s[i].name, s[i].dob, s[i].phone);

    }

    getch();

}

 

2080 GIE Set B Q. No. 16 Write a program to store five employees' records (EID, Name, Post, and Department) and display them using structure. [8]

Ans:

#include<stdio.h>

#include<conio.h>

struct employee

{

    int eid;

    char name[50], post[50], department[50];

};

void main()

{

    struct employee emp[5];

    int i;

    clrscr();

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

    {

        printf("Enter

        printf("Enter EID: ");

        scanf("%d", &emp[i].eid);

        printf("Enter Name: ");

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

        printf("Enter Post: ");

        scanf("%s", emp[i].post);

        printf("Enter Department: ");

        scanf("%s", emp[i].department);

    }

    printf("\nEID\tNAME\tPOST\tDEPARTMENT");

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

    {

        printf("\n%d\t%s\t%s\t%s", emp[i].eid, emp[i].name, emp[i].post, emp[i].department);

    }

    getch();

}

 

2080 Q. No. 16 OR Define the structure. Write a C program using structure to input staff ID, name, and the salary of 50 staff. Display staff ID, name, and salary of those staff whose salary ranges from 25 thousand to 40 thousand. [2+6]

Ans:

#include<stdio.h>

#include<conio.h>

struct staff

{

   char name[20];

   int id,sal;

}

void main()

{

    struct staff s[50];

    int i;

    clrscr();

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

    {

    printf("Enter Staff Id:");

    scanf("%d",&s[i].id);

    printf("Enter Name:");

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

    printf("Enter Salary:");

    scanf("%d",&s[i].sal);

    }

    printf("\nID\tNAME\tSALARY");

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

    {

    if(s[i].sal>25000 && s[i].sal<40000)

    {

    printf("\n%d\t%s\t%d",s[i].id,s[i].name,s[i].sal);

    }

    }

    getch();

    }

}

 

2079 GIE Set A Q. No. Write a C program to enter name, grade, age, and address of 10 students in a structure and display the information. [8]

Ans:

#include<stdio.h>

#include<conio.h>

void main()

{

    struct student

    {

        char name[20],grade[5],address[20];

        int age;

    }s[10];

    int i;

    clrscr();

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

    {

        printf("Enter Name:");

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

        printf("Enter Grade:");

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

        printf("Enter Age:");

        scanf("%d",&s[i].age);

        printf("Enter Address:");

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

    }

    printf("\nNAME\tGRADEVtAGEVtADDRESS");

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

    {

        printf("\n%s\t%s\t%d\t%s",s[i].name,s[i].grade,s[i].age,s[i].address);

    }

    getch();

}

 

2079 Set A Q.No. 16 What is structure? Write a program to input roll, name and age of 5 students and display them properly using structure.

Ans:

#include <stdio.h>

#include <conio.h>

struct student

{

    int roll, age;

    char name[20];

};

void main()

{

    struct student s[5];

    int i;

    clrscr();

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

    {

        printf("Enter Roll Number: ");

        scanf("%d", &s[i].roll);

        printf("Enter Name: ");

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

        printf("Enter Age: ");

        scanf("%d", &s[i].age);

    }

    printf("\nROLL\tNAME\tAGE\n");

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

    {

        printf("%d\t%s\t%d\n", s[i].roll, s[i].name, s[i].age);

    }

    getch();

}

 

2079 NEB Model Q. No. 16 Develop a program in C using structure to ask the information of any 12 students with roll number, name and marks scored in sub1, sub2, and sub3. Also, display them in proper format along with the calculation of total and percentage. [Note: the full mark of each subject is 100]. [8]

Ans:

#include <stdio.h>

#include <conio.h>

struct student

{

    int rn;

    char name[20];

    int sub1, sub2, sub3;

    int total;

    float percent;

};

void main()

{

    struct student s[12];

    int i;

    clrscr();

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

    {

              printf("Roll Number: ");

              scanf("%d", &s[i].rn);

              printf("Name: ");

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

              printf("Marks in Subject 1: ");

              scanf("%d", &s[i].sub1);

              printf("Marks in Subject 2: ");

              scanf("%d", &s[i].sub2);

              printf("Marks in Subject 3: ");

              scanf("%d", &s[i].sub3);

              s[i].total = s[i].sub1 + s[i].sub2 + s[i].sub3;

              s[i].percent = s[i].total / 3.0;

    }

    printf("\nROLLNO\tNAME\tSUB1\tSUB2\tSUB3\tTOTAL\tPERCENTAGE");

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

    {

              printf("\n%d\t%s\t%d\t%d\t%d\t%d\t%f",

                          s[i].rn, s[i].name, s[i].sub1, s[i].sub2, s[i].sub3, s[i].total, s[i].percent);

    }

    getch();

}

 

[Working with File]

2079 GIE Set A Q. No. 16 OR Describe the file handling concept in C. Write a C program to enter names and addresses of the students and store them in a data file "student.dat". [3+5]

Ans:

File handling in C refers to the process of creating, writing, reading, and manipulating data files on the disk using standard library functions (file manipulation/handling functions).

While handling a file in C, it has to be created/opened, perform read/write operations, and close the file as shown below:

Step 1: Creating/Opening the file

Syntax: file_pointer = fopen("file_name", "file_mode");

Example: fptr = fopen("student.dat", "r");

Step 2: Writing data to a file

Syntax: fprintf(file_pointer, "format_specifiers", variables);

Example: fprintf(fptr, "%d %s", rn, name);

Step 3: Reading data from a file

Syntax: fscanf(file_pointer, "format_specifiers", variables);

Example: fscanf(fptr, "%d %s", &rn, name);

Step 4: Closing the file

Syntax: fclose(file_pointer);

Example: fclose(fptr);

 

A C program to enter names and addresses of the students and store them in a data file "student.dat" is as follows:

#include <stdio.h>

#include <conio.h>

void main()

{

    char name[50], address[100];

    int n, i;

    FILE *fptr;

    clrscr();

    fptr = fopen("student.dat", "w");

    printf("Enter number of records:");

    scanf("%d", &n);

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

    {

        printf("Enter name:");

        scanf("%s", name);

        printf("Enter address:");

        scanf("%s", address);

        fprintf(fptr, "\n%s\t%s", name, address);

    }

    fclose(fptr);

 

    fptr = fopen("student.dat", "r");

    printf("\nNAME\tADDRESS");

    while (fscanf(fptr, "%s %s", name, address) != EOF)

    {

        printf("\n%s\t%s", name, address);

    }

    fclose(fptr);

    getch();

}

 

2080 GIE Set B Q. No. 16 OR Describe file handling modes in C. Write a C program to create and write data into a file.  [4+4]

Ans:

In C, file handling modes define how a file is opened and how you can interact with its contents. In other words, it specifies the purpose of opening a file.

There are mainly six modes, these modes are used with the fopen() function, which opens a file and returns a pointer to that file.

The various file handling (opening) modes in C are as follows:

1)      "r" mode (Read): "r" mode opens an existing file for the purpose of reading only. The possible operation is reading from the file.

2)      "w" mode (Write): "w" mode opens a file for the purpose of writing only. The possible operation is writing to the file.

3)      "a" mode (Append): "a" mode opens an existing file for the purpose of appending (i.e., adding new information at the end of file).

4)      "r+" mode (Read + Write): "r+" mode opens an existing file for the purpose of both reading and writing.

5)      "w+" mode (Write + Read): "w+" mode opens a file for the purpose of both writing and reading.

6)      "a+" mode (Append + Read): "a+" mode opens an existing file for the purpose of both reading and appending.

 

A C program to create and write data into a file “student.dat” is as follows:

#include<stdio.h>

#include<conio.h>

void main()

{

  char name[20];

  int rn, age;

  FILE *fptr;

  clrscr();

  fptr=fopen("student.dat","w");

  printf("Enter name:");

  scanf("%s",name);

  printf("Enter rollno:");

  scanf("%d",&rn);

  printf("Enter age:");

  scanf("%d",&age);

  fprintf(fptr,"\n%s\t%d\t%d",name,rn,age);

  fclose(fptr);

  getch();

}

 

2080 GIE Set A Q. No. 16 OR Write a program to read and write data from a file using file handling functions. [5]

2081 GIE Set B Q. No. 16 OR Write a C program that illustrates the use of fprintf() and fscanf() functions of file handling.  [8]

Ans:

The function fprintf() is a formatted output function which is used to write integer, float, char, or string to a file.

Ø  Syntax: fprintf(file_pointer, "format_specifiers", list_of_variables);

Ø  Example: fprintf(fptr, "%d %s", rn, name);

Ø  Explanation: Above example writes the roll no with format specifier %d and name with format specifier %s from a file.

The function fscanf() is a formatted input function which is used to read integer, float, char, or string from a file.

Ø  Syntax: fscanf(file_pointer, "format_specifiers", variable_list);

Ø  Example: fscanf(fptr, "%d %s", &rn, name);

Ø  Explanation: Above example reads the roll no with format specifier %d and name with format specifier %s from a file.

 

A program to read and write data from a file using file handling functions is as follows:

(A C program that illustrates the use of fprintf() and fscanf() functions of file handling is as follows:)

#include <stdio.h>

#include <conio.h>

void main()

{

    char name[20];

    int rn, age, i;

    FILE *fptr;

    clrscr();

    fptr = fopen("student.dat", "w");

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

    {

        printf("Enter name: ");

        scanf("%s", name);

        printf("Enter roll no: ");

        scanf("%d", &rn);

        printf("Enter age: ");

        scanf("%d", &age);

        fprintf(fptr, "\n%s\t%d\t%d", name, rn, age);

    }

    fclose(fptr);

 

    fptr = fopen("student.dat", "r");

    printf("\nNAME\tROLL NO\tAGE");

    while (fscanf(fptr, "%s %d %d", name, &rn, &age) != EOF)

    {

        printf("\n%s\t%d\t%d", name, rn, age);

    }

    fclose(fptr);

    getch();

}

 

2079 Set A Q. No. 16 OR Write a C program to enter an ID, employee_name, and post of the employee and store them in a data file named "emp.txt". Display each record on the screen in an appropriate format.

Ans:

#include <stdio.h>

#include <conio.h>

void main()

{

    char employee_name[50], post[50];

    int id, n, i;

    FILE *fptr;

    clrscr();

    printf("Enter the number of records: ");

    scanf("%d", &n);

    fptr = fopen("emp.txt", "w");

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

    {

        printf("Enter Employee ID: ");

        scanf("%d", &id);

        printf("Enter Employee Name: ");

        scanf("%s", employee_name);

        printf("Enter Employee Post: ");

        scanf("%s", post);

        fprintf(fptr, "\n%d\t%s\t%s", id, employee_name, post);

    }

    fclose(fptr);

 

    fptr = fopen("emp.txt", "r");

    printf("\nID\tNAME\tPOST");

    while (fscanf(fptr, "%d%s%s", &id, employee_name, post) != EOF

    {

        printf("\n%d\t%s\t%s", id, employee_name, post);

    }

    fclose(fptr);

    getch();

}

 

2081 Q. No. 16 OR Discuss the concept of binary file handling in C programming and explain how putw() and getw() functions facilitate binary input/output operations. Give examples. [8]

Ans:

Binary file handling in C refers to reading and writing data in binary format to a file.

This means in binary file handling, the data is stored in the exact byte-for-byte format as it appears in memory, which is different from text file handling where the data is stored as human-readable text.

The primary functions used for binary file handling are:

a) fread()      b) fwrite()       c) putw()       d) getw()     e) fseek()       f) rewind(), etc.

 

Here's a simple example that demonstrates how to use putw() and getw() for writing and reading integers to and from a binary file:

#include <stdio.h>

#include <conio.h>

void main() {

    int num, i;

    FILE *fptr;

    clrscr();

    fptr = fopen("numbers.bin", "wb");

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

    {

        putw(i, fptr);

    }

    fclose(fptr);

    fptr = fopen("numbers.bin", "rb");

    printf("Numbers in the file:\n");

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

    {

        num = getw(fptr);

        printf("%d\n", num);

    }

    fclose(fptr);

    getch();

}

Explanation:

In this example:

  1. The file is opened in "wb+" mode for both writing and reading.
  2. The putw() function writes 10 integers (from 1 to 10) to the file.
  3. The file is reopened in "rb" mode for reading.
  4. The getw() function reads and prints the integers from the file.

This should give a clear demonstration of how putw() and getw() functions facilitate binary input/output operations.

 

2081 Pre-Board KMC Write a C program to create and store name, gender, age, and mobile number of n students in a file named “ADDRESS.DAT”. The program should display records of students aged between 20 to 30 years and count the total number of "Female" students.

Ans:

#include<stdio.h>

#include<conio.h>

void main()

{

    char name[50], gender, mobile[15];

    int age, n, i, fc = 0;

    FILE *fptr;

    clrscr();

    fptr = fopen("ADDRESS.DAT", "w");

    printf("Enter number of students: ");

    scanf("%d", &n);

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

    {

              printf("Enter Name: ");

              scanf("%s", name);

              printf("Enter Gender (M/F): ");

              scanf(" %c", &gender);

              printf("Enter Age: ");

              scanf("%d", &age);

              printf("Enter Mobile Number: ");

              scanf("%s", mobile);

              fprintf(fptr, "\n%s\t%c\t%d\t%s", name, gender, age, mobile);

    }

    fclose(fptr);

   

    fptr = fopen("ADDRESS.DAT", "r");

    printf("\nNAME\tGENDER\tAGE\tMOBILE");

    while (fscanf(fptr, "%s %c %d %s", name, &gender, &age, mobile) != EOF)

             {

                  if (age >= 20 && age <= 30)

                 {

                  printf("\n%s\t%c\t%d\t%s", name, gender, age, mobile);

                  }

                  if (gender == 'F' || gender == 'f')

                 {

                  fc=fc+1;

                  }

    }

    printf("Total number of Female students: %d", fc);

    fclose(fptr);

    getch();

}

 

2079 NEB Model Q. No. 16 OR Demonstrate a program in C to create a data file named score.dat to store students’ information with Reg_no, name, gender, and address. The program should ask the user to continue or not. When finished, the program should also display all the records in the proper format.

Ans:

#include<stdio.h>

#include<conio.h>

void main()

{

    char name[20], gender[10], address[50], ch;

    int reg_no;

    FILE *fptr;

    clrscr();

    fptr = fopen("students.dat", "w");

    do

    {

        printf("Enter Registration Number: ");

        scanf("%d", &reg_no);

        printf("Enter Name: ");

        scanf("%s", name);

        printf("Enter Gender (Male/Female/Other): ");

        scanf("%s", gender);

        printf("Enter Address: ");

        scanf("%s", address);

        fprintf(fptr, "\n%d\t%s\t%s\t%s", reg_no, name, gender, address);       

        printf("Do you need to enter more records (Y/N)? ");

        ch = getche();

    } while (ch == 'Y' || ch == 'y');

    fclose(fptr);

 

    fptr = fopen("students.dat", "r");

    printf("\nREG NO\tNAME\tGENDER\tADDRESS");

    while (fscanf(fptr, "%d%s%s%s", &reg_no, name, gender, address) != EOF)

    {

        printf("\n%d\t%s\t%s\t%s", reg_no, name, gender, address);

    }

    fclose(fptr);

    getch();

}

 

Asmita Sample Model Q. No. 16 OR Write a C program to open a new file and read roll-no, name, address and phone number of students until the user says “no”, after reading the data, write it to the file then display the content of the file.

Ans:

#include <stdio.h>

#include <conio.h>

void main()

{

    char name[50], address[100], phone[15], ch;

    int roll_no;

    FILE *fptr;

    clrscr();

    fptr = fopen("students.dat", "w");

    do

    {

        printf("Enter Roll Number: ");

        scanf("%d", &roll_no);

        printf("Enter Name: ");

        scanf("%s", name);

        printf("Enter Address: ");

        scanf("%s", address);

        printf("Enter Phone Number: ");

        scanf("%s", phone);

        fprintf(fptr, "\n%d\t%s\t%s\t%s", roll_no, name, address, phone);

        printf("Do you want to enter more records (Y/N)? ");

        ch = getche();

    } while (ch == 'Y' || ch == 'y');

    fclose(fptr);

 

    fptr = fopen("students.dat", "r");

    printf("\nROLL NO\tNAME\tADDRESS\tPHONE");

    while (fscanf(fptr, "%d%s%s%s", &roll_no, name, address, phone) != EOF)

    {

        printf("\n%d\t%s\t%s\t%s", roll_no, name, address, phone);

    }

    fclose(fptr);

    getch();

}

 

[Pointer]

2080 GIE Set A Q. No. 16 OR What is a pointer?   [3]

Ans:

In C programming, a pointer is a variable that stores the memory address of another variable. Instead of holding data directly, it holds the address of the variable where the data is stored.

Key Points about Pointers:

1) Declaration: A pointer is declared by placing an asterisk * before the pointer variable name.

Syntax:

dataType *pointerName;

Example:

int *ptr;

This declares a pointer ptr that can point to an integer variable.

 

2) Initialization: A pointer is initialized with the address of a variable using the address-of operator &.

Example:

int x = 10;

int *ptr = &x;

Here, ptr holds the address of x.

 

3) Dereferencing: The value at the memory address pointed to by the pointer can be accessed using the dereference (indirection) operator *.

Example:

printf("%d", *ptr);                                                             

This prints the value of x, which is 10.

 

2080 Q. No. 16 Write the advantage of a pointer.   [3]

Ans:

In C programming, a pointer is a variable that stores the memory address of another variable. Instead of holding data directly, it holds the address of the variable where the data is stored.

Some advantages of using pointers in C programming are as follows:

a) Efficient Memory Management: Allows direct access to memory for dynamic allocation and deallocation.

b) Pass-by-Reference: Enables functions to modify actual variable values, improving performance.

c) Dynamic Data Structures: Essential for creating and managing structures like linked lists and trees.

d) Memory Sharing: Allows multiple parts of a program to share and manipulate the same data.

e) Faster Array Access

f) Interfacing with Hardware

 

2081 GIE Set A Q. No. 16.b Write a C program to find the product of two numbers using a pointer.  [4]

Ans:

#include <stdio.h>

#include <conio.h>

void main()

{

    int a, b, *ptr1, *ptr2, p;

    clrscr();

    printf("Enter first number:");

    scanf("%d", &a);

    printf("Enter second number:");

    scanf("%d", &b);

    ptr1 = &a;

    ptr2 = &b;

    p = (*ptr1) * (*ptr2);

    printf("The product of two numbers is: %d", p);   

    getch();

}

 

Example Output:

Enter first number: 5

Enter second number: 10

The product of two numbers is: 50

 

Out Of Syllabus 2078 NEB Model Q 1) Write a program to enter ten integer numbers into an array, sort and display them in ascending order.   [Only for A+ Students]

Ans:

#include <stdio.h>

#include <conio.h>

void main()

{

    int arr[10], i, j, temp;

    clrscr();

  

    printf("Enter 10 integers:\n");

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

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

    }

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

    {

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

        {

            if (arr[j] > arr[j + 1])

            {

                temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

            }

        }

    }

    printf("\nSorted numbers in ascending order:\n");

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

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

    }

    getch();

}

 

Out Of Syllabus 2078 NEB Model Q 2) Write a program to read the marks of any 5 students in a subject and count how many students are pass and fail.   [Only for A+ Students]

Ans:

#include <stdio.h>

#include <conio.h>

void main()

{

    int marks[5], i, pass = 0, fail = 0;

    clrscr();

    printf("Enter the marks of 5 students:\n");

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

    {

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

        if (marks[i] >= 40)

        {

            pass=pass+1;

        } else {

            fail=fail+1;

        }

    }

    printf("\nNumber of students who passed: %d", pass);

    printf("\nNumber of students who failed: %d", fail);

    getch();

}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home