0% found this document useful (0 votes)
23 views44 pages

PWC Unit-2 (Decision Making and Branching)

This document covers Unit 2 (Part 2) of the Basics of Logic Development course, focusing on decision-making and branching in programming. It details various control structures such as selection statements (if, if-else, nested if, if-else-if ladder, and switch statements) and looping statements (while, for, do-while). Additionally, it provides examples and programs to illustrate the concepts of decision-making in C programming.

Uploaded by

akbarikavya13
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)
23 views44 pages

PWC Unit-2 (Decision Making and Branching)

This document covers Unit 2 (Part 2) of the Basics of Logic Development course, focusing on decision-making and branching in programming. It details various control structures such as selection statements (if, if-else, nested if, if-else-if ladder, and switch statements) and looping statements (while, for, do-while). Additionally, it provides examples and programs to illustrate the concepts of decision-making in C programming.

Uploaded by

akbarikavya13
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/ 44

Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Chhotubhai Gopalbhai Patel Institute of


Technology, Bardoli

Subject
Basics of Logic Development
(IT3009)

Unit – 2 (Part – 2)
Decision Making and Branching

Prepared by – Mr. Viral H. Panchal 1


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

CONTENTS
1. Basics of Decision Making (Branching) Control Structures
2. Selection Statements
2.1. if Statement
2.2. if…else Statement
2.3. Nested if Statement
3. Multiway Selection Statements
3.1. if-else-if ladder (if...else if...else) Statement
3.2. switch Statement
4. Basics of Looping Statements
4.1. while Loop Statement
4.2. for Loop Statement
4.3. do-while Loop Statement
4.4. Difference between while loop and do-while loop
4.5. Nested Loops
5. Solved Programs

Prepared by – Mr. Viral H. Panchal 2


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

1. Basics of Decision Making (Branching) Control Structures


Decision making structures require that the programmer specifies one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to be false.
The general form of a typical decision-making structure found in most of the
programming languages is shown below –

C programming language assumes any non-zero and non-null values as true, and
if it is either zero or null, then it is assumed as false value.
C programming language provides the following types of decision-making
statements.
• Selection Statements
o if Statement
o if-else Statement
o Nested if Statement
• Multiway Selection Statements
o if-else-if ladder (if...else if...else) Statement
o switch statements

Prepared by – Mr. Viral H. Panchal 3


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

2. Selection Statements

2.1. if Statement
An if statement consists of a boolean expression followed by one or more
statements.

Syntax –

The syntax of an 'if' statement in C programming language is −

if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}

If the Boolean expression evaluates to true, then the block of code inside the
'if' statement will be executed. If the Boolean expression evaluates to false, then
the first set of code after the end of the 'if' statement (after the closing curly brace)
will be executed.

C programming language assumes any non-zero and non-null values as true


and if it is either zero or null, then it is assumed as false value.

Flow Diagram –

Prepared by – Mr. Viral H. Panchal 4


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Example –

#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}

Output −

a is less than 20
value of a is : 10

2.2. if…else Statement


An if statement can be followed by an optional else statement, which executes
when the boolean expression is false.

Syntax –

The syntax of an if...else statement in C programming language is –

if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}

If the boolean expression evaluates to true, then the if block of code will be
executed, otherwise else block of code will be executed.

Prepared by – Mr. Viral H. Panchal 5


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Flow Diagram –

Example –

#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
} else {
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}

Output −

a is not less than 20


value of a is : 100

Prepared by – Mr. Viral H. Panchal 6


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Program –

Write a program to check whether the given number is odd or even.

/* C program to check whether the given number is odd or even */

#include <stdio.h>
int main()
{
int n;
printf("Enter an integer: ");
scanf("%d",&n);
if ( n % 2 == 0)
printf("%d is even",n);
else
printf("%d is odd",n);
return 0;
}

Output −

Enter an integer: 5
5 is odd

The Conditional Operator (? :) –

The conditional operator ? : can be used to replace if...else statements. It has


the following general form −

Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement
of the colon.

The value of a ? expression is determined like this −

Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value
of the entire ? expression.

If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the
expression.

Prepared by – Mr. Viral H. Panchal 7


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

2.3. Nested if Statement


