C and DCF
C and DCF
Fundamentals (CSC131)
CSC- 131N
(Week 2)
Ms. Garima Anand
Algorithm
Example 1: Algorithm to add 2 numbers and print their Algorithm to add 2 numbers and print their sum:
sum:
Step 1: Start
Step 1: START
Step 2: Declare variables num1, num2 and sum.
Step 2: Declare 2 integer variables num1 and num2.
Step 3: Read values num1 and num2.
Step 3: Take the 2 numbers, to be added, as inputs in
Step 4: Add num1 and num2 and assign the
variables num1, num2, respectively.
result to sum. sum←num1+num2
Step 4: Declare an integer variable sum to store the resultant
Step 5: Display sum
sum of the 2 numbers.
Step 6: Stop
Step 5: Add the 2 numbers and store the result in the
variable sum.
Step 6: Print the value of variable sum
Step 7: END 2
Excellence and Service
CHRIST
Deemed to be University
3
Excellence and Service
CHRIST
Deemed to be University
4
Excellence and Service
CHRIST
Deemed to be University
Algorithm
Example 1: Algorithm to find the discriminant of a quadratic equation:
Step 1: START
Step 2: Read the coefficients of the quadratic equation, a, b and c from the user.
Step 3: Calculate discriminant = (b * b) – (4 * a * c).
Step 4: Display the discriminant.
Step 5: END
5
Excellence and Service
CHRIST
Deemed to be University
Algorithm
Algorithm
Example 3: Algorithm to find sum of digits of a number:
Step 1: START
Step 2: Input N
Step 3: Sum = 0
Step 4: While (N != 0)
Rem = N % 10;
Sum = Sum + Rem;
N = N / 10;
Step 5: Print Sum
Step 6: END
7
Excellence and Service
CHRIST
Deemed to be University
Algorithm
Example 4: Algorithm to check whether a number is a palindrome:
Step 1: START
Step 2: Take a number n as input
Step 3: Copy value of n to another variable say m i.e. m = n.
Step 4: Initialize reverse = 0.
Step 5: Perform r = n% 10.
Step 6: Perform reverse = reverse * 10 + r.
Step 7: Perform n = n /10.
Step 8: If n = 0 go to step 9 else go to step 5.
Step 9: Check if reverse == m.
Step 10: If step 8 is true then n is palindrome else n is not a palindrome.
Step 11: END
8
Excellence and Service
CHRIST
Deemed to be University
Algorithm :
1. To find the number of odd numbers in a given list of N numbers.
2. To find the largest and smallest of any given N numbers.
9
Excellence and Service
CHRIST
Deemed to be University
Flowcharts
10
Excellence and Service
CHRIST
Deemed to be University
Flowchart
Prepare a tea
Step 1: START
Step 2: Pour 2 cup of water in frying pan and put it on stove
Step 3: Add 2 spoon tea and 2 spoon sugar
Step 4: Add 2 cup milk
Step 5: Wait for water to boil
Step 6: Put tea in a cup
Step 7 :END
11
Excellence and Service
CHRIST
Deemed to be University
Flowchart
12
Excellence and Service
CHRIST
Deemed to be University
Flowchart
13
Excellence and Service
CHRIST
Deemed to be University
Flowchart
Example 1 : Find To check whether a given number is odd or even.
●Algorithm
Step 1: Start
Step 2: READ number
Step 3: remainder=number%2
Step 4: IF remainder = = 0
Print "Even Number"
ELSE
Print "Odd Number"
ENDIF
Step 5: End
14
Excellence and Service
CHRIST
Deemed to be University
Flowchart
Example 2 : Find Area and Perimeter of a Square Start
●Algorithm
Step 1: Start Input Value of Len
Step 2: Input Side Length of Square say Len
Step 3: Area = Len x Len Area = Len X Len
Step 4: PERIMETER = 4 x Len
Step 5: Display AREA, PERIMETER
Perimeter = 4 X Len
Step 6: Stop
Stop
15
Excellence and Service
CHRIST
Deemed to be University
Flowchart
Example 3 : Find Largest of three numbers Start
Algorithm
Input A, B and C
Step 1: Start
Step 2: Read three numbers say A,B,C
Large = A
Step 3: Large = A
Step 4: IF B > Large THEN Is B > Yes Large = B
Large
Large = B
No
ENDIF
Is C > Yes Large = C
Step 5: IF C >Large THEN Large
Large = C No
ENDIF Write Large
Flowchart :
1. To print multiplication table of a given number.
2. To print Fibonacci series from 0 to N.
3. To find sum of two numbers.
4. To find sum of digits of a number.
5. To find factorial of last digit of a number.
6. To accept three sides of a triangle and check whether it is an equilateral triangle.
7. To check whether a given number is positive.
8. To find sum of first N natural numbers.
9. To find the number of negative numbers in a given list of N numbers.
17
Excellence and Service
CHRIST
Deemed to be University
Introduction to C
18
Excellence and Service
CHRIST
Deemed to be University
Introduction
19
Excellence and Service
CHRIST
Deemed to be University
Introduction
● There is a close analogy between learning English language and learning C language.
● Instead of straight-away learning how to write programs, we must first know what alphabets,
numbers and special symbols are used in C, then how using them, constants, variables and keywords
are constructed, and finally, how are these combined to form an instruction.
● A group of instructions would be combined later on to form a program.
20
Excellence and Service
CHRIST
Deemed to be University
C Character Set
● A character denotes any alphabet, digit or special symbol used to represent information.
● Figure shows the valid alphabets, numbers and special symbols allowed in C.
21
Excellence and Service
CHRIST
Deemed to be University
Constants, Variables and Keywords
● The alphabets, digits and special symbols when properly combined form constants, variables and keywords.
● A constant is an entity that doesn’t change.
● A variable is an entity that may change.
● A keyword is a word that carries special meaning.
● In any C program we typically do lots of calculations. The results of these calculations are stored in computer’s
memory. To make the retrieval and usage of these values easy, memory locations are given names.
● Since the value stored in each location may change, the names given to these locations are called variable names.
22
Excellence and Service
CHRIST
Deemed to be University
● Since the location whose name is x can hold different values at different times x is known as a
variable (or a variable name).
● As against this, 3 or 5 do not change, hence are known as constants.
● In programming languages, constants are often called literals, whereas, variables are called
identifiers.
23
Excellence and Service
CHRIST
Deemed to be University
Types of C Constants
24
Excellence and Service
CHRIST
Deemed to be University
Types of C Constants
● The exponential form is usually used if the value of the constant is either too small or too large.
● In exponential form the real constant is represented in two parts. The part appearing before ‘e’ is
called mantissa, whereas the part following ‘e’ is called exponent. Thus 0.000342 can be written in
exponential form as 3.42e-4 (which in normal arithmetic means 3.42 x 10 -4).
● Following rules must be observed while constructing real constants expressed in exponential form:
(a) The mantissa part and the exponential part should be separated by a letter e or E.
(b) The mantissa part may have a positive or negative sign.
(c) Default sign of mantissa part is positive.
(d) The exponent must have at least one digit, which must be a positive or negative integer. Default
sign is positive.
(e) Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
27
Excellence and Service
CHRIST
Deemed to be University
Types of C Constants
28
Excellence and Service
CHRIST
Deemed to be University
Types of C Variables
● A particular type of variable can hold only the same type of constant.
● For example, an integer variable can hold only an integer constant, a real variable can hold only a real
constant and a character variable can hold only a character constant.
● The rules for constructing different types of constants are different.
● However, for constructing variable names of all types, the same set of rules applies.
● Rules for Constructing Variable Names:
(a) A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers
allow variable names whose length could be up to 247 characters. Still, it would be safer to stick
to the rule of 31 characters. Do not create unnecessarily long variable names as it adds to your
typing effort.
29
Excellence and Service
CHRIST
Deemed to be University
Types of C Variables
● It is always advisable to construct meaningful variable names like prin, roi, noy to represent
Principle, Rate of interest and Number of years rather than using the variables a, b, c
30
Excellence and Service
CHRIST
Deemed to be University
Types of C Variables
● C compiler is able to distinguish between the variable names by making it compulsory for you to
declare the type of any variable name that you wish to use in a program.
● This type declaration is done at the beginning of the program.
31
Excellence and Service
CHRIST
Deemed to be University
C Keywords
● Keywords are the words whose meaning has already been explained to the C compiler (or in a broad
sense to the computer).
● There are only 32 keywords available in C.
● keywords cannot be used as variable names because if we do so, we are trying to assign a new
meaning to the keyword, which is not allowed.
32
Excellence and Service
CHRIST
Deemed to be University
The First C Program
/* Calculation of simple interest */
# include <stdio.h>
int main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n=3;
r = 8.5 ;
printf ( "%f\n" , si ) ;
return 0 ;
}
33
Excellence and Service
CHRIST
Deemed to be University
The First C Program
There are certain rules about the form of a C program that are applicable to all C programs. These are as
under:
(a) Each instruction in a C program is written as a separate statement.
(b) The statements in a program must appear in the same order in which we wish them to be executed.
(c) Blank spaces may be inserted between two words to improve the readability of the statement.
(d) All statements should be in lower case letters.
(e) C has no specific rules for the position at which a statement is to be written in a given line. That’s
why it is often called a free-form language.
(f) Every C statement must end with a semicolon ( ; ). Thus ; acts as a statement terminator.
34
Excellence and Service
CHRIST
Deemed to be University
The First C Program
● Here are a few things that you must remember while writing /* formula for simple interest */
si = p * n * r / 100 ;
comments in a C program:
(a) Comment about the program should be enclosed within /* */. printf ( "%f\n" , si ) ;
return 0 ;
Thus, the first statement in our program are comments. }
35
Excellence and Service
CHRIST
Deemed to be University
The First C Program
● In the statement,
si = p * n * r / 100 ;
and / are the arithmetic operators. The arithmetic operators available in C are +, -, * and /.
● C is very rich in operators. There are as many as 45 operators available in C. Surprisingly there is no
operator for exponentiation.
40
Excellence and Service
CHRIST
Deemed to be University
The First C Program
● All output to screen is achieved using readymade library functions. int main( )
{
One such function is printf( ). int p, n ;
float r, si ;
● Let us understand this function with respect to our program.
(b) For us to be able to use the printf( ) function, it is necessary to use /* formula for simple interest */
#include <stdio.h> at the beginning of the program. si = p * n * r / 100 ;
● In addition to format specifiers like %f, %d and %c, the format string /* formula for simple interest */
si = p * n * r / 100 ;
may also contain any other characters. These characters are printed as
they are when printf( ) is executed. printf ( "%f\n" , si ) ;
return 0 ;
}
42
Excellence and Service
CHRIST
Deemed to be University
The First C Program
printf( ) and its Purpose :
/* Calculation of simple interest */
● Given below are some more examples of usage of printf( ) function: # include <stdio.h>
printf ( "%f", si ) ;
int main( )
printf ( "%d %d %f %f", p, n, r, si ) ; {
int p, n ;
printf ( "Simple interest = Rs. %f", si ) ; float r, si ;
printf ( "Principal = %d \nRate = %f", p, r );
p = 1000 ;
● printf( ) can not only print values of variables, it can also print the result of n=3;
r = 8.5 ;
an expression. An expression is nothing but a valid combination of
constants, variables and operators. /* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%d %d %d %d", 3, 3 + 2, c, a + b * c – d ) ;
● \n - It is called newline and it takes the cursor to the next line. Therefore, printf ( "%f\n" , si ) ;
return 0 ;
you get the output split over two lines. }
43
Excellence and Service
CHRIST
Deemed to be University
The First C Program
Receiving Input:
/* Calculation of simple interest */
● In the program discussed in previous slides we assumed the values of
p, n and r to be 1000, 3 and 8.5. Every time we run the program we # include <stdio.h>
int main( )
would get the same value for simple interest. {
int p, n ;
● To make the program general, the program itself should ask the user to float r, si ;
supply the values of p, n and r through the keyboard during
printf ( "Enter values of p, n, r" ) ;
execution. scanf ( "%d %d %f", &p, &n, &r ) ;
● This can be achieved using a function called scanf( ). This function is
si = p * n * r / 100 ;
a counter-part of the printf( ) function.
printf ( "%f\n" , si ) ;
● printf( ) outputs the values to the screen whereas scanf( ) receives return 0 ;
them from the keyboard. }
44
Excellence and Service
CHRIST
Deemed to be University
The First C Program
Receiving Input:
● The first printf( ) outputs the message ‘Enter values of p, n, r’ on the /* Calculation of simple interest */
screen. Here we have not used any expression in printf( ) which means # include <stdio.h>
that using expressions in printf( ) is optional. int main( )
{
● The use of ampersand (&) before the variables in the scanf( ) function is int p, n ;
a must. float r, si ;
● & is an ‘Address of’ operator. It gives the location number (address) printf ( "Enter values of p, n, r" ) ;
used by the variable in memory. scanf ( "%d %d %f", &p, &n, &r ) ;
● When we say &a, we are telling scanf( ) at which memory location should si = p * n * r / 100 ;
it store the value supplied by the user from the keyboard.
printf ( "%f\n" , si ) ;
● A blank, a tab or a new line must separate the values supplied to scanf( ). return 0 ;
}
A blank is created using a spacebar, tab using the Tab key and new line
using the Enter key.
45
Excellence and Service
CHRIST
Deemed to be University
C Instructions
(a) Type Declaration Instruction – This instruction is used to declare the type of variables used in a C program.
(b) Arithmetic Instruction – This instruction is used to perform arithmetic operations on constants and variables.
(c) Control Instruction – This instruction is used to control the sequence of execution of various statements in a C
program.
46
Excellence and Service
CHRIST
Deemed to be University
Type Declaration Instruction
● This instruction is used to declare the type of variables being used in the program.
● Any variable used in the program must be declared before using it in any statement.
47
Excellence and Service
CHRIST
Deemed to be University
Type Declaration Instruction
● There are several subtle variations of the type declaration instruction.
(i) While declaring the type of variable we can also initialize it as :
int i = 10, j = 25 ;
float a = 1.5, b = 1.99 + 2.4 * 1.44 ;
(ii) The order in which we define the variables is sometimes important sometimes not. For example,
int i = 10, j = 25 ;
is same as
int j = 25, i = 10 ;
However,
float a = 1.5, b = a + 3.1 ; is alright,
but
float b = a + 3.1, a = 1.5 ; is not.
This is because here we are trying to use a before defining it.
48
Excellence and Service
CHRIST
Deemed to be University
Type Declaration Instruction
(iii) The following statements would work
int a, b, c, d ;
a = b = c = 10 ;
However, the following statement would not work
int a = b = c = d = 10 ;
Once again we are trying to use b (to assign to a) before defining it.
49
Excellence and Service
CHRIST
Deemed to be University
Arithmetic Instructions
● A C arithmetic instruction consists of a variable name on the left hand side of = and variable names and constants
on the right hand side of =.
● The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *,
and /.
● For example : int ad ;
Here, *, /, -, + are the arithmetic operators.
float kot, deta, alpha, beta, gamma ; = is the assignment operator.
ad = 3200 ; 2, 5 and 3200 are integer constants.
kot = 0.0056 ; .2 and 0.0056 are real constants. ad is an integer variable.
deta = alpha * beta / gamma + 3.2 * 2 / 5; kot, deta, alpha, beta, gamma are real variables
● The variables and constants together are called ‘operands’. While executing an arithmetic statement the operands
on right hand side are operated upon by the ‘arithmetic operators’ and the result is then assigned, using the
assignment operator, to the variable on left-hand side.
50
Excellence and Service