Chapter 4: Review of Programming in C – Class 12 Computer Science Notes
Importantedunotes.com
Back to Computer Notes

Exercise 1: Choose the correct answer.

Select an option to view the correct answer and justification.

1. What is the correct syntax to declare a structure in C?
a) struct {}
b) #define struct {}
c) struct
d) struct <name> {}
Correct Answer: d) struct <name> {}
Justification: A structure in C must be declared using the struct keyword followed by a tag name and a block containing its members, as in struct name {…};
2. In C, which operator is used to get the address of a variable?
a) *
b) &
c) #
d) ~
Correct Answer: b) &
Justification: The address-of operator (&) returns the memory address of a variable, e.g. &b gives the address of b. (The * operator is used to dereference a pointer, not to get an address.)
3. What will be the output of the program code?
char str1[20]="Hello", str2[20]="World"; strcat(str1, str2); puts(str1);
a) Hello
b) HelloWorld
c) World
d) WorldHello
Correct Answer: b) HelloWorld
Justification: strcat() appends the second string directly to the end of the first string with no separator, so str1 becomes “HelloWorld”.
4. What is the correct way to open a file named “data.txt” in C for reading?
a) fopen(“data.txt”, “r”);
b) fopen(“data.txt”, “w”);
c) fopen(“data.txt”, “a”);
d) fopen(“data.txt”, “b”);
Correct Answer: a) fopen(“data.txt”, “r”);
Justification: The “r” mode opens an existing file strictly for reading. “w” is for writing (overwrites), “a” is for appending, and “b” alone is not a valid standalone mode.
5. Which of the following is true about FILE *fpt;?
a) FILE is a keyword in C for representing files and fpt is a variable of file type.
b) FILE is a buffered stream.
c) FILE is a stream.
d) FILE is a structure and fpt is a pointer to the structure of FILE type.
Correct Answer: d) FILE is a structure and fpt is a pointer to the structure of FILE type.
Justification: FILE is actually a structure defined in stdio.h that stores information about a file stream, and fpt is declared as a pointer to that structure.
6. What is the output of the statement printf("%d", a++); in C programming?
a) The current value of a.
b) The value of a incremented by 1.
c) The memory address of a.
d) Garbage.
Correct Answer: a) The current value of a.
Justification: The post-increment operator (a++) uses the current value of a in the expression first, and only increments it afterward.
7. Which statement is true to open a binary file “Record.dat” for reading and writing?
a) file = fopen(“Record.dat”, “wb”);
b) file = fopen(“Record.dat”, “ab”);
c) file = fopen(“Record.dat”, “rb”);
d) file = fopen(“Record.dat”, “rb+”);
Correct Answer: d) file = fopen(“Record.dat”, “rb+”);
Justification: The “rb+” mode opens an existing binary file for both reading and writing, unlike “wb” (write-only, overwrites), “ab” (append-only), or “rb” (read-only).
8. What is the output of the program?
int b = 25; int *p; p = &b; printf("%d %d", b, *p);
a) 25 25
b) 25 1234
c) 1234 1234
d) 1234 25
Correct Answer: a) 25 25
Justification: p stores the address of b, so *p (dereferencing p) gives the value stored at that address, which is 25 тАФ the same as b itself.
9. ___ is a reserved word that has standard and predefined meanings in C.
a) Variable
b) Data
c) Data type
d) Keyword
Correct Answer: d) Keyword
Justification: Keywords are reserved words (like int, for, return) that already have a fixed, predefined meaning in the C language and cannot be used as identifiers.
10. ___ is a quantity that doesn’t change during program execution.
a) Variable
b) Constant
c) Data type
d) None of the above
Correct Answer: b) Constant
Justification: A constant is a fixed value that cannot be altered once assigned, unlike a variable, whose value can change during execution.
11. ___ is a symbolic name, which is used to store different types of data in the computer’s memory and may change during program execution.
a) Variable
b) Constant
c) Identifier
d) None of the above
Correct Answer: a) Variable
Justification: A variable is a named storage location whose value can be changed during program execution, matching the given description exactly.
12. ___ is a symbol that instructs to perform some operation or action on one or more operands.
a) Operand
b) Operation
c) Operator
d) None of the above
Correct Answer: c) Operator
Justification: An operator (e.g. +, -, *) is a symbol that tells the compiler to perform a specific mathematical or logical operation on given operands.
13. ___ is something that an operator acts on.
a) Operand
b) Operation
c) Operator
d) None of the above
Correct Answer: a) Operand
Justification: An operand is the value or variable that an operator performs its action on, e.g. in a + b, both a and b are operands.

