0% found this document useful (0 votes)
8 views6 pages

C Programming: Anshika Satish

The document outlines virtual lab experiments in C programming, focusing on recursion, expression evaluation, and functions. It explains the concepts and objectives of each topic, including how recursion breaks down problems, evaluates expressions based on operator precedence, and the importance of functions for code modularity and reusability. Results indicate successful implementation and understanding of these programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

C Programming: Anshika Satish

The document outlines virtual lab experiments in C programming, focusing on recursion, expression evaluation, and functions. It explains the concepts and objectives of each topic, including how recursion breaks down problems, evaluates expressions based on operator precedence, and the importance of functions for code modularity and reusability. Results indicate successful implementation and understanding of these programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

VIRTUAL LAB EXPERIMENTS

C Programming

Anshika Satish
CSE B
VIRTUAL LAB EXPERIMENTS
C Programming

Recursion

AIM:
Sometimes solving a big problem may require us to solve smaller problems of similar nature. For
example, computing the xth number in the Fibonacci sequence may require us to find the x-1th and the
x-2th numbers in the sequence. If we are able to break the main problem into smaller instances of the
same problem again and again, so that eventually the smaller problems become trivial, then we can
use the solutions to the trivial problems to progressively build bigger solutions. Consequently, we can
reach the solution of our main problem. This concept when used in coding is called recursion.

THOERY:
Recursion is a programming technique where a function calls itself repeatedly until it reaches a
stopping criterion. In other words, a function solves a problem by breaking it down into smaller sub-
problems of the same type, and then solving those sub-problems using the same function. This process
continues until the sub-problems are small enough to be solved directly, without calling the function
again.

SIMULATION
Objectives
 To understand that some problems can be broken down into smaller similar problems.
 To solve such problems using recursive procedures.
To understand the concept of recursion.
Problem Statements
1. Find the sum of first n natural numbers using recursion.
2. Find the power of a number using recursion.
3. Find the factorial of a number using recursion.
4. Find the nth Fibonacci number using recursion.
RESULT:
The recursion is done

Expression Evaluation
AIM:
Evaluating an expression is one of the most common tasks in programming. For example, a+b+c is an
expression. Here + is an operator. The variables a, b and c are operands. The value of the expression is
the sum of a, b and c. An expression may be often evaluated to assign a variable a new value or to
verify the truthfulness of a test condition. For example the operator can be unary(Ex: ~, ++ etc.),
binary(Ex: +, * etc.) or ternary (Ex: (a>b)?p:q)

THOERY:
Evaluating an expression involves repeatedly solving the sub-expressions in the lowest level
parenthesis and substituting its value to solve the bigger expression. If an expression or sub-
expression does not have a parenthesis, then we can directly compute its value based on the
precedence and associativity of the operators present in the expression. That is, the sub expression
involving operator with higher precedence is evaluated before other sub-expressions.
Operators can be broadly categorized as:
1. Arithmetic Operators: These operators are used for arithmetic operations like addition,
subtraction and multiplication. Ex: DIVISION(/),MODULUS OR REMAINDER (%)
2. Relational Operators: These operators are typically used for comparison of two operands. Ex:
GREATER THAN(>),GREATER THAN OR EQUAL TO(>=) etc.
3. Logical Operators: These operators are used for conducting logical operations like AND,OR.
Ex: LOGICAL AND( && ),LOGICAL OR (||) etc.
4. Bitwise Operators: These operators are used to perform operations on bits. Ex: BITWISE
AND( & ), BITWISE OR ( | ) etc.

SIMULATION:

RESULT:
Expression evaluation was successfully performed, and the expected results were obtained based on
operator precedence and associativity.

FUNCTIONS

AIM:
Writing large programs effectively requires decomposition of the code into several independent
modules. This makes the program easier to maintain and edit. This is done by taking the problem and
breaking it into small, manageable pieces. A function is a portion of code within a larger program that
performs a specific task and is relatively independent of the remaining code. This helps in
decomposition of the code into smaller independent modules. The task performed by a function can
be summarised as taking as input a set of variables and returning a value after doing computation with
these values. The value of the input variables may also be updated during the computation. Since the
functions are written independent of the main code, the same function can be called from the main
program with different input variables. The allows reuse of the code and hence shortening of the code.

THEORY:
Function basically is an independent piece of code which takes some variables as input and returns a
result. The function may optionally update the values of the input variables. Writing a function
involves clearly specifying the characteristics of the function in its prototype. The prototype of a
function looks like:
return_type Function_name(DATATYPES_of_input_variables);

For example, the prototype of a function for computing tax and returning the total payable amount
may look like:
float compute_total(float ,float );

This states that the name of the function is compute_total. It accepts two float variables as input and
returns a float value as output. Next, we define the function by writing the code corresponding to the
computation of the return value of the function.
float compute_total(float subtotal,float tax_rate)
{
float total;
total=subtotal*(1+(tax_rate/100));
return total;
}

Simulation:

RESULT:
Functions were implemented successfully, and the expected outputs were obtained, demonstrating
code reusability and modularity.

You might also like