It is always legal in C programming to nest if-else statements, which means you
can use one if or else if statement inside another if or else if statement(s).

Syntax –

The syntax for a nested if statement in C is as follows −

if( boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}

You can nest else if...else in the similar way as you have nested if statement.

Example –

#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
/* check the boolean condition */
if( a == 100 ) {
/* if condition is true then check the following */
if( b == 200 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}

Output –

Value of a is 100 and b is 200


Exact value of a is : 100
Exact value of b is : 200

Prepared by – Mr. Viral H. Panchal 8


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Program –
Write a program to find largest number among three numbers.

/* C program to find largest number among three numbers using if


Statement */

#include <stdio.h>
int main ()
{
int n1, n2, n3;
printf ("Enter three numbers: ");
scanf ("%d%d%d", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf ("Largest number: %d",n1);
if (n2 >= n1 && n2 >= n3)
printf ("Largest number: %d",n2);
if (n3 >= n1 && n3 >= n2)
printf ("Largest number: %d",n3);
return 0;
}

Output –

Enter three numbers: 12 45 23


Largest number: 45

/* C program to find largest number among three numbers using


if...else Statement */

#include <stdio.h>
int main ()
{
int n1, n2, n3;
printf ("Enter three numbers: ");
scanf ("%d%d%d", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf ("Largest number: %d",n1);
else if (n2 >= n1 && n2 >= n3)
printf ("Largest number: %d",n2);
else
printf ("Largest number: %d",n3);
return 0;
}

Prepared by – Mr. Viral H. Panchal 9


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Output –

Enter three numbers: 34 6 2


Largest number: 34

/* C program to find largest number among three numbers using


nested if...else Statement */

#include <stdio.h>
int main ()
{
int n1, n2, n3;
printf ("Enter three numbers: ");
scanf ("%d%d%d", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf ("Largest number: %d",n1);
else
printf ("Largest number: %d",n3);
} else {
if (n2 >= n3)
printf ("Largest number: %d",n2);
else
printf ("Largest number: %d",n3);
}
return 0;
}

Output –

Enter three numbers: 5 34 -7


Largest number: 34

Prepared by – Mr. Viral H. Panchal 10


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

3. Multiway Selection Statements

3.1. if-else-if ladder (if...else if...else) Statement


An if statement can be followed by an optional else if...else statement, which is
very useful to test various conditions using single if...else if statement.

When using if, else if, else statements there are a few points to keep in mind.

o An if can have zero or one else's and it must come after any else if's.
o An if can have zero to many else if's and they must come before the else.
o Once an else if succeeds, none of the remaining else if's or else's will be
tested.

Syntax –

The syntax of an if...else if...else statement in C programming language is −

if(boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
} else if( boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
} else if( boolean_expression 3) {
/* Executes when the boolean expression 3 is true */
} else {
/* executes when the none of the above condition is true */
}

Example –

#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a == 10 ) {
/* if condition is true then print the following */
printf("Value of a is 10\n" );
} else if( a == 20 ) {
/* if else if condition is true */
printf("Value of a is 20\n" );

Prepared by – Mr. Viral H. Panchal 11


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

} else if( a == 30 ) {
/* if else if condition is true */
printf("Value of a is 30\n" );
} else {
/* if none of the conditions is true */
printf("None of the values is matching\n" );
}
printf("Exact value of a is: %d\n", a );
return 0;
}

Output −

None of the values is matching


Exact value of a is: 100

Program –
Write a program to check whether the entered number is positive or negative.

/* C program to check whether the entered number is positive or


negative */

#include <stdio.h>
int main()
{
int num;
printf("Enter integer number: ");
scanf("%d",&num);
//Conditions to check if the number is negative or positive
if (num > 0)
printf("The number is positive");
else if (num < 0)
printf("The number is negative");
else
printf("Zero");
return 0;
}

Output –

Enter integer number: -9


The number is negative

Prepared by – Mr. Viral H. Panchal 12


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Output –

Enter integer number: 0


Zero

Program –
Write a program to read marks from keyboard and display equivalent grade
according to following table (if else ladder)
Marks Grade
100 - 80 Distinction
79 - 60 First Class
59 - 40 Second Class
< 40 Fail
#include<stdio.h>
int main()
{
int marks;
printf("Enter Marks between 0 to 100: ");
scanf("%d",&marks);
if(marks>100 || marks<0)
printf("Invalid Input");
else if(marks>=80)
printf("You got Distinction");
else if(marks>=60)
printf("You got First Class");
else if(marks>=40)
printf("You got Second Class");
else
printf("You are Fail");
return 0;
}

Output –

Enter Marks between 0 to 100: 56


You got Second Class

Output –

Enter Marks between 0 to 100: 93


You got Distinction

Prepared by – Mr. Viral H. Panchal 13


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

3.2. switch Statement


A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is checked
for each case.

Syntax –

The syntax for a switch statement in C programming language is as follows −

switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}

