Professional Documents
Culture Documents
Introduction Unit-1
Introduction Unit-1
Introduction Unit-1
C PROGRAMMING
(21CS107)
COURSE DESCRIPTION AND OBJECTIVE
No. Outcome
CO 1 Understanding of how to write simple, but complete, C programs
Identification of suitable data type for operands and design of expressions
CO 2
having right precedence.
Application of decision making and iterative features of C Programming
CO 3
language effectively.
CO 4 Selection of problem specific data structures and suitable accessing methods.
Design and development of non- recursive and recursive functions and their
CO 5
usage to build large modular programs.
Development of C programs that are understandable, debuggable,
CO 6
maintainable and more likely to work correctly in the first attempt.
Input Processing and Output
Input, processing and output
To design a solution, three major requirements are
there:
• Receive Input
• Process the data
• Produce output
Input, processing and output
• Input: Study the problem and identify the pieces of data.
• Process: Determine the calculations and/or other processes that must be
performed.
• Output: It will produce the results of the program’s calculations and/or other
processes.
The next step is writing the algorithm/pseudocode, or drawing it as a flowchart
Problem 1: Check the attendance percentage
1. Place the kettle under the tap. 6. Place the kettle on the burner
2. Turn on the tap. 7. Turn - on the burner.
3. Check if 90% of the kettle is filled. 8. Check if the water is 100 C
4. If not, repeat the above step. 9. If not, repeat the above step.
5. Turn off the tap. 10. Turn off the burner.
Problem 3: Check whether a person is
eligible to cast a vote or not
• Input: Person Age
• Process: Identify whether the person is Eligible to cast
a vote or not
• Output: Eligible or not
Problem 3: Check whether a person is eligible
to cast a Vote or not
1. Read Age
2. Check if age is greater than 18 then go to step
3 otherwise go to step 4
3. Display " Person is eligible to cast a vote"
4. Display "person is not eligible to cast a vote "
Problem 4: Convert Temperature
From Celsius To Fahrenheit
Clear and
Well Defined
Unambiguous
Output
Characteristics
Language Finiteness
Independent
Feasible
CHARACTERISTICS OF ALGORITHM
• Feasible: The algorithm must be simple, generic and practical, such that it can be executed
upon will the available resources. It must not contain some future technology, or anything.
• Clear & Unambiguous: Algorithm should be clear and unambiguous. Each of its steps (or
phases), and their inputs/outputs should be clear and must lead to only one meaning.
Types of Algorithms
1. Sequence
2. Selection
3. Repetition
Types of Algorithms
Sequence: Begin
Third Task
Done
Selection:
Based upon the condition statements will execute.
Statement is executed when the condition is true and ignored
when it is false e.g if, if else, switch structures.
true
Condition
false
Statement(s)
rest of Code
Repetition:
• In this structure the statements are
executed more than one time. It is also
known as iteration or loop Initialization
true
Loop Body
Update
Design a solution for performing addition on two numbers
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result
to sum. ( sum←num1+num2 )
Step 5: Display sum
Step 6: Stop
I want to Cook Rice in electric rice cooker
Step 1: Start
Step 2: Read number of units consumed
Step 3: Calculate bill amount based on slab rate
(b= u * slab rate)
Step 4: Generate the bill.
Step 5: Stop.
Your grand father is seeking help in withdraw
cash from ATM
Parallelograms
Ovals Input symbols
Terminal
Symbols
Processing
Symbol
Parallelograms
Output symbols
Types of Flowcharts
Sequential
flowchart
Selection
flowchart
Iterative
flowchart
Sequence Structure
Pseudocode1:
Display “Welcome to the Course”
Draw a flowchart to display a message
Start
Stop
Suppose your instructor tells you to write a pseudocode
program that displays your Name and address on the
computer screen.
• Task: Display
Pseudocode:
Display “Ravi”
Display “Guntur”
Draw a flowchart to display Name and address
Start
Display Ravi
Display Guntur
Stop
Input, Output and Variables
Stop
Your little sister wants to add two numbers
Start
Sum=0
Input n1 and n2
Sum = n1+n2
Display Sum
Stop
Calculate the area of a Triangle
Stop
Calculate Simple Interest
Start
Input
P,T,R
I=
(P*T*R)/100.
Print value of
I
Stop
Calculate sales price of an item after discount
Input op
Set sp = op-discount
Display sp
Stop
IDENTIFY THE DATA TYPES
Calculate the Average of three tests
Process 1
Process Y Process x
False
Condition
True
Calculate the sum of first 50 numbers.
• Input: 1 to 50
• Output: Sum
PSEUDOCODE:
1. Declare Integer sum, N
2. Set sum=0, N=0
3. N=N+1
4. Sum=Sum+N
5. If N is less than or equal to 50 then goto step 3,
otherwise goto step 6
6. Display “Sum of first 50 numbers:”
7. Output sum
Flowchart for calculating the sum of first 50 numbers.
Start
Sum=0
N=0
N=N+1
Sum=Sum+ N
No
while
N>=50?
Yes
Print Sum
Stop
Decision Structure
True False
Decision
Process 1
Process x
Calculating the bonus.
• Input: salary
• Output: Bonus
PSEUDOCODE:
1. Declare Integer sal
2. Declare Real bonus
3. If sal is greater than or equal to 5000 then goto
step 4, otherwise goto step 5
4. Set bonus = 0.05*sal
5. Set bonus = 250
6. Display “Bonus:”
7. Output bonus
Flowchart for calculating the bonus.
Start
Input Salary
False True
Is Salary>=5000
Bonus=250 Bonus=0.05*Salary
Print
bonus
Stop
GENERATIONS OF PROGRAMMING LANGUAGES
There are five generation of Programming languages. They are:
First Generation Languages:
These are low-level languages like machine language.
Second Generation Languages:
These are low-level assembly languages used in kernels and hardware drives.
Third Generation Languages:
These are high-level languages like C, C++, Java, Visual Basic and JavaScript.
Fourth Generation Languages:
These are languages that consist of statements that are similar to statements in the
human language. These are used mainly in database programming and scripting.
Example of these languages include Perl, Python, Ruby, SQL, MatLab(MatrixLaboratory).
CONT..
Fifth Generation Languages:
These are the programming languages that have visual tools to develop a
program.
Examples of fifth generation language include Mercury, OPS5, and Prolog.
The first two generations are called low level languages. The next three
generations are called high level languages.
IMPORTANCE OF C
• C programming language is most popular programming language.
• C was created in 1972 by Dennis Ritchie at the Bell Labs in USA as a
part of UNIX operating system.
• C was also used to develop some parts of this operating system.
FEATURES OF C
• Programs Written in C are efficient and fast.
• C is highly portable this means that programs once written can be run on
another machines.
• Another important feature of C program is its ability to extend itself.
• A C program is basically a collection of functions that are supported by C
library. We can also create our own function and add it to C library.
• C language is the most widely used language.
STRUCTURE OF C PROGRAM
• A C program involves the following sections:
• Documentations (Documentation Section)
• Pre-processor Statements (Link Section)
• Global Declarations (Definition Section)
• The main() function
• Local Declarations
• Program Statements & Expressions
• User Defined Functions
DOCUMENTATION SECTION / COMMENTS –
• They are two types of comments:
• Single – line comment.
• Multi – line comment.
}
Output:
Hello World!
COMPILE AND EXECUTE A C PROGRAM
• Open a text editor and add the above mentioned code.
• Save the file as hello.c.
• Open command prompt and go to the directory where we have saved the file.
• Type cc file_name.c and press enter to compile our code.
• If there are no errors in our code, the command prompt will take you to the next line and
would generate a.out executable file.
• Now type ./a.out to execute our program.
• Finally we will get output.
• $cc hello.c
• $./a.out
• Hello world!