0% found this document useful (0 votes)
73 views95 pages

PPS Module-3 2

Loop is used to repeatedly execute a block of code. It executes the code multiple times according to the condition. The while loop is an entry-controlled loop that repeatedly executes the statements inside the body as long as the given condition is true. Pseudo-code is used to design the logic and flow of a program without using a specific programming syntax. It involves initializing variables, reading input, using a while loop to iterate from 1 to n and performing checks within the loop.

Uploaded by

Kapil Bhatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views95 pages

PPS Module-3 2

Loop is used to repeatedly execute a block of code. It executes the code multiple times according to the condition. The while loop is an entry-controlled loop that repeatedly executes the statements inside the body as long as the given condition is true. Pseudo-code is used to design the logic and flow of a program without using a specific programming syntax. It involves initializing variables, reading input, using a while loop to iterate from 1 to n and performing checks within the loop.

Uploaded by

Kapil Bhatt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

Looping

What is loop?
 Loop is used to execute the block of code several times according to the condition
given in the loop. It means it executes the same code multiple times.

“Hello” 5

Output

printf("Hello\n"); Hello
printf("Hello\n"); Hello loop(condition)
{
printf("Hello\n"); Hello //statements
printf("Hello\n"); Hello }

printf("Hello\n"); Hello
if v/s while

Flowchart of if v/s Flowchart of while

False False
condition condition

True True

… …
Looping or Iterative Statements in C
Looping Statements are”

Entry Controlled Loop: while, for

Exit Controlled Loop: do…while

Virtual Loop: goto


While loop
While Loop
 while is an entry controlled loop

 Statements inside the body of while are repeatedly executed till the condition is
true

 while is keyword Syntax


while(condition)
{
// Body of the while
// true part
}
WAP to print 1 to n (while loop)
Program
Output
1 #include <stdio.h>
Enter n:10
2 void main()
1
3 {
2
4 int i,n;
3
5 i=1;
4
6 printf("Enter n:");
5
7 scanf("%d",&n);
6
8 while(i<=n)
7
9 {
8
10 printf("%d\n",i);
9
11 i=i+1;
10
12 }
13 }
WAP to print multiplication table (while loop)

Program Output

Enter n for multiplication table:5


1 #include<stdio.h> 5 * 1 = 5
2 void main() 5 * 2 = 10
3 { 5 * 3 = 15
5 * 4 = 20
4 int i=1,n; 5 * 5 = 25
5 printf("Enter n for multiplication table:"); 5 * 6 = 30
6 scanf("%d",&n); 5 * 7 = 35
5 * 8 = 40
7 while(i<=10) 5 * 9 = 45
8 { 5 * 10 = 50
9 printf("%d * %d = %d\n",n,i,n*i);
10 i=i+1;
11 }
12 }
WAP to Sum of 5 numbers entered by user(while loop)
Program Output

1 #include<stdio.h>
2 void main() Enter a number=10
3 { Enter a number=20
Enter a number=30
4 int sum=0, i=1,n;
Enter a number=40
5 while(i<=5)
Enter a number=50
6 { Sum is=150
7 printf("Enter a number=");
8 scanf("%d",&n);
9 sum=sum+n;
10 i=i+1;
11 }
12 printf("Sum is=%d",sum);
13 }
Syntax and Logic
Swimming Rules To Swim
1. Breath control
2. Kicking legs
3. Back stroke with arms
4. Front stroke with arms
5. Crawling in water

Syntax Logic
while(condition) int i = 1;
{ while (i <= 5)
// Body of the while {
// true part printf("%d\n", i);
} i=i+1;
}
How to build logic? Step-1
Step 1: Understand the problem statement
 e.g. Write a program to find factors of a number.
 Run following questions through mind
 What is the factor of a number?
 Factor is a number that divides another number evenly with no remainder.
 For example, 1,2,3,4,6,12 are factors of 12.
 How many variables needed? What should be their data types?(Inputs/Outputs)
 To get number from user we need variable n.
 Now we need to divide n with 1,2,3,...,n. For this we will declare a loop variable i initialized as 1.
 Both variables should be of integer data type.
 What control structure you require?
 First we need a loop to divide n by 1,2,3,…,n, loop will start from 1 and ends at n.
 Inside loop we need if structure to check n%i==0 (Number n is evenly divisible by i or not).