The following rules apply to a switch statement −

o The expression used in a switch statement must have an integral or


enumerated type, or be of a class type in which the class has a single
conversion function to an integral or enumerated type.
o You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
o The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
o When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
o When a break statement is reached, the switch terminates, and the flow of
control jumps to the next line following the switch statement.
o Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.

Prepared by – Mr. Viral H. Panchal 14


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

o A switch statement can have an optional default case, which must appear
at the end of the switch. The default case can be used for performing a
task when none of the cases is true. No break is needed in the default case.

Flow Diagram –

Example –

#include <stdio.h>
int main ()
{
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n");
break;
case 'B' :
case 'C' :
printf("Well done\n");
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n");

Prepared by – Mr. Viral H. Panchal 15


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

break;
default :
printf("Invalid grade\n");
}
printf("Your grade is %c\n", grade);
return 0;
}

Output –

Well done
Your grade is B

Program –
Write a program to read number 1 to 7 and print relatively day Sunday to
Saturday. (switch case)
/* C program to read number 1 to 7 and print relatively day Sunday
to Saturday */

#include <stdio.h>
int main ()
{
int day;
printf("Enter day number between 1 to 7: ");
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");

Prepared by – Mr. Viral H. Panchal 16


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

break;
case 7:
printf("Saturday\n");
break;
default:
printf("Wrong input\n");
}
return 0;
}

Output –

Enter day number between 1 to 7: 6


Friday

Output –

Enter day number between 1 to 7: 9


Wrong input

Prepared by – Mr. Viral H. Panchal 17


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

4. Basics of Looping Statements


There may be a situation, when you need to execute a block of code several
number of times. In general, statements are executed sequentially: the first statement
in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for more
complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple


times and following is the general from of a loop statement in most of the
programming languages –

C programming language provides the following types of loops to handle looping


requirements.

• while loop
• for loop
• do-while loop
• nested loops

Prepared by – Mr. Viral H. Panchal 18


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Depending on where the condition is checked, we can have two types of loop
structures:

• Entry Control
• Exit Control

In entry control loop, the condition is written first and then the body of statements.
While in exit control loop, the body of the statements is written first and then condition
is written. This means that body of statements in exit control loop will be executed at
least once.

While loop is an entry control loop and do-while is an exit control loop.

Figure 4.1(a) explains Entry control loop while figure 4.1(b) explains Exit control
loop.

Figure 4.1: Types of Loops

Prepared by – Mr. Viral H. Panchal 19


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

4.1. while Loop Statement


A while loop statement repeatedly executes a target statement as long as a given
condition is true.

Syntax –

The syntax of a while loop in C programming language is −

while(condition) {
statement(s);
}

Here, statement(s) may be a single statement or a block of statements.


The condition may be any expression, and true is any non-zero value. The loop
iterates while the condition is true.

When the condition becomes false, program control passes to the line
immediately following the loop.

Flow Diagram –

Prepared by – Mr. Viral H. Panchal 20


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Here, key point of the while loop is that the loop might not ever run. When the
condition is tested and the result is false, the loop body will be skipped and the
first statement after the while loop will be executed.

Example –

#include <stdio.h>
int main ()
{
int a = 10;
while (a < 20) {
printf("value of a: %d\n", a);
a++;
}
return 0;
}

Output –

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

Program –

Write a program to find sum of even numbers between 1 to 100. (while


loop)

/* C program to find sum of even numbers between 1 to 100 */

