EST102 Programming in C
EST102 Programming in C
EST102 Programming in C
PROGRAMMING IN C
MODULE II
SYLLABUS
Program Basics
Basic structure of C program: Character set, Tokens,
Identifiers in C, Variables and Data Types, Constants, Console
IO Operations, printf and scanf
6. Main function
It is the major section from where the
execution of the program begins. The main
section involves the declaration and executable
section.
Example :
#define PI 3.14
3) #undef
• To undefine a macro means to cancel its
definition.
• This is done with the #undef directive.
Syntax :
#undef identifier
Example :
#define PI 3.14
#undef PI
4) #ifdef
• The #ifdef preprocessor directive checks
if macro is defined by #define.
• If yes, it executes the code.
5) #ifndef
• The #ifndef preprocessor directive
checks if macro is not defined
by #define.
• If yes, it executes the code.
6) #if
• The #if preprocessor directive evaluates
the expression or condition.
• If condition is true, it executes the code.
7) #else
• The #else preprocessor directive
evaluates the expression or condition if
condition of #if is false.
• It can be used with
#if, #elif, #ifdef and #ifndef directive.
8) #error
• The #error preprocessor directive
indicates error.
• The compiler gives fatal error
if #error directive is found and skips
further compilation process.
9) #pragma
• The #pragma preprocessor directive is
used to provide additional information to
the compiler.
Character Set
The characters in C are grouped into the
following categories:
1) Letters
2) Digits
3) Special characters
4) White space
Letters
Upper case letters : A to Z
Lower case letters : a to z
Digits
From 0 to 9
Special Characters
Special Characters (Contd…)
White Space
1) Blank space
2) Horizontal tab
3) Carriage return - \r - will point to
beginning of the current line
4) New line - \n - will point to
beginning of the next line
5) Form feed - \f - will point to
beginning of the next page
Tokens
• In a C program the smallest individual
units are known as C tokens.
• C has six types of tokens.
• C programs are written using these
tokens and the syntax of the language.
Keywords
• All keywords have fixed meanings and
these meanings cannot be changed.
• Keywords serve as basic building
blocks for program statements.
• All keywords must be written in
lowercase.
Identifiers
• Identifiers refer to the names of
variables, functions and arrays.
• These are user-defined names.
Rules for Identifiers
1) First character must be an alphabet.
2) Must consist of only letters, digits or
underscore.
3) Only first 31 characters are
significant.
4) Cannot use a keyword.
5) Must not contain white space.
Constants
Fixed values that do not change during
the execution of a program.
‘A’ “re”
a=128 p=3.14 ‘v’ “45”
‘$’ “A”
Variables
• A variable is a data name that may be
used to store a data value.
• Unlike constants that remain
unchanged during the execution of a
program, a variable may take different
values at different times during
execution.
Declaration & Initialization of
Variables
datatype variable_name;
variable_name=value;
OR
datatype variable_name=value;
Data Types
Data types can be broadly classified as
1) Primary (or fundamental) data
types
2) Derived data type
3) User-defined data type
Primary Data Types
User Defined Data Types
• A user can define an identifier that
represents an existing data type.
• The user defined data type identifier
can later be used to declare variables.
1) Type Definition
2) Enumerated Data Type
1) Type Definition
• Keyword used : typedef
• It provides an alternative approach to
existing data type.
Syntax :
typedef datatype identifier;
Example :
float h1,h2;
can be written as,
typedef float height;
height h1,h2;
2) Enumerated Data Type
• Keyword used : enum
Syntax :
enum identifier {value1,value2,..,valuen};
• The identifier is a user defined enumerated
data type which can be used to declare
variables that have one of the values
enclosed within the braces.
• After the definition we can declare variables
to be of this new type.
Example
enum day {Monday, Tuesday, …. Sunday};
day a,b;
a= Monday;
b= Friday;
Questions
1) Explain how characters are stored and processed in C. (4 Marks)
2) Describe the structure of a C program. List out the features of C Language.
(3 Marks)
3) Describe about the fundamental data-types in C. (5 Marks)
4) How do you declare constants in C? (3 Marks)
5) Differentiate between keywords and identifiers. (3 Marks)
6) Describe the four data-type qualifiers in C.(3 Marks)
7) What are the keywords in C? What restrictions apply to their use? (2
Marks)
8) Explain enum data type in C with example. (3 Marks)
9) How will you declare variables using enumerated data type ?Explain with
an example.(3 Marks)
10) What are enumerated data types? How ordinal values are assigned to its
members?(3 Marks)
11) Explain the purpose of typedef construct. (2 Marks)
12) What are identifiers? Give the rules for forming identifiers in C. (3 Marks)
I/O Operations
Library Functions
• Library functions are used to carry out
various commonly used operations or
calculations.
• These functions are accessed by simply
writing the function name followed by a
list of arguments enclosed in parenthesis
and separated by comma.
Input and Output Functions
• These functions permit the transfer of information
between the computer and the standard
input/output devices.
• An input/output function can be accessed from
anywhere within a program by simply writing the
function name, followed by a list of arguments
enclosed in parentheses.
• The arguments represent data items that are sent to
the function.
• Some input/output functions do not require
arguments, though the parentheses must still
appear.
The six input/output functions are :
1) getchar
2) putchar
3) scanf
4) printf
5) gets
6) puts
Entering Input Data---the scanf Function
• The scanf function can be used to enter any
combination of numerical values, single
characters and strings.
• The function returns the number of data items
that have been entered successfully.
• The general form of scanf is:
scanf (“%control string”, &arg1,&arg2,……, &argn ) ;
• The control string specifies the field format in
which the data is to be entered and the
arguments arg 1, arg 2, ….. ,arg n specify the
address of locations where the data is stored.
Writing Output Data---the printf Function
• The printf function can be used to output
any combination of numerical values,
single characters and strings.
• The printf function moves data from the
computer’s memory to the standard output
device.
• The general form of printf is:
printf (“%control string“, arg1,arg2,……, argn ) ;
Questions
1) Explain how one can use the builtin
function in C, scanf to read values of
different data types. Also explain using
examples how one can use the builtin
function in C, printf for text formatting.
(8 Marks)
2) What are library functions? (2 Marks)
Operators
&
Expressions
Operators
• An operator is a symbol that tells the
computer to perform certain mathematical
or logical manipulations.
• Operators are used in programs to
manipulate data and variables.
• An expression is a sequence of operands
and operators that reduces to a single
value.
Types of Operators
1) Arithmetic operators
2) Relational operators
3) Logical operators
4) Assignment operators
5) Increment and decrement operators
6) Conditional operators
7) Bitwise operators
8) Special operators
Arithmetic Operators
Arithmetic Operators take two operands and
performs a calculation on them.
Arithmetic Operators
Arithmetic Operators take two operands and
performs a calculation on them.
Relational Operators
● The term, relational, refers to the
relationships that data items can have with
one another.
● Expressions that use relational operators
yield either true or false as the outcome.
● The relational expressions always return 0
for false and 1 for true.
Logical Operators
● The term, logical, refers to the ways by
which these relationships can be connected.
● Expressions that use logical operators yield
either true or false as the outcome.
● The logical expressions always return 0 for
false and 1 for true.
The logical operators function according to
their truth table.
X Y X && Y X || Y !X !Y
The logical operators function according to
their truth table.
X Y X && Y X || Y !X !Y
0 0
0 1
1 0
1 1
The logical operators function according to
their truth table.
X Y X && Y X || Y !X !Y
0 0 0
0 1 0
1 0 0
1 1 1
The logical operators function according to
their truth table.
X Y X && Y X || Y !X !Y
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
The logical operators function according to
their truth table.
X Y X && Y X || Y !X !Y
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
The logical operators function according to
their truth table.
Lo
a=1
X Y X && Y X || Y !X !Y
b=5
0 0 0 0 1 1 ….
….
0 1 0 1 1 0
{
1 0 0 1 0 1 ……
1 1 1 1 0 0 }
Assignment Operators
• Assignment operators are used to
assign the result of an expression to an
identifier.
• The most commonly used assignment
operator is =.
• Assignment expressions that make use
of this operator are written in the form:
identifier =
expression
Short hand Assignment Operators
v op =
expression ;
Example :
• a = a/b
m=m*n/2
a / = b
m*=n/2
• x=x+y+1
Increment & Decrement Operators
• The Increment (++) and decrement (--)
operators are unary operators and they
require one variable as their operand.
• The increment operator causes its
operand to be increased by 1, whereas
the decrement operator causes its
operand to be decreased by 1.
• The operand used with each of these
operators must be a single variable.
Pre-increment/decrement operator
• If the operator precedes the operand (e.g., ++i/--i),
then it is called as pre-increment/decrement
operator.
• When prefix is used in an expression, the variable is
incremented/decremented first and then the
expression is evaluated using the new value of the
variable.
Example :
a=5
g = --a
=> a=a-1=5-1=4 g=a=4
Pre-increment/decrement operator
• If the operator precedes the operand (e.g., ++i/--i),
then it is called as pre-increment/decrement
operator.
• When prefix is used in an expression, the variable is
incremented/decremented first and then the
expression is evaluated using the new value of the
variable.
Example :
a=5
g = --a
=> g=4 a=4
Pre-increment/decrement operator
• If the operator precedes the operand (e.g., ++i/--i),
then it is called as pre-increment/decrement
operator.
• When prefix is used in an expression, the variable is
incremented/decremented first and then the
expression is evaluated using the new value of the
variable.
Example :
a=5
g = ++a
=> a=a+1=5+1=6 g=a=6
Pre-increment/decrement operator
• If the operator precedes the operand (e.g., ++i/--i),
then it is called as pre-increment/decrement
operator.
• When prefix is used in an expression, the variable is
incremented/decremented first and then the
expression is evaluated using the new value of the
variable.
Example :
a=5
g = ++a
=> g=6 a=6
Post-increment/decrement operator
• If the operator comes after the operand (e.g.,
i++/i--), then it is called as
post-increment/decrement operator.
• When postfix is used with a variable in an
expression, the expression is evaluated first using
the original value of the variable and then the
variable is incremented/decremented by one.
Example :
a=5
g = a--
=> g=a=5 a=a-1=5-1=4
Post-increment/decrement operator
• If the operator comes after the operand (e.g.,
i++/i--), then it is called as
post-increment/decrement operator.
• When postfix is used with a variable in an
expression, the expression is evaluated first using
the original value of the variable and then the
variable is incremented/decremented by one.
Example :
a=5
g = a--
=> g = 5 a = 4
Post-increment/decrement operator
• If the operator comes after the operand (e.g.,
i++/i--), then it is called as
post-increment/decrement operator.
• When postfix is used with a variable in an
expression, the expression is evaluated first using
the original value of the variable and then the
variable is incremented/decremented by one.
Example :
a=5
g = a++
=> g=a=5 a=a+1=5+1=6
Post-increment/decrement operator
• If the operator comes after the operand (e.g.,
i++/i--), then it is called as
post-increment/decrement operator.
• When postfix is used with a variable in an
expression, the expression is evaluated first using
the original value of the variable and then the
variable is incremented/decremented by one.
Example :
a=5
g = a++
=> g = 5 a = 6
Conditional Operators
• Simple conditional operations can be carried out
with the conditional operator (? : ).
• A conditional expression is written in the form:
expression 1 ? expression 2 : expression 3;
• When evaluating a conditional expression,
expression 1 is evaluated first.
• If expression 1 is true (i.e., if its value is nonzero),
then expression 2 is evaluated and this becomes the
value of the conditional expression.
• If expression 1 is false (i.e., if its value is zero), then
expression 3 is evaluated and this becomes the value
of the conditional expression.
Example : C program to find largest among two
numbers using conditional operator.
#include<stdio.h>
int main()
{
int a,b,Largest;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
Largest=a>b?a:b;
printf("Largest number=%d",Largest);
return 0;
}
Bitwise Operators
• The bitwise operators are used for manipulating
data at the bit level.
• Bit level programming means programming at
binary level consisting of 0s and 1s.
• It cannot be directly applied to data types such as
float, double etc.
• They are compatible with the integer data type.
• The bitwise logical operators work on the data bit
by bit, starting from the least significant bit, which
is the rightmost bit, working towards the most
significant bit which is the leftmost bit.
The result of the computation of bitwise logical
operators is shown in the table given below.
Read number, n
Remainder = n % 2
If
True False
Remainder == 0
Stop
Program
#include<stdio.h>
int main()
{
int n,Remainder;
printf("Enter a number\n");
scanf("%d",&n);
Remainder=n%2;
if(Remainder==0)
{
printf("Even number\n");
}
else
{
printf("Odd number\n");
}
return 0;
}
Output 1 Output 2
Enter a number Enter a number
10 35
Even number Odd number
Question No. 2
Write a C program to check whether the
given year is leap year or not.
Leap Year
• A leap year is a year, which is different
than a normal year have 366 days
instead of 365.
• A leap year comes once in four years, in
which February month has 29 days.
• With this additional day in February, a
year becomes a Leap year.
• Some leap years examples are - 1600,
1988, 1992, 1996, and 2000.
Below conditions are used to check that
year is a leap year or not.
1) Year must be divisible by 4, and,
2) Year is divisible by 400 or Year is not
divisible by 100.
By putting these conditions in your code,
you can check year is a leap year or not. If
the above conditions are satisfied, the year
will be leap year.
Algorithm
Step 1 : Start
Step 2 : Read a year, y
Step 3 : If y is divisible by 4, then, go to Step _,
otherwise go to Step _.
Step 4 : If y is divisible by 400 but not divisible
by 100, then, go to Step _, otherwise go
to Step _.
Step 5 : Print “Leap year” go to Step _.
Step 6 : Print “Not a Leap year”
Step 7 : Stop
Algorithm
Step 1 : Start
Step 2 : Read a year, y
Step 3 : If y is divisible by 4, then, go to Step 4,
otherwise go to Step 6.
Step 4 : If y is divisible by 400 but not divisible
by 100, then, go to Step 5, otherwise go
to Step 6.
Step 5 : Print “Leap year” go to Step 7.
Step 6 : Print “Not a Leap year”
Step 7 : Stop
Flow Chart
Start
Read a year, y
If False
y % 4 == 0
True
If
True y % 400 == 0 or False
y % 100 != 0
Print “Leap Year” Print “Not a Leap year”
Stop
Program
#include<stdio.h>
int main()
{
int y;
printf("Enter a year\n");
scanf("%d",&y);
if((y % 4 == 0) && ((y % 400 == 0) || (y % 100 != 0)))
{
printf("Leap year");
}
else
{
printf("Not a leap year");
}
return 0;
}
Output 1 Output 2
Enter a year Enter a year
2000 2021
Leap year Not a leap year
Nested if….else Statement
if(expression1) else
{ {
if(expression2) if(expression3)
{ {
Statement1; Statement3;
} }
else else
{ {
Statement2; Statement4;
} }
} }
Statement-x;
Question No. 3
Write a C program to find largest number
among three numbers using nested if
statement.
Algorithm
Step 1 : Start
Step 2 : Read three numbers, a,b,c
Step 3 : If a > b, then, go to Step _, otherwise go to Step
_.
Step 4 : If a > c, then, go to Step _, otherwise go to Step
_.
Step 5 : If b > c, then, go to Step _, otherwise go to Step
_.
Step 6 : Largest = a go to Step _
Step 7 : Largest = c go to Step _
Step 8 : Largest = b go to Step _
Step 9 : Print Largest
Step 10 : Stop
Algorithm
Step 1 : Start
Step 2 : Read three numbers, a,b,c
Step 3 : If a > b, then, go to Step 4, otherwise go to Step
5.
Step 4 : If a > c, then, go to Step 6, otherwise go to Step
7.
Step 5 : If b > c, then, go to Step 8, otherwise go to Step
7.
Step 6 : Largest = a go to Step 9
Step 7 : Largest = c go to Step 9
Step 8 : Largest = b go to Step 9
Step 9 : Print Largest
Step 10 : Stop
Flow Chart
Program
#include<stdio.h>
int main()
{
int a,b,c,Largest;
printf("Enter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
Largest=a;
}
else
{
Largest=c;
}
}
else
{
if(b>c)
{
Largest=b;
}
else
{
Largest=c;
}
}
printf("Largest Number = %d",Largest);
return 0;
}
Output
Enter three numbers
25
524
189
Largest Number = 524
Else if Ladder
switch Statement
• The switch statement causes a particular group
of statements to be chosen from several
available groups.
• The selection is based upon the current value
of an expression which is included within the
switch statement.
Syntax
Flow Chart
Example
/* Enter your choice
1. Print "Hello"
2. Print "Hi" */
#include<stdio.h>
int main()
{
int c;
printf("Enter your choice\n 1.Print Hello\n 2.Print Hi\n");
scanf("%d",&c);
switch(c)
{
case 1 : printf("Hello");
break;
case 2 : printf("Hi");
break;
default : printf("Wrong choice");
break;
}
return 0;
}
Output 1 Output 2
Enter your choice Enter your choice
1.Print Hello 1.Print Hello
2.Print Hi 2.Print Hi
1 2
Hello Hi
Output 3
Enter your choice
1.Print Hello
2.Print Hi
4
Wrong choice
goto Statement
• C supports the goto statement to branch
unconditionally from one point to another in
the program.
• The goto requires a label in order to identify
the place where the branch is to made.
• A label is any valid variable name, and must
be followed by a colon.
• The label is placed immediately before the
statement where the control is to be
transferred.
General forms of goto statements are:
• The ‘label:’ can be anywhere in the program either
before or after the ‘goto label;’ statement.
• goto breaks the normal sequential execution of the
program.
• If the label: is before the statement goto label; a
loop will be formed and some statements will be
executed repeatedly. Such a jump is known as a
backward jump.
• If the label: is placed after the goto label; some
statements will be skipped and the jump is known
as a forward jump.
Decision Making & Looping
• Sequences of statements are executed until
some conditions for termination of the loop are
satisfied.
• A program loop therefore consists of two
segments, one known as the body of the loop
and the other known as the control statement.
• The control statement tests certain conditions
and then directs the repeated execution of the
statements contained in the body of the loop.
Entry - controlled and Exit - controlled Loops
• Depending on the position of the control statement
in the loop, a control structure may be classified
either as the entry-controlled loop or as the exit-
controlled loop.
• In the entry-controlled loop, the control conditions
are tested before the start of the loop execution.
• In the exit-controlled loop, the test is performed at
the end of the body of the loop and therefore the
body is executed unconditionally for the first time.
• The entry-controlled and exit-controlled loops are
also known as pre-test and post-test loops
respectively.
The C language provides three constructs
for performing loop operations. They are:
1) The while statement
2) The do..while statement
3) The for statement.
The while Statement
• The while is an entry-controlled loop statement.
• The test-condition is evaluated and if the
condition is true, then the body of the loop is
executed.
• This process of repeated execution of the body
continues until the test-condition finally becomes
false and the control is transferred out of the
loop.
• On exit, the program continues with the
statement immediately after the body of the loop.
Syntax
while(condition)
{
Body of loop
}
Flow Chart
Entry
False
Condition
True
Body of loop
Exit
The do….while Statement
• The do….while is an exit-controlled loop
statement.
• The body of loop is executed first.
• At the end, condition is evaluated and if the
condition is true, then the body of the loop is
executed again.
• This process of repeated execution of the body
continues until the test-condition finally becomes
false and the control is transferred out of the
loop.
Syntax
do
{
Body of loop
}while(condition);
Flow Chart Entry
Body of loop
False True
Condition
Exit
The for Statement
• The for loop is an entry-controlled loop.
• The general form of the for loop is :
for(initialization;test-condition;increment)
{
Body of loop
}
The execution of the for statement is as
follows:
1) Initialization of the control variables is
done first, using assignment statements.
2) The value of the control variable is tested
using the test condition.
○ If the condition is true, the body of the
loop is executed.
○ Otherwise the loop is terminated and
the execution continues with the
statement that immediately follows the
loop.
3) When the body of the loop is executed, the
control is transferred back to the for
statement after evaluating the last statement
in the loop.
○ Now the control variable is incremented /
decremented and the new value of the
control variable is again tested to see
whether it satisfies the loop condition.
○ If the condition is satisfied, the body of the
loop is again executed.
○ This process continues till the value of the
control variable fails to satisfy the test-
condition.
Comparison of the Three Loops
for(n=1;n<=10;n++) or
{ n=1;
………. do
} {
or ………..
n=1; n=n+1;
while(n<=10) }while(n<=10);
{
……….
n=n+1;
}
Question No. 4
Write a C program to find sum of digits of a
number.
Sum of digits of a number
Input : 687 ➢ To calculate the sum of digits of
a number, modulus operator (%)
Output : 21 is used to extract individual
(7 + 8 + 6) of a number and keep on
digits
adding them.
➢ Get the rightmost digit of the
Input : 12 number with help of the
remainder ‘%’ operator by
Output : 3 dividing it by 10 and add it to
sum.
(2 +➢
1)Divide the number by 10 with
help of ‘/’ operator to remove the
rightmost digit.
For example,
if the input is 98,
n = 98
The variable Sum is 0 initially
Sum = 0
d = 98%10
=8
Sum = Sum + d
=0+8=8
So Sum = 8 now.
n = 98/10 n
Sum = 0
=9
while(n!=0)
d = 9%10 {
=9 d=n%10
Sum = Sum + d Sum= Sum+d
= 8 + 9 = 17 n=n/10
}
n = 9/10 = 0 n=0
So finally, n = 0, the process stops.
ie, we get the required sum.
Sum = 17
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : Initialize Sum = 0
Step 4 : Check whether n != 0, if it is true, then
go to Step _. Otherwise, go to Step _.
Step 5 : d = n % 10
Step 6 : Sum = Sum + d
Step 7 : n = n / 10 go to Step _
Step 8 : Print Sum
Step 9 : Stop
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : Initialize Sum = 0
Step 4 : Check whether n != 0, if it is true, then
go to Step 5. Otherwise, go to Step 8.
Step 5 : d = n % 10
Step 6 : Sum = Sum + d
Step 7 : n = n / 10 go to Step 4
Step 8 : Print Sum
Step 9 : Stop
Flow Chart
Start
Read a number, n
Sum = 0
Output
Enter a number
264
Sum of digits=12
Question No. 5
Write a C program to check whether the
given number is palindrome or not.
Palindrome Number
Palindrome number is a number which
when reversed is equal to the original
number.
For example: 121, 12321, 1001 etc.
Logic to check palindrome number
● Get the number from user
● Hold the number in temporary variable
● Find the reverse of the number
● Compare the temporary variable with
reversed number
● If both numbers are same, print
palindrome number
● Else print not a palindrome number
For example,
Input number, n = 128
Store it in a temporary variable,
t=n
t = 128
Initialize a variable, which is used to
hold the reverse number, to 0.
rev = 0
d = n %10
= 128 % 10
=8
rev = (rev * 10) + d
= (0 * 10) + 8
=8
n = n / 10
= 128 / 10
= 12
d = n %10
= 12 % 10
=2
rev = (rev * 10) + d
= (8 * 10) + 2
= 82
n = n / 10
= 12 / 10
=1
d = n %10
= 1 % 10
=1
rev = (rev * 10) + d
= (82 * 10) + 1
= 821
n = n / 10
= 1 / 10
=0
So finally, n = 0, the process stops.
Compare the temporary variable with
reversed number
ie, t and rev
Here,
t ≠ rev
which implies
128 is not a palindrome number.
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : t = n
Step 4 : Initialize rev = 0
Step 5 : Check whether n != 0, if it is true,
then go to Step _. Otherwise, go to
Step _.
Step 6 : d = n % 10
Step 7 : rev = (rev * 10) + d
Step 8 : n = n / 10 go to Step _
Step 9 : If t == rev, then go to Step _.
Otherwise, go to Step _.
Step 10 : Print “Palindrome Number” go to
Step _.
Step 11 : Print “Not a Palindrome Number”
Step 12 : Stop
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : t = n
Step 4 : Initialize rev = 0
Step 5 : Check whether n != 0, if it is true,
then go to Step 6. Otherwise, go to
Step 9.
Step 6 : d = n % 10
Step 7 : rev = (rev * 10) + d
Step 8 : n = n / 10 go to Step 5
Step 9 : If t == rev, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Palindrome Number” go to
Step 12.
Step 11 : Print “Not a Palindrome Number”
Step 12 : Stop
Flow Chart
Start
Read a number, n True If False
t == rev
t=n
rev = 0
5%1=
5%2=
5%3=
5%4=
5%5=
Example
Consider 5,
Find the number of divisors of 5 from 1 to
5,
5%1=0
5%2=
5%3=
5%4=
5%5=
Example
Consider 5,
Find the number of divisors of 5 from 1 to
5,
5%1=0
5%2=1
5%3=
5%4=
5%5=
Example
Consider 5,
Find the number of divisors of 5 from 1 to
5,
5%1=0
5%2=1
5%3=2
5%4=
5%5=
Example
Consider 5,
Find the number of divisors of 5 from 1 to
5,
5%1=0
5%2=1
5%3=2
5%4=1
5%5=
Example
Consider 5,
Find the number of divisors of 5 from 1 to
5,
5%1=0
5%2=1
5%3=2
5%4=1
5%5=0
Example
Consider 5,
Find the number of divisors of 5 from 1 to
5,
5%1=0
5%2=1
5%3=2 Divisors of 5 = 1 and 5
5%4=1 Number of Divisors of 5 = 2
5%5=0 Therefore, 5 is a Prime number.
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=
7%2=
7%3=
7%4=
7%5=
7%6=
7%7=
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=
7%3=
7%4=
7%5=
7%6=
7%7=
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=1
7%3=
7%4=
7%5=
7%6=
7%7=
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=1
7%3=1
7%4=
7%5=
7%6=
7%7=
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=1
7%3=1
7%4=3
7%5=
7%6=
7%7=
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=1
7%3=1
7%4=3
7%5=2
7%6=
7%7=
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=1
7%3=1
7%4=3
7%5=2
7%6=1
7%7=
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=1
7%3=1
7%4=3
7%5=2
7%6=1
7%7=0
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=1
7%3=1
7%4=3
7 % 5 = 2 Divisors of 7 = 1 and 7
7 % 6 = 1 Number of Divisors of 7 = 2
7%7=0 Therefore, 7 is a Prime number.
Consider 6,
Find the number of divisors of 6 from 1 to
6,
6%1=
6%2=
6%3=
6%4=
6%5=
6%6=
Consider 6,
Find the number of divisors of 6 from 1 to
6,
6%1=0
6%2=
6%3=
6%4=
6%5=
6%6=
Consider 6,
Find the number of divisors of 6 from 1 to
6,
6%1=0
6%2=0
6%3=
6%4=
6%5=
6%6=
Consider 6,
Find the number of divisors of 6 from 1 to
6,
6%1=0
6%2=0
6%3=0
6%4=
6%5=
6%6=
Consider 6,
Find the number of divisors of 6 from 1 to
6,
6%1=0
6%2=0
6%3=0
6%4=2
6%5=
6%6=
Consider 6,
Find the number of divisors of 6 from 1 to
6,
6%1=0
6%2=0
6%3=0
6%4=2
6%5=1
6%6=
Consider 6,
Find the number of divisors of 6 from 1 to
6,
6%1=0
6%2=0
6%3=0
6%4=2
6%5=1
6%6=0
Consider 6,
Find the number of divisors of 6 from 1 to
6,
6%1=0
6%2=0
6%3=0
6 % 4 = 2 Divisors of 6 = 1, 2, 3 and 6
6%5=1 Number of Divisors of 6 = 4
Therefore, 6 is not a Prime number.
6%6=0
Logic to check prime number
● Get the number from user
● Find the number of divisors of the given
number
● If number of divisors is 2, then print
prime number
● Else print not a prime number
Consider 7,
Find the number of divisors of 7 from 1 to
7,
7%1=0
7%2=1
7%3=1
7%4=3
7 % 5 = 2 Divisors of 7 = 1 and 7
7 % 6 = 1 Number of Divisors of 7 = 2
7%7=0 Therefore, 7 is a Prime number.
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step _. Otherwise, go to Step _.
Step 6 : If n % i == 0, then go to Step _.
Otherwise, go to Step _.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step _.
Step 9 : If count == 2, then go to Step _.
Otherwise, go to Step _.
Step 10 : Print “Prime Number” go to Step _.
Step 11 : Print “Not a Prime Number”
Step 12 : Stop
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Step 9 : If count == 2, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Prime Number” go to Step 12.
Step 11 : Print “Not a Prime Number”
Step 12 : Stop
Flow Chart
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Flow Chart
Start
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Flow Chart
Start
Read a number, n
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Flow Chart
Start
Read a number, n
count = 0
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Flow Chart
Start
Read a number, n
count = 0
i=1
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Flow Chart
Start
Read a number, n
count = 0
i=1
count = 0
False If
n % i == 0
i=1
count = 0
False If
n % i == 0
i=1
i=i+1
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Flow Chart
Start
Read a number, n
count = 0
False If
i=1
n % i == 0
i=i+1
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : count = 0
Step 4 : Initialize i = 1
Step 5 : Check whether i <= n, if it is true, then
go to Step 6. Otherwise, go to Step 9.
Step 6 : If n % i == 0, then go to Step 7.
Otherwise, go to Step 8.
Step 7 : count = count + 1
Step 8 : i = i + 1 go to Step 5.
Step 9 : If count == 2, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Prime Number” go to Step 12.
Step 11 : Print “Not a Prime Number”
Step 12 : Stop
Step 9 : If count == 2, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Prime Number” go to Step 12.
Step 11 : Print “Not a Prime Number”
Step 12 : Stop
Step 9 : If count == 2, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Prime Number” go to Step 12.
Step 11 : Print “Not a Prime Number”
Step 12 : Stop
Step 9 : If count == 2, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Prime Number” go to Step 12.
Step 11 : Print “Not a Prime Number”
Step 12 : Stop
Step 9 : If count == 2, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Prime Number” go to Step 12.
Step 11 : Print “Not a Prime Number”
Step 12 : Stop
Step 9 : If count == 2, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Prime Number” go to Step 12.
Step 11 : Print “Not a Prime Number”
Step 12 : Stop
Flow Chart
Start
Read a number, n
count = 0
True If False
False If i=1 count == 2
n % i == 0