Exercise 2: Write short answers to these questions.

1. Define the term “Constant”, “identifier” and “keyword” in C Program.
Constant: A constant is a name given to the variable whose values can’t be altered or changed. A constant is very similar to variables in the C programming language, but it can hold only a single value during the execution of a program.
Identifier: Identifiers are a string of characters that serve as the name (hence identifier) of a variable, function, array, pointer, structure, etc. In other words, in the C programming language, an identifier can be described as the user-specified name used to identify a specific item.
Keyword: Keywords in C refer to a set of reserved words with predefined meanings that are used to write programs in the C programming language.

2. What is Variable? List the major data types which are used in C Program.

A user-defined or a user-readable custom name assigned to a memory location is known as a variable. The major data types which are used in a C program are as follows:

Char
Int
Float
Double

The four basic data types in C programming are namely char, int, float, and double. In the C programming language, the signed modifier represents both positive and negative values while the unsigned modifier means all positive values.


3. What is structure in C program? Explain.

A structure can be defined as a single entity holding variables of different data types that are logically related to each other. All the data members inside a structure are accessible to the functions defined outside the structure. There are 6 sections in a C program that are Documentation, Preprocessor section, Definition, Global Declaration, main() function, and subprograms.

Syntax of structure:

structure-syntax.c
struct structureName {
    DataType member1;
    DataType member2;
    DataType memberN;
};

4. List out the different types of operators in C. Describe the logical operator with example.

The different types of operators in C are as follows:

Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Assignment operators

Logical operators: A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator. Common logical operators include AND, OR, NOT. An operator is used to compare logical expressions that return a result of true and false.

Note: Symbols such as less than (<), greater than (>), equal to (==), and not equal to (!=) are relational operators used alongside logical ones, not logical operators themselves.

5. Describe the different modes of the file handling concept in C.

File handling in C involves a series of operations including the creation, opening, reading, writing, and closing of files. To accomplish these tasks, C provides a set of functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), among others. The different modes of file handling are as follows:

a) Opening file
b) Closing file
c) Reading file
d) Writing file

6. Differentiate between array and structure with example.
ArrayStructure
Array is a collection of homogeneous data.Structure is a collection of heterogeneous data.
Array data are accessed using an index.Structure elements are accessed using a dot (.) operator.
Array allocates static memory.Structure allocates dynamic memory.
Array element access takes less time than structures.Structure elements take more time to access than Arrays.
Each data item is called an element.Each data item is called a member.
We cannot have an array of arrays (in this context).We can have an array of structures.

Exercise 3: Long Answer Questions (Solved C Programs)

7. Write a C program that uses structures to represent details of five books (title, author, publisher & price) and print them out.
books.c
#include <stdio.h>
#include <conio.h>

struct books {
    char title[50];
    char author[20];
    char publisher[30];
    float price;
} b[5];

int main() {
    int i;
    printf("Enter the details of books:\n");
    for (i = 0; i < 5; i++) {
        printf("Enter book %d details\n", i + 1);
        printf("----\n");
        printf("Enter the title: ");
        scanf("%s", b[i].title);
        printf("Enter the author: ");
        scanf("%s", b[i].author);
        printf("Enter the publisher: ");
        scanf("%s", b[i].publisher);
        printf("Enter the price: ");
        scanf("%f", &b[i].price);
    }
    
    // Display results
    printf("The book details are:\n");
    for (i = 0; i < 5; i++) {
        printf("\nBook details %d", i + 1);
        printf("\nTitle: %s", b[i].title);
        printf("\nAuthor: %s", b[i].author);
        printf("\nPublisher: %s", b[i].publisher);
        printf("\nPrice: %f", b[i].price);
    }
    getch();
    return 0;
}

8. Discuss the concept of binary file handling in C program and explain how putw() and getw() functions facilitate binary input/output operations. Give example.

Binary File Handling is a process in which we create a file and store data in its original format. It means that if we stored an integer value in a binary file, the value will be treated as an integer rather than text. Binary files are mainly used in storing records just as we store records in a database. A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII characters. They contain data that is stored in a similar manner to how it is stored in the main memory. Binary files can be created only from within a program and their contents can only be read by a program.

The getw() function is used to read binary data from a file. A pointer to the file stream from which the data is to be read serves as its sole argument. The file pointer is advanced in accordance with how the function reads the data, which normally reads as an integer (4 bytes).

