logo

Long Answer Questions (8 Marks Each) (Part 1)

ext-base py-[18px] px-6" bis_skin_checked="1">

......................

1. Write a C program 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 a proper format along with the calculation of total and percentage. (Note: The full marks of each subject is 100)

c
#include <stdio.h> struct student { int roll_no; char name[50]; int sub1, sub2, sub3; int total; float percentage; }; int main() { struct student s[12]; for (int i = 0; i < 12; i++) { printf("Enter information for student %d:\n", i + 1); printf("Roll No: "); scanf("%d", &s[i].roll_no); 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].percentage = (float)s[i].total / 3; } printf("\nStudent Information:\n"); for (int i = 0; i < 12; i++) { printf("Roll No: %d | Name: %s | Total: %d | Percentage: %.2f\n", s[i].roll_no, s[i].name, s[i].total, s[i].percentage); } return 0; }

2. 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.

c
#include <stdio.h> struct student { int reg_no; char name[50]; char gender[10]; char address[100]; }; int main() { FILE *fp; struct student s; char choice; fp = fopen("score.dat", "w"); do { printf("Enter registration number: "); scanf("%d", &s.reg_no); printf("Enter name: "); scanf("%s", s.name); printf("Enter gender: "); scanf("%s", s.gender); printf("Enter address: "); scanf("%s", s.address); fprintf(fp, "%d %s %s %s\n", s.reg_no, s.name, s.gender, s.address); printf("Do you want to continue? (y/n): "); scanf(" %c", &choice); } while (choice == 'y' || choice == 'Y'); fclose(fp); fp = fopen("score.dat", "r"); printf("\nStudent Records:\n"); while (fscanf(fp, "%d %s %s %s", &s.reg_no, s.name, s.gender, s.address) != EOF) { printf("Reg No: %d | Name: %s | Gender: %s | Address: %s\n", s.reg_no, s.name, s.gender, s.address); } fclose(fp); return 0; }

3. Write a C program to store the names, roll numbers, and marks of 10 students using a structure and display the records properly.

c
#include <stdio.h> struct student { int roll_no; char name[50]; int marks; }; int main() { struct student s[10]; for (int i = 0; i < 10; i++) { printf("Enter information for student %d:\n", i + 1); printf("Roll No: "); scanf("%d", &s[i].roll_no); printf("Name: "); scanf("%s", s[i].name); printf("Marks: "); scanf("%d", &s[i].marks); } printf("\nStudent Records:\n"); for (int i = 0; i < 10; i++) { printf("Roll No: %d | Name: %s | Marks: %d\n", s[i].roll_no, s[i].name, s[i].marks); } return 0; }

4. Explain the three modes of file opening in C. Write a program to read names and roll numbers from a file named exam.dat and display them.

  • Modes of file opening:
    • r: Read mode, opens a file for reading. The file must exist.
    • w: Write mode, creates a new file or overwrites an existing file.
    • a: Append mode, opens a file for writing, appending data to the end.
c
#include <stdio.h> int main() { FILE *fp; char name[50]; int roll_no; fp = fopen("exam.dat", "r"); if (fp == NULL) { printf("File not found.\n"); return 1; } printf("Reading from exam.dat:\n"); while (fscanf(fp, "%s %d", name, &roll_no) != EOF) { printf("Name: %s | Roll No: %d\n", name, roll_no); } fclose(fp); return 0; }

5. Write a C program to store employee details (ID, name, and salary) in a file named emp.txt and display each record on the screen in an appropriate format.

c
#include <stdio.h> struct employee { int id; char name[50]; float salary; }; int main() { FILE *fp; struct employee e; fp = fopen("emp.txt", "w"); printf("Enter employee details:\n"); for (int i = 0; i < 3; i++) { printf("Enter ID: "); scanf("%d", &e.id); printf("Enter Name: "); scanf("%s", e.name); printf("Enter Salary: "); scanf("%f", &e.salary); fprintf(fp, "%d %s %.2f\n", e.id, e.name, e.salary); } fclose(fp); fp = fopen("emp.txt", "r"); printf("\nEmployee Records:\n"); while (fscanf(fp, "%d %s %f", &e.id, e.name, &e.salary) != EOF) { printf("ID: %d | Name: %s | Salary: %.2f\n", e.id, e.name, e.salary); } fclose(fp); return 0; }

