Module 2 ITC
Module 2 ITC
m
i. Conditional branch statements
ii. Unconditional branch statements
co
based on some conditions are called Conditional branch statements.
goto
break
Unconditional
Branch statements continue
m
return
2.3.1 if Statement
co
The „if‟ statement is the simplest form of decision control statement.
When a set of statements have to be executed when an expression (condition) is evaluated
to true or skipped when an expression (condition) is evaluated to false, then if statement
e.
is used.
It is used whenever there is only one choice (alternative). Hence it is also called as “One- way
decision or selection statement”.
dg
Syntax of if statement
if(test Expression)
{ test
Expression
ue
Statement 1; False
Statement 2; True
... Statement 1
Statement 2
…
…
vt
… …
…
Statement n; Statement n
}
Statement x; Statement x
The „if‟ structure may include one statement or „n‟ statements enclosed within curly brackets.
Working Principle
First the test expression is evaluated. If the test expression is true, then the statements of „if‟
block (statement 1 to n) are executed. Otherwise these statements will be skipped and the
execution will jump to statement x.
m
b) If multiple statements have to be executed when the expression is true, then all those
statements must be enclosed within braces.
c) No semicolon is required for an if-statement.
if(a<b); // statement indicates a NULL statement. It will „do nothing‟.
co
Example Programs for „if‟ statement
1. Write a C program to determine whether a person is eligible to vote or not.
#include<stdio.h>
void main()
{ e.
int age;
printf(“Enter the age:”);
scanf(“%d”,&age);
if(age>=18)
dg
printf(“\nThe person is eligible to vote”);
if(age<18)
printf(“\nThe person is not eligible to vote”);
}
ue
2. Write a C program to check whether the number is even or odd and print the
appropriate message.
#include<stdio.h>
void main()
{
int n;
vt
printf(“Enter a number:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“Number is even”);
if(n%2=0)
printf(“Number is odd”);
}
3. Write a C program to print the largest of two numbers.
#include<stdio.h>
void main()
{
int a,b;
printf(“Enter the two numbers:”);
scanf(“%d%d”,&a,&b);
if(a>b)
m
printf(“\n a is greater than b”);
if(b>a)
printf(“\n b is greater than a”);
}
co
2.3.2 if-else statement
If one set of activities have to be performed when an expression is evaluated to true and
another set of activities have to be performed when an expression is evaluated to false,
then if-else statement is used.e.
Syntax of if-else statement:
if(test Expression)
{
dg
Statement block 1; True False
Test
Expression
}
else
Statement block 1 Statement block 2
{
ue
Statement block 2;
}
Statement x
Statement x;
vt
The is-else statement is used when we must choose between two choices (alternatives).
Hence is also called as “Two-way Decision or Selection Statement”.
Working Principle
According to the if-else construct, first the „test expression‟ is evaluated.
If the expression is true then Statement block 1 is executed and Statement block 2 is skipped.
If the expression is false the Statement block 2 is executed and Statement block 1 is ignored.
Now in any case after the Statement block 1 or 2 gets executed the control will pass to
Statement x. It is executed in every case.
Example Programs for „if-else‟ statement
1. Write a C program to determine whether a person is eligible to vote or not.
#include<stdio.h>
void main()
{
int age;
printf(“Enter the age:”);
scanf(“%d”,&age);
m
if(age>=18)
printf(“\nThe person is eligible to vote”);
else
printf(“\nThe person is not eligible to vote”);
co
}
2. Write a C program to check whether the number is even or odd and print the
appropriate message.
#include<stdio.h>
void main() e.
{
int n,rem;
printf(“Enter a number:”);
scanf(“%d”,&n);
dg
if(n%2==0)
printf(“Number is even”);
else
printf(“Number is odd”);
}
ue
3. Write a C program to enter a character and then determine whether it is a vowel or not.
#include<stdio.h>
void main()
{
vt
char ch;
printf(“Enter any character:”);
scanf(“%c”,&ch);
if(ch==‟a‟||ch==‟e‟||ch==‟i‟||ch==‟o‟||ch==‟u‟||ch==‟A‟||ch==‟E‟||ch==‟I‟||ch==‟O‟||ch==‟U‟)
printf(“\n Character is a vowel”);
else
printf(“\n Character is a consonant”);
}
4. Write a C program to find whether a given year is leap year or not.
#include<stdio.h>
void main()
{
int year;
printf(“Enter any year:”);
scanf(“%d”,&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
m
printf(“%d is a leap year”,year);
else
printf(“%d is not a leap year”,year);
}
co
2.3.3 Nested if-else statement
An if-else statement within if statement or an if-else statement within else statement is
e.
called “nested if or if-else statement”.
When an action has to be performed based on many decisions involving various types of
expressions and variables, then this statement is used. So it is called as “Multi-way decision
statement”.
dg
Syntax for nested if-else statement:
if(test Expression1)
{
if(test Expression2) True Test False
{ Expression1
Statement block 1;
ue
}
else True False Statement
Test
{ Expression2 block 3
Statement block 2;
} Statement Statement
} block 1 block 2
vt
else
{
Statement block 3;
Statement x
}
Statement x;
Working Principle
If the test expression 1 is evaluated to true, then the test expression 2 is checked for true or
false. If the test Expression2 is evaluated to true, then the statements in block 1 are executed,
otherwise the statements in block 2 are executed. After executing the inner if-else the control
comes out and the statement x is executed.
If the test expression1 itself is false, then the statements in block 3 are executed. After
executing these statements, the statement x is executed.
m
Example Programs for „nested if-else‟ statement
1. Write a C program to find the largest of three numbers.
#include<stdio.h>
void main()
co
{
int a,b,c;
printf(“Enter the three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“Max=%d”,a);
else
e.
printf(“Max=%d”,c);
dg
}
else
{
if(b>c)
printf(“Max=%d”,b);
else
printf(“Max=%d”,c);
ue
}
}
The orderly nesting of if-else statement only in the else part is called „else-if-ladder‟.
When an action has to be selected based on the range of values, then this statement is used.
So it is called “Multi-way Decision or Selection Statement”.
Syntax :
if(test expression1)
{
Statement block 1; T Test F
Expression1
}
else if(test expression2)
{ T Test
Statement block 2; Statement Expression 2
} block 1
m
…. Statement
block 2
else
Statement
{ block n
co
Statement block n;
}
Statement x
Statement x;
Working Principle
e.
The „Expressions‟ are evaluated in order. If any expression is true then the statement associated
with it is executed and this terminates the whole chain and statement x is executed.
dg
The last „else‟ part is executed when all the test expression are false.
#include<stdio.h>
void main()
{
int x,y;
vt
m
{
int n;
printf(“Enter a number:”);
scanf(“%d”,&n);
co
if(n==0)
printf(“Number is zero”);
else if(n>0)
e.
printf(“Number is positive”);
else
printf(“Number is negative”);
dg
}
3. Write a C program to input 3 numbers and then find the largest of them using „&&‟
operator.
ue
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
vt
m
printf(“\n First Class with Distinction”);
else if(marks>=60&&marks<75)
printf(“\n First Class”);
else if(marks>=50&&marks<60)
co
printf(“\n Second Class”);
else if(marks>=40&&marks<50)
printf(“\n Third Class”);
else
printf(“\n Fail”); e.
}
m
case value-n: Statement block n;
break; True
Valuen Statement
default: Statements; block n
co
} False
Statement x;
Statement x
Working Principle
e.
First the expression within switch is evaluated.
The value of an expression is compared with all the case values.
The value of an expression within switch is compared with the case value-1. If it matches then
dg
the statements associated with that case are executed. If not then the case value-2 is compared,
if it matches then the associated statements are executed and so on.
The “default” statements are executed when no match is found.
A default is optional.
The „break‟ statement causes an exit from the switch. „break‟ indicates end of a particular case
ue
and causes the control to come out of the switch. [Significance of break within switch
statement]
#include<stdio.h>
void main()
{ char grade;
printf(“Enter the grade:”);
scanf(“%c”,&grade);
switch(grade)
{
case „O‟: printf(“Outstanding”);
break;
case „A‟: printf(“Excellent”);
break;
case „B‟: printf(“Good”);
break;
case „C‟: printf(“Fair”);
break;
m
case „F‟: printf(“Fail”);
break;
default : printf(“Invalid grade”);
}
co
}
2. Write a C program to enter a number from 1 to 7 and display the corresponding day of
the week using switch.
#include<stdio.h> e.
void main()
{ int day;
printf(“Enter any number:”);
scanf(“%d”,&day);
dg
switch(day)
{
case 1: printf(“Sunday”);
break;
case 2: printf(“Monday”);
ue
break;
case 3: printf(“Tuesday”);
break;
case 4: printf(“Wednseday”);
break;
vt
case 5: printf(“Thursday”);
break;
case 6: printf(“Friday”);
break;
case 7: printf(“Saturday”);
break;
default : printf(“invalid input”);
}
}
2.5 LOOPS
A set of statements may have to be repeatedly executed for a specified number of times or till a
condition is satisfied.
The statements that help us to execute a set of statements repeatedly for a specified number of
times or till a condition is satisfied are called as looping constructs or loop control statements.
These statements are also called as Repetitive or Iterative Statements.
m
while loop
co
do-while loop
O True
} O
P Statements
Statement x;
vt
Statement x
Working principle
The test expression is evaluated first, if it is TRUE then the set of statements within the body of the
loop are executed repeatedly as long as specified test expression is TRUE.
If the test expression is false, then the control comes out of the loop by skipping the execution of the
statements within the body of the loop, by transferring the control to the Statement x.
Example programs of „while‟ loop
1. Write a C program to print the natural numbers from 1 to 10.
#include<stdio.h>
void main()
{
int i=1;
while(i<=10)
{
printf(“%d\n”,i);
m
i++;
}
}
2. Write a C program to print the natural numbers from 1 to N.
#include<stdio.h>
co
void main()
{
int i=0,n;
printf(“Enter the value of n:”);
scanf(“%d”,&n);
while(i<=n)
{
e.
printf(“%d\n”,i);
i++;
}
dg
}
3. Write a C program to print the natural numbers from N to 1.
#include<stdio.h>
void main()
{
int n,i=n;
ue
}
}
4. Write a C program to calculate the sum of the first 10 natural numbers.
#include<stdio.h>
void main()
{
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
}
printf(“\nSum=%d”,sum);
}
5. Write a C program to print the characters from A to Z.
#include<stdio.h>
void main()
{
char ch=‟A‟;
m
while(ch<= „Z‟)
{
printf(“%c\t”,ch);
ch++;
}
co
}
6. Write a C program to find the factorial of a given number using while statement.
#include<stdio.h>
void main()
{
int fact=1,i=1,n; e.
printf(“Enter the value of n:\n”);
scanf(“%d”,&n);
while(i<=n)
{
dg
fact=fact*i;
i++;
}
printf(“Factorial of a given number is=%d”,fact);
}
7. Write a C program to read a number and determine whether it is palindrome or not.
ue
#include<stdio.h>
void main()
{ int digit,rev=0,num,n;
printf("Enter the number:\n");
scanf("%d",&num);
n=num;
vt
while(num!=0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
printf("The reversed number is=%d\n",rev);
if(n==rev)
printf(“ The number is a palindrome”);
else
printf(“The number is not a palindrome”);
}
8. Program to find GCD and LCM of 2 numbers.
#include<stdio.h>
void main()
{ int a,b,m,n,gcd,lcm,rem;
printf("Enter the value for m and n :\n");
scanf("%d %d", &m, &n);
m=a;
m
n=b;
while(n!=0) OUT PUT:
{ Enter the value for m and
rem=m%n; n: 6
m = n; 12
co
n = rem; The GCD of 6 12 numbers is= 6
} The LCM of 6 12 numbers is= 12
gcd = m;
lcm = ( a * b ) / gcd;
printf("The GCD of %d %d numbers is=%d\n", a, b, gcd );
}
e.
printf("The LCM of %d %d numbers is=%d\n", a, b, lcm );
{
Statements;
}
Statement x;
vt
False
for(initialization ;test expression; increment/decrement)
L
O
O True
P
Statements
Statement x
Working Principle
initialization: In this section loop variable is initialized, like i=0, n=0, i=1,n=1. (i and n are loop
variables).
test expression: The test expression may be a relational expression or logical expression or both,
which is evaluated to TRUE or FALSE. Depending on the value of the test expression, the body
of the loop is executed. If the test expression is TRUE, then the body of the loop is executed. This
process of execution of body of the loop is continued as long as the expression is TRUE. When
the test expression becomes FALSE, execution of the statements contained in the body of the loop
m
are skipped, thereby transferring the control to the Statement x, which immediately follows the for
loop.
increment/decrement: This section increments or decrements the loop variables after executing
the body of the loop.
co
Infinite Loop
A for loop without a test expression is an Infinite loop.
Ex: for(i=0; ;i++)
{
……… e.
} is an “infinite” loop.
Example programs of „for‟ loop
1. Write a C program to print the natural numbers from 1 to 10.
#include<stdio.h>
dg
void main()
{
int i;
for(i=1;i<=10;i++)
{
printf(“%d\n”,i);
ue
}
}
{
int i,n;
printf(“Enter the value of n:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“%d\n”,i);
}
}
3. Write a C program to compute the sum of the series 1+2+3+……+n.
#include<stdio.h>
void main()
{
int i,sum=0,n;
clrscr();
printf(“Enter the number of terms:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
m
sum=sum+i;
}
printf(“\nSum of the series=%d”,sum);
co
}
4. Write a C program to generate „n‟ Fibonacci numbers.
#include<stdio.h>
void main()
{
int n,f1,f2,f3,i;e.
f1=0;
f2=1;
printf(“Enter the value of n:”);
scanf(“%d”,&n);
dg
if(n==1)
printf(“%d”,f1);
else
{
printf(“%d\t%d\t”,f1,f2);
for(i=3;i<=n;i++)
ue
{
f3=f1+f2;
printf(“%d\t”,f3);
f1=f2;
f2=f3;
}
vt
}
}
for(ch=‟A‟;ch<=‟Z‟;ch++)
{
printf(“%c\t”,ch);
}
}
Output: A B C D E F Z
6. Write a C program to find the factorial of a given number using for statement.
#include<stdio.h>
void main()
m
{
int fact=1,i,n;
printf(“Enter an integer:\n”);
co
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“Factorial of a given number is=%d”,n,fact);
e.
}
Nested Loops
The loops that can be placed inside other loops.
dg
It will work with any loops such as while, do-while and for.
Nested loop is commonly used with the for loop because this is easiest to control.
The „inner for loop‟ can be used to control the number of times that a particular set of
statements will be executed.
ue
The „outer for loop‟ can be used to control the number of times that a whole loop is repeated.
Example Program: Write a C program to print the following output.
1
1 2
#include<stdio.h> 1 2 3
vt
void main( ) 1 2 3 4
{ 1 2 3 4
int i,j;
for(i=1;i<=5;i++)
{
printf(“\n”);
Outer for(j=1;j<=i;j++)
for loop { Inner
printf(“%d”,j); for loop
}
}
}
27
2.5.3 do-while Loop
do- while loop is used when a set of statements have to be repeatedly executed at least
once.
Since the test expression is evaluated to TRUE or FALSE at the end of do-while loop, the do-
while loop is called as exit-controlled loop.
The while and for loop test the expression at the top.
The do-while tests the bottom after making each passes through the loop body.
m
Syntax of do-while loop
Statements
do{
co
Statements;
Working Principle
dg
It is a post-test or bottom-testing loop and hence the statements contained within the body of
the loop are executed first and then the expression is evaluated to TRUE or FALSE. If it is
TRUE, then the statements contained within the body of the loop are executed once again and
the expression is evaluated. This is repeated until the expression is evaluated to FALSE.
ue
If the expression is FALSE, then the control comes out of the loop by skipping the execution
of the statements within the body of the loop, by transferring the control to the Statement x.
28
the body of loop are executed. If the expression statements within the body of the loop
is false, then the statements within the body of irrespective of the test expression. If the
the loop are not executed. expression is evaluated to true, then the
statements within the body of loop are
executed once again. If the expression is
false, then the statements within the body
of the loop are not executed.
5 In while loop, while statement does not ends In do-while loop, while statement ends
with semicolon (;). with semicolon (;).
6 Ex: Ex:
m
int i=0,sum=0; int i=0,sum=0;
while(i<=n) do{
{ sum=sum+i;
sum=sum+i; i=i+1;
i=i+1; }while(i<=n);
co
}
7 Flowchart: Flowchart:
Statements
e. False
while(test expression)
True True
while(test expression);
dg
Statements
False
Statement x
Statement x
ue
{
int i=1,n;
printf(“Enter the value of n:”);
scanf(“%d”,&n);
do
{
printf(“%d\n”,i);
i++;
}while(i<=n);
}
29
2. Write a C program to calculate the sum and average of first „n‟ numbers.
#include<stdio.h>
void main()
{
int n,i=0,sum=0;
float avg=0.0;
printf(“Enter the value of n:”);
scanf(“%d”,&n);
do
{
m
sum=sum+i;
i++;
}while(i<=n);
co
avg=sum/n;
printf(“The sum of numbers=%d”,sum);
printf(“The average of numbers=%f”,avg);
}
}
If „case 1‟ is selected by the programmer then the output will be 1st statement only. It
will directly come out of the switch.
If break is executed in a loop (for/while/do-while) then the control comes out of the loop
and the statement following the loop will be executed.
Syntax
1. while(…)
{
……
30
if(condition)
break;
……
} Transfers the control out of the while loop
……
2. do
{
……
if(condition)
break;
m
…… Transfers the control out of the do-while loop
}while(condition);
……
co
3. for(…)
{
……
if(condition)
break;
…… e.
} Transfers the control out of the for loop
……
Example programs of „break‟ statement
1. Write a C program to print the numbers (0 to 4) using break.
dg
#include<stdio.h>
void main()
{
int i=0;
while(i<=10)
{
ue
if(i==5)
break;
printf(“%d\n”,i);
i=i+1;
}
}
vt
31
……….
}
……….
2. do
{
……
if(condition)
continue;
…… Transfers the control to the expression of the do-while loop
}while(condition);
m
……
3. for(…)
{
……
co
if(condition) Transfers the control to the expression of the for loop
continue;
……
}
……
e.
Example programs of „continue‟ statement
1. Write a C program to display the output in the following form 1 3 4 5
#include<stdio.h>
dg
void main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==2)
ue
continue;
printf(“%d”,i);
}
}
Output: 1 3 4 5 [if i==2 then the continue statement is executed and the statements following
vt
32
Syntax
m
1. Write a C program to calculate the sum of all numbers entered by the user.
#include<stdio.h>
void main()
{
int n,i,sum=0;
co
printf(“Enter the number:\n”);
scanf(“%d”,&n);
sum=i=0;
top: sum=sum+n;
i=i+1;
if(i<=n) goto top;
e.
printf(“Sum of Series=%d”,sum);
}
dg
2.7 Finding Root of Quadratic Equations
Quadratic Equations ax2+bx+c=0.
√
−± −
Formula for calculating Roots of Quadratic equations. =
#include<stdio.h>
ue
#include<math.h>
void main()
{
float a,b,c,d,root1,root2;
printf("enter the co-efficients\n");
vt
scanf("%f%f%f",&a,&b,&c);
if(a==0&&b==0)
{
printf("invaled input");
}
else
{
d=b*b-4*a*c;
printf("discriminate=%f\n",d);
if(d==0)
33
{
root1=root2=-b/(2*a);
printf("roots are equal\n");
printf("root1=%f\n root2=%f\n",root1,root2);
}
else if(d>0)
{
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
m
printf("roots are distinct\n");
printf("root1=%f\n root2=%f\n",root1,root2);
}
else
co
{
printf("roots are imaginary\n");
root1=-b/(2*a);
root2=sqrt(fabs(d))/(2*a);
printf("root1=%f+i%f\n",root1,root2);
}
}
e.
printf("root2=%f-i%f\n",root1,root2);
}
dg
The first thing one needs to know about Pascal‟s triangle is that all the numbers outside the triangle
are “0”s. To build the triangle, start with a “1” at the top, the continue putting numbers below in a
triangular pattern so as to form a triangular array. So, each new number added below the top “1”
is just the sum of the two numbers above, except for the edge which are all “1”s.
vt
34
m
co
e.
dg
The sum of all the elements of a row is twice the sum of all the elements of its preceding row. For
example, sum of second row is 1+1= 2, and that of first is 1. Again, the sum of third row is 1+2+1
=4, and that of second row is 1+1 =2, and so on. This major property is utilized to write the code
in C program for Pascal‟s triangle.
ue
The sequence of the product of each element is related to the base of the natural logarithm, e.
The left and the right edges of Pascal‟s triangle are filled with “1”s only.
All the numbers outside the triangle are “0”s.
The diagonals next to the edge diagonals contain natural numbers (1, 2, 3, 4, ….) in order.
vt
The sum of the squares of the numbers of row “n” equals the middle number of row “2n”.
35
scanf("%d",&row);
for(i=0; i<=row; i++)
{
for(space=1; space<=row-i; space++)
printf (" ");
for(j=0; j<=i; j++)
{
m
if(j==0||i==0)
num=1;
else
co
count=(count*(i-j)/(j+1));
printf("%4d ",count);
}
printf("\n");
}
}
#include<stdio.h>
Void main()
vt
{
int i,n,r;
long int x,y,z,nCr;
printf(“enter the value of n and r\n”);
scanf(“%d %d”, &n, &r);
x = y = z = 1;
for(i = n; i >=1; i--)
36
{
x = x * i; Output:
} Enter the value of n and r:
6
for(i = n-r; i >=1; i--)
2
{ 6C2 = 15
y = y * i;
}
m
for(i = r; i >=1; i--)
{
z = z * i;
co
}
nCr = x / (y * z);
printf(“%d C %d= %d”, n ,r, nCr);
}
e.
dg
ue
vt
37