SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
18CSS101J – Programming for Problem Solving
Unit II - Looping
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
2. 8 Looping Statements
Loop – A segment of the program that is executed repeatedly
until a condition is satisfied
Classification – Entry Controlled & Exit Controlled
Types
a) while do loop
b) do while loop
c) for loop
i. Nested for loop
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
2. 8 Looping Statements Contd...
a) The While Loop
Simplest looping structure in C
Statements in the program may need to repeat
for many times. e.g., calculate the value of n!
Loop consists of two segments
Control Statement
Body of the Loop
How while loop works?
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
Initialize loop counter
variable;
while (condition)
{
Statements;
increment /
Decrement loop
counter
variable;
}
/* Program to Add 3 Numbers*/
#include<stdio.h>
int main( )
{
int a, b, c,
sum;
printf(“\n Enter the Three Numbers: ”);
scanf(“%d%d%d”, &a,&b,&c);
sum = a+b+c;
printf(“The sum of 3 Numbers is %d”, sum);
return 0;
}
Output
Enter the Three Numbers: 10 20 30
The sum of 3 Numbers is: 60
/* Program to Add 1 to n
Numbers*/
#include<stdio.h>
int main( )
{
int i=1,n,
sum=0;
printf(“\n Enter the value for n: ”);
scanf(“%d”, &n);
while (i<=n)
{
sum = sum + i;
i++;
}
printf(“The sum of n Numbers is: %d”, sum);
return 0;
}
Output
Enter the value for n: 5
The sum of n Numbers is: 15
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 8 Looping Statements Contd...
Try it Out Yourself ! Write a C program to:
1) To print all even numbers from 1 to 100
2) To print all even numbers from 1 to n
3) To print table for any number
4) To calculate the sum of its digits
5) To check whether the entered number is Prime or not
6) To get a number as input and print it in reverse.
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 8 Looping Statements Contd...
b) The Do While Loop
The body of the loop is executed at least once
Syntax
do
{
statements;
}
while (condition);
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
Initialize loop counter
variable; do
{
Statements;
increment /
Decrement loop
counter
variabl
e;
}
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 8 Looping Statements Contd...
While Do Loop Do While Loop
Entry Controlled Loop Exit Controlled Loop
Test condition is checked Test condition is checked after the
before body of the loop is executed body of the loop is executed
Loop will not be executed Loop will be executed at least once
if condition is false even if condition is false
Top tested loop Bottom tested loop
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 8 Looping Statements Contd...
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 8 Looping Statements Contd...
c) The for loop
Most commonly and popularly used loop structure
Structure of the for loop
Initialize loop counter variable
Check for condition
Increment / Decrement the loop counter variable
Syntax
for(initialization; condition; increment / decrement)
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 8 Looping Statements Contd...
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 8 Looping Statements Contd...
Examples
i. for(i = 0; i < n; i++)
{
Statements;
}
ii. for(count = 0; count > n; count--)
{
Statements;
}
/* Program to Add n Numbers using for loop */
#include<stdio.h>
int main( )
{
int i, n,
sum=0;
printf(“\n Enter the value for n: ”);
scanf(“%d”, &n);
for (i =1; i<=n; i++)
{
sum = sum + i;
}
printf(“The sum of n Numbers is:
%d”, sum);
return 0;
}
Output
Enter the value for n: 5
The sum of n Numbers is: 15
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 8 Looping Statements Contd...
Try it Out Yourself ! Write a C program to:
1) To print all even numbers from 1 to 100
2) To print all even numbers from 1 to n
3) To print table for any number
4) To calculate the sum of its digits
5) To check whether the entered number is Prime or not
6) To get a number as input and print it in reverse.
SR
INSTITUTEMOF SCIENCE AND
TECHNOLOGY,
CHENNAI.
break
The break statement ends the loop
immediately when it is encountered.
Its syntax is: break;
The break statement is almost always
used with if...else statement inside the
loop.
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
2. 9 Arrays
Definition
An array is defined as finite ordered collection
of homogenous data, stored in contiguous memory
locations.
Array is used to store a collection of data
First Array is a collection of variables of the same type.
Last
Element Element
Numbers Numbers ……. Numbers[
[0] [1] n]
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
Need for Arrays
Used to represent a list of numbers / names
Used to represent tabular data in 2, 3 or more dimensions
Important Data Structure in any programming language
Definition
Collection of elements of similar data types
Each element is located in separate memory locations
Each Array element share a common name
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
2. 9 Arrays Contd...
Characteristics of Arrays
All elements in the arrays share a common name
Elements distinguished by index number
Index (or) element number of an array plays vital role for calling
each element
Specific array elements can be modified
Value of array element can be assigned to variables
Array elements stored in continuous memory locations
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
Storage space for array depends on its data type and size
Total bytes = sizeof (Data type) x Size of Array
Example
int a [5];
Total bytes = sizeof (int) x 5 = 2 x 5 = 10 bytes
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
a) Array Declaration
Syntax
Datatype arrayname [size/subscript];
Data Type: int, float, double, char, structure, union
Array Name: Name given to the Array variable
Size / Subscript: Number of values an Array can hold
Examples
int numbers[5]; float marks[50];
char name[20]; double a[i];
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
Illustration
int a[n];
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
Static Array: Array size (range) declared in the program
Dynamic Array: Array size given during execution
STATIC ARRAYS DYNAMIC ARRAYS
Range / Size of an array included Range / Size of an array
in the Array definition not included in the Array
definition
Static Arrays cannot be changed Dynamic Arrays can be changed
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
b) Array Initialization
Initialization: Assigning values to array elements
Values specified in curly braces separated by commas
Examples
int a[ 5] = {1, 2, 3, 4, 5};
float b[3] = { 40.5, 59.0, 98.5};
char name[6] = ” SRMIST”;
Array element index start from 0
SR
M AND TECHNOLOGY,
INSTITUTE OF SCIENCE
CHENNAI.
2. 9 Arrays Contd...
Array elements are called by array names followed by the
element numbers
int a[ 5] = {1, 2, 3, 4, 5};
a[0] refers to 1st element i.e., 1
a[1] refers to 2nd element i.e., 2
a[2] refers to 3rd element i.e., 3
a[3] refers to 4th element i.e., 4
a[4] refers to 5th element i.e., 5
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
c) Getting Input for Arrays
Use for loops to get input in arrays
Use for loops with regard to the Array’s dimension
Input for One Dimensional Arrays – 1 for
loop for(i = 0; i < 5; i++)
{
scanf(“%d”, &a[i]);
}
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
Input for Two Dimensional Arrays – 2 for loops
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf(“%d”,&a[i][j]);
}
}
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
d) Printing Output in Arrays
Use for loops to print array output
Use for loops with regard to the Array’s dimension
Printing One Dimensional Array Output – 1 for
loop for(i=0;i<5;i++)
{
printf(“%d”,a[i]);
}
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
Printing Two Dimensional Array Output – 2 for loops
for(i = 0; i < 5; i++)
{
for(j=0; j < 5; j++)
{
printff(“%d”, a[i][j]);
}
}
/* Program 1 : Array Declaration & Initialization*/
#include<stdio.h>
int main( )
{
int i, arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for(i=0; i<=n; i++)
{
printf(“%d”\
n, a[i]);
}
return 0;
Output
10
20
30
40
50
/* Program 2 : Array Declaration & Initialization*/
#include<stdio.h>
int main( )
{
int i, arr[5];
arr[5] = {10,
20, 30, 40,
50};
for(i=0; i<=n;
i++)
{
printf(“%
d”, a[i]);
}
return 0;
Output
10
20
30
40
50
/* Program 3 : Array Declaration & Initialization*/
#include<stdio.h>
int main( )
{
int i, n, arr[5];
scanf(“%d”,
&n);
printf(“Enter
the Elements
of Array\n”);
for(i=0; i<n;
i++)
{
scanf(“%d
for(i=0; i<n; i++)
{
printf(“%d”, a[i]);
}
return 0;
}
Output
Enter the Elements of the Array
10 20 30 40 50
The Elements of the Array are
10 20 30 40 50
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
e) Classification of Arrays
i. One-Dimensional Array
ii. Two-Dimensional Array
iii. Multi-Dimensional Array
SR
M SCIENCE AND
INSTITUTE OF
TECHNOLOGY,
CHENNAI.
2. 9 Arrays Contd...
i. One Dimensional Array
Data stored under a single variable using one subscript
1-D Array Declaration – Syntax
datatype arrayname [size/subscript];
Example: int a [5];
1-D Array initialization – Syntax
datatype arrayname [size] = { list of values};
Example: int a [5] = { 10, 20, 30, 40, 50};
/* Program 1 : One Dimensional Array*/
a
#include<stdio.h> a [10]
40
int main ( ) [0] 22
{ a 34
[1] 12
int a[10], n, i, sum;
a 64
clrscr( ); [2]
printf(“Enter the Number of Elements\n”); a
[3]
scanf(“%d”, &n);
a
for(i = 0; i < n; i++) [4]
{ a
scanf(“%d”, & a [i]); [5] i
} 5a 0
[6]
sum = 0;
a su
for(i = 0; i < n; i++) [7] m0
a
/* Program 1 : One Dimensional Array*/
{ a
sum = sum + a[i]; a [10]
40
[0] 22
}
a 34
printf(“The Sum is: %d”, sum); [1] 12
return 0; a 64
[2]
}
a
[3]
Output
a
Enter the Number of Elements [4]
5 a
40 22 34 12 64 [5] i
The Sum is 182 5a 4
[6]
a su
[7] m
182
a
THANK
YOU