The putw() function is used to write binary data to a file. The data to be written and a pointer to the file stream where the data is to be written are its required inputs. The data is written to the file as an integer, which is normally 4 bytes long.


9. Write a program to store five employees’ records (EID, Name, post and department) and display them using structure.
employees.c
#include <stdio.h>
#include <conio.h>

struct employees {
    int EID;
    char Name[50];
    char Post[30];
    char department[10];
} E[5];

int main() {
    int i;
    printf("Enter the details of employees\n");
    for(i = 0; i < 5; i++) {
        printf("Enter the employee information %d\n", i + 1);
        printf("---\n");
        printf("Enter the EID: ");
        scanf("%d", &E[i].EID);
        printf("Enter the Name: ");
        scanf("%s", E[i].Name);
        printf("Enter the Post: ");
        scanf("%s", E[i].Post);
        printf("Enter the department: ");
        scanf("%s", E[i].department);
    }
    
    // Display results
    printf("The employee information are:\n");
    for(i = 0; i < 5; i++) {
        printf("\nEmployee information %d\n", i + 1);
        printf("EID: %d\n", E[i].EID);
        printf("Name: %s\n", E[i].Name);
        printf("Post: %s\n", E[i].Post);
        printf("Department: %s\n", E[i].department);
    }
    getch();
}

10. What is structure? Write a program to store five students’ information (ID, Name, DOB, and Phone) and display them using structure.

Answer: A way to group several related variables into one place is known as a structure.

students.c
#include <stdio.h>
#include <conio.h>

struct student {
    int ID;
    char Name[50];
    int DOB;
    int phone;
} S[5];

int main() {
    int i;
    
    // Input loop
    for(i = 0; i < 5; i++) {
        printf("\nEnter the details of student %d\n", i + 1);
        printf("-------------------\n");
        printf("Enter the Name: ");
        scanf("%s", S[i].Name);
        printf("Enter the DOB: ");
        scanf("%d", &S[i].DOB);
        printf("Enter the ID: ");
        scanf("%d", &S[i].ID);
        printf("Enter the phone num: ");
        scanf("%d", &S[i].phone);
    }
    
    // Display result
    printf("\nThe student details are:\n");
    for(i = 0; i < 5; i++) {
        printf("\nDetails of student %d\n", i + 1);
        printf("Name: %s\n", S[i].Name);
        printf("DOB: %d\n", S[i].DOB);
        printf("ID: %d\n", S[i].ID);
        printf("Phone num: %d\n", S[i].phone);
    }
    
    getch();
    return 0;
}

11. Describe file handling modes in C. Write a C program to create and write data into a file.

Answer: The process of file handling refers to how we store the available data or info in a file with the help of a program. The C language stores all the data available in a program into a file with the help of file handling in C. This data can be fetched/extracted from these files to work again in any program.

To create a file in C, the steps are as follows:

Declare a file type pointer variable to store the reference of the file, say FILE *fptr = NULL;
Create or open a file using the fopen() function.
Input data from the user to write into the file, and store it to some variable, say data.
C provides several functions to perform I/O operations on the file.

12. Define the structure. Write a C program using structure to input staff id, name, and the salary of 50 staffs. Display staff id, name, and salary of those staff whose salary range is from 25 thousand to 40 thousand.

Answer: A collection of logically related variables of different data types is called a structure.

staff-salary.c
#include <stdio.h>
#include <conio.h>

struct Staff {
    int staffid;
    char name[50];
    float salary;
} S[50];

int main() {
    int i;
    
    // Input loop
    for(i = 0; i < 50; i++) {
        printf("\nEnter the information of staff %d\n", i + 1);
        printf("-------------------\n");
        printf("Enter the staff id: ");
        scanf("%d", &S[i].staffid);
        printf("Enter the Name: ");
        scanf("%s", S[i].name);
        printf("Enter the salary: ");
        scanf("%f", &S[i].salary);
    }
    
    // Display result
    printf("\nThe information of staff details are:\n");
    for(i = 0; i < 50; i++) {
        if(S[i].salary >= 25000 && S[i].salary <= 40000) {
            printf("\nInformation of staff %d\n", i + 1);
            printf("Staff id: %d\n", S[i].staffid);
            printf("Name: %s\n", S[i].name);
            printf("Salary: %.2f\n", S[i].salary);
        }
    }
    
    getch();
    return 0;
}

13. Write a C program to enter name, grade, address, and age of 10 students in a structure and display the information.
students-grade.c
#include <stdio.h>
#include <conio.h>

