Soham Alu Loop Structure Programming
Soham Alu Loop Structure Programming
OR
What the different loop control statements used in C? Give the
syntax of each.
Solution:
Looping meant, directs a program to perform a set of operations
again and again until a specified condition is achieved. This condition
causes the termination of the loop. Programming language C contains
three statements for looping:
The while loop
The do…while loop
The for loop
while loop: while loop construct contains the condition first. If the
condition is satisfied, the control executes the statements following the
while loop else, it ignores these statements. The general form of while
loop is:
while(condition)
{
statement1;
statement2;
….
}
repeated. The control will come out from the loop, only when the test
condition is false.
The do-while loop has the following form:
do
{
statement1;
statement2;
………..
}
while(condition);
Solution:
While loop evaluates a test expression before allowing entry into the
loop, whereas do-while loop is executed at least once before it evaluates
the test expression which is available at the end of the loop.
While loop construct contains the condition first. If the condition is
satisfied, the control executes the statements following the while loop
else, it ignores these statements. The general form of while loop is
While(condition)
{
statement1;
statement2;
……
}
Solution:
a. while loop could make 0 iteration.
b. Do-while loop could make 1 iteration.
return;
}
The output of the above program will be:
The Fibonacci series upto 100 is:
0 1 1 2 3 5 8 13 21 34 55 89
Ex: Write a program to read in value of n and then print the first n
Fibonacci numbers.
OR
th
Write a C program to print the n fibonacci number.
Solution:
/* To print Fibonacci series */
#include<stdio.h>
main( )
{
long int a = 0;
long int e = 1;
long int b = 1;
int n, c = 1;
printf(“How many numbers you want in the Fibonacci series ? ”);
scanf(“%d”,&n);
printf(“%d\n”,n);
do
{
printf(“%d\n”,b);
b = e+a;
a = e;
e = b;
c = c+1;
}
while (n >= c);
}
6
goto Statement:
goto label;
Eg.
----------------;
goto display;
----------------;
----------------;
display;
----------------;
break statement:
continue Statement:
Eg.
for(i=1; i<= 20; i++)
{ ------------------
Ch= getche();
if (ch= =‟C‟)
{ Control is transferred to the
printf(“\n C for continue is pressed”); beginning of the block
continue;
}
---------------------
}
Exit () Function:
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.
Eg.
if( n < 0 )
{
printf(“\n Factorial is not available for negative numbers”);
exit(0);
}
------------------;
9
Note that the program execution is terminated when the value of the
variable n is negative.
Return statement:
The keyword return is used to return any value from the function called.
This statement terminates the function and returns a value to its caller.
The return statements may also be used to exit from any function
without returning any value. The return statement may or may not
include an expression. The general format of using return statement is
given below:
return; /* without expression */
return(expression) /* with expression */
Return statement: The keyword return is used to return any value from
the function called. This statement terminates the function and returns a
value to its caller. The return statements may also be used to exit from
10
any function without returning any value. The return statement may or
may not include an expression. The general format of using return
statement is given below:
return; /* without expression */
return(expression) /* with expression */
break statement: The break statement causes an immediate exit from the
innermost loop structure or to exit from a switch. It can be used within a
for, while, do-while, or switch statement.
If we need to come out of a running program immediately
without letting it to perform any further operation in a loop,
then we can use break control statement.
The general format of the break statement is:
break; <Enter>
Following program segment is written to explain the use of break with
while loop structure:
#include<stdio.h>
main( )
{
int i, value;
i=0;
while(i<=10)
{
printf(“Enter a number\n”);
scanf(“%d”,&value);
if value = =0 || value<0)
{
printf(“Zero or negative value found.\n”);
break;
}
i++;
11
}
}
1 2
2 1
Note that when the value of i equals that of j, the continue statement
takes the control to the for loop(inner) bypassing rest of the statements
pending execution in the for loop(inner).
12
Solution:
Ex: Why the use of the goto statement should generally be avoided in a
„C‟ program?
13
Solution: The structured feature in „C‟ requires that the entire program be
written in an orderly, and sequential manner. For this reason, use of the
goto statement should generally be avoided in a „C‟ program.
Solution: The differences between break and exit( ) function are shown
below:
break statement exit( ) function
1. A break statement is used 1. An exit( ) function is
to terminate the execution of used to terminate the
a statement block. The next execution of a C program
statement which follows this permanently.
statement block will be
executed.
2. It is used in a program as It is a built-in function and
break; is used/called with
necessary argument as
exit(0);
Solution:
/* Sum of digits of a 5 digit number */
#include<stdio.h>
#include<conio.h>
main()
{
int num, a, n;
int sum=0; /*sum initialised to zero as otherwise it will contain a
garbage value */
clrscr();
printf("\nEnter a 5-digit number ");
scanf("%d",&num);
a=num%10; /*last digit extracted as remainder */
n=num/10; /*remaining digits*/
sum=sum+a; /*sum updated with addition of extracted digit */
while(n!=0)
15
{
a=n%10; /* 4th digit */
n=n/10;
sum=sum+a;
}
printf("\nThe sum of the 5 digits of %d is %d",num, sum);
printf("\n\n\nPress any key to exit...");
getch();
return;
}
Solution:
/* Generate first n prime numbers */
#include<stdio.h>
#include<math.h>
main()
{
int n,num,d,t,count;
printf("How many primes are required? ");
scanf("%d",&n);
printf("%d\n",n);
printf("First %d primes are:\n");
printf("%5d",2); /* 2 is the first and only even prime number */
count=1;
num=3; /* now start from 3 and test only odd numbers */
while(count<n)
{
t=sqrt(num);
d=2;
while(d<=t)
{
if(num%d==0)
break;
d++;
}
if(d>t)
16
{
printf("%5d",num);
count++;
}
num+=2;
}
}
RUN:
How many primes are required? 50
First 50 primes are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53
59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131
137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223
227 229
case 3: y = 1 + pow(x,n);
break;
default: y = 1 + n * x;
break;
}
Solution:
/* Accept a number and sum up the digit */
/* for example, 173456=1+7+3+4+5+6=26 */
#include<stdio.h>
main( )
{
int n, rem, quo, sum=0;
printf(“Enter a integer number: \n”);
scanf(“%d”,&n);
while(n>0)
{
rem=n%10;
quo=n/10;
sum=sum+rem;
n=quo;
}
printf(“The sum of digit of digits which you entered is =
%d”,sum);
}
{
-------------
-------------
break;
------------
------------
}
--------------------- control transferred to the end of the statement block.
&&&&&&&&&