6. Describe file handling concepts in C. Write a C program to enter names and addresses of students and store them in a file named student.dat.

  • File Handling Concepts:
    • Opening a file using modes (r, w, a).
    • Reading and writing data using functions like fscanf, fprintf, fread, and fwrite.
    • Closing the file with fclose.
c
#include <stdio.h> struct student { char name[50]; char address[100]; }; int main() { FILE *fp; struct student s; fp = fopen("student.dat", "w"); printf("Enter student details:\n"); for (int i = 0; i < 3; i++) { printf("Enter Name: "); scanf("%s", s.name); printf("Enter Address: "); scanf("%s", s.address); fprintf(fp, "%s %s\n", s.name, s.address); } fclose(fp); fp = fopen("student.dat", "r"); printf("\nStudent Records:\n"); while (fscanf(fp, "%s %s", s.name, s.address) != EOF) { printf("Name: %s | Address: %s\n", s.name, s.address); } fclose(fp); return 0; }

7. Define function in C. Write a program to generate the factorial of a given number using a recursive function.

c
#include <stdio.h> int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("Factorial of %d is %d\n", num, factorial(num)); return 0; }

8. Write a C program to enter an array of integers, sort them in ascending order, and display the sorted list.

c
#include <stdio.h> int main() { int n; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; printf("Enter the elements:\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Sorting the array for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } printf("Sorted Array:\n"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }

9. Explain the concept of pointers in C. Write a program to input the radius of a football and find the area using a user-defined function.

  • Pointers in C:
    • A pointer is a variable that stores the address of another variable. Pointers are used for dynamic memory allocation and function argument passing by reference.
c
#include <stdio.h> #define PI 3.14 void calculate_area(float *r) { float area = PI * (*r) * (*r); printf("Area of football with radius %.2f is: %.2f\n", *r, area); } int main() { float radius; printf("Enter the radius of the football: "); scanf("%f", &radius); calculate_area(&radius); return 0; }

10. Define structure in C. Write a program using structure to input staff ID, name, and salary of 50 staff members. Display the staff ID, name, and salary of those whose salary ranges from 25,000 to 40,000.

c
#include <stdio.h> struct staff { int id; char name[50]; float salary; }; int main() { struct staff s[50]; printf("Enter details of 50 staff members:\n"); for (int i = 0; i < 50; i++) { printf("Staff %d - ID: ", i + 1); scanf("%d", &s[i].id); printf("Name: "); scanf("%s", s[i].name); printf("Salary: "); scanf("%f", &s[i].salary); } printf("\nStaff members with salary between 25,000 and 40,000:\n"); for (int i = 0; i < 50; i++) { if (s[i].salary >= 25000 && s[i].salary <= 40000) { printf("ID: %d | Name: %s | Salary: %.2f\n", s[i].id, s[i].name, s[i].salary); } } return 0; }

11. Write a C program to input student marks for 5 subjects, calculate the total and average marks, and display the results using structures.

c
#include <stdio.h> struct Student { char name[50]; int marks[5]; int total; float average; }; int main() { struct Student student; printf("Enter student name: "); fgets(student.name, sizeof(student.name), stdin); student.total = 0; for (int i = 0; i < 5; i++) { printf("Enter marks for subject %d: ", i + 1); scanf("%d", &student.marks[i]); student.total += student.marks[i]; } student.average = student.total / 5.0; printf("\nStudent Name: %s", student.name); printf("Total Marks: %d\n", student.total); printf("Average Marks: %.2f\n", student.average); return 0; }

12. Write a C program to enter 10 integer numbers into an array, sort them in descending order, and display the result.

c
#include <stdio.h> int main() { int arr[10], temp; printf("Enter 10 integers:\n"); for (int i = 0; i < 10; i++) { scanf("%d", &arr[i]); } for (int i = 0; i < 10 - 1; i++) { for (int j = i + 1; j < 10; j++) { if (arr[i] < arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } printf("Sorted array in descending order:\n"); for (int i = 0; i < 10; i++) { printf("%d ", arr[i]); } return 0; }

13. Write a C program to enter the name, age, and salary of an employee and store it in a file named employee.dat. Display the stored records properly.