struct Student {
    int grade;
    char name[50];
    int age;
    char address[20];
} S[10];

int main() {
    int i;
    
    // Input loop
    for(i = 0; i < 10; i++) {
        printf("\nEnter the information of student %d\n", i + 1);
        printf("-------------------\n");
        printf("Enter the grade: ");
        scanf("%d", &S[i].grade);
        printf("Enter the name: ");
        scanf("%s", S[i].name);
        printf("Enter the age: ");
        scanf("%d", &S[i].age);
        printf("Enter the address: ");
        scanf("%s", S[i].address);
    }
    
    // Display result
    printf("\nThe student information details are:\n");
    for(i = 0; i < 10; i++) {
        printf("\nInformation of student %d\n", i + 1);
        printf("Grade: %d\n", S[i].grade);
        printf("Name: %s\n", S[i].name);
        printf("Age: %d\n", S[i].age);
        printf("Address: %s\n", S[i].address);
    }
    
    getch();
    return 0;
}

14. 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”.

Answer: The process of file handling refers to how we store the available data or info in a file with the help of a program. The C language stores all the data available in a program into a file with the help of file handling in C. This data can be fetched/extracted from these files to work again in any program.

Note: The code below demonstrates array of structures input/output rather than active file pointer handling, but is transcribed as provided.
student-dat.c
#include <stdio.h>
#include <conio.h>

struct student_dat {
    char name[50];
    char address[30];
} s_dat[20];

int main() {
    int i;
    
    // Input loop
    for(i = 0; i < 20; i++) {
        printf("\nEnter the information of student %d\n", i + 1);
        printf("-------------------\n");
        printf("Enter the name: ");
        scanf("%s", s_dat[i].name);
        printf("Enter the address: ");
        scanf("%s", s_dat[i].address);
    }
    
    // Display result
    printf("\nThe information of Student details are:\n");
    for(i = 0; i < 20; i++) {
        printf("\nInformation of student %d\n", i + 1);
        printf("Name: %s\n", s_dat[i].name);
        printf("Address: %s\n", s_dat[i].address);
    }
    
    getch();
    return 0;
}

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

Answer: A function declaration specifies the name of the function, the types and number of parameters it expects to receive, and its return type.

factorial-recursive.c
#include <stdio.h>
#include <conio.h>

int fact(int);

int main() {
    int f, n;
    
    printf("Enter any number: ");
    scanf("%d", &n);
    
    f = fact(n);
    printf("The factorial of the number is %d\n", f);
    
    getch();
    return 0;
}

int fact(int n) {
    if(n == 1) {
        return 1;
    } else {
        return (n * fact(n - 1));
    }
}

16. Explain the three modes of file opening in C. Write a program to read name and roll from a file named “exam.dat” and display them.

Answer: The three modes of file opening in C are:

r – read
w – write
a – append

r – read: Only reading is possible. It does not create a file if it does not exist. It opens a text file for reading.

w – write: Only writing is possible. It creates a file if it does not exist, otherwise, it erases the old content of the file and opens it as a blank file.

a – append: Only writing is possible. It creates a file if it does not exist, otherwise, it opens the file and writes from the end of the file (it does not erase the old content).

Note: The code below demonstrates array of structures input/output rather than active file pointer handling, but is transcribed as provided.
exam-dat.c
#include <stdio.h>
#include <conio.h>

struct exam_dat {
    char name[50];
    int roll;
} E_dat[20];

int main() {
    int i;
    
    // Input loop
    for(i = 0; i < 20; i++) {
        printf("\nEnter the information of student %d\n", i + 1);
        printf("-------------------\n");
        printf("Enter the name: ");
        scanf("%s", E_dat[i].name);
        printf("Enter the roll: ");
        scanf("%d", &E_dat[i].roll);
    }
    
    // Display result
    printf("\nThe information of student details are:\n");
    for(i = 0; i < 20; i++) {
        printf("\nInformation of student %d\n", i + 1);
        printf("Name: %s\n", E_dat[i].name);
        printf("Roll: %d\n", E_dat[i].roll);
    }
    
    getch();
    return 0;
}

17. What is a structure? Write a program to input roll, name, and age of 5 students and display them properly using structure.

Answer: A collection of logically related variables of different data types is called a structure.

students-roll.c
#include <stdio.h>
#include <conio.h>

struct Student {
    int roll;
    char name[50];
    int age;
} S[5];

