Programming Principle Algorithm
UNIT-I: Introduction to ‘C’ Language
Introduction to ‘C’ Language: History, Structures of ‘C’ Programming, Function as building blocks. Language Fundamentals: Character set, C Tokens, Keywords, Identifiers, Variables, Constants, Data Types, Comments.
UNIT-II: Operators
Operators: Types of operators, Precedence and Associativity, Expression, Statement and types of statements. Build-in Operators and Functions: Console-based I/O and related built-in I/O functions like printf(), scanf(), getch(), getchar(), putchar(); Concept of header files, Preprocessor directives: #include, #define.
UNIT-III: Control Structures
Control Structures: Decision making structures: If, If-else, Nested If-else, Switch. Loop Control Structures: While, Do-while, for, Nested for loop. Other Statements: break, continue, goto, exit.
UNIT-IV: Introduction to Problem Solving
Introduction to Problem Solving: Concept: Problem solving, Problem-solving techniques (Trial & Error, Brainstorming, Divide & Conquer). Steps in Problem Solving: Define Problem, Analyze Problem, Explore Solution. Algorithms and Flowcharts: Definitions, Symbols, Characteristics of an algorithm. Conditionals in pseudo-code, Loops in pseudo-code, Time complexity: Big-Oh notation, efficiency. Simple Examples: Algorithms and Flowcharts (Real Life Examples).
UNIT-V: Simple Arithmetic Problems
Simple Arithmetic Problems: Addition/Multiplication of integers, Determining if a number is +ve/-ve/even/odd, Maximum of 2 numbers, 3 numbers, Sum of first n numbers, given n numbers, Integer division, Digit reversing, Table generation for n, ab, Factorial, Sine series, Cosine series, nCr, Pascal Triangle, Prime number, Factors of a number, Other problems such as Perfect number, GCD numbers, etc. (Write algorithms and draw flowcharts), Swapping.
UNIT-VI: Functions
Functions: Basic types of functions, Declaration and definition, Function call, Types of functions, Parameter passing, Call by value, Call by reference, Scope of variable, Storage classes, Recursion.

UNIT-I: Introduction to C Language

1. History of C Language

The C programming language was developed by Dennis Ritchie in 1972 at Bell Laboratories. It was designed as a system programming language for developing operating systems, specifically UNIX.

Example:
C was used to write the UNIX operating system, making it one of the most widely used programming languages.
        

2. Structure of C Programming

A C program consists of functions, variables, and statements organized in a specific structure. The general structure of a C program is:

#include
int main() {
    // Variable declaration
    // Statements
    return 0;
}
        

Components:

3. Function as Building Blocks

In C, functions are the fundamental building blocks that perform specific tasks. Functions enhance modularity and code reusability.

Example:
#include
void greet() {
    printf("Hello, World!");
}
int main() {
    greet(); // Function call
    return 0;
}
        

4. Language Fundamentals

C programming has several fundamental components:

4.1 Character Set

The character set includes letters, digits, and symbols that can be used in a C program.

Example:
Letters: A-Z, a-z
Digits: 0-9
Special Characters: +, -, *, /, %, #, etc.
        

4.2 C Tokens

Tokens are the smallest building blocks of a C program. Types of tokens include:

Example:
int a = 5; // Tokens: int, a, =, 5, ;
        

4.3 Keywords

Keywords are reserved words with predefined meanings.

Example:
int, float, if, else, while, for, return
        

4.4 Identifiers

Identifiers are names given to variables, functions, or arrays by the programmer.

Rules:
1. Must start with a letter or underscore.
2. Cannot be a keyword.
3. Case-sensitive.
Example:
int age;
        

4.5 Variables and Constants

Variables: Named storage locations for data. Constants: Fixed values that do not change during program execution.

Example:
Variable: int age = 25;
Constant: const float PI = 3.14;
        

4.6 Data Types

Data types specify the type of data a variable can hold.

Example:
int a = 10;   // Integer
float b = 5.5; // Floating point
char c = 'A';  // Character
        

4.7 Comments

Comments are non-executable statements used to describe the code.

Example:
// This is a single-line comment
/* This is a
   multi-line comment */
        

UNIT-II: Operators

1. Types of Operators

C provides various operators to perform operations on data. Types include:

Example:
int a = 5, b = 10;
int sum = a + b;  // Arithmetic Operator
if (a > b) {      // Relational Operator
    printf("a is greater than b");
}
        

2. Precedence and Associativity

Precedence: Determines the order in which operators are evaluated.

Associativity: Determines the direction of evaluation (left-to-right or right-to-left).

Operators with higher precedence are evaluated first. Associativity resolves conflicts between operators with the same precedence.

Example:
Expression: 5 + 2 * 3
Evaluation: 5 + (2 * 3) = 11 (Multiplication has higher precedence than addition)
        

3. Expression, Statement, and Types of Statements

An expression is a combination of variables, operators, and constants that produce a value.

A statement is a single instruction executed by the program.

Example:
int a = 10;  // Expression statement
if (a > 5) { // Control statement
    printf("a is greater than 5");
}
        

4. Built-in Operators and Functions

C provides several built-in operators and functions for performing standard tasks.

Example:
printf("Enter a number: ");
scanf("%d", &a);
putchar('A');
        

5. Console-based I/O and Built-in I/O Functions

Example:
char ch;
printf("Enter a character: ");
ch = getchar();
putchar(ch); // Prints the entered character
        

6. Concept of Header Files

Header files contain declarations of functions and macros used in C programs. Common header files include:

Example:
#include // Includes standard input/output library
        

7. Preprocessor Directives

Preprocessor directives are instructions processed before compilation. Common directives include:

Example:
#include  // Includes stdio.h
#define PI 3.14     // Defines a constant
        

UNIT-III: Control Structures

1. Decision-Making Structures

Decision-making structures allow a program to execute certain sections of code based on conditions.

1.1 If Statement

The if statement executes a block of code if the condition is true.

Syntax:
if (condition) {
    // Code to execute if condition is true
}
Example:
int a = 5;
if (a > 0) {
    printf("a is positive");
}
        

1.2 If-Else Statement

The if-else statement executes one block of code if the condition is true, and another block if it is false.

Syntax:
if (condition) {
    // Code if condition is true
} else {
    // Code if condition is false
}
Example:
int a = -5;
if (a > 0) {
    printf("a is positive");
} else {
    printf("a is negative");
}
        

1.3 Nested If-Else

Nested if-else allows decision-making within another if-else structure.

Example:
int a = 10;
if (a > 0) {
    if (a % 2 == 0) {
        printf("a is positive and even");
    } else {
        printf("a is positive and odd");
    }
}
        

1.4 Switch Statement

The switch statement selects one block of code to execute from multiple choices.

Syntax:
switch (expression) {
    case value1:
        // Code for value1
        break;
    case value2:
        // Code for value2
        break;
    default:
        // Code if no cases match
}
Example:
int day = 3;
switch (day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    case 3: printf("Wednesday"); break;
    default: printf("Invalid day");
}
        

2. Loop Control Structures

Loop control structures are used to repeat a block of code while a condition is true.

2.1 While Loop

The while loop executes a block of code as long as the condition is true.

Syntax:
while (condition) {
    // Code to execute
}
Example:
int i = 1;
while (i <= 5) {
    printf("%d ", i);
    i++;
}
        

2.2 Do-While Loop

The do-while loop executes a block of code at least once before checking the condition.

Syntax:
do {
    // Code to execute
} while (condition);
Example:
int i = 1;
do {
    printf("%d ", i);
    i++;
} while (i <= 5);
        

2.3 For Loop

The for loop is used to execute a block of code a specific number of times.

Syntax:
for (initialization; condition; increment/decrement) {
    // Code to execute
}
Example:
for (int i = 1; i <= 5; i++) {
    printf("%d ", i);
}
        

2.4 Nested For Loop

Nested for loops allow one loop inside another to handle multi-dimensional data or patterns.

Example:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 2; j++) {
        printf("i = %d, j = %d\n", i, j);
    }
}
        

3. Other Statements

C includes other control statements for specific purposes:

Examples:
break:
for (int i = 1; i <= 5; i++) {
    if (i == 3) break;
    printf("%d ", i); // Outputs: 1 2
}

continue:
for (int i = 1; i <= 5; i++) {
    if (i == 3) continue;
    printf("%d ", i); // Outputs: 1 2 4 5
}

goto:
label:
printf("This is a label.");
goto label; // Infinite loop

