[go: up one dir, main page]

0% found this document useful (0 votes)
64 views23 pages

Bx4002 Unit 2

The document provides an overview of programming, defining it as the process of writing algorithms into computer instructions. It explains key concepts such as computer programs, expressions, statements, and the importance of pseudocode and flowcharts in programming. Additionally, it discusses different types of programming languages, including low-level and high-level languages, and their respective characteristics.

Uploaded by

ajithkumar17366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views23 pages

Bx4002 Unit 2

The document provides an overview of programming, defining it as the process of writing algorithms into computer instructions. It explains key concepts such as computer programs, expressions, statements, and the importance of pseudocode and flowcharts in programming. Additionally, it discusses different types of programming languages, including low-level and high-level languages, and their respective characteristics.

Uploaded by

ajithkumar17366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

PEC,Vaniyambadi

Department of MCA
BX4002 - Problem Solving and Programming in C

UNIT II PROGRAMMING AND ALGORITHMS

Programs and Programming

Programming is an art, skill, poetry that is mastered through immense practice,


patience, and experience. Before I formally define programming, let us talk about
what is a computer program?

What is a computer program?

A Program is a set of instructions compiled together in a file to perform some specific


task by the CPU (Central Processing Unit). It is a series of binary numbers (0s and 1s)
arranged in a sequence, which when given to the computer performs some task.

Computer is a dumb machine with expeditious computational speed. It can give quick
results to many of the complex scientific calculations but it can’t perform a task on its
own. A computer needs set of instructions to do some task. This set of instructions is
contained in a computer program. Computer program is basically in binary language
i.e. series of 0s and 1s. A large bunch of programs makes the computer functional
without which the computer would be like a paralyzed machine.

You may think computer as an idiot person who does not know to cook. If you
provide ingredients for cooking Pasta to that idiot person, you cannot expect a
delicious dish. However, if you provide ingredients along with the full step-by-step
recipe of cooking Pasta then you may expect a real Pasta from that idiot person.
Same is the concept with computers, for computers the ingredients are data (might be
an integer, string, images, videos or anything) and the recipe is a program.

Computer and computer program

1
In my early ages one question always ponders in my mind, how can I create my own
programs? Smaller but effective and my own. Programs like Calculator, Notepad,
Music Player, A Website and many complex as a Remote Administration Tool, Search
Engine etc. I found programming is the only way through which I can create my own
program. Surely you won’t be creating complex and big software’s in few days, with
little or no programming knowledge and experience. But definitely, you can create
different small programs designed for specific tasks.

What is Programming?

Programming is the process of writing an algorithm into a sequence of computer


instructions. Or you can simply say it is the process of writing programs. We
generally transform the solution of a specific problem into computer language. It is
the only way through which we can create our own programs and can execute them
on a computer. Programming requires skill, logical thinking and lots of experience.

Programmers are the person who writes programs in a specific computer


programming language. They are highly skilled, hard working, problem solvers. The
world’s first programmer was Ada Lovelace. She was widely known for her work on
Charles Babbage’s Analytical Engine (general-purpose mechanical computer).
Building blocks for simple programs

• Four basic building blocks of programming languages:

1. Expressions
2. Statements
3. Statement Blocks
4. Function Blocks

1. Expressions

• Expressions in computer programming have the same definition as


expressions in math: they are a combination of an operator and its
operand(s). In keeping with the mathematical definition of an expression,
they are well-defined, meaning that an expression must ultimately resolve to
a value.
• An operator tells the computer to perform some kind of mathematical or
logical manipulation and is performed on one or more operands
• Examples:
• a+b
• + is the operator; a and b are operands
• x-2
• - is the operator; a and b are the operands
• a<b
• < is the operator; a and b are the operands
• With the above examples, two operands are in play, which is why you'll hear
them referred to as binary expressions
• As a result, binary expressions use binary operators; binary operators
operate on two operands
• Operator Classification:

