UNIT-1
UNIT-1
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
21CSS101J – Programming for Problem Solving
Unit I
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
COURSE LEARNING
RATIONALE The purpose of learning this
(CLR) course is to:
Think and evolve a logically to construct an algorithm into a flowchart
CLR -1: and a pseudocode that can be programmed
Utilize the appropriate operators and control statements to solve
CLR -2: problems in engineering
CLR -3: Store and retrieve data in a single and multidimensional array
Create custom designed functions to perform tasks in any application
CLR -4:
Create b a s i c A b s t r a c t Da t a Ty p e s w i t h p y t h o n
CLR -5:
Create aapplications using suitable python library functions for solving data
CLR -6: science problems
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
COURSE
LEARNING At the end of this course, learners will be able
OUTCOMES to:
(CLO)
T o solve a problem through computer programming. List the basic
CLO - data types and variables in C
1:
To use appropriate data types in simple data processing applications. To create
CLO - programs using the concept of arrays.
2:
To create string processing applications with single and multi-
CLO - dimensional arrays.
3:
To Create user defined functions with required operations. To implement
CLO - pointers in application with dynamic memory requirements.
4:
To Create programs using the python data types, loops, control statements
CLO -
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
LEARNING RESOURCES
S. No TEXT BOOKS
4. http://www.c4learn.com/learn-c-programming-language/
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
UNIT I
INTRODUCTION
Evolution of Programming & Languages - Problem Solving
through Programming - Creating Algorithms - Drawing
Flowcharts - Writing Pseudocode - Evolution of C language, its
usage history - Input and output functions: Printf and scanf -
Variables and identifiers – Expressions - Single line and
multiline comments - Constants, Keywords - Values, Names,
Scope, Binding, Storage Classes - Numeric Data types: integer -
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
UNIT I
INTRODUCTION
floating point - Non-Numeric Data types: char and string -
Increment and decrement operator - Comma, Arrow and
Assignment operator - Bitwise and Sizeof operator
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 3 Creating Algorithms
An informal definition of an algorithm is:
i
Algorithm: a step-by-step method for solving a
problem or doing a task.
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 3 Creating Algorithms
Notations
Starting point
Step Numbers – Positions in Algorithm
Incoming Information - Input
Control Flow – Order of evaluating Instructions
Statements
Outgoing Information - Output
Ending Point
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
Step 2: Initialize X as 0,
Step 3: Increment X by 1,
Step 4: Print X,
Step 6: Stop
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
Step 4: Print C
Step 5: Stop
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum Step 6: Stop
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 3 Drawing Flowcharts
Diagrammatic representation
Clear Documentation
Only one flow line should enter a Decision symbol but multiple
1. 4 Writing Pseudocode
Pseudo – Imitation / False
Code – Instructions
Evolution of C
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 5 History & Evolution of C Cont…
Why the Name “C” was given ?
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”.
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 5 History & Evolution of C Cont…
Characteristics of ‘C’
Low Level Language Support
Structured Programming
Extensive use of Functions
Efficient use of Pointers
Compactness
Program Portability
Loose Typing
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 5 History & Evolution of C Cont…
Advantages of C
Compiler based Language
Programming – Easy & Fast
Powerful and Efficient
Portable
Supports Graphics
Supports large number of Operators
Used to Implement Datastructures
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 5 History & Evolution of C Cont…
Disadvantages of C
Not a strongly typed Language
Use of Same operator for multiple purposes
Not Object Oriented
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 6 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
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 6 Structure /*
ofProgram
‘C’ Program Contd…
to Find Area of Circle */ Commen
#include <stdio.h> t
#include <conio.h>
Preprocessor
Directives
const float pi = 3.14;
Global
void main( )
{ Declaration main
float area; Local Declaration &
Function
int r; Initialization
printf(“Enter the Radius of the Circle”);
scanf(“%d”, &r); Executio
area = pi * r * r; n
printf(“The area of the Circle is %f”, area);
getch( );
}
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 8 C Programming Fundamentals
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
C Token - Smallest
individual unit of a C
program
C program broken into
many C tokens
Building Blocks of C
program
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 9 Keywords
Keywords – Conveys special meaning to Compiler
Cannot be used as variable names
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 10 Constants
Definition :Value does not change during execution
Can be a Number (or) a Letter
Types
Integer Constants
Real Constants
Character Constant
Single Character Constants
String Constants
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 10 Constants Contd…
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
a
c
t
u
a
l
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
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;
}
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
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
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 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) double – Double Precision Floating point number
d) char – A Character in the character Set
Qualifiers
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 12 Datatypes Contd…
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 12 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
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 12 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
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 12 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
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 12 Datatypes Contd…
d) Character Data Type
handles one character at a time
Keyword: char
Memory: 1 Byte (8 bits)
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
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
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
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+
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 13 Expressions Contd…
c) Prefix Expression
Operator is used before operands
General Structure : Operator Operand1 Operand2
Example : +ab
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
#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);
}
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
#include<stdio.h>
int main( ){
//printing information
printf("Hello C");
return 0;
}
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
#include<stdio.h>
int main( ){
/*printing information
Multi Line Comment*/
printf("Hello C");
return 0;
}
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 15 Operators in C
C supports rich set of built in Operators
Used to manipulate Constants (Data) & Variables
Part of Mathematical (or) Logical expressions
Operators vs Operands
Operator – Definition
Symbol (or) Special character that instructs the compiler
to perform mathematical (or) Logical operations
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 15 Operators in C Contd…
Classification of Operators
a) Increment & Decrement Operators
b) Comma Operator
c) Arrow Operator
d) Assignment Operators
e) Bitwise Operators
f) Sizeof Operator
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 15 Operators in C Contd…
a) 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 after variable (Post)
The increment operator is written as ++ and the decrement
operator is written as --
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
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
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 15 Operators in C Contd…
a) Increment and Decrement Operators Contd…
Syntax
(pre)++variable_name; (pre)- -variable_name;
(Or)
variable_name++ (post); variable_name – (Post);
Examples
++count, ++a, ++i, +
+count
Count++, a++, i++, count++
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 15 Operators in C Contd…
a)NoIncrement
S. and
Operator Decrement
type Operators Contd…
Operator Description
Value of i is incremented before
1 Pre Increment ++i
assigning it to variable i.
Value of i is incremented after
2 Post Increment i++
assigning it to variable i.
Value of i is decremented before
3 Pre Decrement -- i
assigning it to variable i.
Post Value of i is decremented after
4 i --
Decrement assigning it to variable i.
/* Program for Post Increment*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 0;
while (i+ + < 5)
{
printf(“%d
} ”, i); }
getch
( );
}
Output
12345
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 15 Operators in C Contd…
a) 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 = 0;
while (+
+i< = 5)
{
} printf(“%d}
getch ”, i );
( );
}
Output
12345
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 15 Operators in C Contd…
a) 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 = 10;
while (i--> 5)
{
printf(“%d
} ”, i ); }
getch
( );
}
Output
98765
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 15 Operators in C Contd…
a) 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 = 10;
while (--i> 5)
{
printf(“%d
} ”, i); }
getch
( );
}
Output
9876
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 15 Operators in C Contd…
a) 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”.
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
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
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 15 Operators in C Contd…
b) Comma Operator Contd…
#include<stdio.h>
int main( )
{
int i, j;
i=(j=10, j+20);
printf(“i =
%d\n j = %d\
n” , i,j );
return 0;
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
1. 15 Operators in C Contd…
c) 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
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
1. 15 Operators in C Contd…
d) 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;
SR
INSTITUTE M
OF SCIENCE AND
TECHNOLOGY,
CHENNAI.
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
Value of A is
3
FAHRENHEIT TO CELSIUS CONVERSION
#include<stdio.h>
#include<conio.h>
void main()
{
float farenheit, celcius,x;
printf("Enter the degrees in farenheit:\n");
scanf("%f", &farenheit);
x=farenheit-32;
celsius = x/1.8;
printf("\nThe Celsius Value is %6.2f C", celsius);
getch();
}
LARGEST OF THREE NUMBERS
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf(“Enter the three numbers: “);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b&&a>c)
printf(“The biggest number is: %d”,a);
else if(b>a&&b>c)
printf(“The biggest number is: %d”,b);
else
printf(“The biggest number is: %d”,c);
}
Q1-You have a certain number of 100 rupee notes, 10 rupee notes and 1 rupee notes
with you.
There is an item you want to buy whose price is given to you.
Write a program to find if the item is affordable, that is the price of the item is less
than or equal to the current money you have.
Input
Four non negative integers.
The first input is an integer representing the number of 100 rupee notes.
The second input is an integer representing the number of 10 rupee notes.
The third input is an integer representing the number of 1 rupee notes.
The fourth input is an integer representing the price of the item.
Output
You have to output 1 if the item is affordable.
You have to output 0 if the item is not affordable.
#include <stdio.h>
int main()
{
int hundreds, tens, ones;
int price;
int money;
scanf("%d", &hundreds);
scanf("%d", &tens);
scanf("%d", &ones);
scanf("%d", &price);
money=hundreds*100+tens*10+ones;
if (price <= money){
printf("1");
}
else{
printf("0");
return 0;
}
}
Q2- Given three distinct integers a b and c, write a C program to find the second
largest number among them.
Input
Three distinct integers a b c.
The first input is the integer a.
The second input is the integer b.
The third input is is the integer c.
Output
The second largest among a, b and c
#include <stdio.h>
int main()
{
int a, b,c;
scanf("%d%d%d", &a,&b,&c);
if(a>=b && a>=c)
{
if(b>=c)
printf("%d",b);
else
printf("%d", c);
}
else if(b>=a && b>=c)
{
if (a>=c)
printf("%d",a);
else
printf("%d", c);
}
else
{
if(b>=a)
printf("%d",b);
else
printf("%d",a);
}
return 0;
}
THANK
YOU