How to build logic? Step-2
Step 2: Think for 1 or 2 examples
 Consider n=6, now take i=1
 6%1==0, TRUE; So, 1 is factor of 6
 6%2==0, TRUE; So, 2 is factor of 6
 6%3==0, TRUE; So, 3 is factor of 6
 6%4==2, FALSE; S0, 4 is not factor of 6
 6%5==1, FALSE; S0, 5 is not factor of 6
 6%6==0, TRUE; S0, 6 is factor of 6

 From this we can infer that loop variable I starts with 1 and incremented by one
for next iteration then ends at value n.
 Consider n=10, factors are 1,2,5,10
 Consider n=11, factor is 1,11
 From this we can infer that 1 and number itself are always factors of any number n.
How to build logic? Step-3
Step 3: Draw flowchart/steps on paper or in mind
Start
Steps
i=1 Step 1: Start
Step 2: Declare variables n,i
read n Step 3: Initialize variable
i ← 1
Step 4: Read value of n
i<=n?
True False
Step 5: Repeat the steps until i = n
5.1: if n%i == 0
n%i==0? Display i
True False 5.2: i=i+1
print i Step 7: Stop

i=i+1
Stop
How to build logic? Step-4
Step 4: Writing Pseudo-code
 Pseudo-code is an informal way to express the design of a computer program or
an algorithm.
 It does not require any strict programming language syntax.
Pseudo-code
Initialize i=1 integer
Declare n as integer
Input n
while i<n
if n%i
print i
end if
increment i=i+1
end while
WAP to find factors of a number(while loop)

Program Output
1 #include <stdio.h> Enter n to find factors=12
2 void main() 1,2,3,4,6,12,
3 {
4 int i=1,n;
5 printf("Enter n to find factors=");
6 scanf("%d",&n);
7 while(i<=n)
8 {
9 if(n%i==0)
10 printf("%d,",i);
11 i=i+1;
12 }
13 }
WAP to print reverse a number(while loop)

Program Output
1 #include <stdio.h> Enter a number=1234
2 void main() 4321
3 {
4 int n;
5 printf("Enter a number=");
6 scanf("%d",&n);
7 while(n!=0)
8 {
9 printf("%d",n%10);
10 n=n/10;
11 }
12 }
WAP to check given number is perfect or not(while loop)
1 void main(){
2 int i=1,n,sum=0;
3 printf("Enter a number:"); Output
4 scanf("%d",&n); Enter a number:6
5 while(i<n) 1+2+3=6
6 is a perfect number
6 {
7 if(n%i==0)
Output
8 {
9 printf("%d+",i); Enter a number:8
1+2+4+=7
10 sum=sum+i;
8 is not a perfect number
11 }
12 i=i+1;
Output
13 }
14 printf("=%d",sum); Enter a number:496
1+2+4+8+16+31+62+124+248+=496
15 if(sum==n) 496 is a perfect number
16 printf("\n%d is a perfect number",n);
17 else
18 printf("\n%d is not a perfect number",n);
19 }
WAP to check given number is prime or not(while loop)
1 void main()
2 {
3 int n, i=2,flag=0; Output
4 printf("Enter a number:"); Enter a number:7
5 scanf("%d",&n); 7 is a prime number
6 while(i<=n/2)
7 {
Output
8 if(n%i==0)
9 { Enter a number:9
9 is not a prime number
10 flag=1;
11 break;
12 }
13 i++;
14 }
15 if (flag==0)
16 printf("%d is a prime number",n);
17 else
18 printf("%d is not a prime number",n);
19 }