2
• Operators can be classified based on the number of operands they perform
their operation on:
• Unary Operators
• Take one operand
• Example: & (address-of operator); see Pointer tutorial
• Binary Operators
• Operate on two operands and are by far the most common
• Examples: +, -, <, =, etc.
• Ternary Operators
• Operate on three operands
• Operators can also be classified based on the kind of function they perform:
• Arithmetic (math) Operators:
• i.e. Operators that perform math
• Examples: +, -, /
• Relational Operators:
• Compare the values of two operands
• Examples: >, <, ==
• Return/resolve to a boolean: true (1) or false(0)
• Logical Operators:
• Combine logical expressions
• Examples: && (AND), || (OR)
• Return/resolve to a boolean: true; false

2. Statements:

• Statements are syntactically complete instructions


• In C, syntax dictates that all statements end with a semicolon (this semicolon
is known as a statement terminator)
• Example:
• Variable assignment:
• a = 4;
• This is a syntactically complete instruction; note how it is simply an
expression (a = 4) consisting of the assignment operator (=) with the
operands a and 4, and terminating with a semicolon as required by C. By
syntactically correct, we simply mean that the instruction complies with the
rules of the language (i.e. syntax).

3. Statement Blocks:

• Statement blocks group statements together so they act like a single


statement (i.e. the statements act together as a block)
• In C, statement blocks start with { and end with }. In Python, statement blocks
are controlled by indentation. This is why whitespace matters in Python but
not in C.
• The code inside the statement block is known as the statement block body
• Example:

3
4. Function Blocks:

• Function blocks are blocks of code that accomplish a single task


