Class 11 Computer Science Unit 5 Programming Concepts & C Programming N

NEB Class 11 • Computer Science

Unit 5: Programming Concepts and Logics

Complete Class 11 Computer Science Unit 5 notes covering programming languages, translators, algorithms, flowcharts, C programming, operators, conditions, loops, arrays, strings and solved programs.

✓ NEB Syllabus ✓ C Programming ✓ Solved Programs ✓ Exam Questions

Welcome to Nepal eNotes. Unit 5 introduces the fundamental concepts of programming and the basics of the C programming language.

This chapter covers programming languages, compiler and interpreter, errors, program-design tools, character encoding, C syntax, data types, operators, input/output, selection, loops, arrays and strings.

Study Tip: Programming becomes easier when you understand the logic first and then practice the program yourself.

What is a Programming Language?

A programming language is a formal set of rules and instructions used by programmers to write computer programs.

Computers ultimately work using binary values 0 and 1. Programming languages allow humans to write instructions in a more understandable form before those instructions are translated into machine code.

Term Meaning
Program A set of instructions used to perform a specific task.
Programming Language A formal language used to write programs.
Programmer A person who writes computer programs.
Source Code The original program written by the programmer.
Object / Machine Code The translated binary code that the computer can execute.

Basic Program Development Process

  1. Understand the problem.
  2. Design the solution.
  3. Prepare an algorithm, flowchart or pseudocode.
  4. Write the source code.
  5. Translate or compile the program.
  6. Run and test the program.
  7. Find and correct errors.

Low Level Language

A low-level language works close to computer hardware.

It includes machine language and assembly language.

  • Very fast execution.
  • Harder for humans to understand.
  • Machine dependent.

High Level Language

A high-level language uses English-like words and readable syntax.

Examples include C, C++, Java and Python.

  • Easier to write.
  • Easier to understand and debug.
  • Must be translated into machine language.

Fourth Generation Language – 4GL

A fourth-generation language is designed to solve specific problems using fewer instructions than traditional programming languages.

Example: SQL is commonly considered a fourth-generation language.
Translator How It Works Example
Compiler Translates the entire source program into machine code before execution. C, C++
Interpreter Translates and executes one statement at a time. Python, BASIC
Assembler Translates assembly-language instructions into machine code. Assembly Language
Remember:
Compiler = Entire program
Interpreter = Line by line
Assembler = Assembly to machine code

Syntax Error

An error caused by breaking the grammar rules of the programming language.

Example: Missing semicolon.

Semantic Error

The program is syntactically correct but does not perform the intended task correctly.

Example: Using the wrong variable in a formula.

Runtime Error

An error that occurs while a program is executing.

Example: Division by zero.

A control structure determines the order in which program statements are executed.

Control Structure Purpose
Sequence Statements execute one after another.
Selection Selects a path according to a condition.
Iteration Repeats statements while a condition is satisfied.
Examples:
Selection → if, if-else, switch
Iteration → for, while, do-while

Algorithm

An algorithm is a step-by-step procedure for solving a problem.

A good algorithm should be:

  • Finite
  • Definite
  • Effective

Flowchart

A flowchart is a graphical representation of an algorithm using standard symbols.

Pseudocode

Pseudocode is a code-like description of program logic written using simple English rather than strict programming syntax.

Example: Find Largest of Two Numbers

Algorithm
  1. Start
  2. Read A and B.
  3. If A > B, display A as largest.
  4. Otherwise display B as largest.
  5. Stop
Pseudocode
BEGIN

READ A, B

IF A > B THEN
    PRINT "A is largest"
ELSE
    PRINT "B is largest"
END IF

END

Absolute Binary

Absolute binary represents information directly using 0s and 1s.

BCD – Binary Coded Decimal

BCD represents each decimal digit separately using a 4-bit binary code.

Example:
Decimal 25
2 = 0010
5 = 0101
BCD representation = 0010 0101

ASCII

ASCII stands for American Standard Code for Information Interchange.

It assigns numeric values to English letters, numbers, symbols and control characters.

Example: Capital letter A has ASCII value 65.

Unicode

Unicode is a character encoding system designed to represent text from many writing systems around the world.

It allows computers to display languages such as English and Devanagari together.

C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Laboratories in the early 1970s.

Features of C

Structured

Programs can be divided into functions and logical blocks.

Middle-Level Language

Combines high-level readability with low-level hardware control.

Portable

Programs can run on different systems with little modification.

Rich Library

Provides many standard functions through header files.

Memory Management

Supports direct memory access through pointers.

Fast Execution

Compiled C programs generally execute efficiently.

