0% found this document useful (0 votes)
4 views

unit-5-programing-concept-and-logic

Program related with class 12 c programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

unit-5-programing-concept-and-logic

Program related with class 12 c programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

Unit -5

Programming concepts and logic I


Program:
- A set of instructions or statements that
command a computer to perform specific
tasks.
- Written in a programming language and
translated into machine code by a language
processor for execution.
Programming Concept
The complete understanding of the process involved in
developing a program.
Includes knowledge and application of:
- Compilers, Assembler and Interpreters
- Programming Languages
- Control Structures
- Program Design Tools
- Syntax and Semantics
- Programming Techniques
- Stages of Program Development
Some examples of program files are:
Executable file (.exe or .com),Dynamic Link Library file
(.dll), Initialization files (.ini), Help files (.hlp or .chm)
Qualities/Features of a Good Program
1. Integrity: Accurate calculations that meet
specifications and provide the correct output.
2. Clarity: Easily readable, with in-line or external
documentation for easy maintenance.
3. Simplicity: Uses straightforward logic to solve
problems efficiently.
4. Modularity: Organized into self-contained
modules for easier understanding.
5. Generality: Flexible and operable across
multiple platforms.
6. Robustness: Built to handle errors and
avoid crashes.
7. Security: Protects against tampering
and minimizes vulnerabilities.
8. Efficiency: Balances speed and memory
usage effectively.
9. Documentation: Includes clear guides
for use, modification, and maintenance.
Differences between Program and Software
Program Software
1. It is a set of instructions which 2. It is a collection of instructions,
instructs computer to perform a programs, and data which instruct the
specific job. computer on how to solve computer
problems.
2. Program is the term used to 2. Software is a broad term that covers
describe any code that is used to run computer programs a well as the
the device components that it needs to run.
3. Program cannot be software. 3. Software can be program
4. Programs can be files or even 4. Software typically consists of files
notes.
5. Generally simpler and smaller 5. Can be complex, often integrating
in scale. various programs, databases, libraries,
and user interfaces.
6. Typically limited or command- 6. Generally designed with a user
line-based, may not have a GUI. interface to enhance usability and
experience.
7. Usually independent or has 7. Depends on multiple programs,
limited dependencies on other modules, and resources to work
programs. effectively as a unified system.
8. Example: SYS, FoRMAT.SYS, 8. Example:MS Word, Adobe
interest calculations, etc. Photoshop, Internet Explorer, etc.
Testing and Debugging:
- Testing and debugging are essential steps in software
development to ensure the program runs correctly and meets
user expectations.
- Debugging is the process of finding and fixing errors or bugs
in the code.

Debugging is basically done to find


Syntax error:
The error which occurs when the instruction of program
does not match the structural rule of the programming
language is called syntax error
Semantic error:
Semantic errors are the errors which causes the wrong
output due to wrong calculation or wrong input of data.
Run-time error
- The error which appears during the execution or runtime of a
program is called runtime error.
- Such errors appear when the computer is asked to divide by
zero or when the variable is assigned a large value which is
beyond its capacity.
- The run-time error causes the termination of program
execution.
Syntax and Semantics:
Syntax:
- Syntax refers to the rules for writing code correctly, like
grammar in programming.
- Syntax errors are detected by the computer when these
rules are violated.
- A syntax is the grammatical rules of writing the
programming statement.
Semantics:
- The rule that gives the meaning to programs is called
Semantics and the error which occurs in program logic
is called Semantic error.
- It is also called logical error.
- Semantics gives meaning to the code and is related to
logic.
- Semantic (or logical) errors are not detected by the
computer but cause incorrect outputs, making them
difficult to identify.
For example:
Statements Descriptions
I eat fruits. Syntactically & Semantically
correct.
I eat a laptop. Syntactically correct but
Semantically incorrect.
I eats laptop. Both Syntactically &
Semantically incorrect
PROGRAMMING LANGUAGE
- Language is a means of
communication among humans.
Even animals and every other
living being need language.
- One simply cannot communicate
with others without language.
Likewise, a computer also needs a
language so that it can
communicate with the users,
which is called a programming
language.