int main() {
    int i;
    
    printf("\nThe student details of %d\n", i);
    for(i = 0; i < 5; i++)
{
    printf("Enter the information of student %d", i+1);
    
    printf("\n --- ");
    printf("Enter the roll");
    scanf("%d", &s[i].roll);
    printf("Enter the name");
    scanf("%s", s[i].name);
    printf("Enter the age");
    scanf("%d", &s[i].age);
}

// Display result
printf("The student details is %d are :", i);
for(i=0; i<5; i++)
{
    printf("Enter the information of student %d", i+1);
    
    printf("%d roll", s[i].roll);
    printf("%s name", s[i].name);
    printf("%d age", s[i].age);
}
getch();
}

18. Write a C program to enter 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.
emp-txt.c
#include<stdio.h>
#include<conio.h>

struct emp
{
    int ID;
    char name[20];
    char post[20];
} e_txt[10];

int main()
{
    int i;
    printf("The information of employee is %d ", i);
    for(i=0; i<10; i++)
    {
        printf("Enter the information of employee is %d", i+1);
        
        printf("\n --- ");
        printf("Enter the ID");
        scanf("%d", &e_txt[i].ID);
        printf("Enter the name");
        scanf("%s", e_txt[i].name);
        printf("Enter the post");
        scanf("%s", e_txt[i].post);
    }
    
    // Display result
    printf("The information of employee is %d ", i);
    for (i=0; i<10; i++)
    {
        printf("Enter the information of employee is %d", i+1);
        
        printf("%d ID", e_txt[i].ID);
        printf("%s Name", e_txt[i].name);
        printf("%s post", e_txt[i].post);
    }
    getch();
}

19. Explain any three different types of operators in C with example.

The three different types of operators in C are:

Arithmetic operators: Arithmetic operators in computer science refer to symbols or functions that perform mathematical operations such as addition, subtraction, multiplication, division, and exponentiation.
Relational operator: The relational operators are used to compare two of the available values to understand what relationship the pairs of values share.
Logical operators: A logical operator is a symbol or word used to connect two or more expressions such that the value of the compound expression produced.

20. Write a program to print Fibonacci series of 6, 12, 18, 30 — upto 10 terms using loop.
fibonacci.c
#include<stdio.h>
#include<conio.h>

int fib(int n);
int main()
{
    int i, n, A;
    printf("Enter the number");
    scanf("%d", &n);
    for(i=6; i<=n; i++)
        A = fib(n);
    printf("%d \t", A);
    getch();
}
int fib(int n)
{
    if(n==0 || n==1)
        return n;
    else
        return(fib(n-1) + fib(n-2));
}

21. Write a program to input emp_no, name and salary of 100 employees using structure. Display the name and salary of employee using structure.
employees-100.c
#include<stdio.h>
#include<conio.h>
struct employees
{
    int empno;
    char name[50];
    float salary;
} e[100];

int main()
{
    int i;
    printf("The information of employee is %d", i);
    for(i=0; i<100; i++)
    {
        printf("Enter the information of employee is %d", i+1);
        
        printf("\n --- ");
        printf("Enter the emp_no");
        scanf("%d", &e[i].empno);
        printf("Enter the name");
        scanf("%s", e[i].name);
        printf("Enter the salary");
        scanf("%f", &e[i].salary);
    }
    
    // Display result
    printf("The information of employee is %d ", i);
    for(i=0; i<100; i++)
    {
        printf("Enter the information of employee is %d", i+1);
        
        printf("%d emp_no", e[i].empno);
        printf("%s name", e[i].name);
        printf("%.2f salary", e[i].salary);
    }
    getch();
}

22. How do you define function in c program? Write a program to find out given number is negative or positive using function.

A function is defined as a relation between a set of inputs having one output each.

positive-negative-fn.c
#include<stdio.h>
#include<conio.h>

int num(int n);
int main()
{
    int n;
    printf("Enter the number");
    scanf("%d", &n);
    num(n);
    getch();
}

int num(int n)
{
    if(n>0)
    {
        printf("%d is positive number", n);
    }
    else
    {
        printf("%d is negative number", n);
    }
}

23. What is file handling concept in c program? Write a program to enter name and salary of employee and write it in a file “employee.txt”.

The process of file handling refers to how we store the available data or info in a file with the help of a program.

employee-txt.c
#include<stdio.h>
#include<conio.h>
struct employee
{
    char Name[50];
    float salary;
} e_txt[10];

