0% found this document useful (0 votes)
5 views49 pages

Structured Programming in C

The document provides an overview of control structures in C programming, focusing on looping statements such as while, do-while, and for loops. It includes syntax, explanations, flowcharts, and example programs demonstrating the use of these loops to perform tasks like calculating sums, reversing numbers, and checking for Armstrong numbers. Additionally, it highlights the differences between while and do-while loops and explains the for loop's structure and usage.

Uploaded by

nanditasingh1709
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)
5 views49 pages

Structured Programming in C

The document provides an overview of control structures in C programming, focusing on looping statements such as while, do-while, and for loops. It includes syntax, explanations, flowcharts, and example programs demonstrating the use of these loops to perform tasks like calculating sums, reversing numbers, and checking for Armstrong numbers. Additionally, it highlights the differences between while and do-while loops and explains the for loop's structure and usage.

Uploaded by

nanditasingh1709
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/ 49

Structured Programming using C

Unit 3: Control Structures


Looping Statements- while, do-
while
Looping Statements

• In computer programming, loops are used to repeat a


block of code.

• For example, let's say we want to show a message 100


times. Then instead of writing the print statement 100 times,
we can use a loop.

• With the effective use of loops we can achieve much more


efficiency (in terms of time).
3 Looping Statements, while, do-while
Types of Looping Statements
There are 3 types of
loops :
1. while loop
2. do…while loop
3. for loop

4 Looping Statements, while, do-while