Fig: Programming Language


- A programming language is a language used by
computers by which they understand to do what the
program users want.
- It is a set of codes and written symbols that provide the
platform for instructing computers to perform some
specified tasks.
- The process of writing a program is known as coding or
programming and the person who writes the program
codes is called a programmer.
On the basis of nature, programming languages are
divided into 2 categories.
They are:
1. General purpose programming languages: for
solving almost all types of problems.
Example : C, C++, java, python , JavaScript ,etc.

2. Specific purpose programming languages: for


solving problems of specific nature.
Example: SQL, HTML, MATLAB , etc.
Whereas, generally, programming languages are
divided into 2 categories.
They are:
1. Low-Level Languages (LLL)
2. High Level Language(HLL)
1) Low-Level Languages (LLL)
- A low-level language is a programming language that
provides direct access to a computer's hardware and
resources.
- It include assembly language and machine language.
- It is further divided into two types.
They are :
a) Machine level Language
b) Assembly Level; Language
a) Machine Level Language(mid 1940’s)
- The language that is called the language of CPU
and is written in binary code (composed of 0 and 1)
is called Machine language.
- In this language, a computer can understand every
instructions given only in the form of 0 and 1.
- It is first generation language (low level
language).
- Very difficult to write as well as understand.
- It is direct understood by computer.
The advantages of Machine language are:
- Faster in execution., So it has high
efficiency.
- Translator is not necessary to
convert the language.
- High security can be maintained due
to control of hardware component.
The disadvantages of Machine language
are:
- Machine language is very machine dependent. The
program written for one processor cannot be run in
another processor i.e. a machine code of Intel processor
doesn’t work for Motorola processor.
- Machine language has to be written in the form of 1 and
0. So, it is quite stressful to do programs in machine
language.
- Chances of error are high while writing a
program in machine language.
- It is very difficult to debug the program written
in machine language.
- It is very time-consuming and difficult to solve
complex problems under this language.
b) Assembly Level Language (1950’s to 1958’s):
- It is also known as second generation language (low
level language).
- It Requires language translator program called as
assembler.
- It allows a computer to convert the instruction into
machine instruction.
- The language in which a program is written in short codes
(mnemonics) like ADD, MUL or SUB instead of writing
instruction in a binary number (0 and 1) is called Assembly
language.
OPCODE OPERAND Comment

ADD A, B Add B to A

Fig: Assembly language instruction format


The advantages of Assembly language are:
- Assembly language is easier to understand as compared to
machine language.
Unlike machine language, it is easier to modify.
- Chances of error are less than machine language.
- In assembly language, program execution time is faster than a
program written in any high-level languages.
- As compared to machine language, assembly
language is of a more standard form of language.
- We do not need to take care of storage location
of data and instruction while writing a program in
assembly language.
- This language is more efficient in solving
hardware interfacing problems.
2) High-Level Languages (HLL)
 The languages which are quite similar to written
English and are therefore very easier to use in
compared to machine language and assembly
language are called High-level languages.
 Programs written in high-level languages are
translated into machine language by a language
translator called a compiler or an interpreter.
 The high-level languages are considered as high-
level because they are closer to human languages
than machine-level languages.
 For e.g., java, PHP, python etc.
High-level languages are of 3 types. They are:
1) Procedural oriented language (3GL)
th
2) Problem-oriented (4 generation language (4GL)
3) Natural (5th generation language (5GL)
1) Procedural oriented language:
 Procedural Oriented Languages are the general purpose
programming language.
 It is also known as the 3rd generation languages (3GL).
 It tells the computer “What to do and how to do”.
 These are designed to express the logic and the procedure of a
