0% found this document useful (0 votes)
20 views

Answer Keys For Dand DS

It's an computer science for 2nd semester students to study c language about it's topic recursion

Uploaded by

meenalakshmi182
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Answer Keys For Dand DS

It's an computer science for 2nd semester students to study c language about it's topic recursion

Uploaded by

meenalakshmi182
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Answer keys:

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

Definition Function calls itself. A set of instructions repeatedly executed.

Application For functions. For loops.

Through base case, where there When the termination condition for the
Termination will be no function call. iterator ceases to be satisfied.

Used when code size needs to be


Used when time complexity needs to be
small, and time complexity is not
balanced against an expanded code size.
Usage an issue.

Code Size Smaller code size Larger Code Size.

Relatively lower time


Very high(generally exponential)
Time complexity(generally polynomial-
time complexity.
Complexity logarithmic).

Space The space complexity is higher


Space complexity is lower.
Complexity than iterations.

3. Distinguish between while and do-while loop?

while do-while

Condition is checked first then statement(s) Statement(s) is executed atleast once,


is executed. thereafter condition is checked.
while do-while

It might occur statement(s) is executed zero


At least once the statement(s) is executed.
times, If condition is false.

No semicolon at the end of while. Semicolon at the end of while.


while(condition) while(condition);

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.

while(condition) { statement(s); } do { statement(s); } while(condition);

4. Calculate the sum of all non-diagonal elements in an MATRIX?

#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.

Entry controlled loops:

Entry controlled loops in programming languages allow repetitive


execution of a block of code based on a condition that is checked before
entering the loop. In this article, we will learn about entry controlled loops,
their types, syntax, and usage across various popular programming languages.

Entry controlled loops are loop structures where the condition is


checked before entering the loop body. If the condition evaluates to true, the
loop body is executed, otherwise, the loop is terminated. Entry Controlled
Loops are executed in the following order:

 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;

// Entry controlled loop using while loop


printf("Entry controlled loop using while loop:\n");
while (i < 5) {
printf("%d ", i);
i++;
}
printf("\n");

// Reset i for the next loop


i = 0;

// Entry controlled loop using for loop


printf("Entry controlled loop using for loop:\n");
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
printf("\n");

return 0;
}
Output
Entry controlled loop using while loop:
01234
Entry controlled loop using for loop:
01234

Exit Controlled Loop in Programming


Exit controlled loops in programming languages allow repetitive execution of a
block of code based on a condition that is checked after entering the loop. In this
article, we will learn about exit controlled loops, their types, syntax, and usage
across various popular programming languages.

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.

Below is the implementation of Exit Controlled Loop in C:

#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

You might also like