Control
Structures
Flow of program in C is
sequential (from first to last); unless any control flow statement changes the order.
The statement that is used to check the condition to pass the control to the
other part of the program is called control statement. The control statement
includes
1.
Sequential statement
2.
Selection
or branching (if, if ….else,
switch)
3.
Looping
or iterative or repetitive ( for, while, do while )
1.
Sequential Control
Structure:
Sequential
control structure means executing one instruction after another, in order the
source code occurs.
Flowchart:
Start
|
Instruction 1
|
Instruction 2
|
Instruction N
|
End
|
Syntax:
Start
Instruction 1
Instruction 2
…………….
……………..
Instruction N
End
Example: Read two numbers
and find sum of their square.
#include
<stdio.h>
#include
<conio.h>
main
( )
{
int
a, b, sum;
printf(“Enter
two number:”);
scanf(“%d
%d”, &a, &b);
sum
= a*a + b*b;
printf(“sum
of square of %d and % d is %d”, a,b,
sum);
getch(
);
}
2.
Decisions Control
Structure:
Selection means executing different sections of code depending on a specific
condition or the value of the variable. In C program, a decision causes a onetime
jump to a different part of the program depending on the value of the
expression.
Decision/Selection control structures are
I
) if statement :The if statement checks the condition. If
the condition is true then the statement after the condition will be executed
otherwise execution jump to the next statement.
Syntax:
if(condition)
{
statement(s);
}
Example: Program to read a number and find
even or odd number.
#include<stdio.h>
#include<conio.h>
int
main()
{
int n,r;
printf(“Enter any number:\n”);
scanf(“%d”,&n);
r=n%2;
if (r==0)
{
printf(“%d is an even number”, n);
}
if(r==1)
{
printf(“%d
is anodd number”,n);
}
getch(
);
}
ii) if ( )else statement:
If the condition is true then if() portion statement will be executed otherwise
else part of the statement will be executed.
Flowchart:
Input N
|
Start
|
print it is odd
|
End
|
print it is even
|
if r==0
|
calc r=n%2
|
Syntax:
if(condition)
{
block of statement(s) ;
}
else
{
block of statement(s);
}
Example /*
To check whether the given number is negative or non-negative */
#include<stdio.h>
#include<conio.h>
main ( )
{
int n;
printf(“Enter
any number”);
scanf(“%d”,&n);
if(n>=0)
{
printf(“\n
%d is non-negative number”,n);
}
else
{
printf(“\n
%d is negative number”,n);
}
getch
( );
}
Example: /*
Read a number and find even or odd number */
#include<stdio.h>
#include<conio.h>
main ( )
{
int n,r;
printf(“Enter any number”);
scanf (“%d”,&n);
r=n%2;
if (r==0)
{
printf(“%d is even number”,n);
}
else
{
printf(“%d
is odd number”,n);
}
getch(
);
}
Write a program to input
cost price (cp) and selling price (sp) and determine whether there is gain or
loss.
#include<stdio.h>
#include<conio.h>
int main()
{
float cp,sp;
clrscr( );
printf("\nEnter
Cost Price and Selling Price: ");
scanf("%f%f",&cp,&sp);
if(sp>cp)
{
printf("Rs.
%.2f is Profit",sp-cp); // .2f= 10.00
; .3f=20.000
}
else
{
printf("Rs.
%.2f is Loss",cp-sp);
}
getch( );
}
Write a program that checks whether the numbered
entered by the user is exactly divisible by 5 and not by 11.
#include<stdio.h>
#include<conio.h>
int
main( )
{
int n;
clrscr( );
printf("\nEnter any number: ");
scanf("%d",&n);
if(n%5==0 && n%11!=0)
printf("%d is exactly divisible by 5
but not by 11",n);
else
printf("condition is
incorrect");
getch( );
}
iii)
Nested if..else: If an entire
if..else construct is written under either the body of an if statement or the
body of an else statement, such type of construct is called nested if..else statement.
In nested
form, the condition for the inner if is evaluated only if the condition for the
outer if is satisfied, otherwise it is skipped and the else part of the outer
if is executed.
Syntax:
if(condition1)
{
if (condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if (condition 3)
{
statement-3;
}
else
{
statement-4;
}
}
Write a program to check greatest among user input
three number using nested if…else
#include<stdio.h>
#include<conio.h>
main
( )
{
int
n1, n2, n3;
printf("\n
Enter any three number:");
scanf("%d
%d %d", &n1,&n2,&n3);
if
(n1>n2)
{
if(n1>n3)
{
printf("\n
%d is the greatest number",n1);
}
else
{
printf("\n
%d is the greatest number",n3);
}
}
else
{
if(n2>n3)
{
printf("\n
%d is the greatest number",n2);
}
else
{
printf("\n
%d is the greatest number",n3);
}
}
getch
();
}
iv) If ( ) else if else ()
It is useful only when the
number of choices is larger than two then this type of if( ) else if else ()
control structure is used. It is a multiway decision based conditional structure.
As soon as one of the conditions is true, the statements or block of statements
following them is executed and no further condition is checked.
Syntax:
if (condition1)
{
block of
Statements1;
else
if(conditional 2)
{
block of statements2;
}
-----
------
else
{
block of statement (N+1);
}
Flow chart of if( ) else if else ( )
1.
The
marks obtained by student in 7 different subjects are entered through the
keyboard. The student gets a division as per
the following rules:
Percentage greater
or equal to 60
|
First Division
|
Percentage between
45 and 59
|
Second Division
|
Percentage between
35 and 44
|
Third Division
|
Percentage less
than 35
|
Fail
|
Marks less than 35
in a s subject will be declared as
|
Fail
|
#include<stdio.h>
#include<conio.h>
main ()
{
float eng,eco,com,tnt,mkt,nep,boom, total,per;
printf("Enter the marks in 7 subjects:");
scanf("%f%f%f%f%f%f%f",&eng,&eco,&com,&tnt,&mkt,&nep,&boom);
if(eng>=35&&eco>=35&&com>=35&&tnt>=35&&mkt>=35&&nep>=35&&boom>=35)
{
total=eng+eco+com+tnt+mkt+nep+boom;
per=total/7;
if(per>=60)
{
printf("First division %f",per);
}
else if (per>=45 &&per<=59)
{
printf("Second division %f",per);
}
else if (per>=35 && per<=44)
{
printf("Third
division %f",per);
}
}
else
{
printf("Your r fail",per);
}
getch();
}
. Write a
C program to find the commission amount on the basis of sales amount as per
following conditions:
Sales amount
(Rs) Commission
0-1000 5%
1001-2000 10%
>2000 12%
#include<stdio.h>
#include<conio.h>
int main()
{
float s,com;
clrscr();
printf("\nEnter
sales amount: ");
scanf("%f",&s);
if(s>=0
&& s<=1000)
com=0.05*s;
else
if(s>1000 && s<=2000)
com=0.1*s;
else
com=0.12*s;
printf("Your
Commission is Rs. %.2f",com);
getch();
}
Switch statement ( ):
A switch statement allows the user to choose a
statement (or a group of statements) among several alternative. The switch
statement is useful when a variable is to be compared with different constants,
an in case it is equal to a constant, a set of statement are to be executed.
The constants in the case statement may be either char or int
type only.
Syntax:
switch(expression1)
{
case 1:
statements;
break;
case 2:
statements;
break;
case 3:
statements;
break;
case N:
statements N
break;
default:
default
statement;
}
Write a program to
display the name of day on the basis of entered number 1 to 7 for example, 1
for Sunday
#include <stdio.h>
#include<conio.h>
main ()
{
int a;
printf ("Enter any number\n");
scanf("%d", &a);
switch(a)
///switch (expression)
{
case 1:
printf ("It is Sunday");
break;
case 2:
printf("It is Monday");
break;
case 3:
printf("It is Tuesday");
break;
case 4:
printf("It is Wednesday");
break;
case 5:
printf("It is
Thusday");
break;
case 6:
printf("It is Friday");
break;
case 7:
printf("It is Saturday");
break;
default:
printf("Wrong input");
}
getch();
}
Q. Write a program to display the name of month on the basis of entered number 1 to 12 for
example, 1 for January ….12 for December and other "wrong input".
Looping:
Loop may be defined as block of statements which are repeatedly executed for a
certain number of times or until a particular condition is satisfied. A loop is
executed repeatedly till the expression is true. When the expression becomes
false, the loop is terminated and the control passes on to the statement
following the loop.
A
loop consist of two segments
1. Control
statement:-control statement in loop decides whether the body is to be executed
or not.
2. Body
of the loop
Types
of loop
1) for
2) while
loop
3) do-while
I.
for loop: The for loop is an entry control
loop because it checks the condition at entry point. Hence, it does not execute
even a single statement if the termination condition is set false. for loop has composite structure. There are
three section separated by semicolon (: ).
Syntax:
for(initialization;
test condition; increment/decrement)
{
statement
or body of loop
}
Example: Write a C program to find the factorial of a
given positive number
#include<stdio.h>
#include<conio.h>
int main()
{
int n,f=1,i;
//clrscr();
printf("\n
Enter positive number: ");
scanf("%d",&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n Factorial of %d is %d",n,f);
}
else
{
printf("\n It is not positive number");
}
getch();
}
Output: Enter
positive number: 3
Factorail of 3 is 6
Write a C program to input a number and display its
multiplication table.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
// clrscr();
printf("\n
Enter number:");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("\n%d X %d = %d",n,i,n*i);
}
getch();
}
Output: Enter the number:2
2 X 1= 2
……..
2 X 10
=20
Write a
C program to read a positive number integer less than 20 and display its multiplication
table.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("\n
Enter positive number less than 20:");
scanf("%d",&n);
if(n>0
&& n<20)
{
for(i=1;i<=10;i++)
printf("\n%d X %d = %d",n,i,n*i);
}
else
printf("\n
Invalid number");
getch();
}
Write a C program to print
10 positive integer and their factorial.
#include
<stdio.h>
#include<conio.h>
int
main()
{
int i;
int f= 1;
for(i = 1; i <= 10; i = i + 1)
{
f
= f * i;
printf("%d %d\n",i,f);
}
getch();
}
Write a C program to input a number and display its multiplication
table
#include
<stdio.h>
#include<conio.h>
int
main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; i++)
{
printf("%d * %d = %d \n", n,
i, n*i);
}
getch();
}
Write a program to display the multiplication
table of nth terms of a given number [2070]
#include
<stdio.h>
#include<conio.h>
main()
{
int j,i,n;
printf("Input upto the table number
starting from 1 : ");
scanf("%d",&n);
printf("Multiplication table from 1 to
%d \n",n);
for(i=1;i<=10;i++)
{
for(j=1;j<=n;j++)
{
if (j<=n-1)
printf("%dx%d = %d,
",j,i,i*j);
else
printf("%dx%d = %d",j,i,i*j);
}
printf("\n");
}
getch();
}
Calculate and display the multiplication table using nested loop
#include
<stdio.h>
#include<conio.h>
main()
{
int j,i,n;
printf("Input upto the table number
starting from 1 : ");
scanf("%d",&n);
printf("Multiplication table from 1 to
%d \n",n);
for(i=1;i<=10;i++)
{
for(j=1;j<=n;j++)
{
if (j<=n-1)
printf("%dx%d = %d,
",j,i,i*j);
else
printf("%dx%d = %d",j,i,i*j);
}
printf("\n");
}
getch();
}
Write a C program to print 10 terms of
any series using FOR loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a=5;
clrscr();
printf("\n Enter how many
numbers?");
scanf("%d",&n);
for(i=0;i<n;i++) //for(i=0;i<10;i++)
{
printf("%d\t",a);
a=a+5;
}
getch();
}
Output: Enter how many number : 4
5 10 15 20
Write a C program to print 10 terms of the following
series using FOR loop, 1, 5, 9, 13
#include<stdio.h>
#include<conio.h>
int
main()
{
int i,a=1;
//clrscr();
for(i=0;i<10;i++)
{
a=a+4;
printf("%d\t",a);
}
getch();
}
Nested
Loop:- In many cases we may use loop statement inside another looping
statement. This type of looping is called nested loop. In nested loop the inner
loop is executed first and then outerloop
Syntax:
for ( initialization; condition;
increment/decrement )
{
for ( initialization; condition; increment/decrement )
{
statement(s);
}
statement(s);
}
Write
a program that display the following patterns
1
12
123
1234
12345
#inlcude<stdio.h>
#include<conio.h>
main ()
{
int i,j;
for (i=1,1<=5; i++)
{
for(j=1;j<=i; j++)
{
printf("%",j);
}
printf("\n")
}
getch();
}
2.while
( ):- The while loop is also a entry control loop.The while loop must
initialize variable before the loop begins. The termination condition consists
inside while parantheses. The increment/decrement performed inside the body of
the loop i.e within brace
Syntax
:
initialization
while (test condition )
{
statement(s);
increment/decrement
. Write a C program to read a
four digit number and display it in reverse order.
#include<stdio.h>
#include<conio.h>
int main()
{
int c=0,n,r,s=0;
clrscr();
printf("\n Enter any 4 digit number:
");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
c=c+1;
}
if(c<=4)
printf("\n The reversed number is %d",s);
else
printf("\n It is not a 4 digit
number");
getch();
}
3. do-while:
This loop is an exit control loop. This loop runs at least once even through
the test condition is set to false. This loop tests the condition at exit point
hence it is called exit control loop.
Syntax:
initialization
do
{
Statement(s);
increment/decrement
}
while (test condition);
Difference between break and continue statement with
example.
Break
|
Continue
|
Break statement terminates
loop or block in which it is used
|
Continue
statement will force to skip the rest of the code in loop and take next
iteration.
|
It uses keyword
beak
|
It used keyword
continue
|
#include<stdio.h>
#include<conio.h>
int main ()
{
int i;
for
(i=1;i<=5;i++)
{
if(i==2)
break;
printf("%d", i);
}
getch();
}
|
#include<stdio.h>
#include<conio.h>
int main ()
{
int i;
for (i=1;i<=5;i++)
{
if(i==2)
continue;
printf("%d", i);
}
getch();
}
|
Write a program to input an integer number and check whether it is prime or
not.
#include<stdio.h>
#include<conio.h>
int
main()
{
int num,i;
//clrscr();
printf("\nEnter any integer number:
");
scanf("%d",&num);
for(i=2;i<num;i++)
{
if(num%i==0)
{
printf("\n not prime number");
break;
}
}
if(i==num)
{
printf("\n prime number");
}
getch();
}
Write a C program to display the sum of
‘n’ terms of even numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
int
i,n,s=0,a=2;
clrscr();
printf("\nEnter how many numbers? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
s=a+s;
a=a+2;
}
printf("\n
Sum of %d terms of even numbers is %d",n,s);
getch();
}
Output:
Enter how many number? 2
Sum of 2 terms of even number is 6.
Differences
between while and do-while.
While
|
do while
|
||||||
While loop is
entry-controlled loop i.e. test condition is evaluated first and body of loop
is executed only if this test is true.
|
Do whileloop is
exit-controlled loop i.e. the body of the loop is executed first without
checking condition and at the end of body of loop, the condition for
repeation of next time
|
||||||
The body of the
loop may not be executed at all if the condition is not satisfied at the very
first attempt.
|
The body of loop
is always executed at least once.
|
||||||
Syntax:
while(test
condition)
{
body of loop;
}
|
Syntax:
do
{
body of loop;
}while (test
condition);
|
||||||
#include<stdio.h>
#include<conio.h>
int main ( )
{
int i=1;
while (i<=10)
{
printf("\n Example of
while");
i++;
}
getch( );
}
|
#include<stdio.h>
#include<conio.h>
int main ( )
{
int i=1;
do
{
printf("\n
Example of do-while");
i++;
}while(i<=10);
getch( );
}
|
||||||
|
|
No comments:
Post a Comment