#include <stdio.h>
int main()
{
int i=1,sum=0;
while(i<=100) {
if(i%2==0) {
sum=sum+i;

Prepared by – Mr. Viral H. Panchal 21


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

}
i++;
}
printf("Sum of even numbers between 1 to 100 is: %d", sum);
return 0;
}

Output −

Sum of even numbers between 1 to 100 is: 2550

/* Write a program to print the sum of first n integer numbers using while
loop */
/* C program to find sum of first n integer numbers */

#include <stdio.h>
int main ()
{
int n, sum=0, i=1;
printf("Give integer number: ");
scanf("%d",&n);
while(i<=n) {
sum=sum+i;
i++;
}
printf("Sum of first %d numbers = %d\n",n,sum);
return 0;
}

Output −

Give integer number: 10


Sum of first 10 numbers = 55

Output −

Give integer number: 25


Sum of first 25 numbers = 325

Prepared by – Mr. Viral H. Panchal 22


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

4.2. for Loop Statement


A for loop is a repetition control structure that allows you to efficiently write a
loop that needs to be executed a specific number of times.

A for loop is useful when you know how many times a task is to be repeated.

Syntax –

The syntax of a for loop in C programming language is −

for (init; condition; increment) {


statement(s);
}

The flow of control in a for loop is as follows −

• 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 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 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.

Prepared by – Mr. Viral H. Panchal 23


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Flow Diagram –

Example –

#include <stdio.h>
int main ()
{
int a;
for (a = 10; a < 20; a = a + 1) {
printf("value of a: %d\n", a);
}
return 0;
}

Output –

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14

Prepared by – Mr. Viral H. Panchal 24


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Program –

Write a program to check whether the given number is prime or not.

/* C program to check whether the given number is prime or not */

#include <stdio.h>
int main()
{
int no, i;
printf("Enter a number: ");
scanf("%d",&no);
for(i=2; i<no; i++) {
if(no%i == 0) {
printf("%d is not a prime number", no);
break;
}
}
if(no==i)
printf("%d is a prime number", no);
return 0;
}

Output −

Enter a number: 9
9 is not a prime number

Output −

Enter a number: 89
89 is a prime number

Prepared by – Mr. Viral H. Panchal 25


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to find factorial of a given number.


/* C program to find factorial of a given number */

#include <stdio.h>
int main()
{
int n, i;
long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d",&n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist");
else {
for(i = 1; i <= n; i++) {
factorial = factorial * i;
}
printf("Factorial of %d = %ld", n, factorial);
}
return 0;
}

Output −

Enter a positive integer: 6


Factorial of 6 = 720

Output −

Enter a positive integer: -7


Error! Factorial of a negative number doesn't exist

4.3. do-while Loop Statement


Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop checks its condition at the bottom of the loop.

A do...while loop is similar to a while loop, except that a do...while loop is


guaranteed to execute at least one time.

Syntax –

The syntax of a do...while loop in C programming language is −

Prepared by – Mr. Viral H. Panchal 26


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

do {
statement(s);
}
while (condition);

Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested.

If the condition is true, the flow of control jumps back up to do, and the statement(s)
in the loop execute again. This process repeats until the given condition becomes
false.

Flow Diagram –

Example –

#include <stdio.h>
int main ()
{
int a = 10;
do {
printf("value of a: %d\n", a);
a = a + 1;
} while (a < 20);
return 0;
}

Prepared by – Mr. Viral H. Panchal 27


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Output –

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

Program –

Write a program to print numbers from 1 to n using do-while loop.


/* C program to print numbers from 1 to n using do-while loop */

#include <stdio.h>
int main()
{
int n, i = 1;
printf("Enter number: ");
scanf("%d",&n);
do {
printf("%d ",i);
i++;
} while (i <= n);
return 0;
}

Output −

Enter number: 8
1 2 3 4 5 6 7 8

Write a program to print the sum of first n integer numbers.


/* C program to print the sum of first n integer numbers */

#include <stdio.h>
int main()
{
int n,i,sum=0;
printf("Enter a number: ");

Prepared by – Mr. Viral H. Panchal 28


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

scanf("%d",&n);
do {
sum=sum+i;
i++;
} while(i<=n);
printf("Sum of first %d numbers = %d", n, sum);
return 0;
}

Output −

Enter a number: 7
Sum of first 7 numbers = 28

