Chapter 3
Chapter 3
CHAPTER 3
Control structure in C
break;
#include <stdio.h>
int main()
while(num<=100)
CKPCET, SURAT 1
PPS (3110003)
if (num==2)
break;
num++;
printf("Out of while-loop");
return 0;
Output:
Out of while-loop
The continue statement skips the current iteration of the loop and continues with the next
iteration.
continue;
CKPCET, SURAT 2
PPS (3110003)
#include <stdio.h>
int main ()
int a = 10;
/* do loop execution */
do {
if( a == 15) {
a = a + 1;
continue;
a++;
} while( a < 20 );
return 0;
CKPCET, SURAT 3
PPS (3110003)
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Q.2 Construct ‘C’ program to print the following pattern using loop
statement.(PPS_WIN_2019)
22
333
4444
55555
#include <stdio.h>
void main()
int i,j,rows=5;
for(i=1;i<=rows;i++)
CKPCET, SURAT 4
PPS (3110003)
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
Q.3 Demonstrate a C program to input an integer number and check last digit of
number is even or odd.(PPS_WIN_2019)
#include <stdio.h>
int main()
if(LastDigit%2==0)
else
return 0;
A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
CKPCET, SURAT 5
PPS (3110003)
Syntax
statement(s);
• The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
• Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is
false, the body of the loop does not execute and the flow of control jumps to the next
statement just after the 'for' loop.
• After the body of the 'for' loop executes, the flow of control jumps back up to the
increment statement. This statement allows you to update any loop control variables.
This statement can be left blank, as long as a semicolon appears after the condition.
• The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the 'for' loop terminates.
Example
#include <stdio.h>
int main () {
int a;
return 0;
Output
CKPCET, SURAT 6
PPS (3110003)
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Q.5 Build a function to check number is prime or not. If number is prime then function
return value 1 otherwise return 0. .(PPS_WIN2019) (PPS_WIN_18)
#include<stdio.h>
int check_prime(int);
main()
{
int n, result;
result = check_prime(n);
if ( result == 1 )
printf("%d is prime.\n", n);
else
printf("%d is not prime.\n", n);
return 0;
}
int check_prime(int a)
{
CKPCET, SURAT 7
PPS (3110003)
int c;
Q.6 Construct ‘C’ program to print the following pattern using loop statement.
.(PPS_WIN2019)
1
22
333
4444
55555
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
printf("\n Enter the value of n:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
CKPCET, SURAT 8
PPS (3110003)
}
Q.7 State the difference between entry control loop and exit control loop.
.(CPU_WIN_2019)(CPU_SUM_2016)
BASIS OF
ENTRY CONTROLLED LOOP EXIT CONTROLLED LOOP
COMPARISON
Entry controlled loop is a loop in Exit controlled loop is a loop in which
which the test condition is checked the loop body is executed first and then
Description
first, and then loop body will be the given condition is checked
executed. afterwards.
False Test If the test condition is false, loop body If the test condition is false, loop body
Condition will not be executed. will be executed at least once.
Examples of exit controlled loop An example of exit controlled loop is
Example
include, For Loop and While Loop. Do While Loop.
Entry controlled loop are used when
Entry controlled loop is used when
checking of test condition is
Use checking of test condition is mandatory
mandatory before executing loop
after executing.
body.
Q.8 Compare for loop and while loop with illustrations .(CPU_WIN_2019) (PPS_WIN_18)
BASIS FOR
FOR WHILE
COMPARISON
Use The 'for' loop used only when we The 'while' loop used only when
already knew the number of the number of iteration are not
iterations. exactly known.
CKPCET, SURAT 9
PPS (3110003)
BASIS FOR
FOR WHILE
COMPARISON
Condition If the condition is not put up in 'for' If the condition is not put up in
loop, then loop iterates infinite times. 'while' loop, it provides
compilation error.
Iteration statement In 'for' loop iteration statement is In 'while' loop, the iteration
written at top, hence, executes only statement can be written anywhere
after all statements in loop are in the loop.
executed.
CKPCET, SURAT 10
PPS (3110003)
Value: 7
Value: 8
Value: 9
Example of while loop
#include <stdio.h>
int main () {
int x = 1;
/* Execute While Loop */
while( x < 10 ) {
printf("Value: %dn", x);
x++;
}
return 0;
}
Output
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
Value: 7
Value: 8
Value: 9
CKPCET, SURAT 11
PPS (3110003)
Q.9 Write a program that reads a value n and generates the following pattern:
.(CPU_WIN_2019)
1
123
12345
1234567
123456789
#include<stdio.h>
int main()
{
int i, j, k;
for (i=1;i<=5;i++)
{
for (j=i;j<5;j++)
{
printf(" ");
}
for (k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
return 0;
}
Q.10 Define general form of 1) do while loop 2) Nested if 3) goto (PPS_WIN_18)
The do-while loop is also called exit control loop because, in do-while loop, compiler will 1st
execute the statements, then check the condition, whether it is true or false.
Syntax of Do-While Loop
CKPCET, SURAT 12
PPS (3110003)
do
{
//Statements
}while(condition test);
A nested if in C is an if statement that is the target of another if statement. Nested if statements
means an if statement inside another if statement.
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
The goto statement is a jump statement which jumps from one point to another point within a
function.
Syntax of goto statement
goto label;
..
.
label: statement;
Q.11 Explain switch case statement with example to read number between 1 to 7 and print
relatively day Sunday to Saturday. (PPS_WIN_18)
Switch statement tests the value of a variable and compares it with multiple cases.
General Syntax
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
CKPCET, SURAT 13
PPS (3110003)
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Statement-x;
#include <stdio.h>
int main()
{
int week;
/* Input week number from user */
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
CKPCET, SURAT 14
PPS (3110003)
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}
return 0;
}
Output
Enter week number(1-7): 1
Monday
Q.12 Write a program to find out an area of a circle.(CPU_WIN_2017)
#include<stdio.h>
int main()
{
float radius, area;
printf("\nEnter the radius of Circle : ");
scanf("%d", &radius);
area = 3.14 * radius * radius;
printf("\nArea of Circle : %f", area);
return (0);
}
CKPCET, SURAT 15
PPS (3110003)
Output :
Enter the radius of Circle : 2.0
Area of Circle : 6.14
Q.13 Write a program to print multiplication table of any number. .(CPU_WIN_2017)
int main()
{
int number, i = 1;
printf(" Enter the Number:");
scanf("%d", &number);
printf("Multiplication table of %d:\n ", number);
printf("--------------------------\n");
while (i <= 10)
{
printf(" %d x %d = %d \n ", number, i, number * i);
i++;
}
return 0;
}
Output:
Enter the Number:6
Multiplication table of 6:
--------------------------
6x1=6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
CKPCET, SURAT 16
PPS (3110003)
6 x 9 = 54
6 x 10 = 60
Q.14 Explain Entry Controlled Loop and Exit Controlled Loop with
flowchart.(CPU_WIN_2017) (CPU_SUM_2016)
1)Entry controlled loop - The loop which has a condition check at the entrance of the loop, the
loop executes only and only if the condition is satisfied is called as entry control loop.
EX-
• while loop
• For loop
2) Exit controlled loop - The loop which keeps on executing until a particular condition is satisfied
and when the condition is satisfied according to the criteria the loop exits, this is known as exit
controlled loop.
EX-
• While loop (during polling)
Q.14 Discuss general form of following decision – making statements. (CPU_WIN_2016)
(I) IF (II) SWITCH (III) GOTO
(1) The general form of a if statement is,
if(expression)
CKPCET, SURAT 17
PPS (3110003)
{
statement inside;
}
statement outside;
(2) Switch statement is a control statement that allows us to choose only one choice among the
many given choices.
The general form of switch statement is,
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
}
(3) The goto statement allows us to transfer control of the program to the specified label.
Syntax of goto Statement
goto label;
... .. ...
... .. ...
CKPCET, SURAT 18
PPS (3110003)
label:
statement;
Q.15 List down three constructs for performing loop operations in C language. Write
general form of same. (CPU_WIN_2016) (CPU_WIN_2014)
'C' programming language provides us with three types of loop constructs:
while (condition) {
statements;
}
do {
statements
} while (expression);
Q.16 Develop a simple program to Add, subtract and multiply two numbers using switch
statement. . (CPU_WIN_2016) (CPU_SUM_2016)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
CKPCET, SURAT 19
PPS (3110003)
CKPCET, SURAT 20
PPS (3110003)
CKPCET, SURAT 21
PPS (3110003)
{
if ( i%2 != 0 )
{
printf("%d ", i);
Sum = Sum + i;
}
}
printf("\n \n The Sum of Odd Numbers from 1 to %d = %d", number, Sum);
return 0;
}
OUTPUT
Please Enter the Maximum Limit Value : 10
Odd Numbers between 0 and 10 are : 1 3 5 7 9
The Sum of Odd Numbers from 1 to 10 = 25
Q.19 Explain various types of loop available in C with example. (CPU_WIN_2015)
(CPU_WIN_2013)
Types of Loop
• while loop
• do...while loop
• for loop
While Loop
While loop is also called entry control loop because, in while loop, compiler will 1st check the
condition, whether it is true or false, if condition is true then execute the statements.
initialization;
while (condition)
{
----------
----------
inc/dec;
}
CKPCET, SURAT 22
PPS (3110003)
CKPCET, SURAT 23
PPS (3110003)
} while (condition);
Example of Do-While Loop
#include<stdio.h>
void main()
{
int a=1,num;
printf("Enter any number : ");
scanf("%d",&num);
do
{
printf("\nHello...!!");
a++;
} while (a<=num);
}
Output :
Enter any number : 5
Hello...!!
Hello...!!
Hello...!!
Hello...!!
Hello...!!
For Loop
In for loop we put initialization, contidion and increment/decrement all together. Initialization will
be done once at the beginning of loop. Then, the condition is checked by the compiler. If the
condition is false, for loop is terminated. But, if condition is true then, the statements are executed
until condition is false.
Syntax of For Loop
for (initialization;condition;inc/dec)
{
----------
----------
CKPCET, SURAT 24
PPS (3110003)
CKPCET, SURAT 25
PPS (3110003)
Q.21 Write a C Program to check whether the given number is prime or not.
(CPU_WIN_2014)
#include <stdio.h>
void main()
{
int num,i,ctr=0;
printf("Input a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++)
{
if(num % i==0)
{
ctr++;
break;
}
}
if(ctr==0 && num!= 1)
printf("%d is a prime number.\n",num);
else
printf("%d is not a prime number",num);
}
Output:
CKPCET, SURAT 26
PPS (3110003)
Input a number: 13
13 is a prime number.
CKPCET, SURAT 27
PPS (3110003)
CKPCET, SURAT 28
PPS (3110003)
Q. 24 Write a program to select and print the largest of the three nos. using nested-if-else
statement.
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
return 0;
}
}
Q.25 Explain various types of loop available in C with example. Compare while & do-while
loop(CPU_SUM_2014,2015,)(PPS_SUM_2019)
A Loop executes the sequence of statements many times until the stated condition becomes false.
A loop consists of two parts, a body of a loop and a control statement.
There are 3 types of Loop in C language, namely:
1. while loop
2. for loop
CKPCET, SURAT 29
PPS (3110003)
3. do while loop
while loop
while loop can be addressed as an entry control loop. It is completed in 3 steps.
1. Variable initialization.(e.g int x = 0;)
2. condition(e.g while(x <= 10))
3. Variable increment or decrement ( x++ or x-- or x = x + 2 )
Syntax :
variable initialization;
while(condition)
{
statements;
variable increment or decrement;
}
Example: Program to print first 10 natural numbers
#include<stdio.h>
void main( )
{
int x;
x = 1;
while(x <= 10)
{
printf("%d\t", x);
/* below statement means, do x = x+1, increment x by 1*/
x++;
}
}
Output: 1 2 3 4 5 6 7 8 9 10
For loop
for loop is used to execute a set of statements repeatedly until a particular condition is satisfied.
We can say it is an open-ended loop. General format is,
CKPCET, SURAT 30
PPS (3110003)
CKPCET, SURAT 31
PPS (3110003)
BASIS FOR
WHILE DO-WHILE
COMPARISON
Controlling Condition In 'while' loop the controlling In 'do-while' loop the controlling
condition appears at the start of the condition appears at the end of the
loop. loop.
Iterations The iterations do not occur if, the The iteration occurs at least once
condition at the first iteration, even if the condition is false at the
appears false. first iteration.
CKPCET, SURAT 32
PPS (3110003)
BASIS FOR
WHILE DO-WHILE
COMPARISON
CKPCET, SURAT 33
PPS (3110003)
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
getch();
}
CKPCET, SURAT 34
PPS (3110003)
#include <stdio.h>
#include <conio.h>
void main() {
int i,j,k,t=0;
clrscr();
for (i=1; i<=4; i++) {
for (k=t; k<4; k++) {
printf(" ");
}
for (j=0; j< i; j++) {
printf(" * ");
t = t + 1;
}
printf("\n");
}
getch();
}
Q. 28 Write a C program to convert Celsius to Fahrenheit and vice versa.
(CPU_SUM_2016,18)
int main()
{
float fh,cl;
int choice;
CKPCET, SURAT 35
PPS (3110003)
if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}
Output:
First Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 1
CKPCET, SURAT 36
PPS (3110003)
Second Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 2
CKPCET, SURAT 37
PPS (3110003)
The switch case statement is used when we have multiple options and we need to perform a
different task for each option.
Before we see how a switch case statement works in a C program, let’s checkout the syntax of it.
case constant:
CKPCET, SURAT 38
PPS (3110003)
//C Statements;
case constant:
//C Statements;
default:
//C Statements;
}
Example:
#include <stdio.h>
int main()
int num=2;
switch(num+2)
case 1:
case 2:
case 3:
default:
return 0;
CKPCET, SURAT 39
PPS (3110003)
Output:
Explanation: In switch I gave an expression, you can give variable also. I gave num+2, where num
value is 2 and after addition the expression resulted 4. Since there is no case defined with value 4
the default case is executed.
23
456
78910
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
for(i=1;i<=5;i++)
for(j=1;j<i;j++)
printf("%d",k);
k++
printf("\n");
CKPCET, SURAT 40
PPS (3110003)
getch();
Q. 33 Write a C program to read 10 numbers from user and store them in an array. Display
Sum, Minimum and Average of the numbers. (CPU_SUM_2018)
#include <stdio.h>
int main()
{
int i, num;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
int array[num];
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
for (i = 0; i < num; i++)
{
total+=array[i];/* this means total=total+array[i]; */
}
average = total / num;
printf("\n Sum of all numbers = %.2f\n", total);
CKPCET, SURAT 41
PPS (3110003)
perimeter=2*(length+breadth);
CKPCET, SURAT 42
PPS (3110003)
int main()
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
CKPCET, SURAT 43
PPS (3110003)
return 0;
}
Q. 37 Write a program to reverse a given number. (PPS_SUM_2019)
#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
Output
CKPCET, SURAT 44
PPS (3110003)
CKPCET, SURAT 45
PPS (3110003)
else
printf("%c is a consonant.", c);
return 0;
}
CKPCET, SURAT 46
PPS (3110003)
t = n;
while (t != 0) {
remainder = t%10;
sum = sum + power(remainder, digits);
t = t/10;
}
if (n == sum)
return 1;
else
return 0;
}
int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
CKPCET, SURAT 47
PPS (3110003)
scanf("%d", &a[i][j]);
}
CKPCET, SURAT 48
PPS (3110003)
(CPU_WIN_2017)
Q. Which one is the correct syntax of for loop?
a)for(initialization,condition,increment/decrement)
b)for(initialization;condition;increment/decrement)
c)for(condition,condition,increment/decrement)
d) for(condition;initialization;increment/decrement)
(CPU_WIN_2016)
Q. Continue statement
(a) Breaks loop and goes to next statement after loop.
(b) does not break loop but starts new iteration. (c) exits the program.
(d) Starts from beginning of program.
CKPCET, SURAT 49
PPS (3110003)
(a) 2 times. (b) 1 times. (c) infinite times. (d) loop will not be executed.
(CPU_SUM_2014)
Q. Which of following loop is executed at least once?
a) do-while b) for c) if d) while
(CPU_SUM_2015)
Q. The first expression in a for… loop is
a) Step value of loop b) Condition statement c) Value of the counter variable d) None of the
above
CKPCET, SURAT 50
PPS (3110003)
(CPU_SUM_2016)
Q. Which are not looping structures?
(a) For loop (b) While loop (c) Do...while loop (d) if…else
Q. How many times the following code prints the string “hello” for(i=1;i<=50;i++);
printf(“Hello”);
(a) 1 (b) 50 (c) Zero (d) None of them
(CPU_SUM_2017)
Q. Continue statement
(a) Breaks loop and goes to next statement after loop. (b) Does not break loop but starts new
iteration. (c) Exits the program. (d) Starts from beginning of program.
CKPCET, SURAT 51
PPS (3110003)
printf(“%c,”,i);
}
(a)65,66,67,68,69,70 (b)a,b,c,d,e, (c)A,B,C,D,E, (d)A,B,C,D,E
(CPU_SUM_2018)
Q. For loop is _________.
(a)Function Controlled Loop (b)Entry Controlled Loop (c) Exit Controlled Loop (d) None
of these
(CPU_SUM_2019)
Q. How many times following loop will be rotated?
int a = 10, b = 1;
while(a>b)
{ a--; b++; }
CKPCET, SURAT 52
PPS (3110003)
CKPCET, SURAT 53