exit:
if (condition) {
    printf("Exiting program");
    exit(0);
}
        

UNIT-IV: Introduction to Problem Solving

1. Concept of Problem Solving

Problem solving is the process of identifying solutions to a given problem by understanding and analyzing it systematically.

1.1 Problem Solving Techniques

Example:
Finding a lost key:
1. Trial and Error: Search one place at a time randomly.
2. Brainstorming: List all possible places and prioritize.
3. Divide and Conquer: Divide the area into sections and search systematically.
        

2. Steps in Problem Solving

Example:
Problem: Calculate the area of a rectangle.
1. Define: Input length and breadth.
2. Analyze: Area = length × breadth.
3. Explore: Use a formula or program to compute.
        

3. Algorithms and Flowcharts

3.1 Definitions

Algorithm: A step-by-step procedure to solve a problem.

Flowchart: A graphical representation of an algorithm using symbols.

3.2 Symbols in Flowcharts

Example:
Algorithm:
1. Start
2. Input length and breadth
3. Calculate area = length × breadth
4. Print area
5. Stop

Flowchart:
(Start) → [Input length, breadth] → [Calculate area] → [Print area] → (End)
        

4. Characteristics of an Algorithm

5. Pseudo-code

5.1 Conditionals in Pseudo-code

Conditionals use if-else or switch-case logic to execute specific code based on conditions.

Example:
Pseudo-code:
IF age >= 18 THEN
    PRINT "Eligible to vote"
ELSE
    PRINT "Not eligible"
END IF
        

5.2 Loops in Pseudo-code

Loops are used to repeat code while a condition is true.

Example:
Pseudo-code:
FOR i = 1 TO 5 DO
    PRINT i
END FOR
        

6. Time Complexity: Big-O Notation

Big-O notation describes the efficiency of an algorithm in terms of its execution time relative to the input size.

Example:
O(1): Accessing an array element
O(n): Traversing an array
O(n²): Nested loops
        

7. Simple Examples

7.1 Real-life Example of Algorithm

Task: Make tea
Algorithm:
1. Boil water
2. Add tea leaves
3. Add sugar and milk
4. Stir and boil
5. Strain into cup
        

7.2 Real-life Example of Flowchart

Flowchart:
(Start) → [Boil water] → [Add ingredients] → [Stir and boil] → [Strain] → (End)
        

UNIT-V: Simple Arithmetic Problems

1. Basic Arithmetic Operations

1.1 Addition and Multiplication of Integers

Addition and multiplication are fundamental arithmetic operations.

Example:
Input: a = 5, b = 3
Addition: sum = a + b = 8
Multiplication: product = a * b = 15
        

1.2 Determining if a Number is Positive/Negative/Even/Odd

Algorithm:
1. Input the number.
2. If number > 0, print "Positive".
3. If number < 0, print "Negative".
4. If number % 2 == 0, print "Even"; else print "Odd".

Flowchart:
(Start) → [Input number] → [Check sign] → [Check even/odd] → (End)
Example:
Input: -4
Output: Negative, Even
        

2. Maximum of Numbers

2.1 Maximum of Two Numbers

Algorithm:
1. Input two numbers a and b.
2. If a > b, max = a; else max = b.

Example:
Input: a = 5, b = 3
Output: Maximum = 5
        

2.2 Maximum of Three Numbers

Algorithm:
1. Input three numbers a, b, c.
2. Compare a, b, c to find the largest.

Example:
Input: a = 3, b = 7, c = 5
Output: Maximum = 7
        

3. Summation Problems

3.1 Sum of First n Numbers

Algorithm:
1. Input n.
2. Sum = n * (n + 1) / 2.

Example:
Input: n = 5
Output: Sum = 15
        

3.2 Sum of Given n Numbers

Algorithm:
1. Input numbers in an array.
2. Sum all elements in the array.

Example:
Input: [1, 2, 3, 4, 5]
Output: Sum = 15
        

4. Other Arithmetic Problems

4.1 Integer Division

Algorithm:
1. Input dividend and divisor.
2. Compute quotient = dividend / divisor.

Example:
Input: dividend = 10, divisor = 3
Output: Quotient = 3
        

4.2 Digit Reversing

Algorithm:
1. Input number n.
2. Reverse the digits using a loop.