int main()
{
    int i;
    printf("The information of employee is %d", i);
    for(i=0; i<10; i++)
    {
        printf("Enter the information of employee is %d", i+1);
        
        printf("\n --- ");
        printf("Enter the Name");
        scanf("%s", e_txt[i].Name);
        printf("Enter the salary");
        scanf("%f", &e_txt[i].salary);
    }
    
    // Display result
    printf("The information of employee are: %d", i);
    for(i=0; i<10; i++)
    {
        printf("Enter the information of employee is %d", i+1);
        
        printf("%s name", e_txt[i].Name);
        printf("%.2f salary", e_txt[i].salary);
    }
    getch();
}

24. Write a program to read the marks of any 5 subject in a subject and count how many students are passed or failed. (pass marks = 35)
pass-fail.c
#include<stdio.h>
#include<conio.h>

void main()
{
    int i, marks[100], p=0, f=0;
    for(i=0; i<5; i++)
    {
        printf("enter the marks:");
        scanf("%d", &marks[i]);
        if(marks >= 35)
        {
            printf("student has passed");
            p++;
        }
        else
        {
            printf("student has failed");
            f++;
        }
    }
    printf("Number of student passed: %d", p);
    printf("Number of student failed: %d", f);
    getch();
}

25. Explain the structure in c. Write a program to enter the 5 student, name, age and sex using structure and display.

A structure in c is a user-defined data type that allows one to store data of different types under one name.

student-gender.c
#include<stdio.h>
#include<conio.h>
struct student
{
    char name[50];
    int age;
    char gender[20];
} s[5];

int main()
{
    int i;
    printf("The information of student is %d", i);
    for(i=0; i<5; i++)
    {
        printf("Enter the details of student is %d", i+1);
        
        printf("\n --- ");
        printf("Enter the name");
        scanf("%s", s[i].name);
        printf("Enter the age");
        scanf("%d", &s[i].age);
        printf("Enter the gender");
        scanf("%s", s[i].gender);
    }
    
    // Display result
    printf("The information of student is %d", i);
    for(i=0; i<5; i++)
    {
        printf("Enter the details of student is %d", i+1);
        
        printf("%s name", s[i].name);
        printf("%d age", s[i].age);
        printf("%s gender", s[i].gender);
    }
    getch();
}

26. What is user defined function? Write a program to input length and breadth of a pond and find its area by using user defined function.

A user-defined function is a type of function in c language that is defined by the user himself to perform some specific task.

pond-area.c
#include <stdio.h>

// User-defined function declaration (prototype)
float calculateArea(float length, float breadth);

int main() {
    float length, breadth, area;

    // Prompt user for input
    printf("Enter the length of the pond: ");
    scanf("%f", &length);

    printf("Enter the breadth of the pond: ");
    scanf("%f", &breadth);

    // Call the user-defined function and store the result in 'area'
    area = calculateArea(length, breadth);

    // Display the calculated area
    printf("The area of the pond is: %.2f\n", area);

    return 0;
}

// User-defined function definition
float calculateArea(float length, float breadth) {
    // Area of a rectangle (pond) is length multiplied by breadth
    return length * breadth;
}

27. What is structure? Write a program to input employee id, name, address and post of 20 employees and display them properly.

A collection of logically related variables of different data types is called structure.

employees-20.c
#include<stdio.h>
#include<conio.h>
struct employee
{
    int employee_id;
    char name[50];
    char address[30];
} e[20];

int main()
{
    int i;
    printf("The information of student is %d", i);
    for(i=0; i<10; i++)
    {
        printf("Enter the information of student is %d", i+1);
        
        printf("\n --- ");
        printf("Enter the employee id");
        scanf("%d", &e[i].employee_id);
        printf("Enter the name");
        scanf("%s", e[i].name);
        printf("Enter the address");
        scanf("%s", e[i].address);
    }
    
    // Display result
    printf("The student information is %d :", i);
    for(i=0; i<10; i++)
    {
        printf("Enter the student information is %d", i+1);
        
        printf("%d employee id", e[i].employee_id);
        printf("%s name", e[i].name);
        printf("%s address", e[i].address);
    }
    getch();
}

28. What is function? Write the advantages of function. Differentiate between library and user defined function with example.

A user-defined function is a type of function in c language that is defined by the user himself to perform some specific task.

The advantages of function are:

Enables reusability and reduces redundancy.
Makes a code modular.
Provides abstraction functionality.
The program becomes easy to understand and manage.
Breaks an extensive program into smaller and simpler pieces.

Difference between library and user defined function:

Library-Defined FunctionUser-Defined Function
It refers to predefined functions or built-in functions in C libraries.It is a function which is created by the user as per their own requirements.
It is already created.We have to create it ourselves.
It is simple to use.It is usually more complex to use.
Program development time is faster.Program development time is slower.
It requires a header file to use it.It requires a function prototype to use it.
It is called at run time.It is called at compile time.

29. Write a program to input name, roll, and registration number of 10 students using structure and display them in a proper format.
students-regno.c
#include <stdio.h>
#include <conio.h>

struct student 
{
    char name[20];
    int roll;
    int regno;
} st[10];

int main() 
{
    int i;
    printf("The information of 10 students\n");
    for (i = 0; i < 10; i++) 
    {
        printf("Enter the information of student %d\n", i + 1);
        printf("--------------------------------------\n");
        printf("Enter the name: ");
        scanf("%s", st[i].name);
        printf("Enter the roll: ");
        scanf("%d", &st[i].roll);
        printf("Enter the registration number: ");
        scanf("%d", &st[i].regno);
    }
    // Display result
    printf("\nThe information of student is:\n");
    for (i = 0; i < 10; i++) 
    {
        printf("\nInformation of student %d:\n", i + 1);
        printf("Name: %s\n", st[i].name);
        printf("Roll: %d\n", st[i].roll);
        printf("Registration No: %d\n", st[i].regno);
    }
    getch();
}

30. What is a function? Write a program to read the principle, time, and rate, and find the simple interest using a function.

Definition: A function prototype specifies the name of the function, the types and number of parameters it expects to receive, and its return type.

simple-interest.c
#include <stdio.h>
#include <conio.h>
float si(float p, float t, float r);

int main() 
{
    float P, T, R, SI;
    printf("Enter the principle: ");
    scanf("%f", &P);
    printf("Enter the time: ");
    scanf("%f", &T);
    printf("Enter the rate: ");
    scanf("%f", &R);
    
    SI = si(P, T, R);
    printf("The simple interest is: %f\n", SI);
    getch();
    return 0;
}

float si(float p, float t, float r) 
{
    return (p * t * r) / 100;
}

31. What are the importances of functions in C program? Write a program to find out if a given number is odd or even using a function.

The importance / key features of functions in a C program are:

Rich set of built-in functions.
Operators can be used to write any complex program.
The compiler combines the capabilities of an assembly language with the features of a high-level language.
It is well-suited for writing both system software and business packages.
odd-even-fn.c
#include <stdio.h>
#include <conio.h>

int check(int n);

int main() 
{
    int n;
    printf("Enter the number: ");
    scanf("%d", &n);
    check(n);
    getch();
    return 0;
}

int check(int n) 
{
    if (n % 2 == 0) 
    {
        printf("The even number is %d\n", n);
    } 
    else 
    {
        printf("The odd number is %d\n", n);
    }
}

32. What are the string functions in C? Write a program to print the given text in reverse order.

Definition: The C string functions are built-in functions that can be used for various operations and manipulations on strings. These string functions can be used to perform tasks such as string copying, concatenation, comparison, length, etc. The <string.h> header file contains these string functions.

reverse-text.c
#include <stdio.h>
#include <conio.h>
#include <string.h>

int main() {
    char text[100];
    int i, length;

    printf("Enter the text to reverse: ");

    gets(text); 

    length = strlen(text);

    printf("Reversed text: ");
    
    for(i = length - 1; i >= 0; i--) {
        printf("%c", text[i]);
    }
    printf("\n");

    getch(); 
    
    return 0;
}

33. Describe the structure in C. Write a program to enter 5 subject marks, calculate the sum, and print using a structure.

Definition: A structure in C is a user-defined data type that can be used to group items of possibly different types into a single type.

subject-marks.c
#include <stdio.h>
#include <conio.h>

struct Subject 
{
    int Eng, Nep, Sci, Eco, Acc;
} s[5];

int main() 
{
    int i;
    int sum;
    printf("The information of 5 subjects\n");
    for (i = 0; i < 5; i++) 
    {
        printf("Enter the information of subject group %d\n", i + 1);
        printf("-------------------------------------------\n");
        printf("Enter marks for 5 subjects (English, Nepali, Science, Economics, Account): ");
        scanf("%d%d%d%d%d", &s[i].Eng, &s[i].Nep, &s[i].Sci, &s[i].Eco, &s[i].Acc);
    }
    
    // Display result
    printf("\nThe information of 5 subjects are:\n");
    for (i = 0; i < 5; i++) 
    {
        sum = s[i].Eng + s[i].Nep + s[i].Sci + s[i].Eco + s[i].Acc;
        printf("\nStudent %d marks:\n", i + 1);
        printf("English: %d, Nepali: %d, Science: %d, Economics: %d, Account: %d\n", 
               s[i].Eng, s[i].Nep, s[i].Sci, s[i].Eco, s[i].Acc);
        printf("Sum = %d\n", sum);
    }
    getch();
}

