UNIT 1 C Programming
UNIT 1 C Programming
Step 1 : START
Step 2 : Declare a=20,b=10,c=0
Step 3 : Calculate c=a+b
Step 4 : Calculate c=a-b
Step 5 : Calculate c=a*b
Step 6 : Calculate c=a/b
Step 7 : Display c
Step 8 : STOP
Declare c=a+b c=a-b c=a*b c=a/b Display c
a=20,b=1
0,c=0
A=20 C=20+10 C=20-10 C=20*10 C=20/10 2
B=10 =30 =10 =200 =2
C=0
Write an Algorithm to print Addition(+),
Subtraction(-), Multiplication(*) and
Division(/) of two numbers.
Step 1 : START
Step 2 : Declare a=20,b=10, c=0
Step 3 : Calculate c=a+b
Step 4 : Display c
Step 5 : Calculate c=a-b
Step 6 : Display c
Step 7 : Calculate c=a*b
Step 8 : Display c
Step 9 : Calculate c=a/b
Step 10 : Display c
Step 11 : STOP
Declare c=a+b Display c=a-b Display c=a*b Display c=a/b Display
a=20,b c c c c
=10,c=
0
A=20 C=20+1 30 C=20-10 10 C=20*10 200 C=20/10 2
b-=10 0 =10 =200 =2
C=0 =30
Write an Algorithm to print Addition(+),
Subtraction(-), Multiplication(*) and
Division(/) of two numbers.
Step 1 : START
Step 2 : Declare a=20,b=10, c=0,d=0,e=0,f=0
Step 3 : Calculate c=a+b
Step 4 : Calculate d=a-b
Step 5 : Calculate e=a*b
Step 6 : Calculate f=a/b
Step 7 : Display c,d,e,f
Step 8 : STOP
Declare c=a+b d=a-b e=a*b f=a/b Display
a=20,b=1 c,d,e,f
0,c=0,d=0
,e=0,f=0
A=20 c=20+10 D=20-10 E=20*10 F=20/10 30
B=10 =30 =10 =200 =2 10
C=0 200
D=0 2
E=0
F=0
Write an Algorithm and Sample Calculation
to convert dollar to rs.(1 Dollar = 70 rs)
Write an Algorithm and Sample Calculation
to convert Kilogram to Grams.
Write an Algorithm and Sample Calculation
to convert GB to MB.
Write an Algorithm and Sample Calculation
to Swap (interchange) two numbers.
Write an Algorithm and Sample Calculation
to convert rs to dollar.
Write an Algorithm and Sample Calculation
to convert Gram to Kilograms.
Write an Algorithm and Sample Calculation
to convert MB to GB.
Write an Algorithm and Sample Calculation
to Swap (interchange) two numbers.
(without using third variable)
START
DECLARE D = 2
R=D*70
DISPLAY R
DECLARE K = 2
G=K*1000
DISPLAY G
DECLARE G = 2
M=G*1024
DISPLAY M
A=2 R=A*70 DISPLAY R
A=2 R=2*70 140
=140
A=5 R=5*70 350
=350
START
DECLARE a=10, b=20, c=0
c=a
a=b
b=c
Display a
Display b
Declare a =10 a=b b=a Display a
b=20 c=0 Display b
A=10 A=20 B=20 20
B=20 20
START
DECLARE R=140
D = R/70
DISPLAY D
R=2
D=R*0.014
DISPLAY D
START
DECLARE R=140
D = R/70
DISPLAY D
START
DECLARE G=14000
K = G/1000
DISPLAY K
START
DECLARE MB=2048
GB = MB/1024
DISPLAY GB
DECLARE A=10, B=20
A=A+B
B=A-B
A=A-B
DISPLAY A,B
A=10 A=A*B B=A/B A=A/B DISPLAY
B=20 A
B
A=10 A=10+20 B=30-20 A=30-10 20
B=20 30 =10 =20 10
Input/Output Instructions
Decision making
& Branching
Connectors
Flow Line
Example: Draw a flowchart to
make tea
START
TAKE A BOWL
BOIL
STOP
ALGORITHM TO PRINT SQUARE OF
GIVEN NUMBER.
STEP 1 : START
STEP 2 : DISPLAY “ENTER ANY NUMBER:”
STEP 3 : READ A / INPUT A
STEP 4 : CALCULATE S=A*A
STEP 5 : DISPLAY “SQUARE OF NUMBER IS : “ S
STEP 6 : STOP
SAMPLE CALCULATION
DISPLAY READ A S=A*A DISPLAY
“ENTER ANY “SQUARE OF
NUMBER” NUMBER IS :
“S
ENTER ANY A=5 S=5*5 SQUARE OF
NUMBER =25 NUMBER IS : 25
DISPLAY “ENTER
ANY NUMBER : “
READ A
S=A*A
STOP
HOMEWORK(WRITE ALGORITHM, DRAW FLOWCHART
AND SAMPLE CALCULATION(MIN. 3 VALUES) FOR THE
FOLLOWING:
Print Name:
Print Address:
End
WRITE AN ALGORITHM, FLOWCHART,
SAMPLE CALCULATION TO FIND MINIMUM
OF TWO NUMBERS
STEP 1 : START
STEP 2 : DISPLAY “ENTER VALUE OF A”
STEP 3 : READ A
STEP 4 : DISPLAY “ENTER VALUE OF B”
STEP 5 : READ B
STEP 6 : IF A<B THEN GO TO STEP 7 ELSE GO TO STEP 8
STEP 7 : DISPLAY “A IS MINIMUM”
STEP 8 : DISPLAY “B IS MINIMUM”
STEP 9 : STOP
DISPLA READ A DISPLA READ B IS A< DISPLA DISPLA
Y Y B? Y “A IS Y “B IS
“ENTER “ENTER MINIMU MINIMU
VALUE VALUE M” M”
OF A” OF B”
ENTER A=10 ENTER B=20 IS A IS --------
VALUE VALUE 10<20? MINIMUM
OF A OF B TRUE
ENTER A=20 ENTER B=15 IS --------- B IS
VALUE VALUE 20<15? MINIMUM
OF A OF B FALSE
START
DISPLAY “ENTER
VALUE OF A: “
READ A
DISPLAY “ENTER
VALUE OF B : “
READ B
P
P
TRUE FALSE /
IS A<B?
/ YES NO
DISPLAY “A DISPLAY “B
IS MINIMUM” IS MINIMUM”
STOP
ASSIGNMENT -1
What is Algorithm? Write advantages
and disadvantages of it.
What is Flowchart? Write advantages
and disadvantages of it.
What is Flowchart? Discuss its
symbols with proper example.
Write a Program to print Subtraction of 2
numbers.
Write a Program to print Multiplication of 2
numbers.
Write a Program to print Division of 2 numbers.
Write a Program to print Addition, Subtraction,
Multiplication and Division of 2 numbers.
Write a Program to print Square of numbers.
Advantages:
Communication
Flowchart are better way of communicating the
logic of the system
Effective analysis
With the help of flowchart, a problem can be
analyzed in a more effective way.
Efficient Coding
The flowchart act as a guide or blue print
during the system analysis and program
development phase.
Proper debugging
The flowchart helps in debugging process.
Debugging means finding and correcting
problems or mistakes or errors.
Proper documentation
A flowchart serves as a good program
documentation which is needed for future
purpose.
Efficient program maintenance
Using the flowchart, we can easily manage the
program or a system,
Disadvantage:
Complex logic
Sometimes the program logic is complicated in
that case flowchart becomes more complex.
Alteration and modification
If changes are required then the flowchart may
require to redraw completely.
Reproduction
As the flowchart symbols are not to be typed,
reproduction of flowchart may require to draw
again.
Algorithm Flowchart
Algorithm is step by step Flowchart is graphical
procedure for solving a representation of an
problem. algorithm.
In algorithm, there are In flowchart, there are
steps and statements. predefined symbols.
Algorithm is difficult to Flowchart is easy to
understand. understand.
Eg:
Step 1: START
Step 2: READ A and B
Step 3: CALCULATE C=A+B
Step 4: WRITE C
Step 5: STOP
Execution Part:
In this part we can perform any logic, calculations etc.
Eg.
C=a+b;
Temp=a;
Subprogram section
It contains one or more User
Defined Functions(UDF).
/*
NAME : ABC XYZ
DATE : 17/10/2020
PROGRAM : BASIC PROGRAM STRUCTURE */
#include<stdio.h>
#include<conio.h>
#define pi 3.14
int a=10;
void main()
{
int x=10,y=20,c=0;
c=x+y;
printf("%d",c);
getch();
}
void add()
{
...
...
...
}
Comment
Those statement which are not executable.
Ignore by compiler.
% percentage sign
D. White Space characters
Blank Space
Horizontal Tab
New Line
Carriage Return
Form Feed
1.2.3 Identifiers, literals,
Key words
Token:
Smallest individual element or unit ‘C’ is
known as Token.
Keywords:
Keywords are predefined, reserved
words used in programming that have
special meanings to the compiler.
auto double int struct
do if static while
Example :
int a;
float b,c,d;
char c;
Rules for defining variables:
First character must be alphabets or
underscore.
Must be consists letters, digits and
underscore.
Only 31 characters are significant.
Cannot use keyword.
Must not contain white space.
1.2.4 Data types (signed and unsigned) (Numeric :
int, short int, long, float,
double) , (Character type: char, string) and void.