problem.
 Procedural languages are very flexible because they are able to
solve a variety of problems.
 Examples are: Pascal, C, BASIC, etc.
2) Problem-oriented language:
 These languages are non-procedural languages which
allows the users to specify what the output should be.
 It tells the computer “What to do” and “how to do”.

 Problem-oriented languages are one step ahead of 3GL


and they don’t describe all the details of how the data are
manipulated to produce the result.
 These are result oriented and needs a translator like an
interpreter or a compiler.
 Examples are: C#, PHP, Visual Basic, etc.
Procedural oriented language Problem-oriented language
It tells the computer “What to do It tells the computer “What to do”
and how to do”. and “ not how to do”.
It is difficult to learn. It is easy to learn
It is difficult to debug It is easy to debug.
It is used by professional It is used by professional and
programmer. technical user.
It is file oriented It is database oriented
Its semantics are complex Its semenatics are very simple.
Example: C , FORTRAN, BASIC, Example: java, PHP, C++, python,
Pascal, etc. etc.
3) Natural language:
 Also known as 5 th generation languages (5GL), these
are still in the developing stage.
 In completion, we could write statements that would
look like normal sentences.
 Natural languages such as English, Nepali and other
languages would be used in the computer making them
more intelligent and user-friendly.
Some of its characteristics are:
 They make computers more intelligent and
natural.
 They make human and machine closer.
 They are used in the areas of research,
robotics, defense system and different
types of expert systems.
The advantages of High-level languages are:
 Machine independent (portability)
 Easy to learn and use (readability)
 Less error detection
 Easy debugging
 Economical (software development-less time
and effort)
The disadvantages of High-level languages are:
 The program written in the high-level language is less
efficient as compared to the program written in
assembly and machine language because it requires
more memory and more time to execute.
 The task related to the computer hardware can be
programmed or handled only by either machine or
assembly language. So, for these purposes , high-level
languages cannot be used.
 Dependency on libraries and frameworks.
 Reduced control over the underlying hardware.
Assignment:
1) Difference between high level
language and low level language

2) difference between 3GL and 4GL


programming language.
Language Translator:
 Language translator is a
program which is used to
translate instructions that are
written in the source code to
object code i.e. from high-
level language or assembly
language into machine
language.
 There are 3 types of
language translators.
They are: Fig. Translator
1) Compiler
 A compiler is a language translator that translates high-
level languages program to machine language program.
 While translating, it checks the syntax (grammar of the
source code) and translates it into object code at a single
attempt.
 If any error is found, the compiler produces syntax errors
and causes of the errors.
 The source code file must be syntax error-free for
complete compilation process.
Fig. Compiler

It is more efficient than interpreter.


It creates the object code.
Compiler is faster than interpreter.
It is difficult to trace errors and causes of it
Examples: C, C++, Visual Basic, etc.
2) Interpreter
 The interpreter is the language translator designed to
translate high-level language program into machine
language program, one instruction at a time.
 Unlike the compiler , An interpreter translates and executes
code line by line at runtime rather than converting it into
object code (machine-readable binary code) like a compiler
does.
 If any error is encountered, the translation is halted and an
error message is displayed.
Figure: Interpreter

It is easy to trace errors and causes of it also.


Interpreting process is slower than compiler.
It is less efficient than compiler.
It does not create object code.
Example: BASIC, LISP, etc.
3) Assembler
 Assembler is the language translator
designed to translate assembly language
program (source codes) into machine
language program (object codes).
 The original assembly language program
codes are called source codes and after
translation, the final machine language
program codes are called object codes.

Fig: Assembler
 It is faster than compiler and Interpreter
 It produces binary machine code.
 Difficult to trace errors.
 It is most efficient than compiler and
interpreter.
 Examples: x86, ARM etc.
Assignment:

Write down the difference between compiler,


interpreter and assembler
Program Designing Tools
 A programming tool or software development
