0% found this document useful (0 votes)
11 views

Module 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 1: Structured Programming Paradigm

1. Explain the steps of algorithm development with an example.


Answer:
Steps of algorithm development:
1. Understand the problem: Clearly define the input, process, and
output.
2. Plan the solution: Write logical steps to solve the problem.

3. Design the algorithm: Represent the solution step-by-step.


4. Implement the algorithm: Write the corresponding code.

Example: Algorithm to find the largest of two numbers:

o Step 1: Start.

o Step 2: Input two numbers, a and b.

o Step 3: If a > b, print a is the largest; else, print b is the largest.

o Step 4: Stop.
2. Describe the structure of a C program with an example.
Answer:
A C program typically has the following structure:
1. Preprocessor directives: Include libraries using #include.

2. Global declarations: Declare global variables/constants if needed.

3. Main function: Contains the core logic of the program.


4. User-defined functions: Additional functions to perform specific tasks.

Example:

#include <stdio.h> // Preprocessor directive

int sum(int a, int b); // Function prototype

int main() { // Main function


int x = 5, y = 10;

printf("Sum: %d\n", sum(x, y));

return 0;
}
int sum(int a, int b) { // User-defined function

return a + b;

}
3. What are preprocessor directives? Explain their types with examples.
Answer:
Preprocessor directives are instructions executed before the compilation of
code.
Types:
o File inclusion (#include): Includes libraries.
Example: #include <stdio.h>
o Macro substitution (#define): Defines constants or macros.
Example: #define PI 3.14
o Conditional compilation (#ifdef): Compiles specific code based on
conditions.
Example:

o #ifdef DEBUG

o printf("Debugging enabled\n");

o #endif
4. Explain the different types of storage classes in C.
Answer:
Storage classes define the scope, lifetime, and visibility of variables.
o auto: Default for local variables. Limited to the block in which declared.

o register: Stored in a CPU register for faster access.

o static: Retains value between function calls.

o extern: Declared outside all functions; accessible globally.

Example:

void func() {

static int count = 0; // Retains value between calls

count++;

printf("%d\n", count);
}
5. What are pointers? Write a program to swap two numbers using
pointers.
Answer:
Pointers are variables that store the memory address of another variable.

Program to swap two numbers:

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 5, y = 10;

printf("Before swap: x = %d, y = %d\n", x, y);

swap(&x, &y);

printf("After swap: x = %d, y = %d\n", x, y);

return 0;

}
6. Explain different types of operators in C with examples.
Answer:
Operators in C are categorized as:
o Arithmetic operators: +, -, *, /, %
Example: a + b adds a and b.
o Relational operators: ==, !=, <, >, <=, >=
Example: a < b checks if a is less than b.
o Logical operators: &&, ||, !
Example: (a > 0) && (b > 0) checks if both are positive.
o Bitwise operators: &, |, ^, ~, <<, >>
Example: a & b performs a bitwise AND.
o Assignment operators: =, +=, -=, etc.
Example: a += b adds b to a.
7. What are formatted I/O functions? Give examples.
Answer:
Formatted I/O functions handle input and output with specified formats.
o printf: Outputs data with formatting.
Example: printf("Value: %d", x);
o scanf: Reads formatted input.
Example: scanf("%d", &x);

Example program:

#include <stdio.h>

int main() {

int age;
printf("Enter your age: ");

scanf("%d", &age);

printf("Your age is %d\n", age);

return 0;

Module 2: Iterative Statements and File Operations


8. What is recursion? Write a program to find the factorial of a number
using recursion.
Answer:
Recursion is a technique where a function calls itself to solve smaller
instances of a problem.

Program:

#include <stdio.h>

int factorial(int n) {

if (n == 0)

return 1;
return n * factorial(n - 1);
}

int main() {

int num = 5;

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

}
9. Explain the difference between while and do-while loops with examples.
Answer:

o while loop: Condition is checked before executing the loop body.


Example:

o int i = 0;

o while (i < 5) {
o printf("%d\n", i);

o i++;

o }
o do-while loop: Loop body is executed at least once before checking
the condition.
Example:

o int i = 0;

o do {

o printf("%d\n", i);

o i++;

o } while (i < 5);


10. What are bitwise operators? Explain with examples.
Answer:
Bitwise operators manipulate individual bits of data.
o & (AND): Sets a bit to 1 if both bits are 1.
Example: 5 & 3 results in 1 (0101 & 0011 = 0001).
o | (OR): Sets a bit to 1 if either bit is 1.
Example: 5 | 3 results in 7 (0101 | 0011 = 0111).
o ^ (XOR): Sets a bit to 1 if bits are different.
Example: 5 ^ 3 results in 6 (0101 ^ 0011 = 0110).
o ~ (NOT): Inverts the bits.
11. Explain file operations in C with examples.
Answer:
File operations in C include:
o Opening a file: fopen().
Example: FILE *fp = fopen("file.txt", "r");
o Reading a file: fscanf(), fgets(), or fgetc().
Example: fscanf(fp, "%s", str);
o Writing to a file: fprintf() or fputc().
Example: fprintf(fp, "Hello World");
o Closing a file: fclose().
Example: fclose(fp);

You might also like