Basic C Program
#include <stdio.h>

int globalVar = 10;

void myFunction();

int main()
{
    int localVar = 20;

    printf("Hello, World!\n");
    printf("Global variable: %d\n", globalVar);
    printf("Local variable: %d\n", localVar);

    myFunction();

    return 0;
}

void myFunction()
{
    printf("This is a user-defined function.\n");
}

Main Parts

  • Preprocessor Directive: Instructions beginning with #, such as #include.
  • Global Variable: Declared outside all functions.
  • main(): The main function where program execution begins.
  • Local Variable: Declared inside a function.
  • return 0: Indicates successful program termination.

C Preprocessor and Header Files

Preprocessor directives are processed before compilation.

  • #include – includes a header file.
  • #define – defines a macro or symbolic constant.
Examples:
stdio.h → printf(), scanf()
string.h → strlen(), strcpy(), strcat(), strcmp()

Character Set in C

  • Uppercase alphabets A–Z
  • Lowercase alphabets a–z
  • Digits 0–9
  • Special symbols
  • Spaces, tabs and newline characters

Comments in C

Comments
// Single-line comment

/*
   Multi-line
   comment
*/

Identifier

An identifier is a name given to program elements such as variables and functions.

Examples: total, studentAge, calculateArea

Keyword

Keywords are reserved words with predefined meanings in C.

Examples: int, float, if, else, while, return

Token

Tokens are the smallest individual units recognized by the C compiler.

They include:

  • Keywords
  • Identifiers
  • Constants
  • Operators
  • Punctuation

Rules for Identifiers

  • Must begin with a letter or underscore.
  • Can contain letters, digits and underscores.
  • Cannot contain spaces.
  • Cannot use reserved keywords.
  • C identifiers are case-sensitive.
Example: age and Age are treated as different identifiers.
Data Type Description Typical Size Format
int Whole numbers 2 or 4 bytes %d / %i
float Decimal numbers 4 bytes %f
double Higher-precision decimal values 8 bytes %lf
char Single character 1 byte %c
void No value or type

Variable

A variable is a named memory location whose value can change while the program executes.

int score = 0;

Constant

A constant is a value that does not change during program execution.

Types include:

  • Integer constant – 10
  • Floating constant – 3.14
  • Character constant – ‘A’
  • String constant – “Nepal”
const float PI = 3.14159;

#define PI 3.14159

Type Specifiers

C provides type specifiers such as:

  • short
  • long
  • signed
  • unsigned

Simple and Compound Statements

A simple statement is a single instruction ending with a semicolon.

a = a + 1;

A compound statement contains several statements inside curly braces.

An operator is a symbol that performs an operation. An expression combines operators and operands to produce a value.

Type Operators Purpose
Arithmetic + – * / % Mathematical calculations
Relational == != < > <= >= Compare values
Logical && || ! Combine or reverse conditions
Assignment = += -= *= /= %= Assign values
Increment / Decrement ++ — Increase or decrease by 1
Conditional ? : Short form of simple if-else
Pre-increment: ++a changes the value before use.
Post-increment: a++ changes the value after use.

C commonly uses printf() for output and scanf() for input.

Input / Output Example
#include <stdio.h>

int main()
{
    int rollNo;
    char grade;
    float percentage;

    printf("Enter your Roll Number: ");
    scanf("%d", &rollNo);

    printf("Enter your Grade: ");
    scanf(" %c", &grade);

    printf("Enter your Percentage: ");
    scanf("%f", &percentage);

    printf("\nRoll Number: %d\n", rollNo);
    printf("Grade: %c\n", grade);
    printf("Percentage: %.2f%%\n", percentage);

    return 0;
}

Common Format Specifiers

  • %d – integer
  • %f – float
  • %c – character
  • %s – string
  • \n – new line
  • \t – tab space
Important: The & operator gives scanf() the memory address where the input should be stored.

if Statement

int age = 19;

if(age >= 18)
{
    printf("You are eligible to vote.");
}

if-else Statement

int marks = 65;

if(marks >= 40)
{
    printf("You have passed.");
}
else
{
    printf("You have failed.");
}

Nested if / if-else-if

int num = 10;

if(num > 0)
{
    printf("Positive\n");

    if(num % 2 == 0)
        printf("Even");
    else
        printf("Odd");
}
else if(num < 0)
{
    printf("Negative");
}
else
{
    printf("Zero");
}

switch Statement

char grade = 'B';

