UNIT- 5
INTRODUCTION TO COMPUTER PROGRAMMING
BASICS OF PROGRAMMING:
When we say Environment Setup, it simply implies a base on top of which we can do our
programming. Thus, we need to have the required software setup, i.e., installation on our PC
which will be used to write computer programs, compile, and execute them. For example, if you
need to browse Internet, then you need the following setup on your machine
A working Internet connection to connect to the Internet
A Web browser such as Internet Explorer, Chrome, Safari, etc.
ALGORITHMS:
an algorithm is a set of well-defined STEP BY STEP instructions to solve a particular problem.
The Algorithm designed are language-independent, i.e. they are just plain instructions that
can be implemented in any language, and yet the output will be the same, as expected.
Characteristics/properties of an algorithm:
Clear and Unambiguous: Each of its steps should be clear in all aspects and must lead to
only one meaning.
Well-Defined Inputs: it should be well-defined inputs.
Well-Defined Outputs: It should give atleast 1 output.
Finite-ness: i.e. it should terminate after a finite time.
Feasible: The algorithm must be simple, generic, and practical, such that it can be
executed with the available resources.
Language Independent: i.e. it must be just plain instructions that can be implemented in
any language, and yet the output will be the same, as expected.
Designing structure of an algorithm:
Step1: START
Step2: Declare variables and its data types.
Step3: Read the value from variables/input.
Step4: Logic performing functions.
Step5: Print the value of the stored variable/ output.
Step6: END/final
Examples of algorithms:
1. Write an algorithm to add 2 no’s.
Step 1: Start/initial
Step 2: Declare variables a,b,sum.
Step 3: Read/input values for a,b.
Step 4: Add a,b /sum(a,b)/sum=a+b then store result.
Step 5: Display/print variable sum.
Step 6: Stop/end/final
Note: similarly change the operators only (-,*, /) i.e. for subtraction, multiplication
& division.
2. Write an algorithm to find odd or even number.
Step 1: Start/initial
Step 2: Declare variable n.
Step 3: Read/input value of n.
Step 4: if n%2 rem=0 then store result.
Step 5: Display/print n is even number.
Step 6: if n%2 rem not equal to zero then.
Step 7: print/output n is odd number.
Step 8: Stop/end/final.
3. Algorithm to convert Fahrenheit to centigrade.
Step 1: Start/initial
Step 2: Declare variable f ,c.
Step 3: Read/input value of f.
Step 4: °C = 5/9(°F – 32)
Step 5: Display/print centigrade.
Step 6: Stop/end/final.
4. Algorithm to find prime number.
STEP 1: start
STEP 2: Take num as input.
STEP 3: Initialize a variable temp to 0.
STEP 4: Iterate a “for” loop from 2 to num/2.
STEP 5: If num is divisible by loop iterate, then increment temp.
STEP 6: If the temp is equal to 0,
STEP7: Return “Num IS PRIME”.
STEP8: Return “Num IS NOT PRIME”.
STEP 9: END
5. Algorithm to solve quadratic equation.
Step 1. Start
Step 2. Read the coefficients of the equation, a, b and c from the
user.
Step 3. Calculate discriminant = (b * b) – (4 * a * c)
Step 4. If discriminant > 0:
4.1: Calculate root1 = ( -b + sqrt(discriminant)) / (2 * a)
4.2: Calculate root2 = ( -b - sqrt(discriminant)) / (2 * a)
4.3: Display "Roots are real and different"
4.4: Display root1 and root2
Step 5: Else if discriminant = 0:
5.1: Calculate root1 = -b / (2 *a)
5.2: root2 = root1
5.3: Display "Root are real and equal"
5.4: Display root1 and root2
Step 6. Else:
6.1: Calculate real = -b / (2 * a)
6.2:Calculate imaginary = sqrt(-discriminant) / (2 * a)
6.3: Display “Roots are imaginary”
6.4: Display real, "±" , imaginary, "i"
Step 7. Stop
FLOWCHART:
Pictorial/graphical/diagrammatical representation of logic/program
called flow chart.
Benefits of flowchart.
1. Easily understand
2. Diagnose program errors.
3. Helps program writing.
4. manipulation
5. easy of complex programs
6. Understand workflow of design.
Different symbols of FLOWCHART
NOTE: SYMBOLS PROVIDED IN CLASS/LECTURE hours.
C PROGRAM
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T
(American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc
Language Year Developed By
Algol 1960 International Group
BCPL 1967 Martin Richard
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K&RC 1978 Kernighan & Dennis Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization Committee
Features OF C PROGRAM
Low-level access
Portable
Structured programming
Efficient
Applications OF C PROGRAM
GUI
GAMING
SOFTWARE DEVELOPMENT
EXAMPLE OF C PROGRAM
#include
Void main()
{
int A= 5;
printf("%d",A);
}
C PROGRAM
C PROGRAM IS A STRUCTURED MID LEVEL LAUNGUAGE USED TO CREATE GENRAL PURPOSE APPLICATIONS, EXAMPLE
OPERATING SYSTEM.
STRUCTURE OF C PROGRAM
1. HEADER SECTION
#include<stdio.h>
2. Declaration
Int a;
3. Body section
Main()
{
// statement;
}
C TOKENS
Keywords
IT can be defined as the pre-defined or the reserved words having its own importance, and
each keyword has its own functionality.
EX.
uto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
IDENTIFIERS
Identifiers in C are the user-defined words.
EX. naming variables, functions, arrays, structures, etc.
Strings
Strings are set of characters with enclosing double “ “
Ex. “cse”
Operators
Operators is a special symbol used to perform the functions.
Ex. +,-,/,*
Special charcters
Theses have special meaning which cannot be used for another purpose.
Ex. &,#,(),{}
Char
Charcters are single characters with enclosing single ‘ ‘
Ex. ‘a’,’n’
Constatnts
It is digits from 0-9
Ex. 012,111
Variable:
used to store the value of the memory address location.
Syntax:
Datatype variable; or Datatype variable= value;
Ex. int myNum = 15;
Rules for variables:
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
Names are case sensitive (myVar and myvar are different variables)
Names cannot contain whitespaces or special characters like !, #, %, etc.
Reserved words (such as int) cannot be used as names
Types of variables:
1. Local variable:
These can be used within the blocks of the program.
Ex. Main()
{
Int a, b;
// body;
}
2. Global variable:
These can be used outside the blocks of the program/ throughout the program.
Ex.
#include<stdio.h>
Int a, b;
Main()
{
// body;
}
DATA TYPES:
USED TO tell which type of value used to store the memory.
syntax
data_type variable;
Types of data types
Size Format
Data Type (bytes) Range Specifier
short int 2 -32,768 to 32,767 %hd
unsigned short
2 0 to 65,535 %hu
int
unsigned int 4 0 to 4,294,967,295 %u
-2,147,483,648 to
int 4 %d
2,147,483,647
-2,147,483,648 to
long int 4 %ld
2,147,483,647
unsigned long
4 0 to 4,294,967,295 %lu
int
long long int 8 -(2^63) to (2^63)-1 %lld
unsigned long 8 0 to %llu
Size Format
Data Type (bytes) Range Specifier
long int 18,446,744,073,709,551,615
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
float 4 %f
1.2E-38 to 3.4E+38
double 8 %lf
1.7E-308 to 1.7E+308
long double 16
3.4E-4932 to 1.1E+4932 %Lf
FUNCTIONS:
Functions are the building blocks of the program.
Syntax:
main()
{
//statement;
}
Ex.
main()
{
Sum()
{
C=a+b;
}
Printf(c );
}
DECISION MAKING:
These are used to check whether the condition is true / false.
True=1, false=0.
1. If
if (condition)
//statement;
2. If else
if (condition)
{
// statement;
}
else
{
// statement;
}
3. else if
if (condition)
{
// statement;
}
Elseif (condition)
{
// statement;
}
else
{
// statement;
}
4. nested if
if (condition)
{
// statement;
}
elseif (condition)
{
If (condition)
{
// statement;
}
}
else
{
// statement;
}
Loops
Used for repetitive actions in particular block.
1. for
for(initialization; condition;increment/decrement)
{
//statement;
}
2. while
while(condition)
{
//statement;
}
3. do while
do
{
Printf(“\n”);
}
While (condition);
Operators:
A+B
A, B are operands & + is operator used to perform on operands.
1. Arithmetic operators
+,-,*,/,%
2. Assignment operators
==,=,>,<,=>,<=
3. Logical operators
&&,||,>>>,<<<
Note: operators are explained in class.
Array
Array is data type of C used to give sequence values.
Ex. A[], a[]={0,1,2,3}, a[2];
Types
2 dimensional &
3 dimensional
Array indexing
Array starts with zero [0,1,2,3,4]