Chapter 6: Introduction to Programming

Overview of Chapter 6: Introduction to Programming

Programming is the process of writing instructions that tell a computer how to perform a task. This chapter provides an introduction to the basics of programming, covering fundamental concepts such as programming languages, flowcharts, pseudocode, syntax, and control structures. These concepts will provide you with a solid foundation to start writing your own programs.

What is Programming?

Programming, often referred to as coding, is the practice of writing instructions that a computer can execute to perform specific tasks. Whether you're building an app, a website, or working with hardware, programming allows you to automate tasks, create systems, and solve problems.

Programming Concepts

Programming Languages

Programming languages are the tools that programmers use to communicate with computers. There are two main categories of programming languages:

High-level languages are more commonly used today because they are easier to learn, write, and maintain. They are also portable, meaning that code written in one language can be run on different platforms with little modification.

High-Level Languages

Flowcharts & Pseudocode

Before writing code, programmers often use tools like flowcharts and pseudocode to plan their programs. These tools help organize thoughts and visualize the process.

Flowcharts

Flowcharts are diagrams that represent the flow of a program. They use various shapes (like circles, diamonds, and rectangles) to represent processes, decisions, and inputs/outputs. A flowchart helps you map out the steps in a program before writing any code, making it easier to identify problems and optimize logic.

Flowchart Example

Pseudocode

Pseudocode is a written representation of a program's algorithm that is not bound by the strict syntax of programming languages. It is written in plain English and closely resembles the way you might describe a process to another person.

        Start
        Read user_input
        If user_input > 10
            Print "High"
        Else
            Print "Low"
        End
    

Pseudocode helps clarify the program's logic without worrying about syntax, making it easier to implement in any programming language.

Basic Syntax in Programming

Every programming language has its own syntax (set of rules), which must be followed to write valid programs. Here are some common elements of programming syntax:

For example, in Python, you can declare a variable and perform an operation like this:

        x = 10
        y = 5
        z = x + y
        Print(z)  // Output: 15
    
Basic Syntax Example

Control Structures

Control structures allow programmers to control the flow of a program based on conditions or loops. These structures are essential for writing dynamic, interactive programs.

If-Else Statements

An if-else statement is used to execute different blocks of code based on a condition. For example, if a user's input is greater than 10, the program might print "High"; otherwise, it prints "Low".

        If user_input > 10
            Print "High"
        Else
            Print "Low"
        End
    

Loops

Loops allow you to repeat a set of instructions multiple times. There are two main types of loops:

For example, a for loop might be used to print numbers 1 through 5:

        For i = 1 to 5
            Print(i)
        End
    
Loop Example

Learning Outcomes

By the end of this chapter, students will be able to:

C Programming Concepts and Detailed Explanation

1. Brief Overview of C Programming Keywords

C programming is one of the oldest and most powerful programming languages used for system programming, application programming, and software development. It provides low-level access to memory and system resources. Below is a brief overview of the core concepts of C programming keywords:

2. Detailed Explanation of C Programming Concepts

C programming has several important concepts that programmers must understand in order to write efficient and effective code. Below, we will break down key topics such as data types, loops, control structures, and storage classes in more detail.

2.1 Data Types

Data Types in C define the type of data a variable can hold. Different data types in C allow you to store different kinds of values, such as integers, floating-point numbers, and characters.

2.2 Control Structures

Control Structures allow you to control the flow of the program based on conditions or loops. These structures are essential for making decisions and repeating tasks in the program.

2.3 Loops

Loops are used when you want to repeat a block of code multiple times. In C, there are three main types of loops:

2.4 Functions

Functions are blocks of code that perform a specific task. Functions help in making code modular and reusable. In C, there are two types of functions:

3. Detailed Explanation of Preprocessor Directives

Preprocessor Directives are instructions in C that are processed before the actual compilation. They are used to include files, define constants, and perform conditional compilation.

C Programming Lab Works

Program 1 : To test a number is prime or not

 #include 

int main() {
    int num, i, test=1;
    //defining the vairable types


    printf("Enter a number: ");
    scanf("%d", &num);
    // Asking and storing input form user

    if (num <= 1) {
        test = 0; // Not a prime number
    } else {
        for (i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                test = 0; // Not prime
                break;
            }
        }
    }

    // Checking if the number is prime
    if (test==1) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
    }
    printf("\nCode Executed by Pranjal Khatri (929) \n");
    return 0;
}

Output :

Program 2: To find factors of input number .

#include 

int main() {
    int num, i;

    // Taking input from the user
    printf("Enter a number: ");
    scanf("%d", &num);

    // Displaying factors
    printf("Factors of %d are: ", num);
    for (i = 1; i <= num; i++) {
        if (num % i == 0) {
            printf("%d ", i);
        }
    }
    printf("\n");
    printf("Code Executed by Pranjal Khatri (929) \n");

    return 0;
}