Write a program to check whether the given number is prime or not. (do-
while loop)

/* C program to check whether the given number is prime or not */

#include <stdio.h>
int main()
{
int no, i, count=0;
printf("Enter a number: ");
scanf("%d",&no);
i=2;
do {
// check for non-prime number
if(no % i == 0) {
count=1;
break;
}
i++;
} while(i<=no/2);
if (count==0)
printf("%d is a prime number", no);
else
printf("%d is not a prime number", no);
return 0;
}

Prepared by – Mr. Viral H. Panchal 29


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Output −

Enter a number: 9
9 is not a prime number

Output −

Enter a number: 7
7 is a prime number

4.4. Difference between while loop and do-while loop


while loop do-while loop
It is an entry-controlled loop. It is an exit-controlled loop.
Condition is given at the beginning of Condition is given at the end of
loop. loop.
Loop statements never execute if Loop statements execute once even
condition does not satisfy. if condition does not satisfy.
Syntax: Syntax:
while(condition) do
{ {
statement(s); statement(s);
} } while(condition);

4.5. Nested Loops


C programming allows to use one loop inside another loop.

Syntax –

The syntax for a nested for loop statement in C is as follows −

for (init; condition; increment) {


for (init; condition; increment) {
statement(s);
}
statement(s);
}

Prepared by – Mr. Viral H. Panchal 30


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

The syntax for a nested while loop statement in C is as follows −

while(condition) {
while(condition) {
statement(s);
}
statement(s);
}

The syntax for a nested do...while loop statement in C is as follows −

do {
statement(s);
do {
statement(s);
} while(condition);
} while(condition);

Program –

Write a program to print the following pattern.

*
**
***
****
*****

#include <stdio.h>
int main()
{
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i++) {
// print column
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}

Prepared by – Mr. Viral H. Panchal 31


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to print the following pattern.


*
* *
* * *
* * * *
* * * * *

#include <stdio.h>
int main()
{
int size = 5;
int space = 4;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < space ; j++) {
printf(" ");
}
// print stars
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
space--; /* decrement one space after one row*/
}
return 0;
}

Write a program to print the following pattern.


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Prepared by – Mr. Viral H. Panchal 32


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

#include <stdio.h>
int main()
{
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 1; i <= size; i++) {
// print column
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Write a program to print the following pattern.


A A A A A
B B B B
C C C
D D
E

#include <stdio.h>
int main()
{
int alphabet = 'A';
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--) {
printf("%c ",(char)(alphabet - 1 + i));
}
printf("\n");
}
return 0;
}

Prepared by – Mr. Viral H. Panchal 33


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

5. Solved Programs
Write a program to accept name and marks in 6 subjects, find out percentage
marks and print grade to students according to following condition.
• If student is failed in one subject, then grade is FAIL.
• If Percentage >= 75 then Grade = Distinction.
• If Percentage <75 and Percentage >=60, then grade = First Class.
• If Percentage <60 and Percentage >=50, then grade = Second Class.
• If Percentage <50 and Percentage >=40, then grade = Third Class.
#include<stdio.h>
int main()
{
char name[40];
int m1,m2,m3,m4,m5,m6,total;
float percent;
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);
return 0;
}

Output −

Enter name of the student


Ajay
Enter marks in 6 Subjects
65 66 89 77 76 76
Name: Ajay
Total: 449
Grade: First Class

Prepared by – Mr. Viral H. Panchal 34


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to print the individual digits of a given integer number using
while statement.

#include<stdio.h>
int main()
{
int n,i;
printf("Give Integer 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 */
}
return 0;
}

Output −

Give Integer Number: 14623


The digit is = 3
The digit is = 2
The digit is = 6
The digit is = 4
The digit is = 1

Write a program to reverse a given integer number .

#include<stdio.h>
int main()
{
int num,i;
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;
}
return 0;
}

Prepared by – Mr. Viral H. Panchal 35


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Output –

Give Integer Number


245
Reverse of 245 is = 542

Write a program to check the given number is Armstrong number or not.

/* Armstrong number is equal to summation of cubes of its individual


digits. */

#include<stdio.h>
int main()
{
int n,sum=0; /* sum initialized to 0 */
int i,num; /* separated digit stored in i */
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);
return 0;
}