tool is a computer program that software
developers use to create, debug, maintain, or
otherwise support other programs and
applications.
 Types of programming designing tools are:
1) Algorithm
2) Flowchart
3) Pseudo Code
1) Algorithm
 An algorithm is a finite set of well-
defined instructions or steps designed to
solve a specific problem or perform a
task.
 It is a logical sequence of actions that
can be implemented in programming or
followed manually.
Key Characteristics of an Algorithm:
 Finiteness: It must terminate after a finite number of
steps.
 Definiteness: Each step must be clear and unambiguous.
 Input: It may take zero or more inputs.
 Output: It produces at least one output.
 Effectiveness: The steps must be simple enough to be
performed.
Example: Algorithm to Find the Largest of Two
Numbers
Step 1: Start.
Step 2: Input two numbers, say a and b.
Step 3: Compare the two numbers:
If a > b, then a is the largest.
Else, b is the largest.
Step 4: Output the largest number.
Step 5: End.
2) Flowchart
 A flowchart is a pictorial/graphical
representation of a process, algorithm, or
workflow using symbols, shapes, and
arrows to illustrate the sequence of steps
or actions.
 It is used to visually communicate the
flow of logic or operations in a process.
Key Components/Symbols of a Flowchart:
Types of Flowcharts:
1) System flowchart:
 A flowchart that gives information about a system is called
system flowchart.
 The system can be data processing system, production
system, reservation system, inventory system, accounting
system and evaluation system.
 It plays a very important role in analysis and designing of the
new system.
 In other words, a system flowchart describes the data flow
and operations for a data processing system.
 It shows how the data processing is to be accomplished.
Fig : System flowchart
2) Program flowchart
 Program flowchart describes the
sequence of operations and decisions for
a particular program.
 In other words, we can say that a
diagrammatical or pictorial sequence of
instructions used by a programmer to
solve any problem is called Program
Flowchart
Example: input any two number and find the sum.
3) Pseudo Code
 Pseudocode is a simplified, high-level
description of a program's logic, written
in plain language or a structured format.
 It avoids syntax of specific programming
languages and focuses on logic. Here's a
general guide to writing pseudocode:
1) Write an algorithm and draw the flowchart to find out
addition and subtraction of any two numbers and print the
result.
Solution:
Step 1: Start
Step 2: Assume that first_number=0 and second_number=0.
Step 3: Input the first number and second number (1st number
must be greater than 2nd number).
Step 4: Is first number greater than second number?
If no, go to Step 3
Otherwise go to Step 5
Step 5: Add and print the result.
Step 6: Subtract and print the result.
Step 7: End
Fig: Flowchart to add and subtract
any two number
2) Write an algorithm and draw the flowchart to
check out given number is even or odd.
Solution:
Step 1: Start
Step 2: Enter/ input any number
Step 3: if number%2==0
print/display ”even number”
else
print “Odd Number”
Step 4: End
3. Write an algorithm to find out sum of 20
natural number
Solution:
Step 1: Start
Step 2: Assume that natural_number=20, sum=0 input_number=1.
Step 3: Is input_number greater than natural_number?
If no then calculate sum (sum=sum+input_number) and go
to Step 4
Otherwise go to Step 5.
Step 4: Increase input number by 1and go to Step 3.
Step 5: Display the value of sum.
Step 6: End
3) Write an algorithm to find out total and average mark of
10 students consisting of 7 subjects and print the result.
Solution:
Step 1: Start
Step 2: Assume subject=1, number of students=1, total_mark=0 and
average_mark=0.
Step 3: Input the mark of seven subjects.
Step 4: Are all seven subjects done? If no then increases the subject
number and go to Step 3 Otherwise go to Step 5.
Step 5: Add and print sum or total mark.
Step 6: Calculate average of seven subject’s mark.
Step 7: Are all 10 students mark entered?
If no then increases the student number and go to Step 3
Otherwise go to Step 8.
Step 8: End
PRACTICE THE FOLLOWING QUESTION
1. Draw the flowchart and write the algorithm to find the
greatest among 3 numbers.
2. Draw the flowchart and write the algorithm to check
whether the given number is palindrome or not .
3. Draw the flowchart and write the algorithm to find the
Simple Interest .
4. Draw the flowchart and write the algorithm to check
whether the given number is negative or positive.
5. Draw the flowchart and write the algorithm to find the
reverse of a given number.
Concept of Programming Statement:
 A statement is a command that the