for loop
for Loop
 for is an entry controlled loop

 Statements inside the body of for are


repeatedly executed till the condition
is true

 for is keyword
for Loop
for (initialization; condition; updateStatement)
{
// statements
}

 The initialization statement is executed only once.


 Then, the condition is evaluated.
 If the condition is false, the for loop is terminated.
 If the condition is true, statements inside the body of for loop are executed, and
the update statement is updated.
 Again the condition is evaluated.
WAP to print numbers 1 to n (for loop)

#include<stdio.h> Enter a number:5


void main() 1
{ 2
int i,n; 3
printf("Enter a number:"); 4
scanf("%d",&n); 5
for(i=1;i<=n;i++)
{
printf("%d\n",i);
}
}
WAP to find factors of a number (for loop)

#include <stdio.h>
void main()
{
int i,n;
printf("Enter n to find factors=");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("%d,",i); Enter n to find factors=12
} 1,2,3,4,6,12,
}
void main(){
int i,n,sum=0;
PERFECT NUMBR
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
Enter a number:6
printf("%d+",i); 1+2+3=6
sum=sum+i; 6 is a perfect number
}
}
printf("=%d",sum);
if(sum==n)
printf("\n%d is a perfect number",n);
else
printf("\n%d is not a perfect number",n);}
#include <stdio.h>
void main()
{
int i,n,sum=0;
float avg;
printf("Input the 10 numbers : \n");
for (i=1;i<=10;i++)
{
printf("Number-%d :",i);
scanf("%d",&n);
sum = sum + n;
}
avg=sum/10.0;
printf("The sum of 10 no is : %d\nThe Average is : %f\n",sum,avg);
}
#include <stdio.h> FACTORIAL NO
void main()
{
int i,fact=1,num;
printf("Input the number : ");
scanf("%d",&num);

for(i=1;i<=num;i++)
{
fact=fact*i;
}

printf("The Factorial of %d is: %d\n",num,fact);


}
void main(){
PERFECT NUMBERS BETWEEN A RANGE mn & mx
int n,i,sum;
int mn,mx;

scanf("%d",&mn);
scanf("%d",&mx);
printf("The Perfect numbers within the given range mn & mx: ");

for(n=mn; n<=mx; n++)


{
i=1;
sum = 0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n); }
printf("\n"); }
void main(){
int num,i,ctr,stno,enno; PRIME NUMBERS WITHIN A RANGE
scanf("%d",&stno);
scanf("%d",&enno);
printf("The prime numbers between %d and %d are : \n",stno, enno);

for(num = stno; num<=enno; num++)


{
ctr = 0;

for(i=2;i<=num; i++)
{
if(num%i==0)
{
ctr++;
break;
}
}
if(ctr==0 && num!= 1)
printf("%d ",num);
}
printf("\n"); }
#include <stdio.h> Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].
void main()
{
float x,sum,t,d;
int i,n;
printf("Input the Value of x :"); Input the Value of x :2
scanf("%f",&x); Input the number of terms : 5
printf("Input the number of terms : ");
scanf("%d",&n); the sum = -0.415873
Number of terms = 5
sum =1; t = 1;
value of x = 2.000000
for (i=1;i<n;i++)
{
d = (2*i)*(2*i-1);
t = -t*x*x/d;
sum =sum+ t;
}
printf("\nthe sum = %f\nNumber of terms = %d\nvalue of x = %f\n",sum,n,x);
}
PROGRAM CONTROL
Nested Loops in C

1. C supports nesting of loops in C.


2. Nesting of loops is the feature in C that allows the looping of statements inside
another loop.
3. Any number of loops can be defined inside another loop, i.e., there is no restriction
for defining any number of loops.
4. The nesting level can be defined at n times.
5. You can define any type of loop inside another loop; for example, you can define
'while' loop inside a 'for' loop.

38/77
PROGRAM CONTROL
Syntax of Nested Loop
Outer_loop
{
Inner_loop
{
// i nner loop statements.
}
// outer loop statements.
}