• Functions allow you to reuse code so that if you need to do the same thing
multiple times, you simply call the function tag wherever you need it; you
separately define the function (with its function block) elsewhere in your code
• This makes it easier to maintain your code since, if you need to update your
function, you only have to update the code once in the function itself and not
multiple times wherever the function was called
• Functions also help when working on big projects with multiple developers by
acting as a black box
• Good functions act like a black box in the sense that you don't have to waste
your time, brainpower, or memory knowing exactly how the code inside the
function (it's function block) works; you just have to know that for a given
input, you get a given output
• This is also the basis of the idea behind libraries- you don't have to know
exactly how something is done. You just call the function (written by someone
else) from the library; this forms the very basis of abstraction which allows for
collaboration
• Functions also help for code readability; instead of mentally having to parse
out multiple lines of code you can look at the function name (like
verifyPhoneNumber() ) and know that it verifies the phone number.
• Example:
• int addNum(int a, int b){//Function block here }

Pseudo code representation

Pseudo code is a term which is often used in programming and algorithm based fields.
It is a methodology that allows the programmer to represent the implementation of an
algorithm. Simply, we can say that it’s the cooked up representation of an algorithm.
Often at times, algorithms are represented with the help of pseudo codes as they can
be interpreted by programmers no matter what their programming background or
knowledge is. Pseudo code, as the name suggests, is a false code or a representation of
code which can be understood by even a layman with some school level programming
knowledge.

Algorithm: It’s an organized logical sequence of the actions or the approach towards a
particular problem. A programmer implements an algorithm to solve a problem.
Algorithms are expressed using natural verbal but somewhat technical annotations.

4
Pseudo code: It’s simply an implementation of an algorithm in the form of
annotations and informative text written in plain English. It has no syntax like any of
the programming language and thus can’t be compiled or interpreted by the computer.

Advantages of Pseudocode

• Improves the readability of any approach. It’s one of the best approaches to
start implementation of an algorithm.
• Acts as a bridge between the program and the algorithm or flowchart. Also
works as a rough documentation, so the program of one developer can be
understood easily when a pseudo code is written out. In industries, the
approach of documentation is essential. And that’s where a pseudo-code
proves vital.
• The main goal of a pseudo code is to explain what exactly each line of a
program should do, hence making the code construction phase easier for the
programmer.

How to write a Pseudo-code?

1. Arrange the sequence of tasks and write the pseudocode accordingly.


2. Start with the statement of a pseudo code which establishes the main goal or
the aim.

Example:

This program will allow the user to check


the number whether it's even or odd.

3. The way the if-else, for, while loops are indented in a program, indent the
statements likewise, as it helps to comprehend the decision control and
execution mechanism. They also improve the readability to a great extent.

Example:

if "1"
print response
"I am case 1"

if "2"
print response
"I am case 2"

4. Use appropriate naming conventions. The human tendency follows the


approach to follow what we see. If a programmer goes through a pseudo code,
his approach will be the same as per it, so the naming must be simple and
distinct.
5. Use appropriate sentence casings, such as CamelCase for methods, upper case
for constants and lower case for variables.
6. Elaborate everything which is going to happen in the actual code. Don’t make
the pseudo code abstract.

5
7. Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’
the way we use it in programming.
8. Check whether all the sections of a pseudo code is complete, finite and clear to
understand and comprehend.
9. Don’t write the pseudo code in a complete programmatic manner. It is
necessary to be simple to understand even for a layman or client, hence don’t
incorporate too many technical terms.

Flow charts

A flowchart is a picture of the separate steps of a process in sequential order. It is a


generic tool that can be adapted for a wide variety of purposes, and can be used to
describe various processes, such as a manufacturing process, an administrative or
service process, or a project plan. It's a common process analysis tool and one of the
seven basic quality tools.

6
Elements that may be included in a flowchart are a sequence of actions, materials or
services entering or leaving the process (inputs and outputs), decisions that must be
made, people who become involved, time involved at each step, and/or process
measurements.

When to Use a Flowchart

• To develop understanding of how a process is done


• To study a process for improvement
• To communicate to others how a process is done
• When better communication is needed between people involved with the same
process
• To document a process
• When planning a project

Flowchart Basic Procedure

Materials needed: Sticky notes or cards, a large piece of flipchart paper or newsprint,
and marking pens.

1. Define the process to be diagrammed. Write its title at the top of the work
surface.
2. Discuss and decide on the boundaries of your process: Where or when does
the process start? Where or when does it end? Discuss and decide on the level
of detail to be included in the diagram.
3. Brainstorm the activities that take place. Write each on a card or sticky note.
4. Arrange the activities in proper sequence.
5. When all activities are included and everyone agrees that the sequence is
correct, draw arrows to show the flow of the process.
6. Review the flowchart with others involved in the process (workers,
supervisors, suppliers, customers) to see if they agree that the process is drawn
accurately.

Flowchart Considerations

• Don’t worry about drawing the flowchart the "right way." Ultimately, the right
way is the way that helps those involved understand the process.
• Identify and involve in the flowcharting process all key people involved with
the process. This includes suppliers, customers, and supervisors. Involve them
in the actual flowcharting sessions by interviewing them before the sessions
and/or by showing them the developing flowchart between work sessions and
obtaining their feedback.
• Do not assign a "technical expert" to draw the flowchart. People who actually
perform the process should do it.

Flowchart Examples

7
1. High-Level Flowchart for an Order-Filling Process

2. Detailed Flowchart

Commonly Used Symbols in Detailed Flowcharts

One step in the process. The step is written inside the box. Usually, only one
arrow goes out of the box.

Direction of flow from one step or decision to another.

8
Decision based on a question. The question is written in the diamond.
More than one arrow goes out of the diamond, each one showing the direction the
process takes for a given answer to the question. (Often the answers are "yes" and
"no.")

Delay or wait

Link to another page or another flowchart. The same symbol on the other
page indicates that the flow continues there.

Input or output

Document

Alternate symbols for start and end points

Develop a Flowchart

Use the flowchart template (Excel) to create a graphical representation of the steps in
a process to better understand it and reveal opportunities for improvement.

Programming Languages

As we know, to communicate with a person, we need a specific language, similarly to


communicate with computers, programmers also need a language is called
Programming language.

Before learning the programming language, let's understand what is language?

What is Language?

Language is a mode of communication that is used to share ideas, opinions with


each other. For example, if we want to teach someone, we need a language that is
understandable by both communicators.

9
What is a Programming Language?

A programming language is a computer language that is used by programmers


(developers) to communicate with computers. It is a set of instructions written in
any specific language ( C, C++, Java, Python) to perform a specific task.

A programming language is mainly used to develop desktop applications, websites,


and mobile applications.

Types of programming language

1. Low-level programming language

Low-level language is machine-dependent (0s and 1s) programming language. The


processor runs low- level programs directly without the need of a compiler or
interpreter, so the programs written in low-level language can be run very fast.

Low-level language is further divided into two parts -

i. Machine Language

Machine language is a type of low-level programming language. It is also called as


machine code or object code. Machine language is easier to read because it is
normally displayed in binary or hexadecimal form (base 16) form. It does not require
a translator to convert the programs because computers directly understand the
machine language programs.

The advantage of machine language is that it helps the programmer to execute the
programs faster than the high-level programming language.

ii. Assembly Language

Assembly language (ASM) is also a type of low-level programming language that is


designed for specific processors. It represents the set of instructions in a symbolic
and human-understandable form. It uses an assembler to convert the assembly
language to machine language.

The advantage of assembly language is that it requires less memory and less
execution time to execute a program.

2. High-level programming language

High-level programming language (HLL) is designed for developing user-friendly


software programs and websites. This programming language requires a compiler
or interpreter to translate the program into machine language (execute the program).

The main advantage of a high-level language is that it is easy to read, write, and
maintain.

10
High-level programming language includes Python, Java, JavaScript, PHP, C#,
C++, Objective C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift
programming language.

A high-level language is further divided into three parts -

i. Procedural Oriented programming language

Procedural Oriented Programming (POP) language is derived from structured


programming and based upon the procedure call concept. It divides a program into
small procedures called routines or functions.

Procedural Oriented programming language is used by a software programmer to


create a program that can be accomplished by using a programming editor like IDE,
Adobe Dreamweaver, or Microsoft Visual Studio.

The advantage of POP language is that it helps programmers to easily track the
program flow and code can be reused in different parts of the program.

Example: C, FORTRAN, Basic, Pascal, etc.

ii. Object-Oriented Programming language

Object-Oriented Programming (OOP) language is based upon the objects. In this


programming language, programs are divided into small parts called objects. It
is used to implement real-world entities like inheritance, polymorphism, abstraction,
etc in the program to makes the program resusable, efficient, and easy-to-use.

The main advantage of object-oriented programming is that OOP is faster and easier
to execute, maintain, modify, as well as debug.

Example: C++, Java, Python, C#, etc.

iii. Natural language

Natural language is a part of human languages such as English, Russian, German,


and Japanese. It is used by machines to understand, manipulate, and interpret human's
language. It is used by developers to perform tasks such as translation, automatic
summarization, Named Entity Recognition (NER), relationship extraction, and
topic segmentation.

The main advantage of natural language is that it helps users to ask questions in any
subject and directly respond within seconds.

3. Middle-level programming language

Middle-level programming language lies between the low-level programming


language and high-level programming language. It is also known as the
intermediate programming language and pseudo-language.

11
A middle-level programming language's advantages are that it supports the features of
high-level programming, it is a user-friendly language, and closely related to machine
language and human language.

Example: C, C++, language

Most commonly used Programming Language

As we all know, the programming language makes our life simpler. Currently, all
sectors (like education, hospitals, banks, automobiles, and more ) completely depend
upon the programming language.

There are dozens of programming languages used by the industries. Some most
widely used programming languages are given below -

1. Python
2. Java
3. C
4. C++
5. C#
6. JavaScript
7. R
8. PHP
9. GO
10. Ruby

Compiler

A compiler is a special program that processes statements written in a particular


programming language and turns them into machine language or "code" that a
computer's processor uses. Typically, a programmer writes language statements in a
language such as Pascal or C one line at a time using an editor. The file that is created
contains what are called the source statements. The programmer then runs the
appropriate language compiler, specifying the name of the file that contains the source
statements.

When executing (running), the compiler first parses (or analyzes) all of the language
statements syntactically one after the other and then, in one or more successive stages
or "passes", builds the output code, making sure that statements that refer to other
statements are referred to correctly in the final code. Traditionally, the output of the
compilation has been called object code or sometimes an object module . (Note that
the term "object" here is not related to object-oriented programming.) The object code
is machine code that the processor can execute one instruction at a time.

The Java programming language, a language used in object-oriented programming,


introduced the possibility of compiling output (called bytecode ) that can run on any
computer system platform for which a Java virtual machine or bytecode interpreter is
provided to convert the bytecode into instructions that can be executed by the actual
hardware processor. Using this virtual machine, the bytecode can optionally be

12
recompiled at the execution platform by a just-in-time compiler. (See also: Java
compiler)

Traditionally in some operating systems, an additional step was required after


compilation - that of resolving the relative location of instructions and data when
more than one object module was to be run at the same time and they cross-referred to
each other's instruction sequences or data. This process was sometimes called linkage
editing and the output known as a load module.

A compiler works with what are sometimes called 3GL and higher-level languages.
An assembler works on programs written using a processor's assembler language.

The language processor that reads the complete source program written in high-level
language as a whole in one go and translates it into an equivalent program in machine
language is called a Compiler. Example: C, C++, C#, Java.

In a compiler, the source code is translated to object code successfully if it is free of


errors. The compiler specifies the errors at the end of the compilation with line
numbers when there are any errors in the source code. The errors must be removed
before the compiler can successfully recompile the source code again

Interpreter, Loader and Linker

Interpreter :
The translation of a single statement of the source program into machine code is done
by a language processor and executes immediately before moving on to the next line
is called an interpreter. If there is an error in the statement, the interpreter terminates
its translating process at that statement and displays an error message. The interpreter
moves on to the next line for execution only after the removal of the error. An
Interpreter directly executes instructions written in a programming or scripting
language without previously converting them to an object code or machine
code.

Example: Perl, Python and Matlab.

13
• An interpreter is a common kind of language processor. Instead of producing
target program as a translation, an interpreter appears to directly execute the
operations specified in the source program on inputs supplied by the user.
• In contrast, an interpreter reads a statement from the input, converts it to an
intermediate code, execute it, then takes the next statement in sequence.
• If an error occurs, an interpreter stops execution and reports it.

· Loader
A loader is a major component of an operating system that ensures all necessary
programs and libraries are loaded, which is essential during the startup phase of
running a program. It places the libraries and programs into the main memory in order
to prepare them for execution. Loading involves reading the contents of the
executable file that contains the instructions of the program and then doing other
preparatory tasks that are required in order to prepare the executable for running, all
of which takes anywhere from a few seconds to minutes depending on the size of the
program that needs to run.

• Loader is a part of operating system and is responsible for loading executable


files into memory and execute them.
• It calculates the size of a program (instructions and data) and create memory
space for it. It initializes various registers to initiate execution.

· Linker

• Linker is a computer program that links and merges various object files
together in order to make an executable file. All these files might have been
compiled by separate assembler.
• The major task of a linker is to search and locate referenced module/routines
in a program and to determine the memory location where these codes will be
loaded making the program instruction to have absolute reference.

Linker is a program in a system which helps to link a object modules of program into
a single object file. It performs the process of linking. Linker are also called link
editors. Linking is process of collecting and maintaining piece of code and data into a
single file. Linker also link a particular module into system library. It takes object
modules from assembler as input and forms an executable file as output for loader.

14
Linking is performed at both compile time, when the source code is translated into
machine code and load time, when the program is loaded into memory by the loader.
Linking is performed at the last step in compiling a program.

Source code -> compiler -> Assembler -> Object code -> Linker ->
Executable file -> Loader

Linking is of two types:


1. Static Linking –
It is performed during the compilation of source program. Linking is performed
before execution in static linking. It takes collection of relocatable object file and
command-line argument and generate fully linked object file that can be loaded and
run.

Static linker perform two major task:

• Symbol resolution – It associates each symbol reference with exactly one


symbol definition .Every symbol have predefined task.
• Relocation – It relocate code and data section and modify symbol references
to the relocated memory location.

The linker copy all library routines used in the program into executable image. As a
result, it require more memory space. As it does not require the presence of library on
the system when it is run . so, it is faster and more portable. No failure chance and
less error chance.

2. Dynamic linking – Dynamic linking is performed during the run time. This linking
is accomplished by placing the name of a shareable library in the executable image.
There is more chances of error and failure chances. It require less memory space as
multiple program can share a single copy of the library.

Here we can perform code sharing. it means we are using a same object a number of
times in the program. Instead of linking same object again and again into the library,
each module share information of a object with other module having same object. The
shared library needed in the linking is stored in virtual memory to save RAM. In this
linking we can also relocate the code for the smooth running of code but all the code
is not relocatable.It fixes the address at run time.

Program execution

You may be speculative however the central processor is programmed. It contains a


special register — the instruction register — whose bit pattern determines what the
central processor unit can do. Once that action has been completed, the bit pattern
within the instruction register may be modified, and also the central processor unit can
perform the operation nominative by this next bit pattern.

Since directions are simply bit patterns, they will be kept in memory. The instruction
pointer register continuously has the memory address of (points to) the next

15
instruction to be executed. so as for the management unit to execute this instruction,
it’s derived into the instruction register. the case is as follows:

1. A sequence of instructions is stored in memory.


2. The memory address wherever the first instruction is found is copied to the
instruction pointer.
3. The CPU sends the address within the instruction pointer to memory on the
address bus.
4. The CPU sends a “read” signal to the control bus.
5. Memory responds by sending a copy of the state of the bits at that memory
location on the
data bus, that the CPU then copies into its instruction register.
6. The instruction pointer is automatically incremented to contain the address of
the next
instruction in memory.
7. The CPU executes the instruction within the instruction register.
8. Go to step 3

Steps 3, 4, and 5 are called an instruction fetch. Notice that steps 3 – 8 constitute a
cycle, the instruction execution cycle. It is shown graphically below.

16
Classification of Programming Language

What is a programming language?

A programming language defines a set of instructions that are compiled together to


perform a specific task by the CPU (Central Processing Unit). The programming
language mainly refers to high-level languages such as C, C++, Pascal, Ada, COBOL,
etc.

Each programming language contains a unique set of keywords and syntax, which are
used to create a set of instructions. Thousands of programming languages have been
developed till now, but each language has its specific purpose. These languages vary
in the level of abstraction they provide from the hardware. Some programming
languages provide less or no abstraction while some provide higher abstraction. Based
on the levels of abstraction, they can be classified into two categories:

• Low-level language
• High-level language

The image which is given below describes the abstraction level from hardware. As we
can observe from the below image that the machine language provides no abstraction,
assembly language provides less abstraction whereas high-level language provides a
higher level of abstraction.

Low-level language

The low-level language is a programming language that provides no abstraction from


the hardware, and it is represented in 0 or 1 forms, which are the machine instructions.
The languages that come under this category are the Machine level language and
Assembly language.

17
Machine-level language

The machine-level language is a language that consists of a set of instructions that are
in the binary form 0 or 1. As we know that computers can understand only machine
instructions, which are in binary digits, i.e., 0 and 1, so the instructions given to the
computer can be only in binary codes. Creating a program in a machine-level
language is a very difficult task as it is not easy for the programmers to write the
program in machine instructions. It is error-prone as it is not easy to understand, and
its maintenance is also very high. A machine-level language is not portable as each
computer has its machine instructions, so if we write a program in one computer will
no longer be valid in another computer.

The different processor architectures use different machine codes, for example, a
PowerPC processor contains RISC architecture, which requires different code than
intel x86 processor, which has a CISC architecture.

Assembly Language

The assembly language contains some human-readable commands such as mov, add,
sub, etc. The problems which we were facing in machine-level language are reduced
to some extent by using an extended form of machine-level language known as
assembly language. Since assembly language instructions are written in English
words like mov, add, sub, so it is easier to write and understand.

As we know that computers can only understand the machine-level instructions, so we


require a translator that converts the assembly code into machine code. The translator
used for translating the code is known as an assembler.

The assembly language code is not portable because the data is stored in computer
registers, and the computer has to know the different sets of registers.

The assembly code is not faster than machine code because the assembly language
comes above the machine language in the hierarchy, so it means that assembly
language has some abstraction from the hardware while machine language has zero
abstraction.

Differences between Machine-Level language and Assembly language

The following are the differences between machine-level language and assembly
language:

Machine-level language Assembly language


The machine-level language comes at the The assembly language comes above the
lowest level in the hierarchy, so it has machine language means that it has less
zero abstraction level from the hardware.abstraction level from the hardware.
It cannot be easily understood by humans.It is easy to read, write, and maintain.
The assembly language is written in simple
The machine-level language is written in
English language, so it is easily
binary digits, i.e., 0 and 1.
understandable by the users.

18
It does not require any translator as the In assembly language, the assembler is
machine code is directly executed by the used to convert the assembly code into
computer. machine code.
It is a first-generation programming It is a second-generation programming
language. language.

High-Level Language

The high-level language is a programming language that allows a programmer to


write the programs which are independent of a particular type of computer. The
high-level languages are considered as high-level because they are closer to human
languages than machine-level languages.

When writing a program in a high-level language, then the whole attention needs to
be paid to the logic of the problem.

A compiler is required to translate a high-level language into a low-level language.

Advantages of a high-level language

• The high-level language is easy to read, write, and maintain as it is written in


English like words.
• The high-level languages are designed to overcome the limitation of low-level
language, i.e., portability. The high-level language is portable; i.e., these
languages are machine-independent.

Differences between Low-Level language and High-Level language

The following are the differences between low-level language and high-level
language:

Low-level language High-level language


It is a user-friendly language as this
It is a machine-friendly language, i.e., the
language is written in simple English
computer understands the machine
words, which can be easily understood by
language, which is represented in 0 or 1.
humans.
The low-level language takes more time to
It executes at a faster pace.
execute.
It requires the compiler to convert the
It requires the assembler to convert the
high-level language instructions into
assembly code into machine code.
machine code.
The machine code cannot run on all The high-level code can run all the
machines, so it is not a portable language. platforms, so it is a portable language.
It is memory efficient. It is less memory efficient.
Debugging and maintenance are not easier Debugging and maintenance are easier in
in a low-level language. a high-level language.

19
Structured Programming Concept

Structured Programming Approach, as the word suggests, can be defined as a


programming approach in which the program is made as a single structure. It means
that the code will execute the instruction by instruction one after the other. It doesn’t
support the possibility of jumping from one instruction to some other with the help of
any statement like GOTO, etc. Therefore, the instructions in this approach will be
executed in a serial and structured manner. The languages that support Structured
programming approach are:

• C
• C++
• Java
• C#
• ..etc

On the contrary, in the Assembly languages like Microprocessor 8085, etc, the
statements do not get executed in a structured manner. It allows jump statements like
GOTO. So the program flow might be random.

The structured program mainly consists of three types of elements:

• Selection Statements
• Sequence Statements
• Iteration Statements

The structured program consists of well structured and separated modules. But the
entry and exit in a Structured program is a single-time event. It means that the
program uses single-entry and single-exit elements. Therefore a structured program is
well maintained, neat and clean program. This is the reason why the Structured
Programming Approach is well accepted in the programming world.

20
Advantages of Structured Programming Approach:

1. Easier to read and understand


2. User Friendly
3. Easier to Maintain
4. Mainly problem based instead of being machine based
5. Development is easier as it requires less effort and time
6. Easier to Debug
7. Machine-Independent, mostly.

Disadvantages of Structured Programming Approach:

1. Since it is Machine-Independent, So it takes time to convert into machine


code.
2. The converted machine code is not the same as for assembly language.
3. The program depends upon changeable factors like data-types. Therefore it
needs to be updated with the need on the go.
4. Usually the development in this approach takes longer time as it is
language-dependent. Whereas in the case of assembly language, the
development takes lesser time as it is fixed for the machine.

Illustrated Problems:
Algorithm to check whether a given number is Armstrong number or not

What is an Armstrong number?

An Integer number in which the sum of the cubes of its digits is equal to the number
itself is called Armstrong Number. For example, 153 is an Armstrong number since
1**3 + 5**3 + 3**3 = 153.
(NOTE: 5**3 is nothing but 5*5*5)

Algorithm to find whether number is Armstrong Number or Not

Step 1: Start
Step 2: Declare Variable sum, temp, num
Step 3: Read num from User
Step 4: Initialize Variable sum=0 and temp=num
Step 5: Repeat Until num>=0
5.1 sum=sum + cube of last digit i.e
[(num%10)*(num%10)*(num%10)]
5.2 num=num/10
Step 6: IF sum==temp
Print "Armstrong Number"
ELSE
Print "Not Armstrong Number"
Step 7: Stop

21
Pseudocode to find whether number is Armstrong Number or Not:

READ n
temp=n
sum=0
WHILE n>=0
sum=sum+(n%10)*(n%10)*(n%10)
n=n/10
ENDWHILE

IF sum==temp
WRITE "NUMBER IS AN ARMSTRONG NUMBER"
ELSE
WRITE "NUMBER IS NOT AN ARMSTRONG NUMBER"

We first take input from user and store it in variable n. Then we initialize 2 variables
temp to n and sum to 0. We calculate the cube of last digit by this expression
[(n%10)*(n%10)*(n%10)] and add it to value of sum and also divide n by 10. We
repeat the above step until n is greater than or equal to 0. At last, we check whether
sum is equal to temp, if yes Print "Number is Armstrong Number" else print "Number
is Not Armstrong Number".

Flowchart For Armstrong Number

22
Find factorial of a number

What is Factorial of a number?

Product of all consecutive Integer numbers up to n is called Factorial of a Number and


is denoted by n! For Example, the value of 5! is 120.

Mathematically it is written as,


n! = 1 * 2 * 3 * 4 * ... * (n-1) * n
For example, the factorial of 5 is,
5! = 1 * 2 * 3 * 4 * 5 = 120

Algorithm for Finding Factorial of a Number

Step 1: Start
Step 2: Declare Variable n, fact, i
Step 3: Read number from User
Step 4: Initialize Variable fact=1 and i=1
Step 5: Repeat Until i<=number
5.1 fact=fact*i
5.2 i=i+1
Step 6: Print fact
Step 7: Stop

Pseudocode for Finding Factorial of Number

Read number
Fact = 1
i=1
WHILE i<=number
Fact=Fact*i
i=i+1
ENDWHILE
WRITE Fact

Explanation

We first take input from user and store that value in variable named “n”. Then we
initialize a variable “Fact” with value 1 (i.e Fact=1) and variable i with value 1(i.e
i=1). Repeat next two steps until i is less than n.

1. Multiply Fact with current value of i


2. Increment i with 1

At last, print the value of Fact.

Let’s take an example,


Let the input be 5.
The equation that gets created by our algorithm is 5x4x3x2x1.
So, The Factorial of 5 is 120(5x4x3x2x1).

23

You might also like