0% found this document useful (0 votes)
1K views32 pages

C Programming Control Statements Notes

C Programming Control Statements Notes Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.

Uploaded by

shashidharsm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
1K views32 pages

C Programming Control Statements Notes

C Programming Control Statements Notes Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.

Uploaded by

shashidharsm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 32

Computer Programming in C (Notes) by Dr SM Shashidhar

Control statements
Control statements are used to execute/transfer the control from one part of
the program to another depending on a condition.

There are two types of control/conditional statements used in C language.

if-else statement
switch statement

if-else statement

if-else statement is used to execute a statement block or a single statement


depending on the value of a condition.
It has the following form.

if ( condition )
{ Example

------------ if (bpay >= 3000) /* bpay - basic pay*/


<true block> {
------------ da = 0.32 * bpay;
/* da - dearness allowance*/
} hra = 0.15 * bpay;
else /* hra - house rent allowance*/
{ cca = 325;
/* cca - city compensatory allowance*/
------------ }
<false block> else
------------ {

} da = 0.72 * bpay;
hra = 0.12 * bpay ;
cca = 250;
where <condition> is a
logical expression which will }
have the value true or false.

When this statement is executed, the computer evaluates/checks the value of the
condition.
If the value of the condition is true, then the statements in <true block> will be
executed.
Otherwise the statements in the <false block> will be executed.
The following points in relation to the use of if-else statement are to be taken
care of.

Control Statements Page 2


Computer Programming in C (Notes) by Dr SM Shashidhar

1. <false block> is optional; i.e., it can be omitted when there is no


statement available in that block.
2. The block marker {} may be omitted when there is only one
statement in a block.
3. Nested if-else Statement
4. An if statement may have another if statement in the <true
block> and <false block>.
This compound statement is called nested if statement.
Any number of if statements can be nested.
5. It has the following form.

if(condition 1)
if(condition 2)
{
Example
<true block 1>
}
else
if(a > b)
{
<false block 1>
}
if(a > c)
else
big = a;
if(condition 3)
else
{
big = c;
<true block 2>
}
else
else
{
<false block 2>
if(b > c)
}
big = b;
It is to be noted that any one of the
else
statement blocks in the nested
big = c;
if statement will be executed
depending on the value of the
conditions.

Control Statements Page 3


Computer Programming in C (Notes) by Dr SM Shashidhar

switch Statement

switch statement is used to execute a block of statements depending on the value of


a variable or an expression.
It has the following form.

Example
switch ( <expression>)
switch (sex)
{
{
case <label 1>: {
case ‗m‘:
<statement block 1>
{
break;
male++ ;
}
total++;
case <label 2>: {
break;
<statement block 1>
}
break ;
case ‗f‘:
}
{
case <label n>: {
female++;
<statement block n>
total++;
break;
break;
}
}
default:
default:
{
printf(―\n Error in sex
<default statement block>
code‖);
break;
break;
}
}
}
When this statement is
where <expression> refers any int or char
executed, the computer
expression or variable.
checks the values of the
<label 1>, <label 2>...<label n> are values
expression or variable. If this
which will match with the value of the
value matches with any one of
expression.
the labels given in
break is a statement which will transfer the
case <value>, then that
control to the end of switch statement.
statement block will be
executed.

Note:

The braces{} can be omitted when there is only one statement available in the
statement block.
The break statement is used to transfer the control to the end of switch statement.
The default block is executed when none of the case labels matches with the
value of the expression/variable.
The default block is optional; i.e., this may be omitted while writing a program.

Control Statements Page 4


Computer Programming in C (Notes) by Dr SM Shashidhar

It should also be noted that a switch statement is compact and can be used to
replace a nested if statement.

Loop Control Statements

Loop control structures are used to execute and repeat a block of statements
depending on the value of a condition. There are three types of loop control
structures/statements in C language.

for statement or for loop


while statement or while loop
do-while statement or do-while loop

for Statement or for Loop

A for loop is used to execute and repeat a block of statements depending on a


condition.
It has the following form.

for(<initial value >; <condition>;


Example
<increment>)
{
for(i = 1; i <= 10; i++)
{
––––––––––––––––
<statement block>
s=s+i;
––––––––––––––––
p=p*i;
}
}
where <initial value> is the
When this statement is
assignment expression which
executed, the computer
initializes the value of a variable.
assigns initial value to the
<condition> is a relational or logical
variable and the condition
expression which will have the value
is evaluated. If the value of
true or false.
the condition is true, the
<increment> is the increment value of
statement block will be
the variable which will be added
executed.
every time.