Outer_loop and Inner_loop are the valid loops that can be a 'for' loop,
'while' loop or 'do-while' loop.
38/77
PROGRAM CONTROL
Nested for loop
The nested for loop means any type of loop which is defined inside the 'for'
loop.

for (initialization; condition; update)


{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
38/77
PROGRAM CONTROL
Displaying Tables

38/77
Example of nested for loop
#include <stdio.h>
int main()
{
int n; // variable declaration
printf("Enter the value of n :");
scanf(“%d”,&n);
// Displaying the n tables.
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%d\t",(i*j)); // printing the value.
}
printf("\n");
}
38/77
Explanation of the above code

1. First, the 'i' variable is initialized to 1 and then program control passes to the i<=n.
2. The program control checks whether the condition 'i<=n' is true or not.
3. If the condition is true, then the program control passes to the inner loop.
4. The inner loop will get executed until the condition is true.
5. After the execution of the inner loop, the control moves back to the update of the
outer loop, i.e., i++.
6. After incrementing the value of the loop counter, the condition is checked again, i.e.,
i<=n.
7. If the condition is true, then the inner loop will be executed again.
8. This process will continue until the condition of the outer loop is true.

38/77
Write a program in C to display the pattern like right angle triangle using an asterisk.
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
}
Write a program in C to display the pattern like right angle
triangle with a number.
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d“,j);
printf("\n");
}
}
Write a C program to calculate the factorial of a given number.
#include <stdio.h>
void main()
{
int i,f=1,num;
printf("Input the number : ");
scanf("%d",&num);

for(i=1;i<=num;i++)
{
f=f*i;
}
printf("The Factorial of %d is: %d\n",num,f);
}
Write a program in C to make such a pattern like right angle triangle with
a number which will repeat a number in a row.
The pattern is as follows:
#include <stdio.h>
void main()

