Module - I - CP
Module - I - CP
Module - I - CP
What is a program?
A computer program is a sequence of instructions written using a Computer Programming
Language to perform a specified task by the computer.
Computer Programming
the act of writing computer programs is called computer programming.
As we mentioned earlier, there are hundreds of programming languages, which can be used
to write computer programs and following are a few of them −
Java
C
C++
Python
PHP
Perl
Ruby
Computer Programmer
Someone who can write computer programs or in other words, someone who can do
computer programming is called a Computer Programmer.
Based on computer programming language expertise, we can name a computer
programmers as follows −
C Programmer
C++ Programmer
Java Programmer
Python Programmer
PHP Programmer
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.
1|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
i. Machine 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 main advantage of a high-level language is that it is easy to read, write, and maintain.
High-level programming language includes Python, Java, JavaScript, PHP, C#, C++, Objective
C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.
2|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
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.
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.
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++
Translator:
A translator is a programming language processor that modifies a computer program from
one language to another. It takes a program written in the source program and modifies it
into a machine program. It can find and detect the error during translation.
Compiler
3|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
Interpreter
Assembler
An assembler is a translator which translates an assembly language program into an
equivalent machine language program of the computer. Assembler provides a friendlier
representation than a computer 0’s and 1’s that simplifies writing and reading programs.
Differences between compiler and interpreter
4 Debugging is hard as the error It stops translation when the first error is
messages are generated after met. Hence, debugging is easy.
4|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
Algorithms
A set of finite rules or instructions to be followed in calculations or other problem-solving
operations
Or
” An algorithm is defined as a finite set of steps that provide a chain of actions for solving a problem.
Algorithm is a step-by-step method of solving a problem.”.
5|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
Advantages of Algorithms:
1. It is a step-wise representation of a solution to a given problem, which makes it easy
to understand.
2. An algorithm uses a definite procedure.
3. It is not dependent on any programming language, so it is easy to understand
for anyone even without programming knowledge.
4. Every step in an algorithm has its own logical sequence so it is easy to debug.
5. By using algorithm, the problem is broken down into smaller pieces or steps
hence, it is easier for programmer to convert it into an actual program.
Disadvantages of Algorithms:
1. Algorithm is Time consuming.
2. Difficult to show Branching and Looping in Algorithms.
3. Big tasks are difficult to put in Algorithms.
Qualities of a good algorithm: The following are the primary factors that are often used
to judge the quality of an algorithm.
Time: The lesser is the time required to execute a program better is the algorithm
Memory: Computer takes some amount of memory to storage to execute a program.
The lesser is the memory required the better is the algorithm
Accuracy: Algorithms which provide the accurate results to a given problem should be
chosen. Sequence: The procedure of an algorithm must form in a sequence
Generality: The designed algorithm must solve the problem for a different range of input
data.
6|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
2. Selection
We understand that the algorithms written in sequence fashion are not reliable. There
must be a procedure to handle operation failure occurring during execution.
The selection of statements can be shown as follows
if(condition)
Statement-1;
else
Statement-2;
The above syntax specifies that if the condition is true , statement-1 will be executed
otherwise statement-2 will be executed.
In case the operation is unsuccessful. Then sequence of algorithm should be changed/
corrected in such a way that the system will re-execute until the operation is
successful.
Example:
// Person eligibility for vote
Step 1 : start
Step 2 : read age
Step 3 : if age > = 18 then step_4 else step_5
Step 4 : write “person is eligible for vote”
Step 5 : write “ person is not eligible for vote”
Step 6 : stop
Multi-way:
7|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
For a single problem, three may be multiple solutions. In such a situation we have to apply
multi way (switch case) decision statements or we can extend two-way statements
(example, if else) in a ladder form.
Example:
// Roots of a quadratic Equation
Step 1 : start
Step 2 : read a,b,c
Step 3 : if (a= 0) then step 4 else step 5
Step 4 : Write “ Given equation is a linear equation “
Step 5 : d=(b * b) _ (4 *a *c)
Step 6 : if ( d>0) then step 7 else step8
Step 7 : Write “ Roots are real and Distinct”
Step 8: if(d=0) then step 9 else step 10
Step 9: Write “Roots are real and equal”
Step 10: Write “ Roots are Imaginary”
Step 11: stop
Iteration
In a program, sometimes it is very necessary to perform the same action for a number
of times.
If the same statement is written repetitively, it will increase the program code.
To avoid this problem, iteration mechanism is applied.
The statement written in an iteration block is executed for a given number of times
based on certain condition.
Example:
Step 1 : start
Step 2 : read n
Step 3 : repeat step 4 until n>0
Step 4 : (a) r=n mod 10
(b) s=s+r
(c) n=n/10
Step 5 : write s
Step 6 : stop
8|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
9|Dept. of MCA/NECN
MODULE – I : C PROGRAMMING
Flowchart:
A flowchart is a type of diagram that represents a workflow or process. A flowchart can
also be defined as a diagrammatic representation of an algorithm, a step-by-step
approach to solving a task.
1. Terminal: The terminal (oval)symbol as the name implies is used
to indicate the beginning(start), ending(stop) and pause in the
logic flow. It is the first and last symbol in the programming
logic.
2.Input/output:The input/output (parallelogram) symbol is used to
denote any function of an input/output device in the program. All
input/output instructions in the program are indicated with this symbol.
3. Processing: A processing(rectangle) symbol is used in a flowchart
to represent arithmetic and data movement instructions. Thus all
arithmetic processes of addition, subtraction, multiplication etc
4. Flow lines: Flow lines with arrow heads are used to indicate the flow
of operations i.e. the exact sequence in which the instructions are to
be executed. The normal flow of flowchart is from top to bottom and
left to right.
10 | D e p t . o f M C A / N E C N
MODULE – I : C PROGRAMMING
1.Write an algorithm for finding the average of three numbers and draw the flowchart for the
same.
Step 1: read the value
Read a,b and c
Step2:compute sum=a+b+c
Avg=sum/3
Step 3:print avg
Step 4:stop
Example 2.Write an algorithm for finding greatest among two numbers
Step 1:start
Step 2:read two numbers
Step 3:compare two values(ex a and b)
Step 4:if a is greater than b display F
a is greater otherwise b is greater
Step 5:stop
Example 3: write an algorithm and flowchart for finding the sum of n numbers
Step 1:start
Step 2:read numbers upto n Initialize count =1 sum=0
Step 3:repeat through count<=n
Step 4: x=1 sum=sum+x
Count=count+1
Step 5:print sum
Step 6:stop
11 | D e p t . o f M C A / N E C N
MODULE – I : C PROGRAMMING
Advantages of flowchart:
1. A flowchart can easily explain the program logic to the program development team.
2. The flowchart details help prepare efficient program coding.
3. The flowchart helps detect and remove the mistakes in a program
4. A flowchart is useful to test the logic of the program.
Problem Definition: In this phase, we need to understand what the problem statement is,
what our requirement is and what the output of the problem solution is.
Problem Analysis: Here, we determine the requirements like variables, functions, etc. to
solve the problem. It means that we gather the required resources to solve the problem,
which are defined in the problem definition phase.
Algorithm Development: Here, we develop a step-by-step procedure that is used to solve
the problem by using the specification given in the previous phase.
Coding & Documentation: Here, we use a programming language to write or implement the
actual programming instructions for the steps defined in the previous phase. We write the
12 | D e p t . o f M C A / N E C N
MODULE – I : C PROGRAMMING
program to solve the given problem by using the programming languages like C, C++, Java,
etc.
Testing & Debugging: Here we try to test the program whether it is solving the problem for
various input data values or not. We also test if it is providing the desired output or not.
Maintenance: In this phase, we make the enhancements. If the user gets any problem or
wants any enhancement, then we need to repeat all these phases from the starting, so that
the encountered problem is solved or enhancement is added.
These six phases are depicted in the diagram given below –
1. Algorithm for exchanging the values of given two variables: Algorithm 1: using a
third variable
Step 1 : Start
Step 2 : READ num1, num2
Step 3 : temp = num1
Step 4 : num1 = num2
Step 5 : num2 = temp
Step 6 : PRINT num1, num2
Step 7 : Stop
13 | D e p t . o f M C A / N E C N
MODULE – I : C PROGRAMMING
14 | D e p t . o f M C A / N E C N
MODULE – I : C PROGRAMMING
{
sum += i;
}
printf("Sum = %d", sum); return 0;
}
4. Algorithm for factorial computation:
Step1: Start
Step 2: Declare ‘n’,’i’ and ‘f’ as integer variables
Step 3: Assign ‘f’ as 1
Step 4: Read the ‘n’ value
Step 5: Check for(i=1;i<=n,i++) Step5.1: Assign ‘f’ as f*i
Step 6: Display the factorial value ‘f’
Step 7: Stop
Program:
/* To find factorial of the given number */ #include <stdio.h>
#include <conio.h> int main()
{
int n,i,f=1; clrscr();
printf("\n Enter the number:"); scanf("%d",&n); for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n The factorial of %d is %d",n,f); return(0);
5. Algorithm for generation of the Fibonacci sequence to the first ‘n’ terms:
Step 1: Start
Step 2: Declare i, n, f, f1, f2 as integer variables
Step 3: Read the ‘n’ value
Step 4: Initialize ‘f’ as 0 and ‘f1’ as 1
Step 5: Display ‘f’ and ‘f1’ values
Step 6: Check for(i=1;i<=n-2;i++)
Step 6.1: Find ‘f2’ as f+f1
Step 6.2: Display the value of ‘f2’
Step6.3: Assign ‘f’ as ‘f1’
Step 6.4: Assign ‘f1’ as ‘f2’
Step7: Stop
Program:
/ * c program to generate the first n terms of the fibonacci sequence */ #include<stdio.h>
void main()
{
int i,n,f,f1,f2; clrscr();
printf("Enter Number of Fibonacci Values Needed : "); scanf("%d",&n);
f=0; f1=1;
printf(“%d\n%d\n”,f,f1); for(i=1;i<=n-2;i++)
15 | D e p t . o f M C A / N E C N
MODULE – I : C PROGRAMMING
Step 1: Start
Step 2: Declare n, rev, rem as integer variables
Step 3: Initialize rev as zero
Step 4: Read ‘n’ value
Step 5: Check while (n!=0)
Step 5.1: rem=n%10
Step 5.2: rev=rev*10+rem
Step 5.3: n=n/10
Step 6: Display ‘rev’ value as output
Step 7: Stop
}
Program:
/* Reverse the digits of an given integer */ #include <stdio.h>
#include <conio.h> int main()
{
int n, rev = 0, rem; clrscr();
printf("\nEnter an integer: "); scanf("%d", &n);
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem; n=n/10;
}
printf("\n The Reversed number of %d = %d",n,rev); return(0);
}
16 | D e p t . o f M C A / N E C N