Control Statements Page 5


Computer Programming in C (Notes) by Dr SM Shashidhar

NESTED for STATEMENT or NESTED for LOOP

The statement block of a for loop lies completely inside the block of another for
loop.
This is referred as nested for loop or nested for statement. Any number of for
statements can be nested.

Consider the following example (two loops).

for(i=1; i<=3; i++) ——————————————


{ |
------------
for(j=1; j<=5; j++) |
{——————————————
------------ | Outer
Loop
<statement block> Inner
------------ Loop |
}——————————————

------------ |
}——————————————————————

When this statement is executed, the computer assigns i = 1 and the inner loop is
executed 5 times for j values from 1 to 5 in steps of 1.
Now i is incremented by 1 (i.e. i = 2) and the inner loop is executed 5 times for j
values from 1 to 5. Then i becomes 3 and the inner loop is executed 5 times.
Note that the statement block is executed and repeated 15 times (3 × 5 = 15).

Control Statements Page 6


Computer Programming in C (Notes) by Dr SM Shashidhar

while STATEMENT or while LOOP

A while loop is used to execute and repeat a block of statements depending on a


condition.

It has the following form.

Example
i = 1;
while(<condition>)
while(i <= 10)
{
{
s = s + i;
––––––––––––––––
p = p * i;
<statement block>
i++;
––––––––––––––––
}
When this statement is executed, the
}
computer evaluates the value of the
where <condition> is a relational
condition. If the value is true, the
or logical expression which will
statement block is executed and is
have the value true or false.
repeated until the value of the
condition is false.

_________________________________________________________________

There must be a statement written inside the statement block to change the
value of the condition, otherwise the loop is infinity.
_________________________________________________________________

Control Statements Page 7


Computer Programming in C (Notes) by Dr SM Shashidhar

do-while STATEMENT or do-while LOOP

A do-while statement is also used to execute and repeat a block of statements


depending on a condition.

It has the following form.

Example
i=1;
do
do
{
{
s = s + i;
------------
p = p * i;
<statement block>
i++;
------------
}
}
while(i <= 10);
while (<condition>);
When this statement is executed, the
where <condition> is
computer will execute the statement block
a relational or logical
irrespective of the value of the condition.
expression which will
At the end of statement block, the
have the value true or
condition is evaluated. If the value of the
false.
condition is true the statement block is
executed again and is repeated until the
condition is false.

__________________________________________________________________

Note that the statement block is executed at least once for any value (true or
false) of the condition. It is necessary to have a statement inside the block to
change the value of the condition, otherwise the loop is infinity.

Control Statements Page 8


Computer Programming in C (Notes) by Dr SM Shashidhar

goto Statement

The goto statement is an unconditional transfer of control statement. It is used to


transfer the control from one part of the program to another . The place to which
the control is transferred is identified by a statement label.

It has the following form.

goto label;

where label is the statement label which is available anywhere in the program.

Example
------------;
goto display;
------------;
------------;
display:
------------;

When this statement is executed, the control is transferred to the statement


label display which is followed by a colon. Note that the statements
between goto and statement label display will be skipped because of the transfer.

break Statement

The break statement is used to transfer the control to the end of a statement block
in a loop. It is an unavoidable statement to transfer the control to the end of a
switch statement after executing any one statement block.

It has the following form.

break;

continue Statement

The continue statement is used to transfer the control to the beginning of a


statement block in a loop.

It has the following form.

continue;

Note that this statement is not commonly used.


Consider the following example.

Control Statements Page 9


Computer Programming in C (Notes) by Dr SM Shashidhar

for (i = 1; i <= 80; i++) <------------------------------------


{ ------------ |
ch = getche(); |
if(ch ==‘C‘ ch ==‘c‘) (Control is transfered
{ to the begining of the block)

printf("\n C for continue is pressed"); |


continue; ----------------------------------------------
}
------------
}

When this statement is executed, the control is transferred to the beginning of the
loop such that the loop is repeated 80 times irrespective of the key pressed.
Note that the message ―C for continue is pressed‖ is displayed whenever the key
C is pressed.

exit( ) Statement

The exit() function is used to transfer the control to the end of a program (i.e. to
terminate the program execution). It uses one argument in () and the value is zero
for normal termination or non-zero for abnormal termination.

Consider the following example.

if (n < 0)
{
printf("\n Factorial is not available for negative numbers");
exit(0);
}
------------;