While Loop Statement
▪ Syntax :
while (condition)
{ // body of the loop }

▪ Explanation :
- A while loop evaluates the condition
- If the condition evaluates to true, the code inside
the while loop is executed.
- The condition is evaluated again.
- This process continues until the condition is false.
- When the condition evaluates to false, the loop
terminates.

5 Looping Statements, while, do-while


Flowchart of while Loop Statement

6 Looping Statements, while, do-while


Program 1 :
Q. Write a program to find sum of 1 to 10
numbers.
#include <stdio.h> Output :
#include <conio.h> The sum is 55.
void main()
{
int i = 1; //initialize counter i to 1
int sum = 0;
while (i <= 10) // repeat till we reach limit
{
sum += i; // add number
i++; //increment counter by 1
} // end of while
printf("\nThe sum is %d.",sum);
getch();
}

7 Looping Statements, while, do-while


Program 2 : (Exp-8)
Q. Write a program to calculate reverse of a number.
#include <stdio.h> Output :
#include <conio.h> Enter a number:
void main() 123
{ The reverse of number is 321.
int num;
int rev = 0, rem;
printf("Enter a number:\n");
scanf("%d",&num);
while (num != 0) //repeat till number becomes 0
{
rem=num%10; //separates digits from number
rev = rev*10 + rem; //calculate reverse of number
num=num/10; //number gets updated
} // end of while
printf("\nThe reverse of number is %d.",rev);
getch();
}

8 Looping Statements, while, do-while


Program 3 :
Q. WAP to check number entered by user is Armstrong or
not. (3 digit number)
Note : Number is called Armstrong if sum of cube of digits of
the number is equal to number itself. Ex. 153 = 13 + 53 + 33
#include <stdio.h>
#include <conio.h>
void main()
{
int num, temp;
int sum = 0, rem;
printf("Enter a number:\n");
scanf("%d",&num);
temp=num; //make a copy of number
9 Looping Statements, while, do-while
Program 3 : (cont..)
while (num != 0) //repeat till number becomes 0
{ Output :
rem=num%10; //separates digits from number Enter a number:
sum = sum + (rem*rem*rem); //calculates sum 153
153 is Armstrong number.
num=num/10; //number gets updated
} // end of while
if(temp==sum)
printf("\n%d is Armstrong number.", temp);
else
printf("\n%d is not Armstrong number.", temp);
getch();
}

10 Looping Statements, while, do-while


Do...While Loop Statement
▪ Syntax :
do
{ // body of loop; }
while (condition);
▪ Explanation :
- The body of the loop is executed at first. Then the condition is evaluated.
- If the condition evaluates to true, the body of the loop inside
the do statement
is executed again.
- The condition is evaluated once again.
- If the condition evaluates to true, the body of the loop inside
the do statement
is executed again.
- This process continues until the condition evaluates to false. Then the
loop stops.

11 Looping Statements, while, do-while


Flowchart of do..while Loop Statement

12 Looping Statements, while, do-while


Program 1 :
Q. Write a program to find sum of 1 to 10
numbers.
#include <stdio.h> Output :
#include <conio.h> The sum is 55.
void main()
{
int i = 1; //initialize counter i to 1
int sum = 0;
do
{
sum += i; // add number
i++; //increment counter by 1
} while (i <= 10); // end of do...while
printf("\nThe sum is %d.",sum);
getch();
}
13 Looping Statements, while, do-while
Program 2 :
Q. Write a program to display table of any number entered by user.
#include <stdio.h>
#include <conio.h> Output :
void main() Enter a number:6
{ Table of 6:
int i = 1; //initialize counter i to 1 6 * 1 = 6
int num; 6 * 2 = 12
printf("Enter a number:"); 6 * 3 = 18
scanf("%d",&num);
6 * 4 = 24
printf("\nTable of %d:\n",num);
do
6 * 5 = 30
{ 6 * 6 = 36
printf("%d\t*\t%d\t=\t%d\n",num,i,num*i); 6 * 7 = 42
i++; //increment counter by 1 6 * 8 = 48
} while (i <= 10); // end of do..while 6 * 9 = 54
getch(); 6 * 10 = 60
}

14 Looping Statements, while, do-while


Difference between while and do..while
while do-while
Syntax: Syntax:
while(test expression) do
{ statement } { statement
} while(test expression);
Condition is checked first and then Statement is executed at least once
statement is executed. Hence it is thereafter condition is checked. Hence
entry controlled loop. it is exit controlled loop.

This is pre test loop. This is post test loop.


If condition is not true, for first Though condition is not true for first
iteration the loop will never get iteration , the loop will be executed
executed. once.
No semicolon is given after while While statement ends with semicolon.
statement.
15 Looping Statements, while, do-while
For Loop Statement
For Loop Statement
▪ The for allows us to specify three things about a loop in a single line:
(a) Setting a loop counter to an initial value.
(b) Testing the loop counter to determine whether its value has reached
the number of repetitions desired.
(c) Increasing the value of loop counter each time the body of the loop
has been executed.
▪ Syntax :
for (initialize counter ; test counter ; update counter)
{ // body of-loop }
▪ Explanation :
- initialize counter - initializes variables and is executed only once
- test counter i.e. condition - if true, the body of for loop is executed
if false, the for loop is terminated
- update counter- updates the value of initialized variable and again
checks the condition

17 For Loop Statement


Flowchart of for Loop Statement

18 For Loop Statement


Program 1:
Q. WAP to print numbers from 1
to 5. Working of the Program :
#include <stdio.h>
#include <conio.h> Iteration Variable i <= 5 Action
void main() 1st i=1 true 1 is printed and “i” is
{ increased by 1i.e. i=2.
int i; 2nd i=2 true 2 is printed and “i” is
increased by 1i.e. i=3.
for (i = 1; i <= 5; i++)
3 is printed and “i” is
{ 3rd i=3 true increased by 1i.e. i=4.
printf("%d\t",i); 4 is printed and “i” is
} //end of for loop 4th i=4 true increased by 1i.e. i=5.

getch(); 5 is printed and “i” is


} 5th i=5 true increased by 1i.e. i=6.

Output : For loop will be


1 2 3 4 5 6th i=6 false terminated

19 For Loop Statement


Program 2:
Q. WAP to print sum of 1 to n numbers.
#include <stdio.h> Working of the Program : Assume n=5
#include <conio.h>
void main() Iterati Variabl i <=
Action
{ on e 5
int i, n, sum=0; 1st i=1 true 1&is“i”added in sum i.e. 0+1 =1
printf("Enter limit :\n"); is increased by 1i.e. i=2.
scanf("%d",&n);
for (i = 1; i <= n; i++) 2nd i=2 true 2&is“i”added in sum i.e. 1+2 =3
is increased by 1i.e. i=3.
{
sum=sum+i; 3rd i=3 true 3&is“i”added in sum i.e. 3+3 =6
is increased by 1i.e. i=4.
} //end of for loop
printf("Sum=%d",sum); 4th i=4 true 4&is“i”added in sum i.e. 6+4 =10
is increased by 1i.e. i=5.
getch();
} Output : 5th i=5 true
5 is added in sum i.e. 10+5 =15
& “i” is increased by 1i.e. i=6.
Enter limit :
5 6th i=6 false For loop will be terminated
Sum=15
20 For Loop Statement
Program 3:
Q. WAP to print factorial of number “n”.
#include <stdio.h> Working of the Program : Assume n=5
#include <conio.h>
void main() Iterati Variabl i <=
Action
{ on e 5
int i, n, fact=1; 1st i=1 true 1 is multiplied with fact i.e. 1*1
printf("Enter the number :\n"); =1 & “i” is increased by 1 i.e. i=2.
scanf("%d",&n); 2 is multiplied with fact i.e.
for (i = 1; i <= n; i++) 2nd i=2 true 1*2=2 & “i” is increased by 1
{ i.e. i=3.
3 is multiplied with fact i.e.
fact=fact*i; 3rd i=3 true 2*3=6 & “i” is increased by 1
} //end of for loop i.e. i=4.
printf("Factorial of %d =%d", n, fact); 4th i=4 true 4“i”isismultiplied with fact i.e. 6*4=24 &
increased by 1 i.e. i=5.
getch();
} 5 is multiplied with fact i.e. 24*5
Output : 5th i=5 true =120 & “i” is increased by 1 i.e.i=6.
Enter the number :
For loop will be
5 6th i=6 false terminated
Factorial of 5=120
21 For Loop Statement
Program 4:
Q. WAP to calculate mn . Working of the Program : Assume base
#include <stdio.h> m=2 and power n=4
#include <conio.h>
void main() Iteration Variable i <= 4 Action
{
result is multiplied with base
int m, n, i, result=1; 1st i=1 true i.e. 1*m = 1 *2 =2 & “i” is
printf("Enter base and power:"); increased i=2.
scanf("%d%d",&m,&n); true result is multiplied with base
2nd i=2 i.e. 2*m =2*2 =4 & “i” is
for(i=1;i<=n;i++)
increased i=3.
result=result*m; result is multiplied with base
printf("%d raise to %d is 3rd i=3 true i.e. 4*m = 4*2 =8 & “i” is
%d",m,n,result); increased i=4.
result is multiplied with base
getch();
4th i=4 true i.e. 8*m = 8*2 =32 & “i” is
} Output : increased i=5.
Enter base and power:2
For loop will be
4 5th i=5 false terminated
2 raise to 4 is 16
22 For Loop Statement
Program 5:
Q. WAP to generate “m” number of terms for(i=1;i<=m;i++)
of Fibonacci series. {
Fibonacci Series = 0, 1, 1, 2, 3, 5, 8 ...... c=a+b;
#include <stdio.h> a=b;
#include <conio.h> b=c;
printf("%d\t",c);
void main() }
{ getch();
int m, a, b, c, i; }
printf("Enter limit:");
scanf("%d",&m); Output :
Enter limit:6
a=0; 0 1 1 2 3 5 8 13
b=1;
printf("\n%d\t%d\t",a,b);

23 For Loop Statement


Program 6:

Q. WAP to calculate sum of given for(i=1;i<=m;i++)


series. {
x + x2 + x3 +............+ xm term=term*x;
#include <stdio.h> sum=sum+term;
#include <conio.h> }
void main() printf("Sum of series=%d",sum);
{ getch();
int i, x, m, term=1, sum=0; }
printf("Enter the value of x:");
Output :
scanf("%d",&x);
Enter the value of x:2
printf("Enter the limit:"); Enter the limit:4
scanf("%d",&m); Sum of series=30

24 For Loop Statement


Program 7:
Q. WAP to calculate sum of given series. for(i=1;i<=m;i=i+2)
{
t1=t2*x;
t2=t1*x;
#include <stdio.h> sum=sum+((float)(t1)/t2);
#include <conio.h> }
void main() printf("Sum of series=%f",sum);
{ getch();
}
int i, x, m, t1=1, t2=1;
float sum=1.0;
Output :
printf("Enter the value of x:");
scanf("%d",&x);
Enter the value of x:2
printf("Enter the limit:"); Enter the limit:7
scanf("%d",&m); Sum of series=3.000000

25 For Loop Statement


Program 8:
Q. WAP to calculate sum of given series. for(i=1;i<m;i++)
{
c=sign*(float)a/b;
#include <stdio.h> sum=sum+c;
#include <conio.h> a++;
void main() b++; %.2f will print 2 nos
{ sign=-sign; after decimal point
}
int i, a, b, m, sign=1;
printf("Sum of series=%.2f",sum);
float c, sum=1.0; getch();
printf("Enter the limit:"); }
scanf("%d",&m);
Output :
a=2; Enter the limit:5
b=3; Sum of series=0.88

26 For Loop Statement


Program 9:
Q. WAP to calculate sum of given for(i=1;i<=n;i++)
series. {
f=f*i;
sum=sum+(1.0/(float)f);
#include <stdio.h>
}
#include <conio.h>
printf("Sum of
void main() series=%.3f",sum);
{ getch();
int i, n, f=1; }
float sum=0.0; Output :
printf("Enter the limit:");
Enter the limit:4
scanf("%d",&n); Sum of series=1.708
27 For Loop Statement
Practice Programs

1. Write a C program to print all even and odd numbers from


1 to N.

2. Write a C program to print square and cube of all numbers


from 1 to N.

3. Write a C program to print all leap years from 1 to N.

28 Lecture 1 – Neural Networks


Nested Control Structures
Nested Control Structures
• Nesting of loops is the feature in C that allows the looping of statements
inside another loop.
• Any number of loops can be defined inside another loop, i.e., there is no
restriction for defining any number of loops.
• We can define any type of loop inside another loop; for example, we can
define 'while' loop inside a 'for' loop.
• General Format :
Outer_loop
{
Inner_loop
{
// inner 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.

30 Nested Control Structures


Nested for Loop
• Syntax :
for ( init; condition; increment ) // outer for loop
{
for ( init; condition; increment ) // inner for loop
{
statement(s); // inner loop statements
}
statement(s); // outer loop statements
}
31 Nested Control Structures
Nested while Loop
• Syntax :
while(condition) // outer for loop
{
while(condition) // inner for loop
{
statement(s); // inner loop statements
}
statement(s); // outer loop statements
}
32 Nested Control Structures
Nested do….while Loop
• Syntax :
do // outer for loop
{
do // inner for loop
{
statement(s); // inner loop statements
} while( condition );
statement(s); // outer loop statements
} while( condition );
33 Nested Control Structures
Program 1 :
Q. WAP to print following pattern.
* for(i = 1; i <= rows; i++) //outer for loop
** {
*** for(j = 1; j <= i; j++) // inner for loop
{
**** printf("* "); // inner loop statement
***** }
#include <stdio.h> printf("\n"); // outer loop statement, to
// start printing next row on new line
#include<conio.h> } // end of outer loop
void main() getch();
{ }
int i, j, rows; Output :
printf("Enter the number of Enter the number of rows: 5
rows: "); *
scanf("%d", &rows); **
***
34 Nested Control Structures ****
*****
Program 2 :
Q. WAP to print following pattern. for(i = 1; i <= rows; i++) //outer for loop
1 {
for(j = 1; j <= i; j++) // inner for loop
12 {
123 printf(“%d “,j); // inner loop
1234 statement
}
12345
printf("\n"); // outer loop statement, to
#include <stdio.h> // start printing next row on new
#include<conio.h> line
void main() } // end of outer loop
getch();
{ } Output :
int i, j, rows; Enter the number of rows: 5
printf("Enter the number of 1
rows: "); 12
scanf("%d", &rows); 123
1234
35 Nested Control Structures 12345
Program 3 :
Q. WAP to print following pattern.
1 for(i = 1; i <= rows; i++) //outer for loop
22 {
for(j = 1; j <= i; j++) // inner for loop
333 {
4444 printf(“%d “,i); // inner loop statement
}
55555 printf("\n"); // outer loop statement, to
#include <stdio.h> // start printing next row on new line
} // end of outer loop
#include<conio.h> getch();
void main() }

{
Output :
int i, j, rows; Enter the number of rows: 5
printf("Enter the number of 1
rows: "); 22
scanf("%d", &rows); 333
4444
36 Nested Control Structures 55555
Program 4 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++) //outer for loop
A {
AB ch='A’;
ABC for (j = 1; j <= i; j++) // inner for loop
ABCD {
printf("%c ",ch); // inner loop
ABCDE
statement
#include <stdio.h> ch++;
#include<conio.h> }
void main() printf("\n");
{ }
int i, j, rows; getch();
char ch; }
printf("Enter the number of rows: Output :
"); Enter the number of rows: 5
scanf("%d", &rows); A
AB
ABC
37 Nested Control Structures
ABCD
ABCDE
Program 5 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++) //outer for loop
A {
BB for (j = 1; j <= i; j++) // inner for loop
CCC {
printf("%c ",ch); // inner loop
DDDD
statement
EEEEE }
#include <stdio.h> ch++;
#include<conio.h> printf("\n");
void main() } // end of outer for loop statement
{ getch();
int i, j, rows; }
char ch=‘A’;
Output :
printf("Enter the number of rows: Enter the number of rows: 5
"); A
scanf("%d", &rows); BB
CCC
DDDD
38 Nested Control Structures EEEEE
Program 6 :
Q. WAP to print following for (i = 1; i <= rows; i++) //outer for loop
pattern. {
A for (j = 1; j <= i; j++) // inner for loop
BC {
DEF printf("%c ",ch); // inner loop
GHIJ statement
KLMNO ch++;
#include <stdio.h> }
#include<conio.h> printf("\n");
void main() } // end of outer for loop statement
{ getch();
int i, j, rows; } Output :
char ch=‘A’; Enter the number of rows: 5
printf("Enter the number of A
rows: "); BC
scanf("%d", &rows); DEF
GHIJ
39 Nested Control Structures KLMNO
Program 7 :
Q. WAP to print following pattern. for(i = rows; i >= 1; i--) //outer for loop
{
*****
for(j = 1; j <= i; j++) // inner for loop
**** {
*** printf(“* “); // inner loop statement
** }
printf("\n"); // outer loop statement, to
* // start printing next row on new line
#include <stdio.h> } // end of outer loop
#include<conio.h> getch();
}
void main()
{
Output :
int i, j, rows; Enter the number of rows: 5
printf("Enter the number of *****
rows: "); ****
scanf("%d", &rows); ***
**
40 Nested Control Structures *
Program 8 :
Q. WAP to print following pattern.
12345 for (i = rows; i >= 1; i--)
1234 {
123 for (j = 1; j <= i; j++)
{
12
printf("%d ",j);
1 }
#include <stdio.h> printf("\n");
#include<conio.h> }
void main() getch(); Output :
{ } Enter the number of rows: 5
12345
int i, j, rows;
1234
printf("Enter the number of 123
rows: "); 12
scanf("%d", &rows); 1
41 Nested Control Structures
Program 9 :
Q. WAP to print following pattern.
54321 for (i = rows; i >= 1; i--)
4321 {
for (j =i; j >= 1; j--)
321
{
21 printf("%d ",j);
1 }
#include <stdio.h> printf("\n");
#include<conio.h> }
void main() getch();
} Output :
{ Enter the number of rows: 5
int i, j, rows; 54321
printf("Enter the number of 4321
rows: "); 321
21
scanf("%d", &rows);
1
42 Nested Control Structures
Program 10 :
Q. WAP to print following pattern.
1 for(i = 1; i <= rows; i++)
{
1 2
for(space=i; space<rows; space++) //for
1 2 3 spacing
printf(" ");
1 2 3 4
for(j = 1; j <= i; j++)
1 2 3 4 5 printf("%d ", j);
#include <stdio.h> printf("\n");
#include<conio.h> } Output :
void main() getch(); Enter the number of rows: 5
{ } 1
int i, j, space, rows; 12
printf("Enter the number of 123
rows: "); 1234
scanf("%d", &rows);
12345
43 Nested Control Structures
Program 11 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++)
{
* for(space=i; space<rows; space++) //for
spacing
* * * printf(" ");
* * * * * for(j = 1; j <= i; j++) // for left triangle
printf("* ");
* * * * * * * for(k=1;k<(j-1);k++) // for right triangle
printf("* ");
* * * * * * * * * printf("\n");
#include <stdio.h> }
#include<conio.h> getch();
void main() }
{
Output :
int i, j, k, space, rows;
Enter the number of rows: 3
printf("Enter the number of rows: ");
*
scanf("%d", &rows);
***
*****
44 Nested Control Structures
Program 12 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++)
1 {
for(space=i; space<rows; space++)
1 2 1 printf(" ");
1 2 3 2 1 for(j = 1; j <= i; j++)
printf("%d ",j);
1 2 3 4 3 2 1 for(k=1;k<(j-1);k++)
1 2 3 4 5 4 3 2 1 printf("%d ",k);
printf("\n");
#include <stdio.h> }
#include<conio.h> getch();
void main() }
{ Output :
int i, j, k, space, rows; Enter the number of rows: 3
printf("Enter the number of rows: 1
"); 121
scanf("%d", &rows); 12321
45 Nested Control Structures
Program 13 :
Q. WAP to print following pattern. for (i = 1; i <= rows; i++) {
A ch='A';
for(space=i; space<rows; space++)
A B A printf(" ");
A B C B A for(j = 1; j <= i; j++)
{
A B C D C B A
printf("%c ",ch);
#include <stdio.h> ch++;
#include<conio.h> }
void main() for(k=1;k<(j-1);k++)
{
{
int i, j, k, space, rows;
char ch; printf("%c ",(ch-2));
printf("Enter the number of ch--;
rows: "); }
scanf("%d", &rows); printf("\n"); }
getch(); }
46 Nested Control Structures
Program 14 :
Q. WAP to Floyd's triangle. for (i = 1; i <= rows; i++)
1 {
for (j = 1; j <= i; ++j)
2 3 {
4 5 6 printf("%d ", k);
k++;
7 8 9 10 }
printf("\n");
#include <stdio.h> }
#include<conio.h> getch();
void main() }
{ Output :
int i, j, rows, k=1; Enter the number of
printf("Enter the number of rows: 4
rows: "); 1
scanf("%d", &rows); 23
456
47 Nested Control Structures 7 8 9 10
Practice Programs
Q. Write C programs for the following
pattern.
1. 5 4 3 2 1 4. A
5432 BCB
543 CDEDC
54 DEFGFED
5
5. 1
2. 5 5 5 5 5
4444 22
333 333
22 4444
1 55555
3. 1 2 3 4 5
2345 6. 1
345 232
45 34543
5 4567654
567898765

48 Nested Control Structures


Thank
You

You might also like