Friday, October 3, 2025

Unit 1: Basics, Algorithm & Operators ( Programming in C Language)


1️⃣ Explain Algorithm with Example and Properties.

Definition:
Algorithm is a step-by-step procedure to solve a problem in a finite number of steps.

Example: Add two numbers

  1. Start

  2. Read two numbers, A and B

  3. Calculate Sum = A + B

  4. Print Sum

  5. Stop

Properties of a good algorithm:

  • Input: Accepts zero or more inputs

  • Output: Produces at least one output

  • Definiteness: Steps are clear and unambiguous

  • Finiteness: Algorithm must terminate after finite steps

  • Effectiveness: Steps are basic, feasible, and solvable manually or by machine


2️⃣ Draw Flowchart and Explain Sum of First 10 Numbers

Flowchart:

┌───────┐ │ Start │ └───┬───┘ ↓ i=1, sum=0 ↓ ┌── i ≤ 10 ? ──┐ │ Yes No│ ↓ ↓ sum=sum+i Print sum i=i+1 ↓ │ ┌───────┐ └─────────► Stop │ └─────┘

Explanation:

  • Initialize i=1 and sum=0.

  • Check condition i ≤ 10.

  • Add i to sum and increment i.

  • Repeat until i > 10.

  • Print final sum and stop.


3️⃣ Explain Compilation & Execution Process in C

Steps:

  1. Editing: Write source code in .c file.

  2. Preprocessing: Removes comments, expands macros, includes header files.

  3. Compilation: Converts source code to assembly code.

  4. Assembly: Converts assembly code into object code (.o file).

  5. Linking: Combines object code with libraries to produce executable.

  6. Execution: OS loads and runs the executable file.

Example:

#include <stdio.h> int main() { printf("Hello, World!"); return 0; }

4️⃣ Explain Keywords, Identifiers, and Constants in C

Keywords: Reserved words in C with predefined meaning.
Example: int, float, if, else, return

Identifiers: User-defined names for variables, functions, arrays, etc.
Example: sum, age, myFunction

Constants: Fixed values that do not change.
Example: #define PI 3.14 or const int x = 5;

Difference between Keywords and Identifiers:

KeywordsIdentifiers
Reserved by CUser-defined names
Cannot be used as variable namesCan be used as variable or function names
Example: int, floatExample: sum, age

5️⃣ Explain Operators in C with Examples

Types of operators:

  1. Arithmetic: + - * / %

  2. Unary: ++ --

  3. Relational/Logical: > < == != && || !

  4. Bitwise: & | ^ << >>

  5. Assignment: = += -= *= /= %=

  6. Conditional: ?:

Examples:

int a=5, b=3; int c; c = a + b; // 8 c = a & b; // 1 (bitwise AND) c = a << 1; // 10 (left shift)

Precedence & Associativity:

  • Precedence decides which operator evaluated first

  • Associativity decides order for operators with same precedence

Example:

10 + 20 * 5 // 110, '*' has higher precedence 100 / 10 * 5 // 50, '/' and '*' have same precedence, left-to-right

Unit 2: Control Structures, Functions, Arrays

6️⃣ Explain Control Structures in C with Examples

Definition: Control structures manage the flow of execution in a program.

Types:

  1. Sequential: Statements executed line by line.

  2. Decision Making (Selection): if, if-else, switch-case

  3. Loops (Iteration): for, while, do-while

  4. Jump Statements: break, continue, goto

Examples:

For Loop (Factorial):

int n=5, i, fact=1; for(i=1;i<=n;i++) fact*=i; printf("%d", fact);

While Loop (Even Numbers):

int i=2; while(i<=10){ printf("%d ",i); i+=2; }

Switch-Case:

int day=3; switch(day){ case 1: printf("Mon"); break; case 2: printf("Tue"); break; default: printf("Other"); }

7️⃣ Explain Functions in C with Types and Examples

Definition: Block of code that performs a specific task and can be reused.

Advantages: Code reusability, easy debugging, modularity

Types:

  • Library functions: printf(), scanf()

  • User-defined functions

Categories based on arguments & return type:

  1. No arguments, no return:

void hello(){ printf("Hi"); }
  1. Arguments, no return:

void sum(int a,int b){ printf("%d",a+b); }
  1. No arguments, return value:

int getNum(){ return 10; }
  1. Arguments & return:

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

Call by value vs Call by reference:

  • Call by value → function gets a copy, original variable not changed

  • Call by reference → function gets address, original variable can be changed


8️⃣ Explain Arrays in C with Examples

Definition: Array = collection of elements of same type in contiguous memory.

Types:

  • 1D Array: int arr[5]={1,2,3,4,5};

  • 2D Array: int mat[2][2]={{1,2},{3,4}};

  • Multidimensional Array

Example Programs:

Sum of 1D Array:

int arr[5], i, sum=0; for(i=0;i<5;i++){ scanf("%d",&arr[i]); sum+=arr[i]; } printf("Sum=%d", sum);

Matrix Addition (2D Array):

int a[2][2], b[2][2], sum[2][2], i,j; for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&a[i][j]); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&b[i][j]); for(i=0;i<2;i++) for(j=0;j<2;j++) sum[i][j]=a[i][j]+b[i][j]; for(i=0;i<2;i++){ for(j=0;j<2;j++) printf("%d ",sum[i][j]); printf("\n"); }

9️⃣ Program Based Long Questions

  1. Factorial using recursion:

int fact(int n){ if(n<=1) return 1; else return n*fact(n-1);}
  1. Sum of first 10 numbers using loop & function:

int sum10(){ int i, sum=0; for(i=1;i<=10;i++) sum+=i; return sum; }
  1. Matrix addition program → See 2D array example above.

  2. Demonstrate Bitwise Operators:

int a=5,b=3; printf("%d %d %d %d %d", a&b, a|b, a^b, a<<1, a>>1);

No comments:

Post a Comment

✅ UNIT 4 — POSET, LATTICES & BOOLEAN ALGEBRA (DISCRETE MATHEMATICS)

  ✅ UNIT 4 — POSET, LATTICES & BOOLEAN ALGEBRA 1. Poset Partially Ordered Set A pair (A, ≤) where relation is: Reflexive Anti-...