Note that the program execution is terminated when the value of the variable
n is negative.
The compiler directive #include <stdlib> is used when this function is
used in a program.

Control Statements Page 10


Computer Programming in C (Notes) by Dr SM Shashidhar

Consider a variable big to store the biggest number. A nested if


statement is written using the compound conditions to get the
biggest of given three numbers.

/* program to find biggest of three numbers using nested if */


#include <stdio.h>
#include <conio.h>
main()
{

int a,b,c,big;
clrscr();
printf("\n Enter three numbers : ");
scanf("%d %d %d",&a,&b,&c);
if(a > b)
if(a > c)
big = a;
else
big = c;
else
if(b > c)
big = b;
else
big = c;
printf("\n Biggest number is %d",big);
getch();

Control Statements Page 11


Computer Programming in C (Notes) by Dr SM Shashidhar

To find the value of y using

y(x,n) = 1 + x when n = 1
1 + x/n when n = 2
1 + xˆn when n = 3
1 + nx when n > 3 or n < 1

Solution

The given conditions are considered and a nested if statement


may be written to use the required relation to find the value of y.

/* Program to find value of Y */


#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{

int n;
float x,y;
clrscr();
printf("\n Enter value to x and n : ");
scanf("%f %d",&x,&n);
if(n == 1)
y = 1 + x;
else
if(n == 2)
y = 1 + x/n;
else
if(n == 3)
y = 1 + pow(x,n);
else
y = 1 + n*x;
printf("\n Value of y(x,n) = %6.2f",y);
getch();

Control Statements Page 12


Computer Programming in C (Notes) by Dr SM Shashidhar

To calculate the commission for a sales representative as per the


sales amount given below.
if sales < = Rs. 500, commission is 5%
if sales > 500 but < = 2000, commission is Rs.35 plus 10% above
Rs.500
if sales > 2000 but <= 5000, commission is Rs.185 plus 12%
above Rs.2000
if sales > 5000, commission is 12.5%

Solution

A nested if statement can be used here.

/* Program to find Commission on Sales Amount */


#include <stdio.h>
#include <conio.h>
main()
{

int sales;
float comm;
clrscr();
printf("\n Sales amount ? : ");
scanf("%d",&sales);
if(sales <= 500)
comm = 0.05*sales;
else
if(sales <= 2000)
comm = 35 + 0.10*(sales - 500);
else
if(sales <= 5000)
comm = 185 + 0.12*(sales - 2000);
else
comm = 0.125*sales;
printf("\n Commission Amount Rs.%0.2f",comm);
getch();

Control Statements Page 13


Computer Programming in C (Notes) by Dr SM Shashidhar

To find the roots of a quadratic equation axˆ2 + bx + c = 0 for all


possible combinations of a, b and c.

Solution

A quadratic equation will have two roots which are obtained


using the following expression.
where bˆ2 – 4ac is called discriminant.
Note that when bˆ2 – 4ac > 0, the roots are real and unequal
bˆ2 – 4ac = 0, the roots are real and equal, i.e. x = -b / 2a
bˆ2 – 4ac < 0, the roots are imaginary, i.e. x = -b / 2a ± ( (sqrt ( b
ˆ 2 - 4ac ) / 2a) * i )
Also note that the value of the coefficient a 0.

/* Program to find roots of a Quadratic Equation */


#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{

float a,b,c,d,x1,x2,x;
clrscr();
printf("\n Enter coefficients a,b and c : ");
scanf("%f %f %f",&a,&b,&c);
d = b*b - 4*a*c;
if(d > 0)
{
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
printf("\n Roots are real and unequal");
printf("\n %6.2f %6.2f",x1,x2);
}
else
if(d == 0)
{
x = -b/(2*a);
printf("\n Roots are real and equal");
printf("\n %6.2f",x);
}
else
printf("\n No Real roots,roots are complex");
getch();

Control Statements Page 14


Computer Programming in C (Notes) by Dr SM Shashidhar

To print the grade for the marks obtained.

Average Marks Grade


80 to 100 Honours
60 to 79 First division
50 to 59 Second division
0 to 49 Fail

Solution

A nested if statement can be used here.

/* Program to Print Grade Obtained */


#include <stdio.h>
#include <conio.h>
main()
{

short int avg_marks;


clrscr();
printf("\n Average Marks ? ");
scanf("%d",&avg_marks);
if(avg_marks >= 80 && avg_marks <= 100)
printf("\n Honours");
else
if(avg_marks >= 60 && avg_marks <= 79)
printf("\n First Division");
else
if(avg_marks >= 50 && avg_marks <= 59)
printf("\n Second Division");
else
if(avg_marks >= 49 && avg_marks <= 0)
printf("\n Fail");
getch();

Control Statements Page 15


Computer Programming in C (Notes) by Dr SM Shashidhar

An electric distribution company charges its domestic consumers as follows.

Consumption in units Rate of charge


0—200 Re. 0.50 per unit
201—400 Rs.100 plus Re. 0.65 per unit excess of 200
401—600 Rs.230 plus Re.0.80 per unit excess of 400
Above 600 Rs.425 plus Rs.1.25 per unit excess of 600

Solution

A nested if statement is used here to calculate and print the amount to be paid by
the consumer.

/* program to calculate and print electric bill */


#include <stdio.h>
#include <conio.h>
main()
{

unsigned int units;


float amt;
clrscr();
printf("\n Enter consumed units : ");
scanf("%d",&units);
if(units <= 200)
amt = 0.5 *units;
else
if(units <= 400)
amt = 100 + 0.65*(units - 200);
else
if(units <= 600)
amt = 230 + 0.8*(units - 400);
else
amt = 425 + 1.25*(units - 600);
printf("\n Amount to be paid Rs.%0.2f",amt);
getch();

Control Statements Page 16


Computer Programming in C (Notes) by Dr SM Shashidhar

To find the grade of steel samples considering the following


conditions.
(i) Tensile strength >= 700 kgf/cm2
(ii) Rockwell hardness >= 200
(iii) Carbon content <= 6%
When conditions (i), (ii) and (iii) are satisfied, grade is ‗A‘
condition (i) and (ii) are satisfied, grade is ‗B‘
condition (i) and (iii) are satisfied, grade is ‗C‘
condition (ii) and (iii) are satisfied, grade is‘D‘
condition (i) or (ii) or (iii) is satisfied, grade is ‗E‘;
otherwise grade is ‗F‘.

Solution

A nested if statement is used to find the grade of steel samples.

Control Statements Page 17


Computer Programming in C (Notes) by Dr SM Shashidhar

/* Program to find grade of Steel samples */


#include <stdio.h>
#include <conio.h>
main()
{

int ts,rh,cc;
clrscr();
printf(―\n Enter tensile strength : ―);
scanf(―%d‖,&ts);
printf(―\n Enter rockwell hardness : ―);
scanf(―%d‖,&rh);
printf(―\n Enter carbon content : ―);
scanf(―%d‖,&cc);
if(ts >= 700)
if(rh >= 200)
if(cc <= 6)
printf(―\n Grade is A‖);
else
printf(―\n Grade is B‖);
else
if(cc <= 6)
printf(―\n Grade is C‖);
else
printf(―\n Grade is E‖);
else
if(rh >= 200)
if(cc <= 6)
printf(―\n Grade is D‖);
else
printf(―\n Grade is E‖);
else
if(cc <= 6)
printf(―\n Grade is E‖);
else
printf(―\n Grade is F‖);
getch();

Control Statements Page 18


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to print natural numbers from 1 to n.

Solution

A for loop can be used to generate and print numbers from 1 to


n in steps of 1.

/* program to natural numbers upto N */


#include <stdio.h>
#include <conio.h>
main()
{

int n,i;
clrscr();
printf(―\n Enter value to n : ―);
scanf(―%d‖,&n);
/* loop to generate and print natural numbers */
for(i = 1; i <= n; i++)
printf(―%8d‖,i);
getch();

Control Statements Page 19


Computer Programming in C (Notes) by Dr SM Shashidhar

The formula is to be evaluated for x which


varies from 1.0 to 3.0 in steps of 0.2. Write a C program to perform
this and print a table for various values of x with proper headings.

/* program to find value of Y */


#include <stdio.h>
#include <math.h>
#include <conio.h>
main()
{

float x,y;
clrscr();
printf(―\n-----------------‖);
printf(―\n X Y ―);
printf(―\n-----------------‖);
/* loop to generate and print X and Y */
for(x = 1.0; x <= 3.0; x = x + 0.2)

y = 1.36*sqrt(1+x+x*x*x)+pow(x,1.0/4)+exp(x);
printf(―\n %6.2f %6.2f ― ,x,y);

printf(―\n-----------------‖);
getch();

Control Statements Page 20


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to find factorial of a given integer k.

Solution

We know that k ! = 1 × 2 × 3 × …× k. The program is written to


generate numbers from 1 to k in steps of 1 and multiply them to
get the factorial of k.

/* program to find factorial of a positive integer */


#include <stdio.h>
#include <conio.h>
main()
{ int k,kfact,i;
clrscr();
printf(―\n Enter the number : ―);
scanf(―%d‖,&k);
kfact = 1;
/* loop to generate numbers from 1 to n */
for(i = 1; i <= k; i++)
kfact = kfact*i;
printf(―\n %d factorial is %d‖,k,kfact);
getch();
}

Control Statements Page 21


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to sum the following series.


S = 1 + (1+2) + (1+2+3) +…+(1+2+3+…+N)

Solution

A nested for loop can be written to find the sum of the series. The
inner loop is used to find the term values 1, (1+2), (1+2+3) and so
on. These are added in outer loop to get the sum of the series.

/* program to find sum of series */


#include <stdio.h>
#include <conio.h>
main()
{ int i,j,n,s,term;
clrscr();
printf(―\n Enter value to N : ―);
scanf(―%d‖,&n);
s = 0;
/* outer loop to find sum of series S */
for(i = 1; i <= n; i++)
{
/* inner loop to find the terms */
term = 0;
for(j = 1; j <= i; j++)
{
term = term + j;
}
s = s + term;
}
printf(―\n Sum of the series S = %d‖,s);
getch();
}

Control Statements Page 22


Computer Programming in C (Notes) by Dr SM Shashidhar

z is to be computed as a function of x and y according to z =


3x2 + 2y3 – 25.5. Compute the values of z as x varies from –1.5 to
1.5 in increments of 0.5 and y varies from 0 to 3 in steps of 1.
Write a C program to compute z for all pairs of (x,y) .

Solution

A nested for loop can be written to generate various


combination of x and y and to find z value for every combination
of x and y.

/* program to find value of Z */


#include <stdio.h>
#include <conio.h>
main()
{ float x,y,z;
clrscr();
/* loops to generate values of x and y to find z */
for(x = -1.5; x <= 1.5; x = x + 0.5)
for(y = 0; y <= 3.0; y = y + 1.0)
{
z = 3*x*x + 2*y*y*y - 25.5;
printf("\n\n Value of y(%0.2f,%0.2f) = %6.2f",x,y,z);
}
getch();
}

Control Statements Page 23


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to find the sum of all odd integers between 1


and n.

Solution

A while loop can be used to generate integers from 1 to n in steps


of 2 and add them to get the sum of all odd integers. S = 1 + 3 + 5
+ 7 +…+ N

/* program to find sum of odd integer between 1 and N */


#include <stdio.h>
#include <conio.h>
main()
{ int s,i,n;
clrscr();
printf(―\n Enter value to N : ―);
scanf(―%d‖,&n);
s = 0;
i = 1;
while(i <= n)
{
s = s + i;
i = i + 2;
}
printf(―\n Sum of odd integers = %d‖,s);
getch();
}

Control Statements Page 24


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a program to generate the first 50 positive integers that are


divisible by 7.

Solution

A for loop can be used to print 50 integers and an integer n is


used to generate numbers from 7 in steps of 7.

/* program to print first 50 positive integers which are divisible


by 7 */
#include <stdio.h>
#include <conio.h>
main()
{ int n,i;
clrscr();
printf(―\n Integer divisible by 7 \n‖);
n = 7;
for(i = 1; i <= 50; i++)
{
printf(―%8d‖,n);
n = n + 7;
}
getch();
}

Control Statements Page 25


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to check whether the given number is an


Armstrong number.
An Armstrong number in one that is equal to the sum of cubes of
individual digits. For example, 153 = 13 + 53 + 33
= 1 + 125 + 27
= 153
is an Armstrong number.

Solution

Note that the digits separated are used to get the cubes and the
cubes are added to get the sum. The sum obtained is compared
with the given number and the result in printed.

/* program to check armstrong number */


#include <stdio.h>
#include <conio.h>
main()
{ int n,q,r,s;
clrscr();
printf(―\n Enter an integer number : ―);
scanf(―%d‖,&n);
q = n;
s = 0;
while(q > 0)
{
r = q % 10;
s = s + r*r*r;
q = q/10;
}
if(n == s)
printf(―\n %d is an Armstrong number‖,n);
else
printf(―\n %d is not an Armstrong number‖,n);
getch();
}

Control Statements Page 26


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to accept an integer number and print the


digits using words (for example, 356 is printed as Three Five Six).

Solution

First of all the given number is reversed. Then the digits are
separated to get the first digit of the number. Finally the
corresponding word will be printed.

Control Statements Page 27


Computer Programming in C (Notes) by Dr SM Shashidhar

/* program to print words for the digits in an integer */


#include <stdio.h>
#include <conio.h>
main()
{ int n,q,r,rn;
clrscr();
printf(―\n Enter an integer number : ―);
scanf(―%d‖,&n);
q = n;
rn = 0;
while(q > 0)
{
r = q % 10;
rn = rn*10 + r;
q = q/10;
}
/* loop to separate the digits and print words */
while(rn > 0)
{
r = rn % 10;
switch(r)
{
case 1 : printf(―One‖);
break;
case 2 : printf(―Two ―);
break;
case 3 : printf(―Three ―);
break;
case 4 : printf(―Four ―);
break;
case 5 : printf(―Five ―);
break;
case 6 : printf(―Six ―);
break;
case 7 : printf(―Seven ―);
break;
case 8 : printf(―Eight ―);
break;
case 9 : printf(―Nine ―);
break;
case 0 : printf(―Zero ―);
break;
}
rn = rn/10;
}
getch();
}

Control Statements Page 28


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to check whether the given number n is a


prime number.
(A prime number is an integer which is divisible by unity and the
number itself. For example, 13 is a prime number because it is
divisible by 1 and 13 only.)

Solution

A for loop can be used to find the divisor of the given number
between 2 and n/2 ( for example, when n = 23, it has divisors upto
11 [i.e. 23/2 =11]).
An identification variable flag is used to identify any
divisors for the given number other than 1. It is assumed flag = 0
initially; when a divisor is identified it will be changed to flag = 1.
Based on the value of flag, the loop will be terminated and
the prime number is identified.

/* program to check prime number */


#include <stdio.h>
#include <conio.h>
main()
{ int n,k,flag=0,r;
clrscr();
printf(―\n Enter a positive integer : ―);
scanf(―%d‖,&n);
for(k = 2; k <= n/2 && flag == 0; k++)
{
r = n % k;
if(r == 0)
flag = 1;
}
if(flag == 0)
printf(―\n %d is a prime number‖,n);
else
printf(―\n %d is not a prime number‖,n);
getch();
}

Control Statements Page 29


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to generate the Fibonacci series


0 1 1 2 3 5 8…up to n.

Solution
Note that a term in the series is obtained by adding previous two
terms. Various terms are generated and printed using the steps given
below.

Step 1 : Read n.
Step 2 : Assume n1=0; n2=1 ;
Step 3 : Print n1, n2
Step 4 : newterm = n1 + n2 ;
Step 5 : Print newterm
Step 6 : n1 ← n2
Step 7 : n2 ← newterm ;
Step 8 : Repeat step 4 to step 7 until newterm > n ;

Control Statements Page 30


Computer Programming in C (Notes) by Dr SM Shashidhar

/* program to generate and print Fibonacci series */


#include <stdio.h>
#include <conio.h>
main()
{ int n,n1,n2,newterm;
clrscr();
printf(―\n Enter the final term of the series : ―);
scanf(―%d‖,&n);
n1 = 0;
n2 = 1;
printf(―%8d%8d‖,n1,n2);
newterm = n1 + n2;
while(newterm <= n)
{
printf(―%8d‖,newterm);
n1 = n2;
n2 = newterm;
newterm = n1 + n2;
}
getch();
}

Control Statements Page 31


Computer Programming in C (Notes) by Dr SM Shashidhar

Write a C program to print multiplication table from 1 to 5 up to 10


numbers. Explain the working of the program.

Solution

The nested for loops can be used to print the multiplication tables.
(i) The outer loop is written to print tables from 1 to 5.
(ii) The inner loop is written to generate and print products from 1 to
10.
Using nested for loop, a C program is written as follows to print the
multiplication tables.

/* Program to print multiplication tables */


#include <stdio.h>
#include <conio.h>
main()
{ int i,j;
/* nested loop to print multiplication tables */
for(i = 1; i <= 5; i++) /* outer loop to print tables from 1 to 5
*/
{
clrscr();
printf(―\n\n Multiplication table for %d \n‖,i);
for(j = 1; j <= 10; j++) /* inner loop to print each tables */
printf(―\n %4d x %2d = %4d‖,j,i,j*i);
printf(―\n\n Press any key to continue...‖);
getch();
}
}

Thanks :A First Course in Programming with C by T. Jeyapoovan

Control Statements Page 32

You might also like