c
#include <stdio.h> struct Employee { char name[50]; int age; float salary; }; int main() { struct Employee emp; FILE *file; file = fopen("employee.dat", "w"); if (file == NULL) { printf("Error opening file.\n"); return 1; } printf("Enter employee name: "); fgets(emp.name, sizeof(emp.name), stdin); printf("Enter employee age: "); scanf("%d", &emp.age); printf("Enter employee salary: "); scanf("%f", &emp.salary); fwrite(&emp, sizeof(struct Employee), 1, file); fclose(file); file = fopen("employee.dat", "r"); if (file == NULL) { printf("Error opening file.\n"); return 1; } printf("\nStored Record:\n"); fread(&emp, sizeof(struct Employee), 1, file); printf("Name: %s", emp.name); printf("Age: %d\n", emp.age); printf("Salary: %.2f\n", emp.salary); fclose(file); return 0; }

14. Explain the importance of file handling in C. Write a program to enter employee names and salaries and write them to a file named employee.txt.

File handling is important in C for managing data persistence, which allows programs to save data between runs. File handling enables operations like reading, writing, appending, and modifying files.

c
#include <stdio.h> struct Employee { char name[50]; float salary; }; int main() { struct Employee emp; FILE *file; file = fopen("employee.txt", "w"); if (file == NULL) { printf("Error opening file.\n"); return 1; } printf("Enter employee name: "); fgets(emp.name, sizeof(emp.name), stdin); printf("Enter employee salary: "); scanf("%f", &emp.salary); fwrite(&emp, sizeof(struct Employee), 1, file); fclose(file); printf("Employee record saved.\n"); return 0; }

15. Write a C program to store the student roll number, name, and marks in a file and retrieve the stored information.

c
#include <stdio.h> struct Student { int roll_no; char name[50]; int marks; }; int main() { struct Student student; FILE *file; file = fopen("student.dat", "w"); if (file == NULL) { printf("Error opening file.\n"); return 1; } printf("Enter student roll number: "); scanf("%d", &student.roll_no); printf("Enter student name: "); getchar(); // To consume newline character left by previous scanf fgets(student.name, sizeof(student.name), stdin); printf("Enter student marks: "); scanf("%d", &student.marks); fwrite(&student, sizeof(struct Student), 1, file); fclose(file); file = fopen("student.dat", "r"); if (file == NULL) { printf("Error opening file.\n"); return 1; } printf("\nStored Record:\n"); fread(&student, sizeof(struct Student), 1, file); printf("Roll Number: %d\n", student.roll_no); printf("Name: %s", student.name); printf("Marks: %d\n", student.marks); fclose(file); return 0; }

16. Describe the advantages of pointers in C. Write a C program to demonstrate the relationship between arrays and pointers.

Advantages of pointers:

  • Efficient memory access.
  • Ability to pass large data structures by reference.
  • Dynamic memory allocation.
c
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int *ptr = arr; printf("Array elements using pointers:\n"); for (int i = 0; i < 5; i++) { printf("%d ", *(ptr + i)); } return 0; }

17. Write a program in C to find the sum of n integers using a function.

c
#include <stdio.h> int sum(int n) { int sum = 0, num; for (int i = 0; i < n; i++) { printf("Enter number %d: ", i + 1); scanf("%d", &num); sum += num; } return sum; } int main() { int n; printf("Enter the number of integers: "); scanf("%d", &n); int result = sum(n); printf("Sum of the numbers: %d\n", result); return 0; }

18. Write a program in C to input employee ID, name, address, and post for 20 employees and display them properly using structures.

c
#include <stdio.h> struct Employee { int id; char name[50]; char address[100]; char post[50]; }; int main() { struct Employee emp[20]; for (int i = 0; i < 20; i++) { printf("Enter details for employee %d:\n", i + 1); printf("ID: "); scanf("%d", &emp[i].id); printf("Name: "); getchar(); // To consume newline character left by previous scanf fgets(emp[i].name, sizeof(emp[i].name), stdin); printf("Address: "); fgets(emp[i].address, sizeof(emp[i].address), stdin); printf("Post: "); fgets(emp[i].post, sizeof(emp[i].post), stdin); } printf("\nEmployee Records:\n"); for (int i = 0; i < 20; i++) { printf("ID: %d\n", emp[i].id); printf("Name: %s", emp[i].name); printf("Address: %s", emp[i].address); printf("Post: %s\n", emp[i].post); } return 0; }

19. Explain the concept of recursion in C. Write a program to find the factorial of a given number using recursion.

Recursion is a process where a function calls itself to solve a smaller instance of the same problem.

c
#include <stdio.h> int factorial(int n) { if (n == 0 || n == 1) return 1; return n * factorial(n - 1); } int main() { int n; printf("Enter a number: "); scanf("%d", &n); printf("Factorial of %d is %d\n", n, factorial(n)); return 0; }

