PPS Unit 1 1
PPS Unit 1 1
ENGINEERING COLLEGE
Unit-I
PROGRAMMING FOR PROBLEM SOLVING
USING C
Mr. Bikshapathy Peruka
Assistant Professor
What is a Computer?
Computer is an electronic device which operates under the control of
instructions stored in its memory and it takes the data from the user,
processes that data and gives the output(result), and
The results might be stored in its memory for future use.
Produces the result to the user through output devices (OUTPUT) and
A Computer System
Application software
Types of Software
Computer Science: A
Structured Programming
Approach Using C 16
MATRUSRI
Relationship Between System and Application Software ENGINEERING COLLEGE
Softwares
• Application software works on any Defined System Software.
• Application Software is a part of System Software.
• System software provides the platform and application software performs
the particular task on any platform.
• System software helps in working of Application Software.
• System software is software that deals directly with the architecture of the
computer hardware.
• Application software provides an interface for user to complete a specific
task through a computer hardware by making use of system software.
• User interacts with the computer using programs and that programs are
created using computer programming languages like C, C++, Java etc.,
• “Computer languages are the languages through which user can
communicate with the computer by writing program instructions.”
• Low Level language is the only language which can be understood by the
computer.
• Binary Language is an example of low-level language.
• All the instructions of binary language are written in the form of binary
numbers 1's & 0's.
• A computer can directly understand the binary language.
• Disadvantages
• Very difficult to understand and use.
• Machine dependent, that means a program written for a particular
machine does not executes on another machine.
• More chance for errors and it is very difficult to find errors, debug and
modify.
March 29, 2025 Programming for Problem Solving 22
MATRUSRI
Assembly Language ENGINEERING COLLEGE
Level Language
1 entry main,^m<r2>
1 00000000 00000100 0000000000000000
2 sub12 #12,sp
2 01011110 00001100 11000010 0000000000000010
3 jsb C$MAIN_ARGS
3 11101111 00010110 0000000000000101
4 moveb $CHAR_STRING_CON
4 11101111 10111110 0000000000001011
5
5 11111000 10101101 11011111 0000000000010010
6 pusha1 -8(fp)
6 01100010 11011111 0000000000010101
7 pusha1 (r2)
7 11101111 00000010 11111011 0000000000010111
8 calls #2,SCANF
8 11110100 10101101 11011111 0000000000011110
9 pusha1 -12(fp)
9 00000011 10100010 11011111 0000000000100001
10 pusha1 3(r2)
10 11101111 00000010 11011111 0000000000100100
11 calls #2,SCANF
11 01111110 11110100 10101101
12 mull3 -8(fp),-12(fp),-
12 11111000 10101110 11000101 0000000000101011
13 pusha 6(fp)
13 00000110 10100010 11111011 0000000000110001
14 calls #2,PRINTF
14 11101111 00000010 11111011 0000000000110100
15 clrl r0
15 01010000 11010100 0000000000111011
16 ret
16 00000100 0000000000111101
1 #include<stdio.h>
2 int main(void)
3 {
4 int n1, n2,product;
5 printf(“Enter two numbers : “);
6 scanf(“%d %d”,&n1,&n2);
7 product = n1 * n2;
8 printf(“%d”,product);
9 return 0;
1 }
0
MATRUSRI
Memory Types ENGINEERING COLLEGE
Where, N = a number
b = radix or base to number system
n = number of digits in integer portion
Following table denotes the four number systems, the radix, and different
digit(symbol)s used in alphabet, along with the examples.
31
Decimal to Octal Conversion MATRUSRI
ENGINEERING COLLEGE
32
MATRUSRI
Hexadecimal and Binary Conversion ENGINEERING COLLEGE
result (grade)
START
// Pseudocode of grade printing problem
Input
M1,M2,M3,M4 Function Grade(M1,M2,M3,M4):
Total= M1+M2+M3+M4
AVG(M1+M2+M3+M4)/4 Avg Total/4
IF (Avg < 50) THEN
N
PRINT( “FAIL”)
IS Y
AVG<50
ELSE
PRINT(“PASS”)
PRINT PRINT ENDIF
“PASS” “FAIL”
STOP
• Pseudocode:
Function LengthCovert(length) :
Input Lft
Lcm Lft x 30
Print (Lcm)
March 29, 2025 Programming for Problem Solving 42
MATRUSRI
Example 2 ENGINEERING COLLEGE
• Pseudocode:
Flowchart:
Function LengthCovert(length) : START
Input Lft
Lcm Lft x 30 Input
Lft
Print (Lcm)
Lcm Lft x 30
Print
Lcm
STOP
Example3: Write an algorithm, Pseudocode and draw a flowchart that will read
the two sides of a rectangle and calculate its area.
• Algorithm:
• Input: width(w), length(L) Output : Area(A)
Step1) Input the width (W) and Length (L) of a rectangle
Step2) Calculate the area (A) by multiplying L with W
Step3) Print A
• Pseudocode:
• Function Area( length,width ):
Input W,L
AL x W
Print (A)
START
Pseudocode:
• Function Area( length,width ): Input
Input W,L W, L
AL x W
Print (A)
ALxW
Print
A
STOP
Example4: Write an algorithm and draw a flowchart that will calculate the
roots of a quadratic equation ax2+bx+c=0 Hint: d = sqrt (b2-4*a*c ), and the
roots are: roo1 = (–b + d)/2a and root2 = (–b – d)/2a
• Algorithm: Roots of Quadratic Equation
• Input: a,b,c Output: root1, root2
• Steps: 1) Input the coefficients (a, b, c) of the quadratic equation
2) Calculate d= sqrt (b2-4*a*c)
3) If (d>0) then
3.1) Calculate root1 = (–b + d)/2a
3.2) Calculate root2 = (–b – d)/2a
4) If (d=0) then
4.1) Calculate root1 = (–b + d)/2a
4.2) Calculate root2 = (–b – d)/2a
5) Else Print” Roots are Imaginary”
6) Print root1 and root2
March 29, 2025 Programming for Problem Solving 46
Example 4- Flowchart MATRUSRI
ENGINEERING COLLEGE
START
• Algorithm steps: A simple algorithm and
its flowchart. Input
• Step 1: Input a, b, c a, b, c
• Step 2: d sqrt (b b 4 a c)
• Step 3: root1 (–b + d) / (2 x a) d sqrt(b x b – 4 x a x c)
Print
root1 , root2
STOP
Print A Print B
Example5: Write an algorithm that reads two values, determines the largest value and
prints the largest value with an identifying message.
START
ALGORITHM: Largest of any TWO values
Input: Value1,Value2 Output: Max(Value1,Value2) Input
• Step 1: Input VALUE1, VALUE2 VALUE1,VALUE2
• Step 2: if (VALUE1 > VALUE2) then
• MAX VALUE1 Y Is N
• else VALUE1>VALUE2
• MAX VALUE2
• endif MAX VALUE1 MAX VALUE2
• Step 3: Print “The largest value is”, MAX
Print
“The largest value is”, MAX
STOP
March 29, 2025 Programming for Problem Solving 49
MATRUSRI
IF–THEN–ELSE STRUCTURE ENGINEERING COLLEGE
Relational Operators
Operator Description Result
> Greater than x=10,y=12, Is(x>y)?TRUE( or 1)
< Less than Is(x>y)? FALSE( or 0)
= Equal to Is(x=y)? FALSE( or 0)
Greater than or equal Is(xy)? FALSE( or 0)
to
Less than or equal to Is(xy)? FALSE( or 0)
Not equal to Is(xy)? TRUE( or 1)
Linux Environment
• 1. Go to the Root($ prompt) by typing Cntl+Alt+T and + Enter
• 2. Create your own Directory in the format “ CSE-001” using the command “$
Type “$ mkdir CSEB001” + Enter
• 3. Go to your Directory using $cd CSEB001 + Enter
• 4. Create a C file using an Editor(like gedit, ed, or vi) typing $ gedit prog1.c
(+Enter)
• 5. Compile the Program $ gcc prog.c +Enter
• 6. Execute the program $ .\a.out +Enter
3/29/2025 57
MATRUSRI
ENGINEERING COLLEGE
Keywords: These are the predefined set of words that are assigned a role
(meaning). There are 32 keywords in C(given in the table below).
• Since the purpose is predefined and fixed, C does not allow change of
its purpose. Therefore, they are called reserved words.
auto 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
Computer Science: A Structured Programming Approach Using C
61
Identifiers in C MATRUSRI
ENGINEERING COLLEGE
Identifiers:
• Is a sequence of characters used to identify or name a data item, an array or a
function, program.
• C provides the following rules to write correct identifiers
Rules for Identifiers:
Note: An identifier must start with a letter or underscore: it may not have a
space or a hyphen.
Computer Science: A Structured Programming Approach Using C 63
MATRUSRI
Example Valid and Invalid Identifiers: ENGINEERING COLLEGE
Note: C is a case sensitive language means it treats ‘A’ as a different character to ‘a’. We
should be careful in naming the data item and its usage.
• Data type or just a type defines a set of values and a set of operations that can
be applied on those values.
• For example, a light switch can be compared to a computer type.
• It has a set of two values, on and off. Only two operations can be applied to a
light switch: turn-on and turn-off.
Different Data types in C :
• Following is the list of different classes of data types in C language. These are
classified basically into three types void, integral, and floating point types.
• Void Type
• Intégral Type
• Floating-Point Types
• We will discuss these data types first and will know about the derived and user
defined types later
Computer Science: A Structured Programming Approach Using C 65
MATRUSRI
ENGINEERING COLLEGE
Classification of C Data Types
Data Types
• Character Types: Are used to declare, define and use character data items in
programs. Following are the two different character types available in C.
• For example, char ch1; // declares ch1 as a character and can store a character in it.
Character Types
• Integer Types: Are used to declare, define and use integer data in programs.
Following are the four different integer types available in C.
• For example, int n1; // declares n1 as an integer and can store an integer value in it.
Note: sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)
Integer Types
• Floating Point Types: Are used to declare, define and use real data in
programs. Following are the three different floating point types available in C.
• For example, float n1; /* declares n1 as a real number and can store
• floating point value in it */
• Variables are named memory locations that have a type, such as integer or
character, which is inherited from their data type.
• The data type of a variable denotes the type of data that a variable can
store and the operations that may be performed on it.
• A variable must be declared (and defined) before its use in a statement
• Variable Declaration: Allows us to define a data item of required type.
Following is the syntax of a declaration statement.
• Syntax: <datatype> <variable[s]>; For example, following declaration,
int x, y, sum; /* It’s a declaration statement that declares
variables x,y and sum to be of integer types and assigns 2B of memory to each data items*/
• Variable Initialization: Is a method of assigning initial(first) values in to the
memory locations allocated for a declared variable.
• If not initialized, a variable contains a garbage value. For example,
int rollno = 65; // declares rollno as an integer and initializes it with a value 65.
Computer Science: A Structured Programming Approach Using C 72
More on Variable Declarations MATRUSRI
ENGINEERING COLLEGE
Note:
When
Whenaavariable
variableisisdefined,
defined,but
butnotnotinitialized. Contains a garbage value in it.
initialized,Contains
Therefore,
Therefore,We
Weshould
shouldinitialize
initializeititwith
withprescribed
prescribeddata.
data.
Variable Initialization
• Constants are data values that cannot be changed during the execution of a
program.
• Like variables, constants have a type. In this section, we discuss Boolean,
character, integer, real, complex, and string constants.
• For example, ‘a’ is a character constant, 125 is an integer constant, and
245.20 is a floating point constant
Note
• Special cases of Character and String Constants: are the Null character and Null
Strings which are given below. These are very useful in string processing
applications.
Some Strings
Operators are symbols which take one or more operands( or expressions) and
perform its assigned computational task.
Operands are variables or expressions which are used in conjunction with
operators in the evaluation of an expression. A valid combination of operands
and operators form an expression.
Expressions are sequences of operators, operands, and punctuators that specify
a computation.
Evaluation of expressions is based on the operators that the expressions contain
and the context in which they are used.
Expression can result in a value and can produce side effects.
A side effect is a change in the state of the execution environment.
• Lets assume variable A holds 10 and variable B holds 20. Following table gives the operators
and their functionality.
Operator Description Example
< • Checks if the value of left operand is less than the (A < B) is true.
value of right operand. If yes, then the condition
becomes true.
>= • Checks if the value of left operand is greater than (A >= B) is not true.
or equal to the value of right operand. If yes, then
the condition becomes true.
<= • Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand. If yes, then the
condition becomes true.
• The operators, &&(Logical AND), ||(Logical OR), !(Logical NOT) are logical operators and are
also called as compound relational operators. These are used to compare the relationship
between two or more data items( OR expressions). The value of a logical expression is always
Boolean( 0 or 1). Lets assume variable A holds ZERO(0) and variable B NON-ZERO(1).
Following table gives the operators and their functionality.
Operator Description Example
&& • Called Logical AND operator. If both the operands are (A && B) is false.
non-zero(TRUE), then the condition becomes true.
|| • Called Logical OR Operator. If any of the two operands (A || B) is true.
is non-zero, then the condition becomes true.
! • Called Logical NOT Operator. It is used to reverse the !(A && B) is true.
logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
• These operators &(Bitwise AND),|(Bitwise OR),^(XOR), ~(1’s Complement), <<(Left shift) and
>>(right shift) are bitwise operators because, they are applied on the bits of operands.
• Assume variables ‘p’ and ‘q’ are the corresponding bits of the operands. The result of a bitwise
operation is also either 0 or 1.
• Following table shows the result of bitwise operators for different values of p and q.
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
• Assume variable 'A' holds 60(in binary 111100) and 'B' holds 13(or 001101). Following table
gives the resultant of applying bitwise operators on A and B.
Operator Description Example
• Binary AND Operator performs the bitwise operation on its (A & B) = 12,
& operands. i.e., 0000 1100
• Binary OR Operator biwise OR on the corresponding bits of (A | B) = 61,
| the operands. i.e., 0011 1101
• Binary XOR Operator sets the bit if it is set in one operand but (A ^ B) = 49,
^ not both. i.e., 0011 0001
~ • Binary One's Complement Operator is unary and has the effect (~A ) = ~(60),
of 'flipping' bits. i.e,. 0000 11
<< • Binary Left Shift Operator. The left operands value is moved A << 2 = 240
left by the number of bits specified by the right operand. i.e., 1111 0000
>> • Binary Right Shift Operator. The left operands value is moved A >> 2 = 15
right by the number of bits specified by the right operand. i.e., 0000 1111 93
Assignment Operators MATRUSRI
ENGINEERING COLLEGE
SPECIAL OPERATORS IN C
sizeof() • Returns the size of a variable. sizeof(a), Where a is integer, will return 4.
& • Returns the address of a variable. &a; Returns the address of variable a
* • Pointer to a variable. *a; Declares variable a as a pointer
A = Condition? X :Y ; If Condition is true ?
?: • Conditional Expression. then value X : otherwise value Y is assigned to
A 95
MATRUSRI
Postfix Operators In C ENGINEERING COLLEGE
And Associativity
Precedence and Associativity of an operator defines the relative order of its
use in evaluation of an expression
Precedence: Is defined as the order(or priority) of application of an operator
in a complex(or multi-operator) expression. Such expressions contain more
than one operators with different precedence.
For example, in evaluation of (x+y*z/-d), which is equal to (x+(((y*z)/(-d)))) ,
we first apply –(uminus) because, it has highest precedence, then * and / with
next highest, and apply + as the last operation.
Associativity: When two or more operators have the same precedence then
the concept of associativity comes into discussion.
For example, in evaluation of (x+y-z+d) is equal to (((x+y)-z)+d) because + and –
are left associative
97
Precedence & Associativity of MATRUSRI
ENGINEERING COLLEGE
Operators In C
PRECEDENCE LEVEL) TYPE OPERATOR ASSOCIATIVITY
• L1) Postfix • () [] -> . ++ - - • Left to right
• L2) Unary • + - ! ~ ++ - - (type)* & • Right to left
sizeof
• L3) Multiplicative • */% • Left to right
• L4) Additive • +- • Left to right
• L5) Shift • << >> • Left to right
• L6) Relational • < <= > >= • Left to right
• L7) Equality • == != • Left to right
• L8) Bitwise AND • & • Left to right 98
MATRUSRI
Precedence & Associativity Cntd.. ENGINEERING COLLEGE
Generation of output.
The acceptance of data refers to input and the presentation of data refers to the
output
All I/O is classified into 1) Console I/O 2) Disk(or File) I/O 3) Port I/O
In Console I/O , The source and destination of data is standard I/O devices
In Disk(File) I/O , In this case, the Source and destination of I/O are the files(HDD)
In Port I/O, here the Source and destination os data are various ports
Apart from this we have many string I/O functions that are discussed in
later units of the course.
3/29/2025 Programming for Problem Solving 100
MATRUSRI
Console Input/Output Statements in C ENGINEERING COLLEGE
C provides various functions to receive inputs from Keyboard, and write outputs on
the VDU. These functions are defined in the library “stdio.h”.So, we must include this
library to use them for I/O operations.
For example, scanf() and printf() are the fundamental input/output statements in this
libray and
Are used to perform input(reading from the keyboard) and output(for displaying on
the monitor or an output device) operations respectively.
The syntax of these statements is,
scanf("format string",argument_list);
printf("format string",argument_list);
The format string contains conversion specifiers like %d (for integer), %c (for
character), %s (for string), and %f (for float) which depends on the type of I/O data
given in the argument_list.
3/29/2025 Programming for Problem Solving 101
MATRUSRI
Format Specifiers ENGINEERING COLLEGE
2. int main(){
Syntax errors: Are the errors caused when rules of writing C code are violated.
These are normally detected by the compiler during the compilation and are fixed
before the executable code is generated.
Since, all these errors are detected by compiler, are called as compile-time errors.
Most frequent syntax errors are:
Missing Parenthesis (})
Use or Printing the value of variable without declaring it.
Missing semicolon like this(;) etc.
Logical Errors: A logical error, is a 'bug' or mistake in a program's source code(or
logic) that results in incorrect or unexpected results.
It is a type of runtime or execution time error that may simply produce the wrong
output or cause a program to crash while running.
So, it’s the responsibility of the programmer to write logically correct programs to
avoid such wrong results.
March 29, 2025 Programming for Problem Solving 109
Object Code & Executable Code MATRUSRI
ENGINEERING COLLEGE