Output :

Program 3 : To check whether a number is Palindrome or not

#include 

int main() {
    int num, originalNum, reversedNum = 0, remainder;

    // Taking input from the user
    printf("Enter a number: ");
    scanf("%d", &num);

    originalNum = num;

    // Reversing the number
    while (num != 0) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
        num /= 10;
    }

    // Checking if the number is palindrome
    if (originalNum == reversedNum) {
        printf("%d is a palindrome number.\n", originalNum);
    } else {
        printf("%d is not a palindrome number.\n", originalNum);
    }

    printf("Code Executed by Pranjal Khatri (929) \n");

    return 0;
}

    

Output :

Program 4 : To generate fibonacci series upto user given n th term

#include 

int main() {
    int n, a = 0, b = 1, c, i;

    // Taking input from the user
    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (i = 0; i < n; i++) {
        printf("%d ", a);
        c = a + b;
        a = b;
        b = c;
    }

    printf("\nCode Executed by Pranjal Khatri (929) \n");

    return 0;
}
    

Output :

Program 5 : To check whether a given number is perfect or not ( sum of factor = number )

 #include 

int main() {
    int num, sum = 0, i;

    // Taking input from the user
    printf("Enter a number: ");
    scanf("%d", &num);

    // Calculating sum of proper divisors
    for (i = 1; i < num; i++) {
        if (num % i == 0) {
            sum += i;
        }
    }

    // Checking if the number is perfect
    if (sum == num) {
        printf("%d is a perfect number.\n", num);
    } else {
        printf("%d is not a perfect number.\n", num);
    }

    printf("Code Executed by Pranjal Khatri (929) \n");

    return 0;
}

    

Output :

Program 6,7,8 (combined) : Diagonal sub , even odd sum , lower triangular matrix

 
    #include 
    int main() {
    int choice, n, i, j;

    printf("Choose an operation:\n");
    printf("1. Sum of two major diagonals\n");
    printf("2. Sum of all odd and even numbers\n");
    printf("3. Display lower triangular matrix\n");
    printf("Enter your choice (1/2/3): ");
    scanf("%d", &choice);

    printf("Enter the number of rows for the square matrix: ");
    scanf("%d", &n);

    int matrix[n][n];

    printf("Enter the elements of the %dx%d matrix:\n", n, n);
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("Element [%d][%d]: ", i, j);
            scanf("%d", &matrix[i][j]);
        }
    }

    switch (choice) {
        case 1: {
            int mainDiagonalSum = 0, antiDiagonalSum = 0;
            for (i = 0; i < n; i++) {
                mainDiagonalSum += matrix[i][i];
                antiDiagonalSum += matrix[i][n - 1 - i];
            }
            int totalDiagonalSum = mainDiagonalSum + antiDiagonalSum;
            if (n % 2 == 1) {
                totalDiagonalSum -= matrix[n/2][n/2];
            }
            printf("Sum of the two major diagonals is: %d\n", totalDiagonalSum);
            break;
        }

        case 2: {
            int evenSum = 0, oddSum = 0;
            for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                    if (matrix[i][j] % 2 == 0) {
                        evenSum += matrix[i][j];
                    } else {
                        oddSum += matrix[i][j];
                    }
                }
            }
            printf("Sum of even numbers in the matrix: %d\n", evenSum);
            printf("Sum of odd numbers in the matrix: %d\n", oddSum);
            break;
        }

        case 3: {
            printf("\nLower Triangular Matrix:\n");
            for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                    if (j <= i) {
                        printf("%4d", matrix[i][j]);
                    } else {
                        printf("    ");
                    }
                }
                printf("\n");
            }
            break;
        }

        default:
            printf("Invalid choice.\n");
    }

    printf("Done by Pranjal 929\n");

    return 0;
}

Output:

Frequently Asked Questions (FAQs)

1. What is the difference between flowcharts and pseudocode?

Flowcharts use diagrams and symbols to represent processes, while pseudocode uses plain English to describe the logic of a program. Both tools help programmers plan their code before writing it.

2. How do I choose the right programming language for a project?

The choice of programming language depends on factors like the project’s goals, the environment it will run in, and your familiarity with the language. For example, Python is great for rapid development, while C++ is often used for system-level programming.

3. Can I use pseudocode in my projects?

Yes, pseudocode is a great tool for planning algorithms and understanding the logic before you start writing code. It’s a useful technique for both beginners and experienced programmers.

4. What’s the best way to practice programming?

Start by writing small programs and solving coding challenges. Websites like LeetCode, HackerRank, and Codecademy provide interactive platforms to practice and improve your skills.