Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

EST102 Programming in C

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 302

EST102

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

Operators and Expressions: Expressions and Arithmetic


Operators, Relational and Logical Operators, Conditional
operator, size of operator, Assignment operators and Bitwise
Operators, Operators Precedence

Control Flow Statements: If Statement, Switch Statement,


Unconditional Branching using goto statement, While Loop,
Do While Loop, For Loop, Break and Continue statements
Important!!
➔ Structure of a C program
➔ Types of Preprocessor directives
➔ C Tokens
➔ Rules for Identifier / variables - Examples
➔ Type definition and Enumerated data type
➔ printf and scanf function
➔ Types of operators in C
➔ switch statement
➔ Entry - controlled VS Exit - controlled Loops
➔ while, do….while VS for loop
➔ break VS continue statements -Examples
➔ Programs with syntax errors
➔ C Programs - Palindrome, Armstrong, Prime
Number, Perfect Number, Leap Year or not
Basic structure of C
program
Structure of C Program
1.Documentation Section
It is the section in which you can give
comments to make the program more
interactive. The compiler won’t compile
this and hence this portion would not be
displayed on the output screen.

2. Preprocessor directives Section


This section involves the use of header files
that are to included necessarily program.
3.Definition section
This section involves the constant
variable’s definition and declaration in C.

4. Global declaration Section


This section is used to define the global
variables to be used in the programs, that
means you can use these variables
throughout the program.
5. Function prototype declaration section
This section gives the information about a
function that includes, the data type or the
return type, the parameters passed or the
arguments.

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.

7. User-defined function section


When you want to define your function that
fulfills a particular requirement, you can define
them in this section.
C Program
/* C program to print “Hello World” */
#include <stdio.h> Printf
int main() (

{ Hello
printf("Hello, World! \n"); ,
return 0; World
!
}
\
n
C Program
/* C program to print “Hello World” */
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return 0;
}
The line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in
the program.
C Program
// C program to print “Hello World”
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return 0;
}
The line // will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in
the program.
C Program
/* C program to print “Hello World” */
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return 0;
}
#include <stdio.h> is a preprocessor command, which tells a C
compiler to include stdio.h file before going to actual compilation.
C Program
/* C program to print “Hello World” */
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return 0;
}
The next line int main() is the main function where the
program execution begins.
C Program
/* C program to print “Hello World” */
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return 0;
}
The next line printf(...) is another function available in C which
causes the message "Hello, World!" to be displayed on the screen.
C Program
/* C program to print “Hello World” */
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return 0;
}
The next line return 0; terminates the main()
function and returns the value 0.
C Program
/* C program to print “Hello World” */
#include <stdio.h>
int main()
{
printf("Hello, World! \n");
return 0;
}
Two curly brackets "{...}" are used to group all statements.
It shows how much the main() function has its scope.
Preprocessor Directives
• Preprocessor directives are lines in our
program that start with #.
• The # is followed by an identifier that is
the directive name.
• Whitespace is also allowed before and
after the #.
1) #include
• The #include preprocessor directive is used to paste
code of given file into current file.
• It is used include system-defined and user-defined
header files.
• If included file is not found, compiler renders error.
It has three variants:
#include <file>
• This variant is used for system header files. It
searches for a file named file in a list of directories
specified by you, then in a standard list of system
directories.
#include "file"
• This variant is used for header files of your own
program. It searches for a file named file first in the
current directory, then in the same directories used
for system header files. The current directory is the
directory of the current input file.
#include anything else
• This variant is called a computed #include.
Any #include directive whose argument does not fit
the above two forms is a computed include.
2) #define
• #define is used to create symbolic
constants and the statement is called a
macro.
Syntax :
#define identifier value

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.

X Y X&Y X|Y X^Y


The result of the computation of bitwise logical
operators is shown in the table given below.

X Y X&Y X|Y X^Y


0 0
0 1
1 0
1 1
The result of the computation of bitwise logical
operators is shown in the table given below.

X Y X&Y X|Y X^Y


0 0 0
0 1 0
1 0 0
1 1 1
The result of the computation of bitwise logical
operators is shown in the table given below.

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 result of the computation of bitwise logical
operators is shown in the table given below.

X Y X&Y X|Y X^Y