Example:
Input: n = 1234
Output: Reversed = 4321
        

4.3 Table Generation for n

Algorithm:
1. Input n.
2. Loop from 1 to 10, calculate n * i.

Example:
Input: n = 5
Output: 5, 10, 15, 20, ..., 50
        

4.4 Power Calculation (a^b)

Algorithm:
1. Input a, b.
2. Compute power using loop or pow(a, b).

Example:
Input: a = 2, b = 3
Output: 8
        

5. Advanced Problems

5.1 Factorial

Algorithm:
1. Input n.
2. Multiply numbers from 1 to n.

Example:
Input: n = 5
Output: Factorial = 120
        

5.2 Sine and Cosine Series

Algorithm:
1. Input angle x.
2. Calculate sine and cosine using series expansion.

Example:
Input: x = 30° (in radians)
Output: sin(x), cos(x)
        

5.3 Combination (nCr)

Algorithm:
1. Input n and r.
2. Compute nCr = n! / (r! * (n-r)!).

Example:
Input: n = 5, r = 2
Output: nCr = 10
        

5.4 Pascal Triangle

Algorithm:
1. Generate rows using combination formula.

Example:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
        

5.5 Prime Numbers

Algorithm:
1. Input n.
2. Check if n is divisible only by 1 and itself.

Example:
Input: n = 7
Output: Prime
        

5.6 Factors of a Number

Algorithm:
1. Input n.
2. Find all numbers that divide n without a remainder.

Example:
Input: n = 12
Output: 1, 2, 3, 4, 6, 12
        

5.7 Perfect Numbers

Algorithm:
1. Input n.
2. Check if the sum of its divisors equals n.

Example:
Input: n = 6
Output: Perfect Number
        

5.8 GCD of Numbers

Algorithm:
1. Input two numbers.
2. Use Euclid's algorithm to find GCD.

Example:
Input: a = 54, b = 24
Output: GCD = 6
        

6. Swapping

Algorithm:
1. Input a and b.
2. Swap using a third variable or arithmetic operations.

Example:
Input: a = 3, b = 5
Output: a = 5, b = 3
        

UNIT-VI: Functions

1. Basic Types of Functions

Functions are blocks of code designed to perform specific tasks and enhance reusability.

Example:
Library function: printf("Hello, World!");
User-defined function:
void greet() {
    printf("Hello, User!");
}
        

2. Declaration and Definition

Declaration: Specifies the function name, return type, and parameters without providing the body.

Definition: Specifies the body of the function and its implementation.

Syntax:
Declaration: return_type function_name(parameters);
Definition: return_type function_name(parameters) {
    // Body of the function
}

Example:
Declaration: int add(int, int);
Definition: int add(int a, int b) {
    return a + b;
}
        

3. Function Call

A function call executes the code defined in a function. It can be:

Example:
int add(int a, int b);
int main() {
    int sum = add(5, 3); // Function call
    printf("Sum: %d", sum);
}
        

4. Types of Functions

Functions can be classified based on return type and parameters:

Example:
void greet() {
    printf("Hello!");
}
int add(int a, int b) {
    return a + b;
}
        

5. Parameter Passing

5.1 Call by Value

In Call by Value, the function receives a copy of the variable's value.

Example:
void change(int x) {
    x = 10; // Changes only the local copy
}
int main() {
    int a = 5;
    change(a);
    printf("%d", a); // Output: 5
}
        

5.2 Call by Reference

In Call by Reference, the function receives the variable's address, allowing modifications to the original value.

Example:
void change(int *x) {
    *x = 10; // Changes the actual value
}
int main() {
    int a = 5;
    change(&a);
    printf("%d", a); // Output: 10
}
        

6. Scope of Variables

Example:
int globalVar = 10; // Global scope
int main() {
    int localVar = 5; // Local scope
}
        

7. Storage Classes

Storage classes define the scope, lifetime, and visibility of variables.

Example:
void demo() {
    static int count = 0; // Retains value between calls
    count++;
    printf("%d", count);
}
        

8. Recursion

Recursion occurs when a function calls itself to solve smaller instances of the problem.

Example:
Factorial using recursion:
int factorial(int n) {
    if (n == 0) return 1;
    return n * factorial(n - 1);
}
int main() {
    printf("Factorial: %d", factorial(5)); // Output: 120
}