Programming Concepts and Logic
Chapter 5: Programming Concepts and Logic
Introduction:
Computers do not function autonomously; they require commands
to operate. These commands, written in programming languages, instruct the
computer to perform specific actions. Programs, which are sets of instructions,
are written in these languages and are integrated to create software. Software
is essentially a collection of interrelated programs performing specific
functions.
Programs written in programming languages are not directly
understood by computers. Therefore, language translators or processors like
assemblers, compilers, and interpreters are used to convert these programs into
machine-level language.
Qualities of Good Programs:
- Understandability: Programs should be easily
understood.
- Correctness: Programs should be error-free.
- Reliability: Programs should be dependable.
- User
Interface:
Programs should have an easily understandable user interface.
- Portability
and Flexibility:
Programs should be portable and flexible.
Programming Language:
To facilitate communication between users and computers,
programming languages are used. These languages consist of keywords, variables,
operators, loops, and other entities, utilizing different character sets.
Types of Programming Languages:
Low-Level Language:
a. Machine Level Language:
It is machine dependent language which uses sequence of 0s
and 1s to write a program. Program written in machine level language
Should not be converted so, program execution time is higher
as compared to other languages.
- Advantage:
- Directly
understood by the processor, leading to higher execution speed.
- No
need for a language translator or processor.
- Disadvantage:
- Difficult
and time-consuming development and debugging.
- Machine-dependent
language.
b. Assembly Language:
- Advantage:
- Easier
to write and understand compared to machine level language.
- Faster
execution than high-level languages.
- Disadvantage:
- Machine-dependent.
- More
complex due to the use of mnemonics.
High-Level Language:
It is closer to English language. High level language code is
written in English like structure using mathematical notations. It is machine
Independent language i.e. the program written for one
computer can run on another computer. Language translator named interpreter
or compiler are required to convert the program from source
code to object code. FORTAN (formula translator) is the first high-level
programming language developed in 1956 AD. C, C++, Python,
JavaScript etc are some examples of high-level programming language.
- Procedural
Oriented Language (POPs):
This is the type
of high-level language which primarily focuses on procedure rather than data.
It is complex and time consuming to
Write large program.
It follows from top to bottom. It is the primitive way of programming. it
provides less security compared to
Object
oriented programming languages. C, FORTAN, QBASIC etc are the examples of
Procedural Oriented Language.
- Advantage:
- Easier
development and debugging compared to low-level languages.
- Used
for general-purpose programming.
- Disadvantage:
- Requires
a language translator.
- Slower
program execution.
- Problem/Object
Oriented Language (OOPs):
This is the type
of high-level language which primarily focuses on data rather than procedure.
It follows from bottom to top approach.
It is the modern
way of programming. It has some powerful features that provides high security Compared
to
procedural
oriented programming languages. C#, C++, Java etc are the examples of object-Oriented
Language.
- Advantage:
- Development
of web-based applications and software.
- More
advanced and user-friendly software.
- Disadvantage:
- Requires
a language translator.
- Slower
program execution.
- Natural
Language:
Natural
Language uses simple statement of common communicating language which we could
write statements
that would
look like normal sentences. It is still in developing stage; Programming
language like PROLOG
(Programming
with logic) is currently in use.
- Advantage:
- Easier
program development and debugging.
- Machine-independent.
- Disadvantage:
- Requires
a language translator.
- Slower
program execution.
Language Translators/Processors:
Language translator is a system software that convert program
written in any other language to machine code.
Types of language translator
- Assembler: It is a language translator
that convert program written in assembly level language to machine code.
- Converts
assembly language to machine-level language.
- Compiler:
It is a
language translator that convert program written in high level language to
machine-level language
whole program
at a time.
- Converts
high-level language to machine-level language.
- Translates
the entire program at once.
- Examples:
C, C++, Java.
- Interpreter:
It is a
language translator that convert program written in high level language to
machine-level language
One statement
at a time.
- Converts
high-level language to machine-level language.
- Translates
one statement at a time.
- Examples:
BASIC, C#, PHP.
Difference between Compiler and Interpreter:
Compiler |
Interpreter |
Translates the entire
program at once. |
Translates one line or
statement at a time. |
Finds errors after
compiling the complete program. |
Finds errors after
translating a line of the program. |
Faster translating
process. |
Slower translating
process. |
Difficult to code and
debug. |
Easy to code and
debug. |
Larger program,
requires more memory space. |
Smaller program,
requires less memory space. |
Not commonly used for
new programming languages. |
Commonly used for new
programming languages. |
Examples: C, C++,
Java, FORTAN. |
Examples: BASIC, C#,
PHP. |
Syntax and Semantics:
Programming language syntax refers to the structure of codes,
while semantics refers to the meaning attached to
individual words or symbols.
Example in Q Basic:
qbasic
Print "This is programming";
Here, "Print" is semantics, and the whole line is
syntax.
Errors (Bugs):
Errors in a program, known as bugs, can be syntax, logical,
or runtime errors. Debugging is the process of removing errors.
- Syntax
Error:
- Arises
due to not following proper rules or format.
- Detected
by language processors.
- Logical
Error:
- Arises
due to poorly developed logic.
- Not
detected by language processors.
- Runtime
Error:
- Occurs
while the program is being executed.
- Not
detected by language translators.
Data Types:
- Specify
the type of data used in a program.
- Numeric,
character, etc.
Variable and Constant:
- Variable:
Holds numeric or alphanumeric values, changes during program execution.
- Constant:
Holds values that do not change during program execution.
Operands and Operators:
- Operands:
Entities operated on (e.g., variables).
- Operators:
Symbols representing operations (e.g., +, =).
Operations:
- Specific
tasks performed by operators. (sum=a+b)
Program Design Tools:
- Algorithm:
- Step-by-step
procedure for solving a problem.
- Written
in simple English language.
- Not
tied to any programming language.
Rules
for writing algorithm
1. Be
Clear: Make each step easy to understand.
2. Define
Inputs/Outputs: Clearly say what goes in and what comes out.
3. Follow
Logic: Organize steps in a logical order.
4. Save
Resources: Be efficient in using time and space.
Example1: Calculate
sum of 5 and 4
Step1: Start
Step2: Read A as 5 and B as 4
Step3: Add A and B and store the result in sum.
Step4: Display the value of Sum.
Step5: Stop
Example2: Algorithm to Calculate Simple Interest:
Step 1: Start
Step 2: Read the principal amount (P), time duration (T), and
rate of interest (R)
Step 3: Calculate simple interest using the formula:
Simple
Interest (I) = (P * T * R) / 100
Step 4: Display the calculated simple interest (I)
Step 5: Stop
Example 3: Algorithm to Check Greatest Number Among
Two Numbers:
Step 1: Start
Step 2: Read two numbers, a and b
Step 3: If (a > b), then
Display
"The greatest number is a"
Else
Display
"The greatest number is b"
End If
Step 4: Stop
Example2: Algorithm
to Calculate Area of Circle:
Step 1: Start
Step 2: Read the radius
as (R)
Step 3: Calculate area of
circle using this formula:
Area (A) = 3.14*R^2
Step 4: Display the
calculated Area (A)
Step 5: Stop
Flowchart:
It is a diagrammatic or
pictorial representation of steps involve in algorithm for solving a particular
problem is known as flowchart.
It uses several geometrical shapes, symbol for representing different action involve in problem solving. There are two types of flowcharts:
a. Program Flowchart: It
is a type of flowchart which focuses on a specific problem. This type of
flowchart portage only a single task (logic) at a time.
C Programming Language:
- Developed
by Dennis Ritchie at AT&T's Bell Laboratories in 1970.
- High-level
and middle-level programming language, supporting both system and
application programming.
- Known
for its efficiency, powerful tools, and keyword set.
- Features
its own text editor and compiler.
- Supports
structured programming.
- Portable
and flexible.
Advantages of C:
- Suitable
for system programming.
- Easy
interaction with hardware.
- Efficient
program development.
- Compact
and secure code.
Disadvantages of C:
- Lack
of runtime checking.
- No
strict data type checking.
- Debugging
complexity increases with program size.
- Semicolon
required for each statement.
Definitions:
- Editor:
The area where the program is typed; C language has its own text editor.
- Pre-processor:
Collection of codes using # directives, e.g., #include for header files.
- Compiler:
Transforms high-level source code into machine-readable object code.
- Linker:
Links various library files with object codes, creating an executable
(.exe) file.
Fundamentals of C:
Character Set:
- Set of
alphabets, digits, white characters, and symbols used in a programming
language.
- C
character set includes alphabets (both uppercase and lowercase), digits,
and special symbols.
Comment:
- Text
or characters not compiled or executed, used for providing information to
programmers.
- Single-line
comment: // Comment text
- Multi-line
comment: /* Comment text */
C Tokens:
- Set
of identifiers, keywords, constants, variables, and operators.
- Remain
as a single unit during compilation.
Definitions:
- Identifiers:
Names for entities like variables, functions, etc.
- Keyword:
Reserved words in C; cannot be used as identifiers.
- Data
Types: int, float, char, void represent types of data in C.
- Constants:
Fixed values in a program.
- Variables:
Named memory locations for storing data.
- Statements:
Set of keywords, identifiers, operators, variables, and constants, ending
with a semicolon or curly brackets.
Escape Sequences:
- Special
sequences with \ or ' as escape characters.
- Examples
include \n for a new line and \t for horizontal tab.
Operators:
- Symbols
used for mathematical and logical calculations.
- Binary,
unary, and ternary operators.
- Examples:
Arithmetic, Relational, Logical, Assignment, Increment/Decrement,
Conditional, Bitwise.
Library Functions:
- Ready-made
functions stored in the C language library.
- Examples:
abs(), printf(), scanf(), clrscr(), getch(),
strlen(), sqrt().
Input/Output (I/O) Functions:
- printf():
Formatted output function.
- scanf():
Input function for reading values into the program.
c) Write a C program to calculate the area of a rectangle.
[Formula: Area = Length * Breadth]
int main(){
int l, b, a;
printf("Enter Length: ");
scanf("%d", &l);
printf("Enter Breadth: ");
scanf("%d", &b);
a = l * b;
printf("The area is %d\n", a);
return 0;
}
d) Write a C program to calculate the perimeter of a rectangle.
[ Formula: Perimeter = 2 * (Length + Breadth)]
int main(){
int l, b, p;
printf("Enter Length: ");
scanf("%d", &l);
printf("Enter Breadth: ");
scanf("%d", &b);
p = 2 * (l + b);
printf("The perimeter is: %d\n", p);
return 0;
}
e) Write a C program to calculate the area of a triangle.
[ Formula: Area = 1/2 * base * height]
int main(){
int b, h, a;
printf("Enter base and height separated by space: ");
scanf("%d %d", &b, &h);
a = 0.5 * b * h;
printf("The area is %d\n", a);
return 0;
}
f) Write a C program to calculate the area and perimeter of a square.
Formula: Area = side * side, Perimeter = 4 * side
int main(){
int l, a, p;
printf("Enter length of a side: ");
scanf("%d", &l);
a = l * l;
p = 4 * l;
printf("The area is %d and the Perimeter is %d\n", a, p);
return 0;
}
g) Write a C program to calculate the volume of a box.
[Formula: Volume = length * breadth * height]
int main(){
int l, b, h, v;
printf("Enter Length: ");
scanf("%d", &l);
printf("Enter Breadth: ");
scanf("%d", &b);
printf("Enter Height: ");
scanf("%d", &h);
v = l * b * h;
printf("The volume is %d\n", v);
return 0;
}
h) Write a C program to enter the radius of a circle and print its circumference.
[Formula: Circumference = 2 * pi * radius]
int main(){
float r, c, pi = 3.14;
printf("Enter the radius: ");
scanf("%f", &r);
c = 2 * pi * r;
printf("The circumference is %f\n", c);
return 0;
}
Control structures
It in programming are constructs that enable developers to manage the flow of execution in a program. They determine the order in which statements are executed based on certain conditions or loops. There are three main types of control structures:
Sequence Structure:
This is the default flow of execution where statements are executed sequentially from top to bottom, following the natural order of the program.
Example program
i) Write a C program to enter the length, breadth, and height of a box and calculate the total surface area (TSA).
[Formula: TSA = 2 * (L * B + B * H + L * H)]
int main(){
int l, b, h, tsa;
printf("Enter length, breadth, and height separated by space: ");
scanf("%d %d %d", &l, &b, &h);
tsa = 2 * (l * b + b * h + l * h);
printf("Total Surface Area is %d\n", tsa);
return 0;
}
Selection Structure (Conditional Statements):
These structures allow the program to make decisions based on specific conditions. Common conditional statements include if, else if, and else. They enable the execution of different blocks of code based on whether certain conditions are true or false.
a) Conditional Statements: They break the program flow if the condition is matching. Eg. if, if….else and if…..else….if
if statement:
The if statement is used to execute a block of code if a specified condition is true.
Syntax
// Code to be executed if the condition is true
}
int main() {
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
}
return 0;
}
Looping Structure (Repetition Statements):
Looping structures facilitate the repetition of a set of statements multiple times. This repetition is based on certain conditions, and common loop constructs include for, while, and do-while. Loops provide a way to efficiently repeat a block of code until a specified condition is met.
l) Write a program to print "Ekta Academy" 100 times using for loop
int main(){
int i;
for(i=1;i<=100;i++){
printf("%d times Ekta Academy\n",i);
}
return 0;
}
m) Write a program to print natural number upto 100 using for loop
int main(){
int i;
for(i=1;i<=100;i++){
printf("%d \n",i);
}
return 0;
}
n) Write a program to print whole number upto 100 using for loop
int main(){
int i;
for(i=0;i<=100;i++){
printf("%d \n",i);
}
return 0;
}
o) Write a program to print odd number upto 100 using for loop
int main(){
int i;
for(i=1;i<=100;i+=2){
printf("%d \n",i);
}
return 0;
}
p) Write a program to print even number upto 100 using for loop
int main(){
int i;
for(i=2;i<=100;i+=2){
printf("%d \n",i);
}
return 0;
}
q) Write a program to print following series
// 5,10,15,20.........50
int main(){
int i;
for(i=5;i<=50;i+=5){
printf("%d \n",i);
}
return 0;
}
r) Write a program to print following series
// 100,90,80........0
int main(){
int i;
for(i=100;i>=0;i-=10){
printf("%d \n",i);
}
return 0;
}
s) Write a program to print following series
// 1,4,9,16,25........10th term
int main(){
int a=1,i;
for(i=1;i<=10;i++){
a=i*i;
printf("%d \n",a);
}
return 0;
}
t) Write a program to print following series
// 1,3,5 ..... upto 15th term
int main(){
int a=1,i;
for(i=1;i<=15;i++){
printf("%d \n",a);
a=a+2;
}
return 0;
}
u) Write a program to print following series
// 2,4,7 ..... upto 8th term
int main(){
int a=2,i;
for(i=1;i<=8;i++){
printf("%d \n",a);
a=a+(i+1);
}
return 0;
}
v) Write a program to print following series
// 1,1,2,3,5 ..... upto 9th term (fibonacci series)
int main(){
int a=1,b=1,i,c;
printf("%d, %d",a,b);
for(i=1;i<=9;i++){
printf("%d,",c);
a=b;
b=c;
}
return 0;
}
Comments
Post a Comment