Unit 1
Unit 1
1. Start
2. Put the teabag in a cup
3. Fill the kettle with water
4. Boil the water in the kettle
5. Add milk to the cup
6. Add sugar to the cup
7. Stir the tea
8. Drink the tea
8. Stop
-> Like this, we can write an algorithm for any logic
Some more examples of an algorithm :
• Input/Output
Used for input and output operation
• Process/Instruction
• Connector/Arrow
• During the program development cycle, the flowchart plays the role of a blueprint,
which makes the program development process easier.
Input
Number1,Number2
Sum=Number1+Number2
Print Sum
End
• Find A Simple Interest Algorithm
Step 1:Start
Step 2:Read Principle Amount, rate of interest, and time period
Step 3:Calculate Simple Interest
Simple Interest --- (Principle amount x rate of interest x
time period)/100
START
Step 4:Display Simple Interest
Step 5:Stop Input P,R,T
• Flowchart Calculate
I = (P*R*T)/100
Display = I
End
• Algorithm to Check Positive Number
Step 1:Start
Step 2: Read Number
Step 3:If Number >0
Step 4:Print Positive Number
Step 5:Stop START
Input Number
IF Number>0
No
Yes
Print Positive
Number
End
• Algorithm to check even or odd number
Step 1:Start Start
Step 2: Read Number
Step 3:If Number %2 == 0 Input Number
Step 4:Print Even Number
Step 3:Else Print Odd Number
If Number %
Step 5:Stop Yes 2 ==0 No
• Flowchart
Print Even Print Odd
Number Number
End
• Difference between Algorithm and Flowchart :
Algorithm Flowchart
Algorithm is step by step procedure to Flowchart is a diagram created by
solve the problem. different shapes to show the flow of data.
Algorithm is complex to understand. Flowchart is easy to understand.
In algorithm plain text are used. In flowchart, symbols/shapes are used.
Algorithm is easy to debug. Flowchart it is hard to debug.
Algorithm is difficult to construct. Flowchart is simple to construct.
Algorithm does not follow any rules. Flowchart follows rules to be constructed.
Algorithm is the pseudo code for the Flowchart is just graphical representation
program. of that logic.
Flow Of A C Program :
Character Set :
• As every language contains a set of characters used to construct words,
statements, etc., C language also has a set of characters which
include alphabets, digits, and special symbols. C language supports a total of
256characters.
Identifiers
Keywords
Constants
Operators
String
Special Characters
Identifiers :
• Each program element in a C program is called an identifier. An identifier is a
variable that holds a value and stores it in the memory.
• 33 Keywords in C
double
Constants :
• A constant is a fixed value that cannot be altered during the
execution of a program.
• Values that don’t change (fixed).
Types
Integer Character
Constant Real Constant
1,2,3,0,-1,-2 Constant ‘a’, ‘b’, ‘A’,
1.0,2.0,3 ‘#’, ‘&’
.14,
-0.956
Special Characters :
• All the characters other than a to z, A to Z, and 0 to 9 are special
characters.
• Example: {},[],(),#,@,$,%,^,*.
String:
• A string is a group of characters that should be enclosed in double
quotations.
• For example: char string[]=“hello”; ,”abc85”;
WHAT IS A VARIABLE?
a b Memory
25 s
• Declared by writing type variable_name;
A variable name can contain alphabets, digits, and underscore (_) only.
The starting letter can not be a digit, A variable name can start with the
alphabet and underscore only
We can declare and assign value to a variable in two ways int =a;
Int a; a = 12;
int a=2;
int b=a;
int c=b+1;
int d=5,e;
int a,b,c;
int a = b = c = 1;
Comments:
• Lines that are not part of the program.
// /*
*/
FOR EXAMPLE :
// Write A Program Hello World//
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hello World\n”);
getch();
}
Output :
Hello World
WRITE A ALGORITHM TO PRINT THE FIBONACCI SERIES
STEP 1. START
STEP 2.Declare Variable i , a, b, show
a = 0 , b = 1 , show = 0
STEP 3.Enter the numbers of terms of Fibonacci Series (i)
STEP 4.if i < 2 then display message “Enter Number >2”,and go to STEP 8
STEP 5.Print the first two terms of the Fibonacci series
STEP 6.using loop show = a + b
a=b
b = show
increase value each time
until show < i-2
STEP 7.Print value of show
STEP 8.STOP
Concept of Editor, Interpreter, and compiler
• A language translator is software that translates the program from a source language that is
in human-readable form into an equivalent program in an object language.
• The Source language is generally a high-level programming language, and the object
language is typically the machine language of an actual compiler.
Compiler :
• A compiler is a computer program that transforms code written in a high-level programming
language into the machine code.
• The compiler is only a program and can not fix errors found in that program. So, if you make a
mistake, you need to make changes in the syntax of your program. Otherwise, it will not
compile.
Role of Compiler
• Both compiler and interpreters do the same job which is converting high-
level programming language to machine code. However, a compiler will
convert the code into machine code (create an exe) before program run.
• Interpreters convert code into machine code when the program is run.
Role of Interpreter
• The interpreter converts the source code line-by-line during RUN Time.
• Relatively less time spent for analyzing and processing the program
Translates program one statement at a Scans the entire program and translates
time. it as a whole into machine code.
Interpreters usually take less amount Compilers usually take a large amount
of time to analyze the source code. of time to analyze the source code.
However, the overall execution time is However, the overall execution time is
comparatively slower than compilers. comparatively faster than interpreters.
• The interface of Turbo C is very simple. when the IDE screen appears, the
menu bar is activated. It contains various menus such as;
• File: This menu contains a group of commands used for save, edit , print program, exit
from Turbo C editor etc.
• Edit: This menu contains a group of commands used for editing C program source code.
Example Copy, Cut, Paste, Undo etc.
• Search: This menu contains group of commands used for searching specific word as well
as replacing it with another one.
• Run: This menu contains group of commands used for running C program.
• Compile: This menu contains group of commands used for compiling C program.
• Debug: This menu contains group of commands used for debugging C program.
• Project: This menu contains group of commands used for opening, closing and creating
projects.
• Options: This menu contains group of commands used for configuring IDE of Turbo C and setting up
directories etc.
• Windows: This menu contains group of commands used for opening, closing various windows of IDE.
• Help: This menu is used to get help about a specific topic of C language. Similarly to get help about a specific
keyword or identifier of C.
#include <stdio.h>
int main()
{
printf(“Hello World\n”);
return 0;
}
• Documentation section : The documentation section consists of a set of
comment lines giving the name of the program, the author and other
details, which the programmer would like to use later.
• Link section : The link section provides instructions to the compiler to link
functions from the system library.
• Global declaration section : There are some variables that are used in
more than one function. Such variables are called global variables and are
declared in the global declaration section that is outside of all the
functions. This section also declares all the user-defined functions.
• main () function section: Every C program must have one main function
section. This section contains two parts; the declaration part and the
executable part
• Declaration part: The declaration part declares all the variables used in the
executable part.
• Executable part: There is at least one statement in the executable part. These
two parts must appear between the opening and closing braces. The program
execution begins at the opening brace and ends at the closing brace. The
closing brace of the main function is the logical end of the program. All
statements in the declaration and executable part end with a semicolon.