Answer Keys For Dand DS
Answer Keys For Dand DS
1. Define Recursion?
The recursion process in C refers to the process in which the program repeats a
certain section of code in a similar way. Thus, in the programming languages, when the
program allows the user to call any function inside the very same function, it is referred
to as a recursive call in that function.
2. Distinguish between Iterative and recursive function with suitable examples?
Recursion Iteration
Through base case, where there When the termination condition for the
Termination will be no function call. iterator ceases to be satisfied.
while do-while
Variable in condition is initialized before the variable may be initialized before or within
execution of loop. the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50][50],i,j,m,n;
int sum=0;
clrscr();
printf("\n enter the limit m and n");
scanf("%d%d",&m,&n);
printf("\n enter the matrix element \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n the matrix elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i!=j)
sum=sum+a[i][j];
}
}
printf("\n sum of non diagonal elements=%d",sum);
getch();
}
5. What is the use of looping? Explain about the entry-controlled and exit- controlled
loops available in c with appropriate sample C program?
In C, entry controlled and exit controlled loops are two ways to control how
often code in a program is repeated.
Use of looping:
Loops allow you to repeat a process over and over without having to write
the same (potentially long) instructions each time you want your program to
perform a task. Two major types of loops are FOR LOOPS and WHILE LOOPS.
Condition Check: The loop condition is checked before executing the loop body.
Execution: If the condition is true, the loop body is executed; otherwise, the loop
terminates without executing the loop body.
Initialization and Increment/Decrement: For for loops, initialization, condition
checking, and increment/decrement are all handled in the loop’s syntax. For while loops,
the initialization and increment/decrement must be handled separately within the loop’s
body or before and after the loop.
sTypes of Entry Controlled Loops:
There are two types of Entry Controlled Loops in Programming:
For Loop: For loop is a control flow statement in programming that allows you to
execute a block of code repeatedly based on a specified condition. It is commonly used
when you know how many times you want to execute a block of code.
While Loop: While loop is a fundamental control flow structure (or loop statement ) in
programming, enabling the execution of a block of code repeatedly as long as a
specified condition remains true. Unlike the for loop, which is used for
iterating a fixed number of times, the while loop is preferred in scenarios
where the number of iterations is uncertain or dependent on dynamic
conditions.
Entry Controlled Loop in C:
Below is the implementation of Entry Controlled Loop in C:
#include <stdio.h>
int main()
{
int i = 0;
return 0;
}
Output
Entry controlled loop using while loop:
01234
Entry controlled loop using for loop:
01234
Exit Controlled loops are loop structures where the loop’s condition is checked
after the loop body has executed. This means that the loop will always execute at
least once, regardless of whether the condition is true or false initially. The most
common examples of exit-controlled loops are the do-while loop in languages
like C, C++, and Java.
#include <stdio.h>
int main()
{
int i = 5;
// Exit Controlled Loop
do {
printf("%d\n", i);
i--;
} while (i < 5 && i > 0);
return 0;
}
Output
5
4
3
2
1