Programming For Problem Solving: (ES101CS)
Programming For Problem Solving: (ES101CS)
Text Books:
T1: Computer Science A structured programming approach
using C, Behrouz Forouzan and Richard F. Gilberg ,
Cengage Learning , 2007 ,Third Edition (Unit 1-5)
Programming for Problem Solving T2: Schaum's Outline of Programming with C, Byron
Gottfried, McGraw-Hill 2019, Fourth Edition (Unit 1-5)
(ES101CS)
T3: Data Structures and Program Design in C, Robert Kruse,
N Srikanth Prasad Bruce Leung, Tondo, Pearson, II Edition
[email protected]
+91 – 93 9104 9134 References/ Suggested Reading
R1. C Programming Language, Brian W Kenningham, Dennis
M Ritchie, Pearson, II Edition
R2. How to solve it by Computer, R G Dromey, Pearson
Edition
1 2
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
3 4
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
• The C programming language originated in the early 1970s at Bell • 1973: The Unix operating system kernel is largely rewritten
Labs, developed by Dennis Ritchie. It evolved from earlier in C, demonstrating C's power and efficiency for system
languages like ALGOL 60, BCPL (Basic Combined Programming programming.
Language), and B. • 1978: Brian Kernighan and Dennis Ritchie publish "The C
Programming Language" (K&R C), which becomes the de
• 1960s: ALGOL 60 introduces the concept of block structure,
influencing future languages. Martin Richards develops BCPL in facto standard for the language.
1966 for system programming. • 1989: The American National Standards Institute (ANSI)
• 1970: Ken Thompson creates 'B,' a simplified version of BCPL, standardizes C, leading to ANSI C, enhancing portability and
used for early Unix development. consistency.
• 1990:The International Organization for Standardization
• 1972: Dennis Ritchie develops C at Bell Labs, building upon
BCPL and B. (ISO) adopts the ANSI C standard, creating ISO C.
5 6
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
1
08-09-2025
• 1991: Linus Torvalds begins developing the Linux kernel • 2020s: C continues to be a foundational language for
using C, further solidifying C's role in operating system system programming, embedded systems, operating
development.
systems (like Linux and parts of Windows), and
• 1999: The C99 standard is published by ISO, introducing high-performance computing, with ongoing
new features like variable-length arrays, long long int, and maintenance and minor updates to the standard as
improved floating-point support. needed. While newer languages emerge, C's efficiency,
control over hardware, and vast existing codebase
• 2011: The C11 standard is released, adding features such as ensure its continued relevance through 2025 and
multi-threading support, atomic operations, and generic
selections. beyond.
Features
Preprocessor # include <stdio.h> /*printf, scanf definitions*/
directive # define KMS_PER_MILE 1.609 /*printf, scanf definitions*/
• Efficiency and Speed: C provides low-level memory access and direct reserved words
constant
hardware manipulation, leading to highly efficient and fast programs. int
main(void)
• Portability: C programs can be compiled and run on various hardware {
platforms and operating systems with minimal modifications.
double miles, /*distance in mile*/
• Structured Programming: It supports structured programming variable kms; /*equivalent distance in kilometer*/
constructs like functions, loops, and conditional statements, promoting
/* Get the distance in miles. */ comment
modular and organized code.
printf("Enter the distance in miles> ");
• Rich Library: C has a rich set of built-in functions and libraries that standard
identifier scanf("%lf", &miles);
simplify common programming tasks.
• Memory Management: It provides features for dynamic memory /* Convert the distance to kilometers. */
allocation and deallocation, giving programmers fine-grained control
over memory. kms = KMS_PER_MILE * miles;
special symbol
9 10
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
reserved word return (0); punctuation Preprocessor # include <stdio.h> /*printf, scanf definitions*/
directive # define KMS_PER_MILE 1.609 /*conversion constant*/
} special symbol reserved words
constant
int
main(void)
{
double miles, /*distance in mile*/
variable kms; /*equivalent distance in kilometer*/
/* Get the distance in miles. */ comment
printf("Enter the distance in miles> ");
standard
identifier scanf("%lf", &miles);
11 12
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
2
08-09-2025
15 16
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
1. Problem: 2. Analysis:
Your summer surveying job requires you to study some 1. Problem Input: miles
maps that give distances in kilometers and some that use 2. Problem Output: kilometers
miles.You and your coworkers prefer to deal in metric
measurements. Write a programthat performs the 3. Relevant Formula: 1 mile = 1.609 kilometers
necessary conversion.
17 18
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
3
08-09-2025
4. Implementation:
3. Design / Algorithm:
1. Get the distance in miles.
2. Convert the distance to kilometers.
(1 kilometer = 1.609 miles)
3. Display the distance in kilometers.
19 20
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
5. Testing: 6. Maintenance :
1. Verify that the program works properly. 1. Remove undetected errors.
2. Try few test cases. 2. Keep it up-to-date.
21 22
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
Variables Variables
Variables are used to store the value during the execution of There are set of rules to be followed while declaring
variables and data types in C Programming:
a program.
• The 1st letter should be alphabet.
In C Programming we always have to declare variable
• Variables can be combination of alphabets and digits.
before we can use them. Note that the space is allocated to
• Underscore (_) is the only special character allowed.
variable in memory during execution or run-time.
• Variables can be written in both Uppercase and Lowercase or
C is a strongly typed language. What this means it that, the
combination of both.
type of a variable cannot be changed. Suppose we
• Variables are Case Sensitive.
declared an integer type variable so we cannot store
• No Spaces allowed between Characters.
character or a decimal number in that variable.
• Variable name should not make use to the C Reserved
Keywords.
• Variable name should not start with a number.
23 24
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
4
08-09-2025
Variables Variables
Use variables to store the values in the algorithm.
Variables vs.Constants . Choose good names for the variables:
Algorithm without variables: • No need to write any comment
1. Get the distance in miles. • Easy to track
2. Convert the distance to kilometers. • Case sensitive
(l kilDmeter = 1.609 miles) • Don't use a Reserved Word
3.Display the distance The algorithm with good variables' names:
Algorithm with variables:
1. Get the value X (X: the distance in miles) 1. Get the value TotalMiles
2. Y= X / 1.609 (Y: the distance in kilo) 2. TotalKilo = TotalMiles/1.609
3. Display the value Y
3. Display the value Totalkilo
25 26
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
5. Y =Y+1; 6. Z = Z/2 + 5; 5. 30
7. X = Z % 3; 6. 20
6. Z = Y-1.5+5; 7. 2
7. 23 = Z; FaIse; Why! What are the values of (X, Y, Z) in the memory after
8. Z = Z/2 + 5; executing the program?
27 28
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
Data Types
29 30
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
5
08-09-2025
3.Derived Data Types: These are those data types that are derived from the
other basic data types in C. Some common examples of the same
are Arrays (i.e., a collection of elements having the same data type stored at
contiguous memory locations), Pointers (that store address to a memory location
that holds some particular value), Structure (struct, i.e., a user-defined
composite datatype containing fields each having different datatypes), and
Union contains set of variables sharing common storage area.
31 32
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
Memory Size
Data Types In C Format Specifier Range
Primary Data Types In C (in bytes)
unsigned short int %hu 0 to 65,535 2
short int %hd -32,768 to 32,767 2
usigned int %u 0 to 4,294,967,295 4
-2,147,483,648 to
Int %d 4
2,147,483,647
-2,147,483,648 to
long int %ld 4
2,147,483,647
unsigned long int %lu 0 to 4,294,967,295 4
long long int %lld -(2^63) to (2^63)-1 8
unsigned long long int %llu 0 to 8
18,446,744,073,709,551,615
signed char
%c -128 to 127 1
33 34
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
}
Output:
Minimum limit for Integer data stores: -2147483648
35 36
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
6
08-09-2025
37
The output from this program would be This is my float: 3.14. 38
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
7
08-09-2025
• C Programming Language support several operators to • Arithmetic C operators are used to perform
perform different operations. These operators in c mathematical operations such as addition, subtraction,
programming are mostly in the form of symbols which used
multiplication and division on numerical values
to perform logical and arithmetic operations. These operators
generally work on many types of variables and constants (variables and constants).
although some are restricted to work on specific types.
Operators Operations Example
+ Addition a+b
– Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus a%b
43 44
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade
int main()
{
int a = 6, b = 4, c;
c = a+b;
c=a/b;
printf("a+b = %d \n",c);
printf("a/b = %d \n",c);
c = a-b;
c=a%b;
printf("a-b = %d \n",c); printf("Remainder when a divided by b = %d \n",c);
c = a*b; return 0;
printf("a*b = %d \n",c); }
45
MET HODIST COLLEGE OF ENGINEERING AND T ECHNOLOGY (AN AUT ONOMOUS INST IT UT ION) Accredited by NBA & NAAC with A+ Grade