PPS Chapter-3
PPS Chapter-3
PPS Chapter-3
Subject
Programming for Problem Solving (3110003)
Chapter – 3
Control Structure in C
CONTENTS
1. INTRODUCTION
2. DECISION MAKING (BRANCHING) CONTROL STRUCTURES
2.1. Simple if statement
2.2. if … else statement
2.3. Nested if statement
2.4. if…else…if ladder (Multiple if…else) statement
2.5. Switch Statement
3. LOOPING CONTROL STRUCTURES
3.1. Types of Loops
3.2. while Loop
3.3. do…while Loop
3.4. Difference between while Loop and do-while Loop
3.5. for Loop
4. NESTING OF CONTROL STRUCTURES (NESTING OF LOOPS)
5. break STATEMENT
6. continue STATEMENT
7. goto STATEMENT
8. SOLVED PROGRAMS
1. INTRODUCTION
• The important and essential part of a programming language is their control
structures. The programming languages support following control structures.
o Decision making (Branching)
o Looping
• When a program breaks the sequential flow and jumps to another part of the
code, it is called branching.
• Conditional branching statements checks the condition first and then control is
transfer to another part of the code. Example: if, switch.
if (condition)
statement(s);
• If there are more than one statements to be executed then they are put within { }
symbols.
#include <stdio.h>
#include <conio.h>
main ()
{
float num1, num2;
clrscr();
printf(“Enter first number\n”);
scanf(“%f”,&num1);
printf(“Enter second number\n”);
scanf(“%f”,&num2);
if(num1 > num2)
printf(“%f is larger\n”,num1);
else
printf(“%f is larger\n”,num2);
getch();
}
Output:
Enter first number
4.3
Enter second number
5.7
5.700000 is larger
#include <stdio.h>
#include <conio.h>
main ()
{
int num1, num2, num3;
int max;
clrscr();
printf(“Enter three numbers\n”);
scanf(“%d%d%d”,&num1,&num2,&num3);
if (num1 > num2)
{
if (num1 > num3)
max = num1;
else
max = num3;
}
else
{
if (num2 > num3)
max = num2;
else
max = num3;
}
printf(“Maximum of %d %d and %d is = %d\n”,
num1,num2,num3,max);
getch();
}
Output-1:
Enter three numbers
32 43 15
Maximum of 32 43 and 14 is = 43
Output-2:
Enter three numbers
-5 -36 -54
Maximum of -5 -36 and -54 is = -5
• From the syntax, it is very clear that only one set of statement(s) will be
executed, depending on the condition being true.
#include <stdio.h>
#include <conio.h>
main ()
{
int day;
clrscr();
printf(“Enter day number between 1 – 7\n”);
scanf(“%d”,&day);
if (day == 1)
printf(“Sunday\n”);
else if (day == 2)
printf(“Monday\n”);
else if (day == 3)
printf(“Tuesday\n”);
else if (day == 4)
printf(“Wednesday\n”);
else if (day == 5)
printf(“Thursday\n”);
else if (day == 6)
printf(“Friday\n”);
else if (day == 7)
printf(“Saturday\n”);
else
printf(“Wrong input\n”);
getch();
}
Output-1:
Enter day number between 1 – 7
3
Tuesday
Output-2:
Enter day number between 1 – 7
8
Wrong input
#include <stdio.h>
#include <conio.h>
main ()
{
char ch;
clrscr();
printf(“Enter one character\n”);
scanf(“%c”,&ch);
if ((ch >= ‘0’) && (ch <= ‘9’))
printf(“The character %c is digit\n”,ch);
else if ((ch >= ‘a’) && (ch <= ‘z’))
printf(“The character %c is Lower Case\n”,ch);
else if ((ch >= ‘A’) && (ch <=’Z’))
printf(“The character %c is Upper Case\n”,ch);
else
printf(“The character %c is other
symbol\n”,ch);
getch();
}
Output-1:
Enter one character
5
The character 5 is digit
Output-2:
Enter one character
b
The character b is Lower Case
Output-3:
Enter one character
H
The character H is Upper Case
Output-4:
Enter one character
*
The character * is other symbol
#include <stdio.h>
#include <conio.h>
main ()
{
int day;
clrscr();
printf(“Enter day number between 1 – 7\n”);
scanf(“%d”,&day);
switch (day)
{
case 1 :
printf(“Sunday\n”);
break;
case 2 :
printf(“Monday\n”);
break;
case 3 :
printf(“Tuesday\n”);
break;
case 4 :
printf(“Wednesday\n”);
break;
case 5 :
printf(“Thursday\n”);
break;
case 6 :
printf(“Friday\n”);
break;
case 7 :
printf(“Saturday\n”);
break;
default :
printf(“Wrong input\n”);
}
getch();
}
Output-1:
Enter day number between 1 – 7
4
Wednesday
Output-2:
Enter day number between 1 – 7
9
Wrong input
• If the code to be executed for different cases is same, then the code for different
cases can be grouped together by writing those cases with their values as
follows.
• For example, if x is an integer variable, whose valid values are from 1 and 3,
and if code for value 1 and value 3 are same, then it can be written like this.
switch (x)
{
case 1 :
case 3 :
statement(s);
break;
case 2 :
statement(s);
break;
default :
statement(s);
}
#include <stdio.h>
#include <conio.h>
main ()
{
int month;
clrscr();
printf(“Give month number between 1 – 12\n”);
scanf(“%d”,&month);
switch (month)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
printf(“Number of days in month number %d is =
31\n”, month);
break;
case 4 :
case 6 :
case 9 :
case 11 :
printf(“Number of days in month number %d is =
30\n”, month);
break;
case 2 :
printf(“Number of days in month number %d is =
28 or 29\n”, month);
break;
default :
printf(“Wrong input\n”);
}
getch();
}
Output-1:
Give month number between 1 – 12
10
Number of days in month number 10 is = 31
Output-2:
Give month number between 1 – 12
14
Wrong input
while(condition)
{
statement(s);
}
• The statements within the { } symbols are known as body part of the loop.
• The { } are not required if there is only one statement in the body part.
• Here, the condition is checked first and then only the statements are executed.
That’s why while loop is an entry-controlled loop.
• The statements in the body part will be executed till the condition is true.
• As soon as the condition becomes false, the control will come out of the while
loop. If the condition remains true always then it will lead to infinite loop. So,
writing a valid condition is important.
• For example, the while loop can be written as follows to print 1 to 10.
....
i=1;
while(i<=10)
{
printf(“%d\n”,i);
i=i+1;
}
....
• Here, first i is initialized to 1. The value of i is printed and then i is incremented.
This process is repeated if i is less than or equal to 10. Once the i is 11, the
condition in while is false and control comes out of loop.
#include <stdio.h>
#include <conio.h>
main ()
{
int n, sum=0;
int i=1;
clrscr();
printf(“Give integer number: ”);
scanf(“%d”,&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf(“Sum of first %d numbers = %d\n”,n,sum);
getch();
}
Output:
Give integer number: 6
Sum of first 6 numbers = 21
• Here, the statement(s) are executed till the condition is true. As soon as the
condition evaluates to false, the loop terminates and the next statement after
do…while loop is executed.
• All the programs written in while loop can always be written using do…while
loop.
#include <stdio.h>
#include <conio.h>
main ()
{
int n, sum=0;
int i=1;
clrscr();
printf(“Give integer number: ”);
scanf(“%d”,&n);
do
{
sum=sum+i;
i++;
} while(i<=n);
printf(“Sum of first %d numbers = %d\n”,n,sum);
getch();
}
Output:
Give integer number: 6
Sum of first 6 numbers = 21
• We require one variable which is used to control the loop, which is called as
control variable.
• The control variable is initialized in initialization expression. This statement
executes only once.
• The condition expression checks the value of control variable. If the condition
expression is true, then the statements written are executed.
• After every execution of statements, the last expression increment/decrement is
executed. Then again, the condition is checked, and body is executed if the
condition evaluates to true.
• Loop terminates when the condition evaluates to false.
• For example, following for loop will print the numbers 1 to 10 each on separate
line.
....
....
for(i=1; i<=10; i++)
printf(“%d\n”,i);
....
....
• Here, i=1; statement is initialization which will execute only once.
• Then the second expression i<=10 will be evaluated. It is true, so loop body will
be executed i.e., printf() statement will be executed, then i++ statement will
execute, which will make i=2, then again the condition i<=10 will be evaluated,
which is true, so again printf() statement will be executed.
• This sequence will go on till the condition is true. When i becomes 11, at that time
condition evaluated to false, so loop is over.
#include <stdio.h>
#include <conio.h>
main ()
{
int n, i, sum=0;
clrscr();
printf(“Give integer number: ”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
sum=sum+i;
}
printf(“Sum of first %d numbers = %d\n”,n,sum);
getch();
}
Output:
Give integer number: 6
Sum of first 6 numbers = 21
• In above example, for(i=1; i<=3; i++) is outer for loop. We can say that
control variable for outer loop is ‘i’.
• for(j=1; j<=3; j++) is inner for loop. Variable ‘j’ is control variable for inner
for loop. For each value of outer loop control variable i.e., ‘i’, all the values of
inner loop control variable are tried i.e., ‘j’.
• Following table explains the set of values for variable ‘i’ and ‘j’ for above
example.
i j
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
• From above table, it is very clear that for i=1, j goes from 1 to 3. When all
values of j are done, i gets next value i.e., i=2, again j goes from 1 to 3.
Similarly for i=3, j goes from 1 to 3.
[In this program, from the pattern, it is clear that number of stars in line i is equal
to i. So this example can be solved using nesting of loops. The outside loop will
count number of lines and provide newline character while inner loop will count
number of starts in current line.]
#include <stdio.h>
#include <conio.h>
main ()
{
int i,j,n;
clrscr();
printf(“How many lines?\n”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}
Output:
How many lines?
6
*
**
***
****
*****
******
5. break STATEMENT
• The break statement is used to jump out of a loop. 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.
• Example:
main()
{
int i;
for (i=1;i<=10;i++)
{
if(i==6)
break;
printf(“%d ”,i);
}
getch();
}
Output:
1 2 3 4 5
6. continue STATEMENT
• The continue statement causes the loop to continue with the next iteration skipping
the remaining statements in that loop.
• Example:
main()
{
int i;
for(i=1;i<=5;i++)
{
printf(“%d”,i);
if(i==3)
continue;
printf(“**”);
}
getch();
}
Output:
1**2**34**5**
7. goto STATEMENT
• The goto statement changes the normal sequence of the program execution by
transferring the control to other part of the program.
• Example:
#include <stdio.h>
#include <conio.h>
main ()
{
int i=1, sum=0, n;
clrscr();
printf(“Enter value of N\n”);
scanf(“%d”,&n);
next:
sum=sum+i;
i+=1;
if(i<=n)
goto next;
printf(“Sum = %d\n”,sum);
getch();
}
Output:
Enter value of N
4
Sum = 10
8. SOLVED PROGRAMS
/* Write a program to find out whether number is even or odd */
#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
printf(“Enter any Number\n”);
scanf(“%d”,&a);
if(a%2==0)
printf(“The %d is even\n”,a);
else
printf(“The %d is odd\n”,a);
getch();
}
Output 1:
Enter any Number
4
The 4 is even
Output 2:
Enter any Number
9
The 9 is odd
float percent;
clrscr();
printf(“Enter name of the student\n”);
scanf(“%s”,&name);
printf(“Enter marks in 6 subjects\n”);
scanf(“%d%d%d%d%d%d”,&m1,&m2,&m3,&m4,&m5,&m6);
total=m1+m2+m3+m4+m5+m6;
percent=total/6;
if((m1<40)||(m2<40)||(m3<40)||(m4<40)||(m5<40)||(m6<40))
printf(“Name: %s \nTotal: %d \nGrade: Fail”
,name,total);
else if(percent >= 75)
printf(“Name: %s \nTotal: %d \nGrade:
Distinction”,name,total);
else if(percent >= 60)
printf(“Name: %s \nTotal: %d \nGrade: First
Class”,name,total);
else if(percent >= 50)
printf(“Name: %s \nTotal: %d \nGrade: Second
Class”,name,total);
else
printf(“Name: %s \nTotal: %d \nGrade: Third
Class”,name,total);
getch();
}
Output 1:
Enter name of the student
Ajay
Enter marks in 6 Subjects
65 66 89 77 76 76
Name: Ajay
Total: 449
Grade: First Class
Output 2:
Enter name of the student
Radha
Enter marks in 6 Subjects
55 66 77 1 88 99
Name: Radha
Total: 386
Grade: Fail
#include<stdio.h>
#include<conio.h>
main()
{
int n,i;
clrscr();
printf(“Give Interger Number: “);
scanf(“%d”,&n);
while(n!=0)
{
i = n % 10; /* separate the digit */
printf(“The digit is = %d \n”,i);
n = n/10; /* new value of n after separation
of digit */
}
getch();
}
Output:
Give Integer Number: 15689
The digit is = 9
The digit is = 8
The digit is = 6
The digit is = 5
The digit is = 1
#include<stdio.h>
#include<conio.h>
main()
{
int num,i;
clrscr();
printf(“Give Integer Number\n“);
scanf(“%d”,&num);
printf(“Reverse of %d is = ”,num);
while(num!=0)
{
i = num % 10;
printf(“%d”,i);
num = num/10;
}
getch();
}
Output:
Give Integer Number
2156
Reverse of 2156 is = 6512
#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0; /* sum initialized to 0 */
int i,num; /* separated digit stored in i */
clrscr();
printf(“Give Integer Number: “);
scanf(“%d”,&n);
num = n; /* n stored in num, because n will change
in loop */
while(n!=0)
{
i = n % 10; /* separate the digit */
sum = sum + i*i*i; /* find cube of digit and
add to sum */
n = n/10; /* new value of n after separation
of digit */
}
if (sum == num)
printf(“Given number %d is Armstrong\n”,num);
else
printf(“Given number %d is not Armstrong\n”,num);
getch();
}
Output 1:
Give Integer Number
245
Given Number 245 is not Armstrong
Output 2:
Give Integer Number
153
Given Number 153 is Armstrong
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2,num3,n;
int count;
clrscr();
num1 = 0;
num2 = 1;
printf(“How many Fibonacci Numbers?\n”);
scanf(“%d”,&n);
if(n > 2)
{
printf(“The Fibonacci Numbers are\n”);
printf(“%d%5d”,num1,num2);
count = 2;
while(count < n)
{
num3 = num1 + num2;
printf(“%5d”,num3);
num1 = num2;
num2 = num3;
count++;
}
}
else
printf(“By definition first two numbers
are 0 1\n”);
getch();
}
Output 1:
How many Fibonacci Numbers?
9
The Fibonacci Numbers are
0 1 1 2 3 5 8 13 21
Output 2:
How many Fibonacci Numbers?
1
By definition first two numbers are 0 1
#include<stdio.h>
#include<conio.h>
main()
{
int n,sum=0;
int digit;
clrscr();
printf(“Give Positive Integer Number: “);
scanf(“%d”,&n);
printf(“Given Number = %d\n”,n);
if(n < 0)
printf(“Please Give Positive Number\n”);
else
{
do
{
digit = n % 10;
sum = sum + digit;
n = n/10;
} while(n > 0);
printf(“Summation of Individual Digits
= %d\n”,sum);
}
getch();
}
Output 1:
Give Positive Integer Number: 2314
Given Number = 2314
Summation of Individual Digits = 10
Output 2:
Give Positive Integer Number: -21
Given Number = -21
Please Give Positive Number
#include<stdio.h>
#include<conio.h>
main()
{
int num,temp;
int i,rev=0;
clrscr();
printf(“Give Integer Number\n”);
scanf(“%d”,&num);
printf(“Reverse of %d is = “,num);
temp = num;
while(num != 0)
{
i = num % 10;
rev = rev * 10 + i;
num = num/10;
}
printf(“%d\n”,rev);
if(temp == rev)
printf(“Given Number %d is Palindrome\n” ,temp);
else
printf(“Given Number %d is not Palindrome\n”
,temp);
getch();
}
Output 1:
Give Integer Number
123
Reverse of 123 is = 321
Given Number 123 is not Palindrome
Output 2:
Give Integer Number
121
Reverse of 121 is = 121
Given Number 121 is Palindrome
#include<stdio.h>
#include<conio.h>
main()
{
int i,n;
long int fact;
clrscr();
printf(“Give Positive Integer Number\n”);
scanf(“%d”,&n);
if(n < 0)
printf(“Factorial of –ve number not
defined\n”);
else
{
fact = 1;
for(i=1;i<=n;i++)
fact = fact * i;
printf(“Factorial of %d = %ld\n”,n,fact);
}
getch();
}
Output 1:
Give Positive Integer Number
5
Factorial of 5 = 120
Output 2:
Give Positive Integer Number
-6
Factorial of –ve number not defined
Output:
How many lines?
5
1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,n;
clrscr();
printf(“How many lines?\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++) /* controls number of lines */
{
for(j=1;j<=n-i;j++) /* controls number of
blanks */
printf(“ “,j);
for(k=1;k<=i;k++) /* controls number
printing */
printf(“%d”,k);
printf(“\n”);
}
getch();
}
Output :
How many lines?
6
1
12
123
1234
12345
123456
Output :
How many lines?
6
1
22
333
4444
55555
666666
Output:
How many rows you want?
5
1
13
135
1357
13579
Output :
How many rows you want?
5
1
AB
123
ABCD
12345
Output:
How many rows you want?
5
1
AB
234
CDEF
56789