Unit 1
Unit 1
Problem Solving
Unit I
UNIT I INTRODUCTION
Evolution of Programming & Languages - Problem Solving through
Programming - Writing Algorithms & Pseudo code - Single line and multiline
comments-Introduction to C : Structure of C the Program - Input and output
statements - Variables and identifiers, Constants, Keywords –Values, Names,
Scope, Binding, Storage Classes - Numeric Data types: integer, floating point –
Non-Numeric Data types : char and string- L value and R value in expression,
Increment and Decrement operator-Comma, Arrow and Assignment operator –
Arithmetic, Relational and Logical Operators – Condition Operators, Operator
Precedence – Expressions with pre/post increment operator.
1. 1 Evolution of Programming & Languages
A Computer needs to be given instructions in a programming
language that it understands
Programming Language
Artificial language that controls the behavior of computer
Defined through the use of syntactic and semantic rules
Used to facilitate communication about the task of organizing
and manipulating information
Used to express algorithms precisely
1. 1 Evolution of Programming & Languages Contd…
Period Programming Langugaes
1950’s Creation of high-level languages
1960’s Forth. Simula I. Lisp, Cobol
1970’s Pascal, C language
1980’s ML. Smalltalk, C++
1990’s Java, Perl, Python languages
2000 Internet Programming
2010 Concurrency and asynchronicity. JavaScript and Go language
1. 2 Problem Solving through Programming
Problem - Defined as any question, something involving doubt, uncertainty,
difficulty, situation whose solution is not immediately obvious
Computer Problem Solving
Understand and apply logic
Success in solving any problem is only possible after we have
made the effort to understand the problem at hand
Extract from the problem statement a set of precisely defined tasks
1. 2 Problem Solving through Programming Contd…
i. Creative Thinking
Proven method for approaching a challenge or opportunity in an imaginative way
Process for innovation that helps explore and reframe the problems faced, come
up with new, innovative responses and solutions and then take action
It is generative, nonjudgmental and expansive
Thinking creatively, a lists of new ideas are generated
1. 2 Problem Solving through Programming Contd…
ii. Critical Thinking
Engages a diverse range of intellectual skills and activities that are concerned
with evaluating information, our assumptions and our thinking processes in a
disciplined way so that we can think and assess information more
comprehensively
It is Analytical, Judgmental and Selective
Thinking critically allows a programmer in making choices
1. 2 Problem Solving through Programming Contd…
1. 2 Problem Solving through Programming Contd…
Program - Set of instructions that instructs the computer to do
a task
Programming Process
a) Defining the Problem
b) Planning the Solution
c) Coding the Program
d) Testing the Program
e) Documenting the Program
1. 2 Problem Solving through Programming Contd…
1. 2 Problem Solving through Programming Contd…
A typical programming task can be divided into two phases:
i. Problem solving phase
Produce an ordered sequence of steps that describe solution
of problem this sequence of steps is called an Algorithm
ii. Implementation phase
Implement the program in some programming language
Steps in Problem Solving
a) Produce a general algorithm (one can use pseudocode)
1. 2 Problem Solving through Programming Contd…
b) Refine the algorithm successively to get step by step detailed
algorithm that is very close to a computer language
c) Pseudocode is an artificial and informal language that helps
programmers develop algorithms
Pseudocode is very similar to everyday English
1. 3 Creating Algorithms
An informal definition of an algorithm is:
a) Develop an algorithm for finding the largest integer among a list of positive
integers
b) The algorithm should find the largest integer among a list of any values
c) The algorithm should be general and not depend on the number
of integers
1. 3 Creating Algorithms Contd…
Solution
Step 2: Initialize X as 0,
Step 3: Increment X by 1,
Step 4: Print X,
Step 6: Stop
1. 3 Creating Algorithms Contd…
Example 3
Convert Temperature from Fahrenheit (℉) to Celsius (℃)
Step 1: Start
Step 4: Print C
Step 5: Stop
1. 3 Creating Algorithms Contd…
Example 4
Algorithm to Add Two Numbers Entered by User
Step 1: Start
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum Step 6: Stop
1. 3 Creating Algorithms Contd…
Write an Algorithm to:
Clear Documentation
Only one flow line should enter a Decision symbol but multiple
Code – Instructions
ACCEPT num1,num2,num3
Sum = num1+num2+num3
Print sum
End Program
1. 4 Writing Pseudocode Contd…
2. Calculate Sum and product of two numbers
Use Variables: sum, product, num1, num2 of type real
ACCEPT num1,num2
Sum = num1+num2
product = num1*num2
End Program
1. 4 Writing Pseudocode Contd…
3. Input examination marks and award grades
Use Variables: mark of type integer
End Program
•History & Evolution of C
C – General Purpose Programming Language
Developed by Dennis Ritchie in 1972
Developed at Bell Laboratories
Principles taken from BCPL and CPL
Structured Programming Language
C Program
Collection of Functions
Supported by C library
• History & Evolution of C Cont…
1960
1967
1970
1972
1978
1989
1990
1999
Why the Name “C” ?
Many of C’s principles and ideas were derived from the earlier language B
BCPL and CPL are the earlier ancestors of B Language (CPL is
• common Programming Language)
In 1967, BCPL Language ( Basic CPL ) was created as a scaled
• down version of CPL
As many of the features were derived from “B” Language the new language was
named as “C”.
Characteristics of ‘C’
Low Level Language Support
Structured Programming
Extensive use of Functions
Efficient use of Pointers
Compactness
Program Portability
Loose Typing
Advantages of C
Compiler based Language
Programming – Easy & Fast
Powerful and Efficient
Portable
Supports Graphics
Supports large number of Operators
Used to Implement Data structures
Disadvantages of C
Not a strongly typed Language
Use of Same operator for multiple purposes
Not Object Oriented
•Structure of ‘C’ Program
Structure based on Set of rules defined by the Compiler
Sections
1) Documentation 5) Local Declaration
2) Preprocessor 6) Program Statements
3) Global Declaration
4) main( ) function
Rules for Writing a C Program
a) All statements should be written in lower case
b) All statements should end with a semicolon
c) Upper case letters are used for symbolic constants
d) Blank spaces can be inserted between words
e) No blank space while declaring a variable, keyword, constant
f) Can write one or more statement in same line separated by
• comma
g) Opening and closing of braces should be balanced
/* Program to Find Area of Circle */ Comm
#include <stdio.h> ent
#include <conio.h>
Preprocessor Directives
a
c
t
u
a
l
1. 12 Scope of Variables Contd…
b) Global Variables
Defined outside a function, usually on top of the program
Hold their values throughout the lifetime of the program
Can be accessed inside any of the functions defined for the
program
Can be accessed by any function
That is, a global variable is available for use throughout
the entire program after its declaration
/* Program for Demonstrating Global Variables*/
#include <stdio.h>
/* global variable declaration */
int g;
int main ( )
{
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10; b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
1. 12 Scope of Variables Contd…
Note: A program can have same name for local and global variables but the
value of local variable inside a function will take preference
12 Datatypes
Defines a variable before use
Specifies the type of data to be stored in variables
Basic Data Types – 4 Classes
a) int – Signed or unsigned number
b) float – Signed or unsigned number having Decimal Point
c) char – A Character in the character Set
Qualifiers
1. 12 Datatypes Contd…
Datatypes Contd…
Datatypes Contd…
a) Integer Data Type
Whole numbers with a range
No fractional parts
Integer variable holds integer values only
Keyword: int
Memory: 2 Bytes (16 bits) or 4 Bytes (32 bits)
Qualifiers: Signed, unsigned, short, long
Examples: 34012, 0, -2457
Datatypes Contd…
b) Floating Point Data Type
Numbers having Fractional part
Float provides precision of 6 digits
Integer variable holds integer values only
Keyword: float
Memory: 4 Bytes (32 bits)
Examples: 5.6, 0.375, 3.14756
Datatypes Contd…
c) Double Data Type
Also handles floating point numbers
Double provides precision of 14 digits
Integer variable holds integer values only
Keyword: float
Memory: 8 Bytes (64 bits) or 10 Bytes (80 bits)
Qualifiers: long, short
1. 12 Datatypes Contd…
d) Character Data Type
handles one character at a time
Keyword: char
Memory: 1 Byte (8 bits)
1. 13 Expressions
Expression : An Expression is a collection of operators
and
operands that represents a specific value
Operator : A symbol which performs tasks like
arithmetic
operations, logical operations and conditional operations
Operands : The values on which the operators perform the task
Expression Types in C
a) Infix Expression
b) Postfix Expression
c) Prefix Expression
1. 13 Expressions Contd…
a) Infix Expression
The operator is used between operands
General Structure : Operand1 Operator Operand2
Example : a + b
b) Postfix Expression
Operator is used after operands
General Structure : Operand1 Operand2 Operator
Example : ab+
1. 13 Expressions Contd…
c) Prefix Expression
Operator is used before operands
General Structure : Operator Operand1 Operand2
Example : +ab
1. 14 Input and Output Functions
Ability to Communicate with Users during execution
Input Operation
Feeding data into program
Data Transfer from Input device to Memory
Output Operation
Getting result from Program
Data Transfer from Memory to Output device
Header File : #include<stdio.h>
1. 14 Input and Output Functions Contd…
Input / Output Function Types
a) Formatted Input / Output Statements
b) Unformatted Input / Output Statements
1. 14 Input and Output Functions Contd…
a) Formatted Input / Output Statements
Reads and writes all types of data values
Arranges data in particular format
Requires Format Specifier to identify Data type
Basic Format Specifiers
%d – Integer
%f – Float
%c – Character
%s - String
1. 14 Input and Output Functions Contd…
i. The scanf ( ) Function
Reads all types of input data
Assignment of value to variable during Runtime
Syntax
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main( ) void main( )
{ {
int a; int a;
a=10; scanf(“%d”,
} &a);
}
1. 14 Input and Output Functions Contd…
/* Getting Multiple Different Inputs
/* Getting Multiple Input using scanf ( )
using scanf ( ) function */
function */
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h> void
void main( )
main( )
{
{
int a, b;
int a, b, c;
float c;
scanf(“%d%d
%d”,&a,&b,&c); scanf(“%
d%d
}
%f”,&a,&
b,&c);
}
1. 14 Input and Output Functions Contd…
/* Getting Multiple Input using scanf ( ) function */
#include<stdio.h>
#include<conio.h> void main( )
{
int a, b; float c;
scanf(“%d %d”, &a,
&b);
scanf(“%f ”, &c);
}
1. 14 Input and Output Functions Contd…
ii. The printf ( ) Function
To print Instructions / Output onto the Screen
Requires Format Specifiers & Variable names to print
data
Syntax printf(“Control String/Format Specifier”,arg1,arg2,… argn)
b) Relational Operators
c) Logical Operators
d) Conditional Operators
e) Increment & Decrement Operators
f) Comma Operator
g) Arrow Operator
h) Assignment Operators
a) Arithematic Operators
Arithematic Expression : An expression
containing arithematic operator in between with two
operands.
/* Program for Arithematic Operations*/
#include<stdio.h>
int main( )
{
int a,b;
printf("Enter the two Values\n");
scanf("%d%d", &a, &b);
printf(“a+b is %d\n“, (a+b));
printf(“a-b is %d\n“, (a-b));
printf(“a*b is %d\n“, (a*b));
printf(“a/b is %d\n“, (a/b));
printf(“a%b is %d\n“, (a%b));
printf(“a^b is %d\n“, (a^b));
return 0;
}
Output
4
2
a + b is 6
a - b is 2
a * b is 8
a / b is 2
a % b is 0
a ^ b is 16
b) Relational Operators
Binary Operators (or) Boolean Operators
Produces an integer result
Condition True : Integer value is 1
Condition False : Integer value is 0
Compares
Values between two variables
Values between variables and constants
1.15 Operators in C Contd…
b) Relational Operators Contd…
Relational Expression / Boolean Expression
: An expression containing a relational
operator
b) Relational Operators Contd…
Consider a = 10 and b =4. The relational expression
returns the following integer values
#include<stdio.h>
int main( )
{
int a,b;
printf("Enter the two Values\n");
scanf("%d%d", &a, &b);
printf(“a>b is %d\n“, (a>b));
printf(“a<b is %d\n“, (a<b));
printf(“a>=b is %d\n“, (a>=b));
printf(“a<=b is %d\n“,
(a<=b)); printf(“a==b is %d\
n“, (a==b)); printf(“a!=b is %d\
n“, (a!=b)); return 0;
}
Output
4
2
a > b is 1
a < b is
0
a > = b is 1
a < = b is
0 a==b
is 0 a ! = b
is 1
1.15 Operators in C Contd…
c) Logical Operators
Combines two or more relations
Used for testing one or more conditions
1.15 Operators in C Contd…
c) Logical Operators Contd…
Logical Expression / Compound Relational Expression :
An expression which combines two or more relational
expression
#include<stdio.h>
int main( )
{
int age,height;
printf("Enter Age of Candidate:\n");
scanf("%d", &age);
printf(“Enter Height of Candidate:\
n“);
scanf("%d", &height);
if ((age>=18) && (height>=5))
printf(“The Candidate is Selected”);
else
printf(“Sorry, Candidate not
Selected”);
return 0;
Output 1
Enter Age of Candidate: 18
Enter Height of Candidate: 6
The Candidate is Selected
Output 2
Enter Age of Candidate: 19
Enter Height of Candidate: 4
Sorry, Candidate not
Selected
1.15 Operators in C Contd…
d) Conditional Operators
? and are the Conditional Operators
Also called as Ternary Operators
Shorter form of if-then-else statement
Syntax Expression 1 ? Expression 2 : expression 3
#include <stdio.h>
int main( )
{
int a, b, c, max;
printf("Enter
three
numbers: ");
scanf("%d%d
%d",&a, &b,
&c);
max = (a > b
&& a > c) ? a :
(b > c) ? b : c;
Output
Enter three numbers: 30 10 40
Maximum between a, b and c = 40
1. 15 Operators in C Contd…
e) Increment and Decrement Operators
Increment and decrement operators are unary operators
that add or subtract one from their operand
C languages feature two versions (pre- and post-) of each
operator
Operator placed before variable (Pre)
Operator placed before variable (Post)
The increment operator is written as ++ and the decrement
operator is written as --
1. 15 Operators in C Contd…
a) Increment and Decrement Operators Contd…
Classification
Pre Increment Operator
Post Increment Operator
Pre Decrement Operator
Post Decrement Operator
1. 15 Operators in C Contd…
a) Increment and Decrement Operators Contd…
Syntax
Output
12345
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Step 1 : In this program, value of i “0” is compared with 5 in while
expression.
Step 2 : Then, value of “i” is incremented from 0 to 1 using post-
increment operator.
Step 3 : Then, this incremented value “1” is assigned to the
variable “i”.
Above 3 steps are continued until while expression becomes false and
output is displayed as “1 2 3 4 5”.
/* Program for Pre Increment*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 1;
while (+
+i<5)
{
}printf(“ }
getch%d”,
( ); i );
}
Output
1234
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Step 1 : In above program, value of “i” is incremented from 0 to 1 using pre-
increment operator.
Step 2 : This incremented value “1” is compared with 5 in while
expression.
Step 3 : Then, this incremented value “1” is assigned to the
variable “i”.
Above 3 steps are continued until while expression becomes false and output
is displayed as “1 2 3 4”.
/* Program for Post Decrement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 1;
while (i--
<5)
{
} printf(“ }
getch%d”, i );
( );
}
Output
98765
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Step 1 : In this program, value of i “10” is compared with 5
in while expression.
Step 2 : Then, value of “i” is decremented from 10 to 9 using post-
decrement operator.
Step 3 : Then, this decremented value “9” is assigned to
the
variable “i”.
Above 3 steps are continued until while expression
becomes false and output is displayed as “9 8 7 6 5”.
/* Program for Pre Decrement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 1;
while (--
i<5)
{
} printf(“ }
getch%d”, i);
( );
}
Output
9876
1. 15 Operators in C Contd…
e) Increment and Decrement Operators Contd…
Step 1 : In above program, value of “i” is decremented from 10 to
9 using pre-decrement operator.
Step 2 : This decremented value “9” is compared with 5 in while
expression.
Step 3 : Then, this decremented value “9” is assigned to
the
variable “i”.
Above 3 steps are continued until while expression becomes false
and output is displayed as “9 8 7 6”.
1. 15 Operators in C Contd…
b) Comma Operator
Special operator which separates the declaration of multiple
variables
Has Lowest Precedence i.e it is having lowest priority so it is
evaluated at last
Returns the value of the rightmost operand when multiple
comma operators are used inside an expression
Acts as Operator in an Expression and as a
Separator while Declaring Variables
1. 15 Operators in C Contd…
f) Comma Operator
#include<stdio.h>
int main( )
{
int i, j;
i=(j=10, j+20);
printf(“i =
%d\n j = %d\
n” , i,j );
return 0;
1. 15 Operators in C Contd…
g) Arrow Operator (->)
Arrow operator is used to access the structure
members when we use pointer variable to access it
When pointer to a structure is used then arrow operator is
used
1. 15 Operators in C Contd…
h) Assignment Operators
Assigns result of expression to a variable
Performs Arithmetic and Assignment operations
Commonly used Assignment operator: =
Syntax variable = expression;
Examples
num = 25; age = 18; pi = 31.4; area = 3.14 * r * r;
1. 15 Operators in C Contd…
Shorthand Assignment Operators
Simple Assignment
Shorthand Operator
Operator
a=a+1 a+=1
a=a–1 a-=1
a=a*2 a*=2
a=a/b a/=b
a=a%b a%=b
c = c * (a + b) c *= (a + b)
b = b / (a + b) b /=(a + b)
/* Program for Assignment Operations*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
a = 11;
a+ = 4;
printf(“Value of A is %d\n”,a);
a = 11;
a- = 4;
printf(“Value of A is %d\n”,a);
a = 11;
a* = 4;
printf(“Value of A is %d\n”,a);
a = 11;
a/ = 4;
printf(“Value of A is %d\n”,a);
a = 11;
a% = 4;
printf(“Value of A is %d\n”,a);
getch ( );
}
Output
Value of A
is 15
Value of A
is 7
Value of A
is 44
Value of A
is 2
1. 16 Single Line and Multiline Comments Contd…
a) Single Line Comment
Represented by double slash \\
#include<stdio.h>
int main( ){
//printing information
printf("Hello C");
return 0;
}
1. 16 Single Line and Multiline Comments Contd…
b) Multi-Line Comment
Represented by slash asterisk \* ... *\
#include<stdio.h>
int main( ){
/*printing information
Multi Line Comment*/
printf("Hello C");
return 0;
}
1. 16 Single Line and Multiline Comments
Comment – Definition
Used to provide information about lines of code
Provide clarity to the C source code
Allows others to better understand what the code was
intended to
Helps in debugging the code
Important in large projects containing
hundreds or
thousands of lines of source code
Types – Single line and multiline comment
1. 16 Single Line and Multiline Comments Contd…
Single-Line Comments Multi-Line Comment
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=++i;
printf("x:
%d",x);
printf("i: %d",i);
return 0;
}
Output
x: 11
i: 11
/* Expresssions using Pre-Decrement Operator*/
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=--i;
printf("x:
%d",x);
printf("i: %d",i);
return 0;
}
Output
x: 9
/* Expresssions using Post-Increment Operator*/
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=i++;
printf("x:
%d",x);
printf("i: %d",i);
return 0;
}
Output
x: 10
i: 11
/* Expresssions using Post-Decrement Operator*/
#include<stdio.h>
int main( )
{
int x,i;
i=10;
x=i--;
printf("x:
%d",x);
printf("i: %d",i);
return 0;
}
Output
x: 10
i: 9
/* Expresssions using Increment / Decrement Operators*/
#include<stdio.h>
int main( )
{
int
p,q,x,y;
printf(“E
nter the
value of x
\n”);
scanf(“%
d” ,&x);
printf(“Enter the value of y \n”);
scanf(“%d” ,&y);
printf(“x=%d\ny=%d\n”,x,y);
p=x++;
q=y++;
printf(“x=%d\ty=%d\n”,x,y);
printf(“x=%d\tq=%d\n”,p,q);
p=--x;
q=--y;
Output
Enter the value of x 10
Enter the value of y
20 x = 10
y = 20
x = 11 y = 21
p = 10 q = 20
x = 10 y = 20
p = 10 q = 20
1.19 Expressions using Conditional Operator
Any operator is used on three operands or variable is known
as Ternary Operator
It can be represented with ? : . It is also called as conditional
operator
/* Program for Printing Odd or Even Number*/
#include<stdio.h>
int main( )
{
int num;
printf("Enter the
Number : ");
scanf("%d",&num); (num%2==0)?
printf("Even\n"):printf("Odd");
}
Output
Enter the Number : 10
Even
/* Program for Eligibility to Vote*/
#include<stdio.h>
int main( )
{
int age;
printf(" Please Enter your age here: \n ");
scanf(" %d ", &age);
(age >= 18) ? printf(" You are eligible to
Vote ") : printf(" You are not
e
l
i
g
i
b
l
e
/* Program for Finding Biggest of 2 Numbers*/
#include<stdio.h>
int main( )
{
int a, b, max;
printf("Enter a and b: ");
scanf("%d%d", &a, &b);
max = a > b ? a : b;
printf("Largest of the
two numbers = %d\n",
max);
return 0;
Output
}
Enter a and b: 10 20
Largest of the two numbers = 20
1.20 Expressions using Assignment Operator
Assignment Operator is used to assign value to an variable
Assignment Operator is denoted by equal to sign
Assignment Operator is binary operator which operates on two
operands
Assignment Operator have Two Values – L-Value and R-Value
Operator = copies R-Value into L-Value
Assignment Operator have lower precedence than all available
operators but has higher precedence than comma Operator
1.21 L-Value and R-Value of Expression
a) L-Value stands for left value
L-Value of Expressions refer to a memory locations
In any assignment statement L-Value of Expression must be a
container(i.e. must have ability to hold the data)
Variable is the only container in C programming thus L Value
must be any Variable.
L Value cannot be a Constant, Function or any of the available
data type in C
1.21 L-Value and R-Value of Expression Contd…
Diagram Showing L-Value of Expression :
1.21 L-Value and R-Value of Expression Contd…