switch(grade)
{
    case 'A':
        printf("Excellent!");
        break;

    case 'B':
        printf("Good job!");
        break;

    case 'C':
        printf("Satisfactory.");
        break;

    default:
        printf("Invalid grade.");
}
break: Stops execution of the switch after a matching case.
if-else switch
Supports complex conditions. Best for fixed values.
Can check ranges. Checks one expression against case values.
Uses relational/logical expressions. Usually needs break after each case.

for Loop

for(int i = 1; i <= 5; i++)
{
    printf("%d\n", i);
}

while Loop

int i = 1;

while(i <= 5)
{
    printf("%d\n", i);
    i++;
}

do-while Loop

int i = 1;

do
{
    printf("%d\n", i);
    i++;

} while(i <= 5);
Loop Condition Checked Minimum Execution
while Before loop body 0
do-while After loop body 1
for Before loop body 0
Remember: A do-while loop always runs at least once.

An array is a collection of elements of the same data type stored under one variable name.

Important: Array indexing starts from 0.

One-Dimensional Array

int studentMarks[5] = {75, 80, 65, 90, 88};

for(int i = 0; i < 5; i++)
{
    printf("%d\n", studentMarks[i]);
}

Two-Dimensional Array

A two-dimensional array stores elements in rows and columns.

int matrix[2][3];

Matrix Addition Logic

for(i = 0; i < 3; i++)
{
    for(j = 0; j < 3; j++)
    {
        c[i][j] = a[i][j] + b[i][j];
    }
}

A string in C is an array of characters ending with the null character \0.

Function Purpose Example
strlen() Finds string length strlen(“Nepal”) = 5
strcpy() Copies a string strcpy(dest, “Hello”)
strcat() Combines strings strcat(s1, ” World”)
strcmp() Compares strings Returns 0 when equal
strrev() Reverses a string Reverse text
strupr() Converts to uppercase hello → HELLO
strlwr() Converts to lowercase HELLO → hello
String Functions
#include <stdio.h>
#include <string.h>

int main()
{
    char name[20] = "Kathmandu";
    char city[20];
    char greeting[30] = "Namaste";

    printf("Length: %d\n", strlen(name));

    strcpy(city, name);
    printf("Copied: %s\n", city);

    strcat(greeting, " Nepal!");
    printf("Combined: %s\n", greeting);

    return 0;
}
Length: 9 Copied: Kathmandu Combined: Namaste Nepal!
Note: strrev(), strupr() and strlwr() are described in the source notes as compiler extensions rather than standard ANSI C functions.

1. Sum of Two Numbers

C Program
#include <stdio.h>

int main()
{
    int a, b, sum;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    sum = a + b;

    printf("Sum = %d", sum);

    return 0;
}
Enter two numbers: 4 7 Sum = 11

2. Check Even or Odd

C Program
#include <stdio.h>

int main()
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if(n % 2 == 0)
        printf("%d is Even", n);
    else
        printf("%d is Odd", n);

    return 0;
}

3. Factorial Using Loop

C Program
#include <stdio.h>

int main()
{
    int i, n, fact = 1;

    printf("Enter a number: ");
    scanf("%d", &n);

    for(i = 1; i <= n; i++)
    {
        fact = fact * i;
    }

    printf("Factorial = %d", fact);

    return 0;
}

4. Sum and Average Using Array

C Program
#include <stdio.h>

int main()
{
    int numbers[5], sum = 0, i;
    float average;

    printf("Enter 5 numbers:\n");

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

    average = (float)sum / 5;

    printf("Sum = %d\n", sum);
    printf("Average = %.2f\n", average);

    return 0;
}

5. Input and Display 2 × 3 Matrix

C Program
#include <stdio.h>

int main()
{
    int matrix[2][3], i, j;

    printf("Enter matrix elements:\n");

    for(i = 0; i < 2; i++)
    {
        for(j = 0; j < 3; j++)
        {
            scanf("%d", &matrix[i][j]);
        }
    }

    printf("\nMatrix:\n");

    for(i = 0; i < 2; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("%d\t", matrix[i][j]);
        }

        printf("\n");
    }

    return 0;
}

6. Reverse a String

C Program
#include <stdio.h>
#include <string.h>

int main()
{
    char name[50];

    printf("Enter a string: ");
    scanf("%s", name);

    strrev(name);

    printf("Reversed = %s", name);

    return 0;
}

🧠 Activities and Practice

Algorithm to Check Even or Odd

  1. Start.
  2. Declare integer variable num.
  3. Read num.
  4. Check num % 2 == 0.
  5. If true, display “Number is Even”.
  6. Otherwise display “Number is Odd”.
  7. Stop.

while vs do-while

A while loop checks its condition before executing the body, while a do-while loop executes its body first and checks the condition afterwards.

Therefore, a do-while loop executes at least once.

