# Introduction to C Programming Sem1 Assignment 2

MathQuestion.in

## 1. Describe the basic structure of “C” Program.

Sem1 Assignment 3

The basic structure of a C program includes the preprocessor directives, global declarations, function definitions, and the main function.

## Q2. Define the algorithm with suitable examples

A2. An algorithm is a set of instructions or rules that are followed to solve a problem. For example, an algorithm for finding the maximum number in a list of integers could be:

1. Set the maximum value to the first number in the list.
2. Loop through each number in the list.
3. If a number is greater than the current maximum value, update the maximum value to that number.
4. Once all numbers have been checked, the maximum value will be found.

## Q3. What are the characteristics of C language.

A3. The characteristics of C language include its low-level nature, ability to manipulate memory directly, and efficiency in execution. It also has a rich set of operators and functions, making it versatile and capable of handling complex tasks. However, it requires careful management of memory and can be prone to errors if not written properly.

## Q4. Write a simple interest program take input through keyboard.

A4. Here is an example program in C that calculates simple interest based on user input:

#include <stdio.h>

int main() {
float principal, rate, time, interest;

printf("Enter principal amount: ");
scanf("%f", &principal);

printf("Enter rate of interest: ");
scanf("%f", &rate);

printf("Enter time period in years: ");
scanf("%f", &time);

interest = (principal * rate * time) / 100;

printf("Simple interest = %f", interest);

return 0;
}

In this program, the user inputs the principal amount, rate of interest, and time period in years. The program then calculates the simple interest using the formula (P * R * T) / 100 and outputs the result.

## Q.5 Explain identifiers, keywords and constants.

Ans. Identifiers in C are used to name variables, functions, and other program elements. They can be composed of letters, digits, and underscore characters and must start with a letter or underscore. Keywords, on the other hand, are reserved words in the C language that have a specific meaning and cannot be used as identifiers. Examples of keywords include "int", "float", and "if". Constants are values that do not change during program execution and are assigned a fixed value. Examples of constants include integers, floating-point numbers, and character literals.

## Q.6 Explain if and if-else statements with suitable examples

Ans. The "if" statement in C is used to execute a block of code if a certain condition is true. For example:

if (x > 5) {
printf("x is greater than 5");
}

In this example, if the value of x is greater than 5, the code inside the if statement will be executed. If the condition is false, the code will be skipped.

The "if-else" statement in C allows for two different blocks of code to be executed depending on whether a condition is true or false. For example:

if (x > 5) {
printf("x is greater than 5");
} else {
printf("x is less than or equal to 5");
}

In this example, if the value of x is greater than 5, the first block of code will be executed. If the condition is false, the second block of code will be executed instead.

## Q7. What is the difference between continue and break statement.

A. The "continue" statement in C is used to skip the current iteration of a loop and continue with the next iteration. This means that any code after the "continue" statement within the loop will be skipped, but the loop will continue to run. The "break" statement, on the other hand, is used to immediately exit a loop, regardless of whether or not the loop has finished running all its iterations.

## Q8. What is loop in C? write a program to print 1 to 20 using for loop.

B. A loop in C is a control structure that allows a block of code to be executed repeatedly, as long as a certain condition is true. There are three types of loops in C: the "for" loop, the "while" loop, and the "do-while" loop.

Here's an example program that uses a "for" loop to print the numbers 1 to 20:

#include <stdio.h>

int main() {
int i;

for (i = 1; i <= 20; i++) {
printf("%d\n", i);
}

return 0;
}

In this program, the variable "i" is initialized to 1 and incremented by 1 after each iteration of the loop. The loop will continue to run as long as "i" is less than or equal to 20. Within the loop, the printf() function is used to print the value of "i" to the console. Once "i" reaches 21, the loop will exit and the program will end.

## Q9. What is a 1-Dimenasional array? Describe with example.

Ans. 1-Dimensional array in C is a collection of elements of the same data type, arranged in a linear fashion. Each element in the array is identified by its index number, starting from 0.

Here's an example of how to declare and initialize a 1-Dimensional array in C:

int numbers = {1, 2, 3, 4, 5};

In this example, we declare an array called "numbers" that can hold 5 integers. We then initialize the array with the values 1, 2, 3, 4, and 5.

## Q.10 Write a “C” program of swapping of two numbers using call by value and call by reference.