programmer gives to the computer.
 For example: Print "Hello, world!"
 An instruction or expression in any high-level
language is called statement.
 A statement causes the computer to carry out
some calculation.
 A statement may consist of constants,
variables and operators.
Mainly three types of statements are used in high level languages.
They are described below:
a. Simple statement:
It is a basic part of a program and it’s a single line expression which
is used to carry out an assignment, calculation or to test logical
decision.
Examples are: a = b + c, a > b, etc.
b. Compound statement:
: Compound statement is a group of simple statements within a
block. The block can be enclosed by a pair of curly braces. For
Example:
{ r =7; interest = (P*t*r)/100; }
where one instruction is for multiplication of l and b and another
instruction is for assigning multiplication value to the variable A.
C) Control statement:
It is also known as control structure in high level
language.
 It is the basic building blocks of high-level languages.
 It controls the flow of statements in a program.
 There are three types of control statements. They are
sequence, selection and iteration.
Program Control Structures:
 A control structure in programming refers to the
logical construct that governs the flow of execution of
a program based on certain conditions or repetitions.
 It determines the order in which statements,
instructions, or function calls are executed.
 There are three types of control structure. They are:
1) Sequence
2) Selection
3) Iteration
Codes
 A computer can only understand binary
numbers which are in the form of two electronic
states i.e., high voltage and low voltage.
 Such notations are further derived into standard
codes and such codes can represent the data
for users' convenience. Some of the popular
codes are:
1. Absolute Binary (pure binary):
 In an absolute binary method, 0 is placed
before the binary number to represent positive
number and 1 is placed before the binary
number to represent a negative number.
 The most significant bit in binary number
denotes the sign bit and the rest bits represent
the actual number. The binary number is
expressed in 8,16,32,64, etc. bit format
2. BCD (Binary Coded Decimal):
 It is a simple system for converting decimal
numbers into a binary form where each decimal
number is converted separately into binary and
placed spaces in between numbers.
 In BCD, each decimal digit occupies 4 bit.
 For example, the decimal number 24 can be
represented in BCD as (0010 0100)2
3. ASCII (American Standard Code for
Information Interchange):
 ASCII is a standard coding system that assigns numeric
values to the letter, numbers, punctuation marks and control
characters to achieve compatibility with different hardware and
peripherals.
 ASCII was developed in 1968 and was divided into 2 sets:
Standard ASCII (7 bits code, 128 characters) and Extended
ASCII (8 bits code, 256 characters).
 Most systems use 8 bit extended ASCII to represent foreign
language characters and other graphical symbols.
 In ASCII, each character is represented by a unique
integer value from 0 to 255.
 The value 0 to 31 is used for non-printing control
characters and the range from 32 to 127 is used to
represent the letters of the alphabet and common
punctuation symbols.
 For example: ASCII code for capital letter A is 65 and a
is 97, for * is 42, etc.
4. EBCDIC (Extended Binary Coded Decimal
Interchange Code):
 It is an 8-bit code system which is commonly used on
large IBM mainframe computers, most IBM
minicomputers and computers from many other
manufacturers.
 It allows 256 characters to be represented in computers.
EBCDIC to ASCII and vice versa.
 In this code, placement of the letters of the alphabet is
discontinuous and there is no direct character to
character match when converting from EBCDIC to ASCII
and vice versa.
5. Unicode:
 It is a 16-bit character code defined by the Unicode