Output –

Give Integer Number: 153


Given number 153 is Armstrong

Output –

Give Integer Number: 246


Given number 246 is not Armstrong

Prepared by – Mr. Viral H. Panchal 36


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to print first N Fibonacci numbers.

#include<stdio.h>
int main()
{
int num1,num2,num3,n;
int count;
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");
return 0;
}

Output –

How many Fibonacci Numbers?


7
The Fibonacci Numbers are
0 1 1 2 3 5 8

Output –

How many Fibonacci Numbers?


1
By definition first two numbers are 0 1

Prepared by – Mr. Viral H. Panchal 37


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to sum the individual digits of a given positive number.

#include<stdio.h>
int main()
{
int n,sum=0;
int digit;
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);
}
return 0;
}

Output –

Give Positive Integer Number: 2345


Given Number = 2345
Summation of Individual Digits = 14

Prepared by – Mr. Viral H. Panchal 38


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to reverse a given integer number and also check for palindrome.
/* Palindrome is a number which is same after reversal also */
#include<stdio.h>
int main()
{
int num,temp;
int i,rev=0;
printf("Give Integer Number: ");
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);
return 0;
}

Output –

Give Integer Number: 123


Reverse of 123 is = 321
Given Number 123 is not Palindrome

Output –

Give Integer Number: 232


Reverse of 232 is = 232
Given Number 232 is Palindrome

Write a program to print the following pattern.


*
**
***
****
*****

Prepared by – Mr. Viral H. Panchal 39


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

#include <stdio.h>
int main()
{
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 1; j < size - i; j++) {
printf(" ");
}
// print stars
for (int k = 0; k <= i; k++) {
printf("*");
}
printf("\n");
}
return 0;
}

Write a program to print the following pattern.


*
***
*****
*******
*********
#include <stdio.h>
int main()
{
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
printf(" ");
}
// print stars
for (int k = 0; k < 2 * i + 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}

Prepared by – Mr. Viral H. Panchal 40


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to print the following pattern.


*********
*******
*****
***
*
#include <stdio.h>
int main()
{
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < i; j++) {
printf(" ");
}
// print stars
for (int k = 0; k < 2 * (size - i) - 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}

Write a program to print the following pattern.


*
***
*****
#include <stdio.h>
int main()
{
int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i=i+2) {
// print column
for (int j = 0; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}

Prepared by – Mr. Viral H. Panchal 41


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to print the following pattern.


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

#include <stdio.h>
int main()
{
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}

Write a program to print the following pattern.


A
A B
A B C
A B C D
A B C D E

#include <stdio.h>
int main()
{
int alphabet = 'A';
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ",(char)(alphabet + j-1));
}
printf("\n");
}
return 0;
}

Prepared by – Mr. Viral H. Panchal 42


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to print the following pattern.


A
B B
C C C
D D D D
E E E E E

#include <stdio.h>
int main()
{
int alphabet = 'A';
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ",(char)(alphabet + i-1));
}
printf("\n");
}
return 0;
}

Write a program to print the following pattern.


1
2 3
4 5 6
7 8 9 10

#include <stdio.h>
int main()
{
int rows=4, number = 1;
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= i; ++j) {
printf("%d ", number);
number++;
}
printf("\n");
}
return 0;
}

Prepared by – Mr. Viral H. Panchal 43


Basics of Logic Development (IT3009) Unit - 2 (Part - 2): Decision Making and Branching

Write a program to print the following pattern.


1
3 5
7 9 11
#include <stdio.h>
int main()
{
int rows=3, number = 1;
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= i; ++j) {
printf("%d ", number);
number=number+2;
}
printf("\n");
}
return 0;
}

Write a program to print the following pattern.


1
A B
1 2 3
A B C D
1 2 3 4 5
#include<stdio.h>
int main()
{
int number=1,alpha=0,ch='A',loop,count;
for(loop=1; loop<=5; loop++) {
for(count=0; count<loop; count++) {
if(alpha==0)
printf("%d ", count+1);
else
printf("%c ", ch+count);
}
printf("\n");
if(alpha==0)
alpha=1;
else
alpha=0;
}
return 0;
}

Prepared by – Mr. Viral H. Panchal 44

You might also like