Basics of programming



Approaches to Problem Solving

Problem solving in programming means understanding a problem clearly and writing a step-by-step solution that a computer can execute.

There are three major approaches:

A. Top-Down Approach

  • Breaks the problem into major modules first, then into smaller sub-problems.
  • Starts from the overall view and goes toward detailed steps.
  • Easy to manage and understand.

    Example: Design “Online Shopping System” → break into Login, Product Search, Cart, Payment.

    Advantages

    • Better organization
    • Easier debugging
    • Supports modular programming

      B. Bottom-Up Approach

      • Starts from the smallest components (functions, modules) and builds up to the complete system.
      • Focuses on creating reusable components first.

        Example: Design functions like “CalculatePrice()”, “AddToCart()” → combine them into full shopping system.

        Advantages

        • Promotes reusability
        • Reduces redundancy
        • Strong component architecture

          C. Modular Approach

          • Divides the program into independent, logical modules.
          • Each module can be developed and tested separately.

            Advantages

            • Easy maintenance
            • Code reusability
            • Parallel development by teams

              Use of High-Level Programming Languages (HLL) for Systematic Development

              High-Level Languages (HLL) like Python, Java, C++, C#, PHP make programming easier because they are closer to human language.

              Why HLL is used for development?

              1. Easy to Read and Write

              • Code is simple and English-like.
                Example: print("Hello")


              2. Increased Productivity

              • Programmers can write complex logic with fewer lines.


              3. Portability

              • Code can run on multiple platforms (OS-independent).


              4. Large Standard Libraries

              • Built-in functions reduce coding effort.


              5. Error Reduction

              • Strong syntax rules reduce mistakes.


              6. Faster Development Cycle

              • Supports debugging tools, IDEs, and frameworks.


              7. Structured and Object-Oriented Programming

              • Makes programs modular, reliable, and easier to scale.


              Conclusion: High-level languages allow systematic, faster, and error-free development of large applications.

              Concept of Algorithm

              An algorithm is a finite set of well-defined, step-by-step instructions to solve a given problem.

              Characteristics of a Good Algorithm

              1. Clear and Unambiguous – Every step is simple and understandable.
              2. Finite – Must end after a limited number of steps.
              3. Input/Output – Should accept input and produce output.
              4. Correctness – Must solve the problem accurately.
              5. Efficient – Uses minimum time and memory.
              6. Independent – Can be implemented in any programming language.

                Example Algorithm: Find Sum of Two Numbers

                
                Step 1: Read A, B 
                Step 2: Sum = A + B 
                Step 3: Print Sum 
                Step 4: Stop
                
                

                Concept of Flowchart

                A flowchart is a visual representation of the steps in an algorithm using symbols, arrows, and shapes.

                Common Flowchart Symbols

                Symbol Meaning
                Oval Start/Stop
                Parallelogram Input/Output
                Rectangle Process/Calculation
                Diamond Decision (Yes/No)
                Arrow Flow direction

                Benefits of Flowcharts

                • Easy to understand
                • Helps find errors
                • Good for documentation
                • Represents logic clearly

                  Example Flowchart for Addition

                  Start → Input A, B → Calculate A+B → Display Result → Stop

                  Concept and Role of Structured Programming

                  Structured Programming is a programming method based on the use of clear, logical structures for writing programs.

                  It avoids unorganized “spaghetti code”.

                  Three Basic Control Structures

                  1. Sequence – Instructions executed in order
                  2. Selection – Decisions (if-else, switch)
                  3. Iteration – Loops (for, while)

                    Role and Importance of Structured Programming

                    • Improves Readability - Code becomes easier to understand.
                    • Easier Debugging - Errors can be located easily because logic is clean.
                    • Reduces Complexity - Breaks program into smaller blocks.
                    • Enhances Reusability - Modules and functions can be reused.
                    • Supports Team Work - Different programmers can work on separate modules.
                    • Improves Quality - Produces reliable and maintainable software.

                    Summary Table

                    Topic Key Points
                    Approaches to Problem Solving Top-down, bottom-up, modular
                    High-Level Languages Easy, portable, fast development
                    Algorithm Step-by-step instructions
                    Flowchart Visual diagram of algorithm
                    Structured Programming Sequence, selection, iteration

                    History of C

                    • C was developed in 1972 by Dennis Ritchie at AT&T Bell Labs, USA.
                    • It was originally created to develop UNIX operating system.
                    • C evolved from two earlier languages: BCPL and B.
                    • The first standardized version of C was ANSI C (C89) followed by C99 and C11.
                    • C influenced many modern languages like C++, Java, C#, Python, PHP, etc.

                      Salient Features of C

                      • Simple & Efficient - Easy to learn but powerful for system-level programming.
                      • Structured Programming Language - Supports functions and modular programming.
                      • Portable- Programs can run on different machines without modification.
                      • Fast Execution - Compiled language → very high performance.
                      • Rich Library - Provides built-in functions for I/O, math, string operations, etc.
                      • Low-Level Access - Supports pointers, memory management, hardware interaction.
                      • Extensible - Allows creation of user-defined functions.

                      Structure of a C Program

                      A typical C program contains:

                      #include <stdio.h> // Preprocessor directives int main() // Main function { printf("Hello C"); // Statements return 0; // Return statement }

                      Basic Sections:

                      1. Documentation Section – Comments
                      2. Link Section#include <stdio.h>
                      3. Definition Section – Macros
                      4. Global Declaration Section – global variables
                      5. Main() Function Section – entry point
                      6. Subprogram Section – user-defined functions

                        Compiling, Linking & Running a C Program

                        C program execution happens in three steps:

                        A. Compile

                        • Convert source code (.c) into object file (.o or .obj).
                        • Command: gcc program.c -c

                          B. Link

                          • Link object file with libraries to create executable file.
                          • Command: gcc program.o -o program.exe

                            C. Run

                            • Execute the program.
                            • Command: ./program.exe

                              (In IDEs like CodeBlocks/Dev-C++, this happens automatically when we press Run.)

                              Character Set in C

                              C uses:

                              1. Letters

                              A–Z, a–z

                              2. Digits

                              0–9

                              3. Special Characters

                              + - * / % = ; , [ ] { } ( ) # & * ^ ! < > etc.

                              4. White Spaces

                              Spaces, tabs, newline

                              Tokens

                              A token is the smallest individual unit in a C program.

                              Types of tokens:

                              1. Keywords
                              2. Identifiers
                              3. Constants
                              4. Strings
                              5. Operators
                              6. Punctuators (Symbols)

                                Keywords

                                These are reserved words with predefined meaning — cannot be used as identifiers.

                                Example keywords (32 total):
                                int, float, if, else, for, while, void, return, char, break, continue, struct, union, sizeof, switch, case, default, long, short

                                Identifiers

                                Names given to:

                                • Variables
                                • Functions
                                • Arrays
                                • Structures

                                  Rules

                                  • Must start with letter or underscore
                                  • No special characters except '_'
                                  • Case sensitive
                                  • Cannot be a keyword

                                    Examples: total, _count, sum1, marks

                                    Constants

                                    Fixed values that do not change during execution.

                                    Types of Constants

                                    1. Numeric constants

                                    • Integer: 10, -5, 300
                                    • Floating: 5.67, -3.14

                                      2. Character constants

                                      • 'A', '%', '3'

                                      3. String constants

                                      • "Hello", "C Program"

                                      4. Boolean constants

                                      • true, false (in C treated as 1 and 0)

                                      Variables

                                      A variable is a named memory location that stores data.

                                      Syntax: datatype variable_name;

                                      Example:

                                      int age; float salary; char grade;

                                      Instructions in C

                                      1. Type Declaration Instructions

                                      int a, b; float x;

                                      2. Arithmetic Instructions

                                      c = a + b;

                                      3. Control Instructions

                                      • if, if-else
                                      • switch
                                      • loops: for, while, do-while
                                      • break, continue

                                        4. Input-Output Instructions

                                        scanf() and printf()

                                        Data Types

                                        C data types are divided into:

                                        A. Basic Data Types

                                        Data Type Size Example
                                        int 2 or 4 bytes 10
                                        float 4 bytes 3.14
                                        double 8 bytes 12.567
                                        char 1 byte 'A'

                                        B. Derived Data Types

                                        • Array
                                        • Pointer
                                        • Structure
                                        • Union
                                        • Function

                                          C. Enumeration Type

                                          enum month {Jan, Feb, Mar};

                                          Standard Input/Output

                                          A. printf()

                                          • Used to display output.
                                          • Syntax: printf("Message");
                                          • Example: printf("Sum = %d", sum);

                                          B. scanf()

                                          • Used to take input from the keyboard.
                                          • Syntax: scanf("%d", &num);

                                          Operators and Expressions

                                          A. Types of Operators

                                          1. Arithmetic Operators: + - * / %
                                          2. Relational Operators: == != > < >= <=
                                          3. Logical Operators: && || !
                                          4. Assignment Operators: =, +=, -=
                                          5. Increment/Decrement: ++ --
                                          6. Bitwise Operators: & | ^ << >>
                                          7. Conditional (Ternary): ? :
                                          8. Special Operators: sizeof, comma, pointer operators (*, &)

                                            Expressions

                                            Combination of:

                                            • Variables
                                            • Operators
                                            • Constants

                                            Example: result = (a + b) * c;

                                            Summary Table

                                            Topic Key Points
                                            History Developed by Dennis Ritchie in 1972
                                            Features Fast, portable, structured, rich library
                                            Structure Documentation → Link → main() → functions
                                            Compiling Compile → Link → Run
                                            Tokens Keywords, identifiers, constants, operators
                                            Data Types int, float, char, double, arrays, pointers
                                            I/O printf(), scanf()
                                            Operators Arithmetic, logical, bitwise