Consortium and International Standard Organization (ISO)
that supports up to 65,536 characters.
 It allows all the characters and symbols in any language in
the world to be represented by a single code.
 For example, the Chinese language has almost 10,000
characters which can be represented by Unicode only. If
Unicode is universally adopted, then it will make multilingual
software much easier to write and maintain.
Introduction, Overview and History of C:
 C is a programming language developed at AT & T’s Bell
laborites of USA in 1972 AD.
 It was designed and written by Dennis Ritchie.
 It is popular because, it is simple reliable and easy to use.
 C has relatively good programming efficiency as compared
to machine-oriented language and relatively good machine
efficiency as compared to problem-oriented language or
high-level language.
 The history of C programming language is summarized in
the table
Year Language Developed by Remarks

1960 ALGOL International committee Too general, too


abstract
1963 CPL Cambridge University Hard to learn,
difficult to
implement
1967 BCPL Martin Richard (Cambridge Could deal with only
University) specific problems

1970 B Ken Thompson (AT & T Bell Lab) Could deal with only
specific problems

1972 C Dennis Ritchie (AT & T Bell Lab) Lost generality of


BCPL and B restored.
Advantages of C Language
 C is basic programming language or base for
modern programming language.
 C is building blocks for currently known language
python.
 It is portable language.
 It is a free form of language.
 C is most highly flexible programming language.
 It gets easily compile without taking much
memory.
Disadvantages of C Language
 C does not have concept of OOP.
 C does not have namespace
 C does not have concept of constructors and
Destructors
 C does not have runtime checking that’s way
it is difficult to fix the bugs.
 C is case sensitive.
Structure of C Program:
The basic structure of a C program would be as:
Header file
void main ()
{
Variable declaration or initialization;
Input Statements;
Processing Statements;
Output Statements;
}
// Write a program to print hello world Documentation

Documentation
// Write a program to to add any two number and display it

return 0;
1. Documentation
 Documentation refers to the comments and descriptions
included in the code to explain its purpose, structure, and
functionality.
 It makes the code easier to read, maintain, and debug.
 Documentation doesn't affect program execution; it is for the
benefit of developers.
 It may includes:
/*
* File Name: <file_name>.c
* Author: <your_name>
* Date: <creation_date>
* Version: <version_number>
* Description: <short_description_of_file_purpose>
*/
2. C Preprocessor and Header Files
 The C preprocessor is a program that processes the
source program before it is passed to the compiler for
compilation.
 The preprocessor offers several features, which known as
preprocessor directives.
 The preprocessor directives begin with a # symbol.
 Such directives can be placed anywhere in the program but
are mostly placed at the beginning of a program.
 Preprocessor directives are not program statements and so
that they are not terminated by a semicolon.
Header Files:
 C header files are used to provide why of telling the
compiler what interface specific function requires in
order to be used.
 The header files give the information to the compiler
that what actually the source code does.
 In c, the usually convention is to give header file
names that end with .h.
The standard libraries of header files include
the followings.

string.h: for string handling


stlib.h: for some miscellaneous functions
stdio.h: Standard input and output
conio.h: Consolidate input and output
math.h: mathematical function
3. Global Declarations (Optional)
Declare global variables or constants if needed.
Example:
int globalVar = 10;

4. main() Function
The entry point of the program where execution begins.
Example:

int main() {
// Your code
return 0; // Indicates successful execution
}
5. Variable Declarations
Declare variables inside main() or other functions.
Example:

int a, b, sum;

6. Program Logic
Write the logic using operators, conditionals, loops,
and function calls.
Example:

sum = a + b; // Add two numbers


7. Input/Output Operations
Use printf() for output and scanf() for input.
Example:

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

8. User-Defined Functions (Optional)


Define reusable functions to modularize the code.
Example:
int add(int x, int y) { return x + y; }

9. Return Statement
•End the main() function with return 0;.

You might also like