34. Describe the function in C. Write a program to find the sum of n integer numbers using a function.

Definition: Functions in C are the basic building blocks of a program. A function is a set of statements enclosed within curly brackets {} that takes inputs, performs computations, and provides the resultant output.

sum-n-numbers.c
#include <stdio.h>
#include <conio.h>

int sum(int n);

int main() 
{
    int num, A;
    printf("Enter the number: ");
    scanf("%d", &num);
    
    A = sum(num);
    printf("The sum of natural numbers is %d\n", A);
    getch();
    return 0;
}

int sum(int n) 
{
    if (n == 0) 
    {
        return 0;
    } 
    else 
    {
        return (n + sum(n - 1));
    }
}

35. Differentiate between array and structure.
ArrayStructure
An array is a collection of homogeneous data.A structure is a collection of heterogeneous data.
Array elements are accessed using an index.Structure elements are accessed using the dot (.) operator.
Arrays allocate static memory.Structures allocate dynamic memory.
Accessing array elements takes less time.Accessing structure elements takes more time.
Each data item is called an element.Each data item is called a member.
We cannot have an array of arrays (directly as a single linear type).We can have an array of structures.

36. Define control structure. Write a program to find the sum, difference, and greater of two integer numbers using a switch-case statement.

Definition: Control structures enable a programmer to determine the order in which program statements are executed.

switch-case.c
#include <stdio.h>

int main() 
{
    int num1, num2, sum, diff, greater;
    int choice;
    
    printf("Enter two numbers: ");
    scanf("%d%d", &num1, &num2);
printf("Choose an operation:\n");
    printf("1. Sum\n");
    printf("2. Difference\n");
    printf("3. Greater number\n");
    scanf("%d", &choice);
    
    switch (choice) 
    {
        case 1:
            sum = num1 + num2;
            printf("Sum = %d\n", sum);
            break;
            
        case 2:
            diff = num1 - num2;
            printf("Difference = %d\n", diff);
            break;
            
        case 3:
            greater = (num1 > num2) ? num1 : num2;
            printf("Greater number = %d\n", greater);
            break;
            
        default:
            printf("Invalid Choice\n");
    }
    return 0;
}

37. Write a program to add two integer numbers using functions.
add-fn.c
#include <stdio.h>
#include <conio.h>

int add(int a, int b);

int main() 
{
    int a, b, A;
    printf("Enter the two numbers: ");
    scanf("%d%d", &a, &b);
    
    A = add(a, b);
    printf("The sum of two numbers is %d\n", A);
    getch();
    return 0;
}

int add(int a, int b) 
{
    return (a + b);
}

38. Write a program to multiply two integer numbers using functions.
multiply-fn.c
#include <stdio.h>
#include <conio.h>

int mul(int a, int b);

int main() 
{
    int a, b, M;
    printf("Enter any two numbers: ");
    scanf("%d%d", &a, &b);
    
    M = mul(a, b);
    printf("The multiplication of two numbers is %d\n", M);
    getch();
    return 0;
}
int mul(int a, int b) 
{
    return (a * b);
}

39. Write a program which asks the user to input ‘n’ terms of numbers and find out the greatest and smallest number among those numbers.
greatest-smallest.c
#include <stdio.h>
#include <conio.h>

void main() 
{
    int n, num, greatest, smallest, i;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    
    for (i = 1; i <= n; i++) 
    {
        printf("Enter number %d: ", i);
        scanf("%d", &num);
        
        if (i == 1) 
        {
            greatest = num;
            smallest = num;
        }
        
        if (num > greatest) 
        {
            greatest = num;
        }
        
        if (num < smallest) 
        {
            smallest = num;
        }
    }
    printf("The greatest number is %d\n", greatest);
    printf("The smallest number is %d\n", smallest);
    getch();
}

ЁЯУД View / Download Full PDF Notes

If the PDF doesn’t load, click here to open it in Google Drive.

ЁЯУЪ Also Read: NEB Notes

Scroll to Top