Why is & not always used with scanf()?

The & operator provides the memory address of a normal variable. An array name already represents its base address, so when reading a string with %s, the array name can be used without &.

⚠ Common C Programming Mistakes

  • Forgetting the semicolon ;.
  • Using = instead of == inside a condition.
  • Forgetting the correct header file.
  • Using the wrong format specifier.
  • Forgetting & with scanf() variables.
  • Forgetting break inside switch cases.
  • Forgetting that arrays begin at index 0.

⚡ Quick Revision – Unit 5

  • Programming language = rules and instructions used to create programs.
  • Low-level language is closer to hardware.
  • High-level language is easier for humans to understand.
  • Compiler translates the entire program.
  • Interpreter translates line by line.
  • Assembler translates assembly language.
  • Main error types: Syntax, Semantic and Runtime.
  • Control structures: Sequence, Selection and Iteration.
  • Program-design tools: Algorithm, Flowchart and Pseudocode.
  • BCD represents each decimal digit using 4 bits.
  • ASCII is a character encoding system.
  • Unicode supports characters from many writing systems.
  • C was developed by Dennis Ritchie.
  • Program execution begins in main().
  • stdio.h provides printf() and scanf().
  • Basic data types: int, float, double, char and void.
  • Selection: if, if-else and switch.
  • Loops: for, while and do-while.
  • do-while always executes at least once.
  • Array indexing begins from 0.
  • A string is a character array ending with \0.

📝 Sample Board Exam Questions

Multiple Choice Questions

1. Which translator converts an entire source program before execution?
  1. Interpreter
  2. Compiler
  3. Assembler
  4. Linker
2. An error that occurs while a program is running is called:
  1. Syntax Error
  2. Semantic Error
  3. Runtime Error
  4. Compilation Error
3. Which design tool uses standard graphical symbols?
  1. Algorithm
  2. Pseudocode
  3. Flowchart
  4. Source Code
4. Which header file is used for printf() and scanf()?
  1. math.h
  2. stdio.h
  3. string.h
  4. conio.h
5. Which loop is guaranteed to execute at least once?
  1. for
  2. while
  3. do-while
  4. nested loop

Short Answer Programming Question

Write a C program to input five numbers and calculate their sum and average using a one-dimensional array.

Long Answer Practice

Define an array. Explain one-dimensional and two-dimensional arrays with examples, and write a program to input and display a 2 × 3 matrix.

⭐ Important Questions

  1. What is a programming language? Differentiate between low-level and high-level languages.
  2. Differentiate between compiler and interpreter.
  3. Define syntax, semantic and runtime errors.
  4. Explain sequence, selection and iteration.
  5. What is an algorithm?
  6. Write an algorithm to find the largest of three numbers.
  7. Draw a flowchart to check whether a number is even or odd.
  8. Differentiate between ASCII and Unicode.
  9. What is BCD?
  10. List the features of C.
  11. What is a header file?
  12. Differentiate between keyword and identifier.
  13. List the basic data types in C.
  14. Differentiate between constant and variable.
  15. Write a C program to find the sum of two numbers.
  16. Differentiate between if-else and switch.
  17. Differentiate between while and do-while.
  18. Write a C program to print 1 to 10 using a for loop.
  19. Differentiate between 1D and 2D arrays.
  20. Write a C program to add two 3 × 3 matrices.
  21. Define string and list four string functions.
  22. Write a C program to calculate sum and average using an array.

Frequently Asked Questions

A programming language is a formal language used to write instructions that tell a computer how to perform a particular task.

A compiler translates the complete program before execution, while an interpreter translates and executes the program one statement at a time.

An algorithm describes the solution using written steps, while a flowchart represents the same logic graphically using standard symbols.

Because the loop body executes before its condition is checked.

An array is a collection of elements of the same data type stored under one variable name and accessed using indexes.

A string is an array of characters ending with the special null character \0.

Conclusion

Unit 5 is one of the most important foundations of Class 11 Computer Science because it introduces both programming logic and the C programming language.

For exam preparation, focus especially on compiler vs interpreter, programming errors, algorithms, flowcharts, C data types, operators, input/output, if-else, switch, loops, arrays, strings and common C programs.

Continue learning with Nepal eNotes for more NEB Class 11 Computer Science notes, solutions and exam preparation materials.

Discussion

Share a helpful question, idea, or explanation with other students.

Leave a Comment

Write a clear question, answer, or helpful explanation.
Your email will not be published.

Download Our Offline App

Study class-wise notes even when internet is not available. Get the app from Play Store.

Nepal eNotes offline app preview
Get it on Google Play