0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise AND
• This is one of the most commonly used
logical bitwise operators.
• It is represented by a single ampersand sign
(&).
• Two integer expressions are written on each
side of the operator.
• The result of the bitwise AND operation is 1
if both the bits have the value as 1;
otherwise, the result is always 0.
Let us consider two variables X and Y with
values as follows:
X = 0000 1101
Y = 0001 1001
X = 0000 1101 &
Y = 0001 1001
X = 0000 1101 &
Y = 0001 1001
1
X = 0000 1101 &
Y = 0001 1001
01
X = 0000 1101 &
Y = 0001 1001
001
X = 0000 1101 &
Y = 0001 1001
1001
X = 0000 1101 &
Y = 0001 1001
0000 1001
Let us consider two variables X and Y with
values as follows:
X = 0000 1101
Y = 0001 1001
The result of the AND operation on variables
X and Y will be,
Result = 0000 1001
Two variables are compared bit by bit.
Whenever the value of a bit in both the
variables is 1, then the result will be 1 or else
0.
Bitwise OR
• It is represented by a single vertical bar
sign (|).
• Two integer expressions are written on each
side of the operator.
• The result of the bitwise OR operation is 1 if
at least one of the expression has the value
as 1; otherwise, the result is always 0.
Let us consider two variables X and Y with
values as follows:
X = 0000 1101
Y = 0001 1001
X = 0000 1101 |
Y = 0001 1001
X = 0000 1101 |
Y = 0001 1001
1
X = 0000 1101 |
Y = 0001 1001
01
X = 0000 1101 |
Y = 0001 1001
101
X = 0000 1101 |
Y = 0001 1001
1101
X = 0000 1101 |
Y = 0001 1001
0001 1101
Let us consider two variables X and Y with
values as follows:
X = 0000 1101
Y = 0001 1001
The result of the OR operation on variables X
and Y will be,
Result = 0001 1101
Two variables are compared bit by bit.
Whenever the value of a bit in one of the
variables is 1, then the result will be 1 or else
0.
Bitwise Exclusive OR
• It is represented by a symbol (^).
• Two integer expressions are written on each
side of the operator.
• The result of the bitwise exclusive OR
operation is 1 if only if both inputs have
different values, otherwise, the result is
always 0.
Let us consider two variables X and Y with
values as follows:
X = 0000 1101
Y = 0001 1001
X = 0000 1101 ^
Y = 0001 1001
X = 0000 1101 ^
Y = 0001 1001
0
X = 0000 1101 ^
Y = 0001 1001
00
X = 0000 1101 ^
Y = 0001 1001
100
X = 0000 1101 ^
Y = 0001 1001
0100
X = 0000 1101 ^
Y = 0001 1001
0001 0100
Let us consider two variables X and Y with
values as follows:
X = 0000 1101
Y = 0001 1001
The result of the exclusive OR operation on
variables X and Y will be,
Result = 0001 0100
Two variables are compared bit by bit. If both
bits have same value, then the result is 0,
otherwise result is 1.
Bitwise shift operators
• The bitwise shift operators are used to
move/shift the bit patterns either to the left or
right side.
• Left and right are two shift operators provided
by 'C' which are represented as follows:
Operand << n (Left Shift)
Operand >> n (Right Shift)
• An Operand is an integer expression on which
we have to perform the shift operation.
• 'n' is the total number of bit positions that we
have to shift in the integer expression.
• The left shift operation will shift the 'n' number
of bits to the left side. The leftmost bits in the
expression will be popped out, and n bits with
the value 0 will be filled on the right side.

• The right shift operation will shift the 'n'


number of bits to the right side. The rightmost
'n' bits in the expression will be popped out, and
the value 0 will be filled on the left side.
Example
x is an integer expression with data 1111.
After performing shift operation the result will
be:

x << 2 (left shift) = 1111<<2 = 1100


x>>2 (right shift) = 1111>>2 = 0011
Special Operators
Operator Precedence and
Associativity
• The precedence is used to determine how an
expression involving more than one operator is
evaluated. 5+4*2/1
• The operators at the higher level of precedence are
evaluated first.
• The operators of the same precedence are evaluated
either from left to right or right to left, depending
on the level. This is known as the associativity
property of an operator.
https://docs.google.com/document/d/1Haom
fQ9pUv9gU-Y69sXPQ9y2F1oPXMRkj6FC
C25YgGM/edit?usp=sharing
Control Flow
Statements
Decision Making & Branching

• This statements Statements


