Introduction to c Programming 1
Introduction to c Programming 1
C PROGRAMMING
ALGORITHM
A programming algorithm is a procedure or formula used for
solving a problem. It is based on conducting a sequence of specified
actions in which these actions describe how to do something, and your
computer will do it exactly that way every time.
Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:
1.START
2.Declare 3 integer variables num1, num2, and num3.
3.Take the three numbers, to be added, as inputs in variables num1, num2,
and num3 respectively.
4.Declare an integer variable sum to store the resultant sum of the 3
numbers.
5.Add the 3 numbers and store the result in the variable sum.
6.Print the value of the variable sum
7.END
ALGORITHM REPRESENTATION
Algorithm representation refers to the different ways of expressing an
algorithm to make it understandable and executable. There are three
main representations:
1. Descriptive Representation
2. Natural Language Representation
3. Pseudocode Representation
4. Flow Chart Representation
Descriptive Representation
Provides a detailed explanation of each step, including reasoning
and constraints.
1.Start.
2.Ask the user to enter two numbers.
3.Add the two numbers.
4.Display the sum.
5.Stop.
Pseudocode Representation
Uses a structured and code-like format to express the algorithm
without specific programming syntax.
Pseudocode Syntax
Pseudocode uses a structured, high-level format that resembles
programming logic but is not bound to any specific programming
language.
Pseudocode Syntax
Start and Stop
- Used to define the beginning and end of the algorithm.
“START” and ”STOP”
Arithmetic Operations
- Used on arithmetic operations
Operation Symbol Representation
Addition + sum ← num1 + num2
Subtraction - difference ← num1 - num2
Multiplication * product ← num1 * num2
Division / quotient ← num1 / num2
Modulus (Remainder) MOD remainder ← num1 MOD num2
Variable Assignment
- Used to define the beginning and end of the algorithm.
“(← or =)”
“sum ← num1 + num2 or sum = num 1+ num2”
Repeat-Until Loop
“REPEAT
num ← num + 1
PRINT num
UNTIL num = 10”
EXAMPLE: Find the Sum of Two Numbers
1. Start
2.Input num1, num2
3. sum ← num1 + num2
4. Print sum
5. Stop
Flowchart
Flowcharts are the visual representations of an algorithm or a
process. Flowcharts use symbols/shapes like arrows, rectangles, and
diamonds to properly explain the sequence of steps involved in the
algorithm or process.
The oval symbol indicates Start, Stop and Halt in a program’s logic flow. A
pause/halt is generally used in a program logic under some error conditions. Terminal is the
first and last symbols in the flowchart.
2. Input/Output
3. Action/Process
Diamond symbol represents a decision point. Decision based operations such as yes/no
question or true/false are indicated by diamond in flowchart.
5. On-Page Connector/Reference
Whenever flowchart becomes complex or it spreads over more than one page, it is useful to use
connectors to avoid any confusions. connectors are used to indicate a jump from one part of the
flowchart to another without drawing long or complicated lines. Off-Page Connector is
represented by a pentagon.
7. Flow lines
Flow lines indicate the exact sequence in which instructions are executed. Arrows represent the
direction of flow of control and relationship among different symbols of flowchart.
8. Document
Symbolizes a report or document or display, shown as a rectangle with a wavy
bottom. Used when the process involves creating, reading, or outputting a document.
9. Preparation
Represents an initialization or preparation step before execution begins. sed for
setting up initial conditions, declaring variables, or preparing values before a process starts.
OTHER SYMBOLS
Rules For Creating a Flowchart
A flowchart is a graphical representation of an algorithm. It should follow some
rules while creating a flowchart
Rule 1: Flowchart opening statement must be ‘start’ keyword.
Rule 3: All symbols in the flowchart must be connected with an arrow line.
Rule 4: Each decision point should have two or more distinct outcomes.
Rule 5: Flow should generally move from top to bottom or left to right.
Example of a Flowchart
Draw a flowchart to input two numbers from the user and display
the largest of two numbers.
Start: The process begins with the Start symbol,
indicating the start of the program.
Pseudocode
1. Start
2. Declare area, length and width
3. Input length and width values
4. Calculate the area, area = length * width
5. Display Area
6. Stop
Flowchart
Write an algorithm for Calculating area and
circumference of circle
Pseudocode
1. Start
2. Declare variables radius, area, circumference
3. Define constant as PI = 3.14159
4. Input the radius of the circle
5. Calculate the area, area = PI * radius * radius
7. Calculate the circumference, circumference = 2 * PI * radius
8. Display the calculated area and circumference
8. End
Flowchart
Write an algorithm to check the least of two
numbers?
Pseudocode
1. Start
2. Declare variables of num1, num2, least
3. Input from the user to enter two numbers num1 and num2
4. Compare the two values
- If
num1 < num2 (least = num 1)
- Else
(least = num2)
5. Display the least number
6. End
Flowchart
GROUP ACTIVITY OF
PSEUDOCODE AND
FLOWCHART
1. Print the largest number among three numbers. Let the three
numbers be represented by A, B, and C.
Onlinе IDEs offer a convenient option for those who choose not to
set up a local environment.
GeeksforGeeks
https://ide.geeksforgeeks.org/ide/online-cpp14-compiler
Programiz C Compiler
https://www.programiz.com/c-programming/online-compiler/
Jdoodle
https://www.jdoodle.com/c-online-compiler
SETTING UP A LOCAL ENVIRONMЕNT
Must secured and installed a text editor and saved your program
with a '. c' extension and is acquiring a C compiler. This compiler is
responsible for translating your high-level C code into a machine-
understandable low-level language. In other words, we can say that it
converts the source code written in a programming language into another
computer language that the computer understands.
TEXT EDITOR
Codе::Blocks Installation
• Download Code::Blocks by selecting the setup package based on your OS
from this link.
• Open the downloaded Code::Blocks setup file.
• Follow the on-screen instructions for installation.
• After successfully installing Code::Blocks, open the application.
• Navigate to the File menu.
• Select "New" and choose "Empty file."
• In the newly created empty file, write your C program.
• Save the file with a '.c' extension.
• Go to the Build menu in Code::Blocks.
• Choose the "Build and Run" option.
After the above discussion, we can formally assess the basic structure of a C
program. By structure, it is meant that any program can be written in this
structure only. Writing a C program in any other structure will lead to a
Compilation Error. The structure of a C program is as follows:
Header Files Inclusion – Line 1 [#include <stdio.h>]
The first and foremost component is the inclusion of the header files in a C program.
A header file is a file with extension .h which contains C function declarations and macro
definitions to be shared between several source files. All lines that start with # are processed by a
preprocessor which is a program invoked by the compiler.
In the above example, the preprocessor copies the preprocesses code of stdio.h to our file.
The .h files are called header files in C.