C Programming 2022 Regular (NEP) Questions with Answers (Important)
Section - A
1. Answer any Six question.
a.Mention the data types used in C.
- int
- float and double
- char
- void
- long
b. Define getchar() and putchar().
The getchar() function reads a single character from the standard input stream, which is usually the keyboard. It returns an integer value representing the ASCII code of the character read.
The putchar() function writes a single character to the standard output stream, which is usually the console. It takes an integer value representing the ASCII code of the character to be written as input.
c. Write the syntax of switch statement.
switch (expression) {
case value1:
// Statements to be executed if expression == value1.
break;
case value2:
// Statements to be executed if expression == value2.
break;
...
default:
// Statements to be executed if expression does not match any of the case values.
break;
}
d. Define Looping.
Looping in programming is a control structure that allows a block of code to be executed repeatedly until a certain condition is met. This can be useful for tasks such as iterating over a collection of data, performing a calculation multiple times, or waiting for a specific event to occur.
e. What are arrays?
An array is a data structure that stores a collection of elements of the same data type. Arrays are very efficient for storing and accessing data, and they are used in a wide variety of programming tasks.
f. How strings are defined in C?
In C, a string is defined as a sequence of characters terminated by a null character (\0). The null character is a special character that has the value 0. Strings are stored in memory as arrays of characters, but they are treated differently by the compiler and the programmer.
g. Name different types of functions.
- Library functions
- User-defined functions
h. Define Structure.
A structure in C is a user-defined data type that can be used to group together related data items of different data types. Structures are declared using the struct keyword, followed by the name of the structure and a list of its members.
Section - B
Answer any Three Questions.
a. Explain different types of constants.
Integer Constants
An integer constant is a sequence of digits from 0 to 9 without decimal points or fractional part or any other symbols. There are 3 types of integers namely decimal integer, octal integers and hexadecimal integer.
Real Constants
Real Constants consists of a fractional part in their representation. Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082. Example of real constants are
Single Character Constants
A Single Character constant represent a single character which is enclosed in a pair of quotation symbols.
String Constants
A string constant is a set of characters enclosed in double quotation marks. The characters in a string constant sequence may be a alphabet, number, special character and blank space.
b. Explain basic structure of c program.
C programs can also be divided into the following sections:
Preprocessor section: This section contains preprocessor directives, which are used to control the compilation process.
Definition section: This section contains definitions for variables, constants, and macros.
Global declaration section: This section contains declarations for global variables and functions.
Main function section: This section contains the code for the main() function.
User-defined function section: This section contains definitions for user-defined functions.
c. What are keywords and tokens? Mention them.
Keywords in C are reserved words that have special meaning to the compiler. They cannot be used as identifiers or variable names.
Tokens in C are the small meaningful units of code. They can be keywords, identifiers, operators, constants, or special characters.
Keywords: auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while.
Identifiers: Names of variables, functions, structures, unions, and enums.
Operators: Arithmetic operators (+, -, *, /, %), relational operators (==, !=, <, >, <=, >=), logical operators (&&, ||, !), bitwise operators (&, |, ^, ~, <<, >>), assignment operators (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=), conditional operator (?:), and comma operator (,)
Constants: Integer constants, floating-point constants, character constants, and string literals
Special characters: Punctuation marks (such as ;, ., (), {}, [])
d. Differenctiate between formatted and unformatted I/O statements.
Formatted I/O vs Unformatted I/O
Answer any Three Questions.
a. What is branching? Explain varieties of if statements.
Branching in C is a programming technique that allows the flow of execution of a program to be altered based on the outcome of a condition. This is done using conditional statements, which are statements that evaluate a condition and then execute a different block of code depending on the outcome of the condition.
There are four varieties of if statements in C:
1.Simple if statement: The simple if statement is the most basic type of if statement. It has the following syntax:
if (condition) {
// Code to be executed if the condition is true.
}
2.Else if statement: The else if statement is used to check multiple conditions in order. The code inside the else if block is executed if the condition is true and all previous conditions were false. The else if statement has the following syntax:
if (condition1) {
// Code to be executed if condition1 is true.
} else if (condition2) {
// Code to be executed if condition1 is false and condition2 is true.
} else if (condition3) {
// Code to be executed if condition1 and condition2 are false and condition3 is true.
} else {
// Code to be executed if all conditions are false.
}
3.Switch statement: The switch statement is used to select one of a number of code blocks to execute based on the value of an expression. The switch statement has the following syntax:
switch (expression) {
case value1:
// Code to be executed if expression == value1.
break;
case value2:
// Code to be executed if expression == value2.
break;
...
default:
// Code to be executed if expression does not match any of the case values.
break;
}
4.Nested if statements: Nested if statements are if statements that are contained inside other if statements. Nested if statements can be used to implement complex branching logic.
For example, the following code shows a nested if statement:
int x = 10;
int y = 5;
if (x > 5) {
if (y > 10) {
printf("x is greater than 5 and y is greater than 10.\n");
} else {
printf("x is greater than 5 and y is less than or equal to 10.\n");
}
} else {
printf("x is less than or equal to 5.\n");
}
Output:
x is greater than 5 and y is less than or equal to 10.
b. Write a program to find largest of three numbers.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
printf("%d is the largest number.\n", a);
}
else if (b >= a && b >= c)
{
printf("%d is the largest number.\n", b);
}
else
{
printf("%d is the largest number.\n", c);
}
return 0;
}
c. Explain logical & Relational operators?
Logical operators in C are used to combine two or more boolean expressions into a single boolean expression. The following are the three logical operators in C:
- AND (&&): The AND operator returns true if both of its operands are true, and false otherwise.
- OR (||): The OR operator returns true if either of its operands is true, and false otherwise.
- NOT (!): The NOT operator returns the opposite of its operand.
Relational operators in C are used to compare two operands and return a boolean value indicating the relationship between them. The following are the six relational operators in C:
- Equal to (==): The equal to operator returns true if its two operands are equal, and false otherwise.
- Not equal to (!=): The not equal to operator returns true if its two operands are not equal, and false otherwise.
- Greater than (>): The greater than operator returns true if its first operand is greater than its second operand, and false otherwise.
- Less than (<): The less than operator returns true if its first operand is less than its second operand, and false otherwise.
- Greater than or equal to (>=): The greater than or equal to operator returns true if its first operand is greater than or equal to its second operand, and false otherwise.
- Less than or equal to (<=): The less than or equal to operator returns true if its first operand is less than or equal to its second operand, and false otherwise.
d.Differentiate break and continue statements with an example.
Example
Break Statement in C
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
printf("%d\n", i);
}
Continue Statement in C
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue;
}
printf("%d\n", i);
}
Answer any Three Questions.
a. What is string.h? Mention any four string functions.
string.h is a standard header file in the C programming language. It defines a number of functions for manipulating strings, which are arrays of characters.
Here are four string functions from string.h:
strlen(): Returns the length of a string.
strcpy(): Copies one string to another.
strcat(): Concatenates two strings.
strcmp(): Compares two strings.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, world!";
char destination[20];
// Copy the source string to the destination string.
strcpy(destination, source);
// Print the destination string.
printf("%s\n", destination);
return 0;
}
b. Write a program to find length of string without using built in function.
#include <stdio.h>
int main()
{
char string[100];
int length = 0;
// Get the string from the user.
printf("Enter a string: ");
scanf("%s", string);
// Iterate over the string and count the characters.
for (int i = 0; string[i] != '\0'; i++)
{
length++;
}
// Print the length of the string.
printf("The length of the string is %d\n", length);
return 0;
}
c. Explain one dimension and two dimension array.
One-dimensional array
A one-dimensional array is a list of elements of the same data type. The elements in the array are accessed using an index, which is an integer value that starts from 0 and goes up to the length of the array minus 1.
Exampel :
int numbers[10];
for (int i = 0; i < 10; i++)
{
numbers[i] = i + 1;
}
for (int i = 0; i < 10; i++)
{
printf("%d\n", numbers[i]);
}
Example :
Two-dimensional array
A two-dimensional array is a table of elements of the same data type. The elements in the array are accessed using two indices, one for the row and one for the column.
int matrix[3][3];
int count = 1;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
matrix[i][j] = count++;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
d. Write a program to add two matrices.
#include <stdio.h>
int main()
{
int rows, columns;
// Get the number of rows and columns from the user.
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &columns);
// Declare two matrices to store the user input.
int matrix1[rows][columns];
int matrix2[rows][columns];
// Get the elements of the two matrices from the user.
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter the elements of the second matrix:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
scanf("%d", &matrix2[i][j]);
}
}
// Declare a matrix to store the sum of the two matrices.
int result[rows][columns];
// Add the two matrices and store the result in the result matrix.
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print the result matrix.
printf("The sum of the two matrices is:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Answer any Three Questions.
a. Explain with syntax declaration of functions.
A function in C is a self-contained block of code that performs a specific task. Functions can be used to organize code, make it more modular and reusable, and to improve the readability and maintainability of code.
syntax:
return_type function_name(parameter_list);
Use code with caution. Learn more
Where: return_type is the type of data that the function returns. If the function does not return any data, the return type is void.
function_name is the name of the function.
parameter_list is a list of parameters that the function can accept. The parameters are listed within parentheses, and each parameter is separated by a comma.
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int sum = add(1, 2);
printf("The sum of 1 and 2 is %d\n", sum);
return 0;
}
b. Explain recursive functions.
A recursive function is a function that calls itself directly or indirectly. Recursive functions can be used to solve a variety of problems, such as calculating the factorial of a number, generating the Fibonacci sequence, and traversing a tree.
Example :
int factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
int main()
{
int factorial_of_5 = factorial(5);
printf("The factorial of 5 is %d\n", factorial_of_5);
return 0;
}
Output:
The factorial of 5 is 120
c. Write a program to find the given number in prime using function.
#include <stdio.h>
int is_prime(int n)
{
for (int i = 2; i < n / 2 + 1; i++)
{
if (n % i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (is_prime(number))
{
printf("%d is a prime number.\n", number);
}
else
{
printf("%d is not a prime number.\n", number);
}
return 0;
}
d. Write a program using structure to store the record of n students.
#include <stdio.h>
struct Student
{
int roll_number;
char name[100];
float marks;
};
int main()
{
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++)
{
printf("Enter the roll number of student %d: ", i + 1);
scanf("%d", &students[i].roll_number);
printf("Enter the name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter the marks of student %d: ", i + 1);
scanf("%f", &students[i].marks);
}
printf("\nThe student records are as follows:\n");
for (int i = 0; i < n; i++)
{
printf("Roll number: %d\n", students[i].roll_number);
printf("Name: %s\n", students[i].name);
printf("Marks: %f\n", students[i].marks);
}
return 0;
}