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.
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.
- 5.1 Programming Concept
- 5.1.2 Types of Programming Languages
- 5.1.3 Compiler, Interpreter & Assembler
- 5.1.4 Programming Errors
- 5.1.5 Control Structures
- 5.1.6 Program Design Tools
- 5.1.7 Binary, BCD, ASCII & Unicode
- 5.2 C Programming Language
- Structure of C Program
- Identifiers, Keywords & Tokens
- Data Types, Variables & Constants
- Operators and Expressions
- Input and Output Functions
- Selection Statements
- Iteration / Loops
- Arrays
- Strings
- Solved C Programs
- Activities and Practice
- Common Mistakes
- Quick Revision
- Sample Board Exam Questions
- Important Questions
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
- Understand the problem.
- Design the solution.
- Prepare an algorithm, flowchart or pseudocode.
- Write the source code.
- Translate or compile the program.
- Run and test the program.
- 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.
| 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 |
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. |
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
- Start
- Read A and B.
- If A > B, display A as largest.
- Otherwise display B as largest.
- Stop
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.
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.
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.
#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.
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
// 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.
| 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 |
Post-increment: a++ changes the value after use.
C commonly uses printf() for output and scanf() for input.
#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
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.");
}
| 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 |
An array is a collection of elements of the same data type stored under one variable name.
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 |
#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;
}
1. Sum of Two Numbers
#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;
}
2. Check Even or Odd
#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
#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
#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
#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
#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
- Start.
- Declare integer variable num.
- Read num.
- Check num % 2 == 0.
- If true, display “Number is Even”.
- Otherwise display “Number is Odd”.
- 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
- Interpreter
- Compiler
- Assembler
- Linker
- Syntax Error
- Semantic Error
- Runtime Error
- Compilation Error
- Algorithm
- Pseudocode
- Flowchart
- Source Code
- math.h
- stdio.h
- string.h
- conio.h
- for
- while
- do-while
- nested loop
Short Answer Programming Question
Long Answer Practice
⭐ Important Questions
- What is a programming language? Differentiate between low-level and high-level languages.
- Differentiate between compiler and interpreter.
- Define syntax, semantic and runtime errors.
- Explain sequence, selection and iteration.
- What is an algorithm?
- Write an algorithm to find the largest of three numbers.
- Draw a flowchart to check whether a number is even or odd.
- Differentiate between ASCII and Unicode.
- What is BCD?
- List the features of C.
- What is a header file?
- Differentiate between keyword and identifier.
- List the basic data types in C.
- Differentiate between constant and variable.
- Write a C program to find the sum of two numbers.
- Differentiate between if-else and switch.
- Differentiate between while and do-while.
- Write a C program to print 1 to 10 using a for loop.
- Differentiate between 1D and 2D arrays.
- Write a C program to add two 3 × 3 matrices.
- Define string and list four string functions.
- 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.