{
int i,j,rows;
printf("Input number of rows : );
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
}
Write a program in C to make such a pattern like a pyramid with numbers increased by 1
void main()
{
int i,j,spc,rows,k,t=1;
printf("Input number of rows : ");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("%d ",t++);
printf("\n");
spc--;
} }
Write a program in C to print the Floyd's Triangle. The Floyd's triangle is as
below :
#include <stdio.h> for(i=1;i<=n;i++)
void main() {
{ int i,j,n,p,q; if(i%2==0)
printf("number of rows : "); {
scanf("%d",&n); p=1;q=0;
}
else
{
p=0;q=1;
}
for(j=1;j<=i;j++)
if(j%2==0)
printf("%d",p);
else
printf("%d",q);
printf("\n");
}
}
A Program to Display the pattern in which the first and a last number of each
row will be 1
#include <stdio.h> for(i=0; i<=n; i++)
{
void main() /* print blank spaces */
{ for(j=1; j<=n-i; j++)
int i,j,n; printf(" ");
printf("Input number of /* Display number in ascending order upto middle*/
rows : "); for(j=1;j<=i;j++)
scanf("%d",&n); printf("%d",j);

/* Display number in reverse order after middle */


for(j=i-1;j>=1;j--)
printf("%d",j);

printf("\n");
}
}
A Program to Display the pattern like a diamond
for(i=0;i<=r;i++)
#include <stdio.h>
{
void main()
{
for(j=1; j<=r-i; j++)
int i,j,r; printf(" ");
printf("Input number of rows (half of for(j=1; j<=2*i-1; j++)
the diamond) :"); printf("*");
scanf("%d",&r); printf("\n");
}

for(i=r-1; i>=1; i--)


{
for(j=1;j<=r-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
}
A Program to Display Pascal's triangle

 Pascals triangle is a triangular array of the


binomial coefficients.

 The numbers outside Pascal's triangle are all


"0".

 These "0s" are very important for the


triangular pattern to work to form a triangular
array.

 The triangle starts with a number "1" above,


and any new number added below the upper
number "1" is just the sum of the two
numbers above, except for the edge, which is
all "1".
row 0 =1
row 1 = (0+1), (1+0) = 1, 1
row 2 = (0+1), (1+1), (1+0) = 1, 2, 1
row 3 = (0+1), (1+2), (2+1), (1+0) = 1, 3, 3, 1
row 4 = (0+1), (1+3), (3+3), (3+1), (1+0) = 1, 4, 6, 4, 1
row 5 = (0+1), (1+4), (4+6), (6+4), (4+1),(1+0) = 1, 5, 10, 10, 5, 1
row 6 = (0+1), (1+5), (5+10), (10+10), (10+5), (5+1), (1+0) = 1, 6, 5, 20, 15, 6, 1
for (i = 0; i < rows; i++)
#include <stdio.h>
{
int main() {
for (space = 1; space <= rows - i; space++)
int rows, coef = 1, space, i, j;
printf(" ");
printf("Enter the number of rows: ");
for (j = 0; j <= i; j++)
scanf("%d", &rows);
{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
#include <stdio.h> REVERSE NO

void main()
{
int num,r,sum=0,t;

printf("Input a number: ");


scanf("%d",&num);

for(t=num; num!=0; num=num/10)


{
r=num % 10;
sum=sum*10 + r;
}
printf("The number in reverse order is : %d \n", sum);
}

do while loop
do while Loop

 do while is an exit controlled loop.

 Statements inside the body of do while


are repeatedly executed till the condition
is true.

 Do and while are keywords.


do while Loop

 Loop body will be executed first, and


then condition is checked.

do  If the condition is true, the body of the


{ loop is executed again and the condition
is evaluated.
// statement/s
}
 This process goes on until the condition
while (condition); becomes false.

 If the condition is false, the loop ends.


WAP to print Odd numbers between 1 to n(do while loop)
void main()
{
int i=1,n;
printf("Enter a number:");
scanf("%d",&n);
do
{
if(i%2 != 0)
{
printf("%d,",i);
}
i=i+1;
}
while(i<=n);
}
WAP To add numbers until the user enters zero

#include <stdio.h>
int main() { Enter a number: 1.5
float number, sum = 0; Enter a number: 2.4
do { Enter a number: -3.4
printf("Enter a number: ");
Enter a number: 4.2
scanf("%f", &number);
sum = sum = number; Enter a number: 0
} Sum = 4.70
while(number != 0.0);

printf("Sum = %f",sum);
return 0;
}
WAP to find factors of a number(do while loop)
void main()
{
int i=1,n;
printf("Enter a number:");
Enter a
scanf("%d",&n); number:6
do 1,2,3,6,
{
if(n%i==0)
{
printf("%d,",i);
}
i=i+1;
}
while(i<=n);
}
WAP to print reverse a number(do while loop)

void main()
{
int n;
Enter a
printf("Enter a number:");
number=1234
scanf("%d",&n);
4321
do
{
printf("%d",n%10);
n=n/10;
}
while(n!=0);
}

goto statement
goto Statement

 goto is an virtual loop

 The goto statement allows us to transfer control of the program to the specified
label.

 goto is keyword
goto Statement
Syntax
 The label is an identifier.

goto label; label:  When the goto statement is


. . encountered, the control of the
program jumps to label: and
. . starts executing the code.
. .
label: goto label;
WAP to print Odd numbers between 1 to n(goto)
void main() {
Output
int i=1,n;
printf("Enter a number:");
scanf("%d",&n);
odd: Enter a
if(i%2!=0)
{
number:5
printf("%d,",i); 1,3,5
}
i=i+1;
if(i<=n)
{
goto odd;
}
}
WAP to find factorial of a number (goto)
#include <stdio.h>
int main(){
int a,n=1;
printf("\nEnter the number :"); Enter the number:5
scanf("%d",&a); The Factotial is:120
start:
n=n*a;
a--;
if(a>0)
{
goto start;
}
printf("\nThe Factotial is:%d",n);
return 0;
}
Types of loops
Entry Control Loop Entry Control Loop Exit Control Loop Virtual Loop
int i=1; int i; int i=1; int i=1;
while(i<=10) for(i=1;i<=10;i++) do labelprint:
{ { { printf("%d",i++);
printf("%d",i++); printf("%d",i); printf("%d",i++); if(i<=10)
} } } goto labelprint;
while(i<=10);

False
Loop Body Label Statement
condition
True True
condition condition
Loop Body
False True False
goto
Always detect pattern in pattern
How continue statement works?

• The continue statement skips the current iteration of the loop


and continues with the next iteration.
• Its syntax is:
continue;

• The continue statement is almost always used with


the if...else statement.
How continue statement works?
How continue statement works?
#include <stdio.h>
int main(void)
{
int iNum;
for(iNum = 1; iNum <= 10; iNum++)
{
// skip remaining code in loop only if iNum == 5
if(iNum == 5)
continue;
printf("%d ", iNum);
}
printf("\nUsed continue to skip printing the value
5\n");
return 0;
}
52/77
#include <stdio.h> // Program to calculate the sum of
int main() numbers (10 numbers max) //
{
If the user enters a negative number, it's
int i; double number, sum = 0.0; not added to the result
for (i = 1; i <= 10; ++i)
{ Enter a n1: 1.1
Enter a n2: 2.2
printf("Enter a n%d: ", i);
Enter a n3: 5.5
scanf("%lf", &number); Enter a n4: 4.4
if (number < 0.0) Enter a n5: -3.4
{ Enter a n6: -45.5
continue; Enter a n7: 34.5
} Enter a n8: -4.2
sum = sum + number;
Enter a n9: -1000
Enter a n10: 12
}
Sum = 59.70
printf("Sum = %.2lf", sum); return 0; }
#include<stdio.h> // Example Program Continue
int main() Statement
{
for(int i=1; i<5; i++)
{
for(int j=1; j<5; j++)
{
if(i%2==0 || j%2==0)
Output:-
continue;
printf("%d\t%d\n", i,j); 11
} 13
} 31
return 0; 33
}
How break statement works?
 The break statement is used to end the loop immediately when it is
encountered.

Syntax:
break;

The break statement is almost always used with if...else statement inside the
loop.
How break statement works?
#include <stdio.h> OUTPUT: • This program calculates the sum
int main() { Enter a n1: 2.4 of a maximum of 10 numbers.
int i; Enter a n2: 4.5
double number, sum = 0.0; Enter a n3: 3.4
for (i = 1; i <= 10; ++i) • Why a maximum of 10 numbers?
Enter a n4: -3
{ Sum = 10.30 • It's because if the user enters a
printf("Enter a n%d: ", i); negative number,
scanf("%lf", &number); the break statement is executed.
// if the user enters a negative number, break the loop
if (number < 0.0)
• This will end the for loop, and
{
the sum is displayed.
break;
}
sum += number; • In C, break is also used with
} the switch statement. This will be
printf("Sum = %.2lf", sum); return 0; } discussed in the next tutorial.
How break statement works?
#include <stdio.h>
#include<stdio.h>
#include<stdlib.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}
#include<stdio.h> C break statement with the nested loop
int main(){ In such case, it breaks only the inner loop, but
int i=1,j=1;//initializing a local variab not outer loop.
le
for(i=1;i<=3;i++)
{
OUTPUT
for(j=1;j<=3;j++)
{ 11
printf("%d &d\n",i,j); 12
if(i==2 && j==2){ 13
21
break;//will break loop of j only 22
} 31
}//end of for loop 32
return 0; 33
}
Break Continue

break statement is used in switch and loops. continue statement is used in loops only.

When continue is encountered, the statements after it


When break is encountered the switch or loop
are skipped and the loop control jump to next
execution is immediately stopped.
iteration.
Example: Example:
#include<stdio.h> #include<stdio.h>
int main(){ int main(){
int i; int i;
for(i=0;i<5;++i){ for(i=0;i<5;++i){
if(i==3) if(i==3)
break; continue;
printf(“%d “,i); printf(“%d “,i);
} }
return 0; return 0;
} }

Output: 0 1 2 Output: 0 1 2 4

Pattern
Always detect pattern in pattern
Pattern
There are important points to note in pattern
1. Determine, how many rows?
2. Determine, how many numbers/characters/columns in a row?
3. Determine, Increment/Decrement among the number of rows.
4. Determine, starting in each row

1 1 1 *
11 12 23 * *
111 123 456 * * *
1111 1234 78910 * * * *
11111 12345 * * *
* *
*
WAP to print given pattern (nested loop)
* Program
** 1 void main()
*** 2 {
**** 3 int i,j;
***** 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5 6 for(j=1; j<=i; j++)
No. of characters 7 {
Row-1: * 8 printf("*");
Row-2: ** 9 }
Row-3: *** 10 printf("\n");
Row-4: **** 11 }
Row-5: ***** 12 }

Inner loop: Increment


Outer loop: Increment

Starting: *
WAP to print given pattern (nested loop)
1 Program
12 1 void main()
123 2 {
1234 3 int i,j;
12345 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5 6 for(j=1; j<=i; j++)
No. of values 7 {
Row-1: 1 8 printf("%d",j);
Row-2: 12 9 }
Row-3: 123 10 printf("\n");
Row-4: 1234 11 }
Row-5: 12345 12 }

Inner loop: Increment


Outer loop: Increment

Starting: 1
WAP to print given pattern (nested loop)
5 Program
54 1 void main()
543 2 {
5432 3 int i,j;
54321 4 for(i=5;i>0;i--)
5 {
No. of rows: 5
6 for(j=5; j>=i ; j--)
No. of values 7 {
Row-1: 5 8 printf("%d",j);
Row-2: 54 9 }
Row-3: 543 10 printf("\n");
Row-4: 5432 11 }
Row-5: 54321 12 }

Inner loop: Decrement


Outer loop:
Decrement/Increment
Starting: 5
WAP to print given pattern (nested loop)
* Program
** 1 void main() First we need to print 4
*** 2 { spaces before printing *
**** 3 int i,j,k;
***** 4 for(i=1;i<=5;i++)
*
5 {
No. of rows: 5 6 for(k=5;k>i;k--) **

No. of values 7 { ***


Row-1: ----* 8 printf(" ");
****
Row-2: ---** 9 }
Row-3: --*** 10 for(j=1;j<=i;j++) *****
Row-4: -**** 11 {
Row-5: ***** 12 printf("*"); After printing spaces
13 } this inner loop prints *
Inner loop: Decrement 14 printf("\n");
Outer loop: Decrement/Increment 15 }
16 }
Starting: -(space)
Ending: *
Practice programs
1) Write a program to find sum of first N odd numbers. Ex. 1+3+5+7+………..+N
2) Write a program to find 1+1/2+1/3+1/4+....+1/n.
3) Write a program to print all Armstrong numbers in a given range. For example 153 = 1^3 + 5^3 +
3^3. So, 153 is Armstrong number.
4) Write a program to print given number in reverse order
5) Write a program to check whether a given string is palindrome or not.
6) Write a program to print Multiplication Table up to n.
1 2 3 4 5 6 7 .
2 4 6 8 10 12 14 .
3 6 9 12 15 18 21 .
4 8 12 16 20 24 28 .
5 10 15 20 25 30 35 .
. . . . . . . .

7) Construct C programs to print the following patterns using loop statement.


1 * 1 1 1 * * * * * * * * * *
22 # # 0 1 2 2 A B * * * * * *
333 * * * 1 0 1 3 3 3 2 3 4 * * * * *
4444 # # # # 0 1 0 1 4 4 4 4 C D E F * * * * * * *
55555 * * * * * *

Thank you

You might also like