20. Write a C program to differentiate between structure and union by taking an example of an employee’s data storage.

c
#include <stdio.h> struct EmployeeStruct { int id; char name[50]; float salary; }; union EmployeeUnion { int id; char name[50]; float salary; }; int main() { struct EmployeeStruct empStruct; union EmployeeUnion empUnion; printf("Size of Structure: %lu\n", sizeof(empStruct)); printf("Size of Union: %lu\n", sizeof(empUnion)); return 0; }

21. Write a C program to input student records (name, roll number, and department) and store them in a file named student.dat. Read and display the stored records properly.

c
#include <stdio.h> struct Student { char name[50]; int roll_no; char department[50]; }; int main() { struct Student student; FILE *file; file = fopen("student.dat", "w"); if (file == NULL) { printf("Error opening file.\n"); return 1; } printf("Enter student details:\n"); printf("Name: "); fgets(student.name, sizeof(student.name), stdin); printf("Roll Number: "); scanf("%d", &student.roll_no); getchar(); // To consume newline character printf("Department: "); fgets(student.department, sizeof(student.department), stdin); fwrite(&student, sizeof(struct Student), 1, file); fclose(file); file = fopen("student.dat", "r"); if (file == NULL) { printf("Error opening file.\n"); return 1; } fread(&student, sizeof(struct Student), 1, file); printf("\nStored Student Record:\n"); printf("Name: %s", student.name); printf("Roll Number: %d\n", student.roll_no); printf("Department: %s", student.department); fclose(file); return 0; }

22. Differentiate between an array and a structure. Write a C program to store employee data (name, post, and salary) using structures and display the stored records.

An array stores multiple elements of the same type, while a structure can store different data types together under one name.

c
#include <stdio.h> struct Employee { char name[50]; char post[50]; float salary; }; int main() { struct Employee emp; printf("Enter employee name: "); fgets(emp.name, sizeof(emp.name), stdin); printf("Enter employee post: "); fgets(emp.post, sizeof(emp.post), stdin); printf("Enter employee salary: "); scanf("%f", &emp.salary); printf("\nEmployee Record:\n"); printf("Name: %s", emp.name); printf("Post: %s", emp.post); printf("Salary: %.2f\n", emp.salary); return 0; }

23. Write a program to enter the ID, name, and salary of 100 employees using structures and display the employee details.

c
#include <stdio.h> struct Employee { int id; char name[50]; float salary; }; int main() { struct Employee emp[100]; for (int i = 0; i < 100; i++) { printf("Enter details for employee %d:\n", i + 1); printf("ID: "); scanf("%d", &emp[i].id); getchar(); // To consume newline character left by previous scanf printf("Name: "); fgets(emp[i].name, sizeof(emp[i].name), stdin); printf("Salary: "); scanf("%f", &emp[i].salary); } printf("\nEmployee Details:\n"); for (int i = 0; i < 100; i++) { printf("ID: %d\n", emp[i].id); printf("Name: %s", emp[i].name); printf("Salary: %.2f\n", emp[i].salary); } return 0; }

24. Write a C program to store names and marks of 20 students, sort them in descending order, and display the sorted list.

c
#include <stdio.h> #include <string.h> struct Student { char name[50]; int marks; }; int main() { struct Student students[20], temp; for (int i = 0; i < 20; i++) { printf("Enter name of student %d: ", i + 1); fgets(students[i].name, sizeof(students[i].name), stdin); printf("Enter marks for student %d: ", i + 1); scanf("%d", &students[i].marks); getchar(); // To consume newline character } for (int i = 0; i < 19; i++) { for (int j = i + 1; j < 20; j++) { if (students[i].marks < students[j].marks) { temp = students[i]; students[i] = students[j]; students[j] = temp; } } } printf("\nSorted list of students by marks:\n"); for (int i = 0; i < 20; i++) { printf("%sMarks: %d\n", students[i].name, students[i].marks); } return 0; }

25. Describe the different types of operators in C with examples.

C has several types of operators, including:

  • Arithmetic operators: +, -, *, /, %
  • Relational operators: ==, !=, <, >, <=, >=
  • Logical operators: &&, ||, !
  • Bitwise operators: &, |, ^, <<, >>
  • Assignment operators: =, +=, -=, *=, /=
  • Increment/Decrement operators: ++, --
  • Conditional (ternary) operator: condition ? expr1 : expr2

Discussion (20)