are used to work a particular
block of statements according to a condition.
• It is classified into four types:
1) if Statement
2) switch Statement
3) Conditional Operator Statement
4) goto Statement
if Statement
• The if statement is used to control the flow of
execution of statements.
• It is a two-way decision statement and is used in
conjunction with an expression.
• The if statement may be implemented in different
forms depending on the complexity of conditions to
be tested. The different forms are:
i. Simple if statement
ii. if.....else statement
iii. Nested if…else statement
iv. Else if ladder
Simple if Statement
if(expression)
{
Statement-block;
}
statement-x;
False
if….else Statement
if(expression)
{
Statement-block
1;
}
else
{
Statement-block
2;
}
Question No. 1
Write a C program to check whether the
given number is odd or even.
Algorithm
Step 1 : Start
Step 2 : Read number, n
Step 3 : Remainder = n % 2
Step 4 : If Remainder == 0, then, go to Step 5,
otherwise go to Step 6.
Step 5 : Print “Even Number” go to Step 7.
Step 6 : Print “Odd Number”
Step 7 : Stop
Flow Chart
Start

Read number, n

Remainder = n % 2
If
True False
Remainder == 0

Print “Even Number” Print “Odd Number”

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

Check whether False


n != 0
True
Print Sum
d = n % 10
Sum = Sum + d Stop
n = n / 10
Program
#include<stdio.h>
int main()
{
int n,Sum,d;
printf("Enter a number\n");
scanf("%d",&n);
Sum=0;
while(n!=0)
{
d=n%10;
Sum=Sum+d;
n=n/10;
}
printf("Sum of digits=%d",Sum);
return 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

Check whether False


n != 0
Print Print
True “Palindrome “Not a Palindrome
d = n % 10 Number” Number”
rev = (rev * 10) + d
n = n / 10
Stop
Program
#include<stdio.h>
int main()
{
int n,t,rev,d;
printf("Enter the Number\n");
scanf("%d",&n);
t=n;
rev=0;
while(n!=0)
{
d = n % 10;
rev = (rev * 10) + d;
n = n / 10;
}
if(t==rev)
{
printf("Palindrome Number\n");
}
else
{
printf("Not a Palindrome Number\n");
}
return 0;
}
Output 1 Output 2
Enter the Number Enter the Number
121 165
Palindrome Number Not a Palindrome Number
Question No. 6
Write a C program to check whether the
given number is armstrong or not.
Armstrong Number
Armstrong number is a number that is
equal to the sum of cubes of its digits.
For example,
0, 1, 153, 370, 371 and 407 are the
Armstrong numbers.
ie,
153 = (1*1*1)+(5*5*5)+(3*3*3)
=1+125+27=153
Logic to check armstrong number
● Get the number from user
● Hold the number in temporary variable
● Find the sum of cubes of digits in the
number
● Compare the temporary variable with
sum
● If both numbers are same, print
armstrong number
● Else print not an armstrong number
For example,
Input number, n = 371
Store it in a temporary variable,
t=n
t = 371
Initialize a variable, which is used to
hold the sum of cubes of digits in the
number, to 0.
Sum = 0
d = n %10
= 371 % 10
=1
Sum = Sum + (d * d * d)
= 0 + (1 * 1 * 1)
=1
n = n / 10
= 371 / 10
= 37
d = n %10
= 37 % 10
=7
Sum = Sum + (d * d * d)
= 1 + (7 * 7 * 7)
= 344
n = n / 10
= 37 / 10
=3
d = n %10
= 3 % 10
=3
Sum = Sum + (d * d * d)
= 344 + (3 * 3 * 3)
= 371
n = n / 10
= 3 / 10
=0
So finally, n = 0, the process stops.
Compare the temporary variable with sum
ie, t and Sum
Here,
t = Sum
which implies
371 is an armstrong number.
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : t = n
Step 4 : Initialize Sum = 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 : Sum = Sum + (d * d *d)
Step 8 : n = n / 10 go to Step _
Step 9 : If t == Sum, then go to Step _.
Otherwise, go to Step _.
Step 10 : Print “Armstrong Number” go to
Step _.
Step 11 : Print “Not an Armstrong Number”
Step 12 : Stop
Algorithm
Step 1 : Start
Step 2 : Read a number, n
Step 3 : t = n
Step 4 : Initialize Sum = 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 : Sum = Sum + (d * d *d)
Step 8 : n = n / 10 go to Step 5
Step 9 : If t == Sum, then go to Step 10.
Otherwise, go to Step 11.
Step 10 : Print “Armstrong Number” go to
Step 12.
Step 11 : Print “Not an Armstrong Number”
Step 12 : Stop
Flow Chart
Start
Read a number, n True If False
t == Sum
t=n
Sum = 0

Check whether False


n != 0
Print Print
True “Armstrong “Not an Armstrong
d = n % 10 Number” Number”
Sum=Sum+(d*d*d)
n = n / 10
Stop
Program
#include<stdio.h>
int main()
{
int n,t,Sum,d;
printf("Enter the Number\n");
scanf("%d",&n);
t=n;
Sum=0;
while(n!=0)
{
d=n%10;
Sum=Sum+(d*d*d);
n=n/10;
}
if(t==Sum)
{
printf("Armstrong Number\n");
}
else
{
printf("Not an Armstrong Number\n");
}
return 0;
}
Output 1 Output 2
Enter the Number Enter the Number
153 121
Armstrong Number Not an Armstrong Number
Question No. 7
Write a C program to check whether the
given number is prime or not.
Prime Number
A prime number is a positive integer that is
divisible only by 1 and itself.
For example,
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ………….
Example
Consider 5,
Find the number of divisors of 5 from 1 to
5,

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

True Check whether False


i <= 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.
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
n % i == 0
i=1

True True Check whether False


i <= 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.
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
n % i == 0
i=1

True True Check whether False


count = i <= n
count + 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

True True Check whether False


count = i <= n
count + 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.
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

True True Check whether False Print “Not a


count = i <= n Prime
Number”
count + 1
Print “Prime
Number”
i=i+1
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
Program
#include<stdio.h>
int main()
{
int n,count,i;
printf("Enter a number\n");
scanf("%d",&n);
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
printf("Prime Number\n");
}
else
{
printf("Not a Prime Number\n");
}
return 0;
}
Output 1 Output 2
Enter a number Enter a number
5 9
Prime Number Not a Prime Number
Question No. 8
Write a C program to print prime numbers
within the given intervals.
1 to 100 => 2 to 100
2
3
5
…..
97
Program
#include<stdio.h>
int main()
{
int n,i,count;
printf("Prime numbers\n");
for(n=2;n<=100;n++)
{
count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
printf("%d\n",n);
}
}
return 0;
}
Output
Prime numbers
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
The break Statement
• When a break statement is encountered inside
a loop, the loop is immediately exited and the
program continues with the statement
immediately following the loop.
• When the loops are nested, the break would
only exit from the loop containing it.
• The break will exit only a single loop.
-----
The continue Statement
• The continue causes the loop to be continued
with the next iteration after skipping any
statements in between.
• In while and do loops, continue causes the
control to go directly to the test-condition and
then to continue the iteration process.
• In the case of for loop, the increment section
of the loop is executed before the test-
condition is evaluated.
Question No. 9
What will be the output of the following C
code?
#include <stdio.h>
int main()
{
int i = 0;
if (i == 0) Output
{ Compile time error -
printf("Hello");
continue;
Continue statement
} not within a loop
return 0;
}
Question No. 10
What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 0;
while (i < 2)
{
if (i == 1)
break;
i++;
if (i == 1)
continue;
printf("In while loop\n");
}
printf("After loop\n");
return 0;
}
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int i = 0; int i = 0;
while (i < 2)
while (i < 2)
{
{
if (i == 1)
if (i == 1) {
break; break;
i++; or }
if (i == 1) i++;
continue; if (i == 1)
printf("In while loop\n"); {
} continue;
printf("After loop\n"); }
return 0; printf("In while loop\n");
}
}
printf("After loop\n");
return 0;
}
#include <stdio.h>
int main() i=0
{ while (i < 2)
while (i < 2)
int i = 0; => while (0 < 2)
while (i < 2) => True => while (1 < 2)
{ => True
if (i == 1) if (i == 1)
{ => if (0 == 1) if (i == 1)
break; => False => if (1 == 1)
}
i++;
i++ => True
if (i == 1) => i = i + 1 break
{ =0+1=1 After loop
continue; if(i ==1)
}
printf("In while loop\n"); =>if (1==1)
=>True
}
Output
printf("After loop\n"); continue
return 0; After Loop
}
Questions
1) Write a C program to read a Natural Number through keyboard and to display
the reverse of the given number. For example, if “3214567” is given as input,
the output to be shown is “7654123”. (3 Marks)
2) Compare while loop and do-while loop in C with an example. (2 Marks)
3) Write a C program to print the factors of a given number. (3 Marks)
4) Is it advisable to use goto statements in a C program? Justify your answer.
(3 Marks)
5) Explain the use of continue statement with the help of an example. (3 Marks)
6) Explain switch construct with an example. (3 Marks)
7) Write a program to check whether a number is perfect or not. (3 Marks)
8) Differentiate break and continue statements with examples. (5 Marks)
9) Give the syntax of “do..while” loop. How is it different from “while” loop?
Explain with examples. (5 Marks)
10) Write a C program to check whether a number is Armstrong or not. (3
Marks)
Thank you…….

You might also like