Ans . Here's an example program that demonstrates swapping two numbers using both call by value and call by reference:

#include <stdio.h>

// function prototype for call by value
void swap_by_value(int a, int b);

// function prototype for call by reference
void swap_by_reference(int *a, int *b);

int main() {
int x = 10;
int y = 20;

// swapping using call by value
printf("Before swapping using call by value: x = %d, y = %d\n", x, y);
swap_by_value(x, y);
printf("After swapping using call by value: x = %d, y = %d\n", x, y);

// swapping using call by reference
printf("Before swapping using call by reference: x = %d, y = %d\n", x, y);
swap_by_reference(&x, &y);
printf("After swapping using call by reference: x = %d, y = %d\n", x, y);

return 0;
}

// function definition for call by value
void swap_by_value(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}

// function definition for call by reference
void swap_by_reference(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}

In this program, we declare two variables x and y, with initial values of 10 and 20 respectively. We then call two functions: swap_by_value() and swap_by_reference(), passing in the values of x and y as arguments.

The swap_by_value() function swaps the values of its two arguments using a temporary variable. However, since the arguments are passed by value, the original values of x and y remain unchanged.

The swap_by_reference() function, on the other hand, swaps the values of its two arguments using pointers. By passing in the addresses of x and y as arguments, the function is able to modify the values of x and y directly, resulting in a successful swap.

## Q11. Explain user defined functions ? What are the advantages of the functions ?

A. User-defined functions are functions that are created by the user to perform specific tasks or operations. These functions are written in a programming language and can be used repeatedly throughout the program. The advantages of using user-defined functions are:

1. Reusability: Once a function is defined, it can be used multiple times in the program, reducing the amount of code that needs to be written.

2. Modularity: Functions allow for the code to be divided into smaller, more manageable pieces, making it easier to debug and maintain.

3. Abstraction: Functions hide the complexity of the code from the user, allowing them to focus on the task at hand.

4. Code readability: Functions make the code more readable and organized, making it easier for other programmers to understand.

## Q12. What is structures in “C”? Explain with suitable example.

B. In C programming language, a structure is a user-defined data type that allows grouping of variables of different data types under a single name. It is also known as a record. For example, a structure can be used to represent a person's details such as name, age, and address. The syntax for declaring a structure is as follows:

struct person {
char name;
int age;
};

In this example, we have defined a structure called "person" that contains three variables: a character array "name" of size 50, an integer "age", and a character array "address" of size 100. Once the structure is defined, we can create variables of this type and access its members using the dot (.) operator. For example:

struct person p1;
p1.age = 25;
strcpy(p1.name, "John");

Here, we have created a variable "p1" of type "person" and assigned values to its members using the dot operator. We can also pass structures as arguments to functions and return them from functions.

## 13. What is file handling in “C” language? Explain input file in file handling.

Q1. File handling in "C" language refers to the process of reading from or writing to files. Files are used to store data permanently on a storage device such as a hard drive. In "C" language, file handling is done using a set of functions that allow the programmer to create, open, read, write, and close files.

An input file in file handling is a file that is used for reading data. It contains data that is to be read by the program. The data can be in any format, such as text, numbers, or binary data. The program reads the data from the input file and uses it for processing.

## Q15. What is fclose(), fopen() functions? Explain with example.

Ans. The fopen() function in "C" language is used to open a file. It returns a pointer to the file that can be used for reading or writing data. The syntax for the fopen() function is as follows:

FILE *fopen(const char *filename, const char *mode);

The first argument is the name of the file to be opened, and the second argument specifies the mode in which the file is to be opened (read, write, append, etc.). For example:

FILE *fp;
fp = fopen("file.txt", "r");

In this example, we have opened a file called "file.txt" in read mode ("r"). The fopen() function returns a pointer to the file, which is stored in the variable "fp".

The fclose() function in "C" language is used to close a file that has been opened using the fopen() function. It frees up the resources used by the file and ensures that any data that has not been written to the file is saved before closing. The syntax for the fclose() function is as follows:

int fclose(FILE *stream);

The argument is a pointer to the file that is to be closed. For example:

fclose(fp);

In this example, we have closed the file that was opened earlier using the fopen() function. The file pointer "fp" is passed as an argument to the fclose() function.