Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

19AI304-Fundamentals-of-C-Programming-2025-Odd-M5

IAPR-5- Module 5 - FoC

9. Implementation of recursion.

10. Implementation of programs using pointer arithmetic.

Ex.No:21

Implement a C program to demonstrate call by value and call by reference by swapping two integers using separate functions.

Date :

Aim:

To implement a C program that illustrates the difference between call by value and call by reference by swapping two integer variables using two separate functions.

Algorithm:

Step 1:

Start

Step 2:

Include the standard input-output library: #include<stdio.h>.

Step 3:

Declare two functions:

  • swapv(int, int) for swapping using call by value
  • swapr(int *, int *) for swapping using call by reference

Step 4:

In the main() function, declare two integer variables a and b and initialize them with values (e.g., 10 and 20).

Step 5:

Print the values of a and b before calling swapv().

Step 6:

Call the function swapv(a, b) and print the values of a and b after the function call to show that call by value does not change the original values.

Step 7:

Print the values of a and b before calling swapr().

Step 8:

Call the function swapr(&a, &b) using the addresses of a and b.

Step 9:

Print the values of a and b after the swapr() function call to show that call by reference successfully swaps the original values.

Step 10:

Inside swapv(x, y) function:

  • Step 10.1: Swap the values of x and y using a temporary variable.
  • Step 10.2: Print the swapped values (formal parameters).

Step 11:

Inside swapr(*x, *y) function:

  • Step 11.1: Swap the values pointed to by x and y.
  • Step 11.2: Print the swapped values (affects actual parameters).

Step 12:

Stop

Program:

#include <stdio.h>

// Function to swap using call by value void swapByValue(int a, int b) { int temp = a; a = b; b = temp; printf("Inside swapByValue: a = %d, b = %d\n", a, b); }

// Function to swap using call by reference void swapByReference(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }

int main() { int x, y;

printf("Enter two integers: ");
scanf("%d %d", &x, &y);

printf("\nBefore swap:\n");
printf("x = %d, y = %d\n", x, y);

// Call by value
swapByValue(x, y);
printf("After swapByValue in main:\n");
printf("x = %d, y = %d\n", x, y);  // Values remain unchanged

// Call by reference
swapByReference(&x, &y);
printf("After swapByReference in main:\n");
printf("x = %d, y = %d\n", x, y);  // Values are swapped

return 0;

}

Output:

Screenshot 2025-12-28 110208 # Result : Thus, the program was implemented and executed successfully, and the required output was obtained.

19AI304-Fundamentals-of-C-Programming-2025-Odd-M5

IAPR-5- Module 5 - FoC

Ex.No:22

Implement a C program to generate the Fibonacci series using a recursive function. The program should accept a positive integer n and display the first n terms of the Fibonacci sequence.

Date :

Aim:

To implement a C program that uses a recursive function to generate and display the Fibonacci series for a given number of terms.

Algorithm:

Step 1:

Start

Step 2:

Include the standard input-output library: #include<stdio.h>.

Step 3:

Declare a recursive function fibo(int x) that returns the Fibonacci number at position x.

Step 4:

In the main() function, declare variables n and i.

Step 5:

Prompt the user to enter a positive integer n.

Step 6:

Read the value of n.

Step 7:

Display a message indicating that the Fibonacci series of n terms will be printed.

Step 8:

Use a for loop from i = 0 to i < n to:

  • Step 8.1: Call the recursive function fibo(i)
  • Step 8.2: Print the returned Fibonacci value

Step 9:

Define the recursive function fibo(x) as follows:

  • Step 9.1: If x == 0 or x == 1, return x.
  • Step 9.2: Otherwise, return fibo(x - 1) + fibo(x - 2).

Step 10:

Stop

Program:

#include <stdio.h>

// Recursive function to find the nth Fibonacci number int fibonacci(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); }

int main() { int n, i;

printf("Enter a positive integer: ");
scanf("%d", &n);

if (n <= 0) {
    printf("Please enter a positive integer greater than 0.\n");
    return 1;
}

printf("First %d terms of the Fibonacci series:\n", n);
for (i = 0; i < n; i++) {
    printf("%d ", fibonacci(i));
}

printf("\n");
return 0;

}

Output:

Screenshot 2025-12-28 110614

Result:

Thus, the program was implemented and executed successfully, and the required output was obtained.

19AI304-Fundamentals-of-C-Programming-2025-Odd-M5

IAPR-5- Module 5 - FoC

Ex.No:23

Implement a C program to demonstrate recursion by printing a sequence of even or odd numbers from a given lower limit to an upper limit, with each recursive call progressing by 2.

Date :

Aim:

To implement a C program that uses a recursive function to print even or odd numbers in a specified range based on the starting value provided by the user.

Algorithm:

Step 1:

Start

Step 2:

Include the standard input-output library: #include<stdio.h>.

Step 3:

Declare a recursive function printEvenOdd(int cur, int limit) to print numbers from cur to limit with a step of 2.

Step 4:

In the main() function, declare two integer variables: lowerLimit and upperLimit.

Step 5:

Prompt the user to enter the lower limit of the range.

Step 6:

Read and store the lower limit.

Step 7:

Prompt the user to enter the upper limit of the range.

Step 8:

Read and store the upper limit.

Step 9:

Display a message indicating that the even/odd numbers in the given range will be printed.

Step 10:

Call the recursive function printEvenOdd(lowerLimit, upperLimit).

Step 11:

Inside the function printEvenOdd(cur, limit):

  • Step 11.1: If cur > limit, terminate the recursion.
  • Step 11.2: If cur == limit, print the value without a trailing comma.
  • Step 11.3: Otherwise, print the current value followed by a comma.
  • Step 11.4: Recursively call printEvenOdd(cur + 2, limit) to print the next number.

Step 12:

Stop

Program:

#include <stdio.h>

// Recursive function to print numbers by 2 void printSequence(int current, int upper) { if (current > upper) { return; // Base case: stop recursion }

printf("%d ", current);

// Recursive call increasing by 2
printSequence(current + 2, upper);

}

int main() { int lower, upper;

printf("Enter the lower limit: ");
scanf("%d", &lower);

printf("Enter the upper limit: ");
scanf("%d", &upper);

if (lower > upper) {
    printf("Lower limit should be less than or equal to upper limit.\n");
    return 1;
}

// Adjust lower limit to match even/odd sequence
if (lower % 2 != upper % 2) {
    printf("Lower and upper limit should be of the same parity (both even or both odd).\n");
    return 1;
}

printf("The sequence is:\n");
printSequence(lower, upper);

printf("\n");
return 0;

}

Output:

Screenshot 2025-12-28 110740

Result:

Thus, the program was implemented and executed successfully, and the required output was obtained.

19AI304-Fundamentals-of-C-Programming-2025-Odd-M5

IAPR-5- Module 5 - FoC

Ex.No:24

Implement a C program that dynamically allocates memory using calloc(), accepts integer inputs from the user, computes their sum, and prints the sum.

Date :

Aim:

To implement a C program that dynamically allocates memory for an array of integers using calloc(), accepts elements from the user, computes their sum, and displays the sum.

Algorithm:

Step 1:

Start

Step 2:

Include the standard input-output library: #include<stdio.h>.

Step 3:

a. Declare a pointer ptr to int.
b. Declare integers n, i, and sum (initialize sum = 0).

Step 4:

Read the integer n from the user (the number of integers to be stored).

Step 5:

Use the calloc() function to allocate memory for n integers:
ptr = calloc(n, sizeof(int))

Step 6:

If ptr is not NULL, continue to the next step; otherwise, memory allocation failed (the program exits).

Step 7:

For each i from 0 to n - 1:
a. Read an integer from the user.
b. Store it at memory location ptr + i.

Step 8:

For each i from 0 to n - 1:
a. Access the value stored at ptr + i.
b. Add it to sum.

Step 9:

Print the value of sum.

Step 10:

Call free(ptr); to release the memory allocated by calloc().

Step 11:

Stop

Program:

#include <stdio.h> #include <stdlib.h>

int main() { int n, i, sum = 0; int *arr;

printf("Enter the number of integers: ");
scanf("%d", &n);

if (n <= 0) {
    printf("Number of integers must be positive.\n");
    return 1;
}

// Allocate memory using calloc
arr = (int *)calloc(n, sizeof(int));
if (arr == NULL) {
    printf("Memory allocation failed.\n");
    return 1;
}

// Accept integers from the user
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
}

// Compute sum
for (i = 0; i < n; i++) {
    sum += arr[i];
}

printf("The sum of the entered integers is: %d\n", sum);

// Free allocated memory
free(arr);

return 0;

}

Output:

Screenshot 2025-12-28 110858

Result:

Thus, the program was implemented and executed successfully, and the required output was obtained.

19AI304-Fundamentals-of-C-Programming-2025-Odd-M5

IAPR-5- Module 5 - FoC

Ex.No:25

Implement a C program that reads a set of integers into an array and displays the array elements using a user-defined function.

Date :

Aim:

To implement a C program that reads integers into an array and displays the elements using a user-defined function.

Algorithm:

Step 1:

Start

Step 2:

Include the standard input-output library: #include<stdio.h>.

Step 3:

Declare the function prototype: void displayArray(int *arr, int size);

Step 4:

In the main() function, declare an integer array of size 5 and a loop variable.

Step 5:

Prompt the user to enter the required number of integers.

Step 6:

Read the integers from the user and store them in the array using a loop.

Step 7:

Call the displayArray function, passing the array and its size as arguments.

Step 8:

Define the function displayArray(int *arr, int size) to print the array elements:

  • Loop through the array using either pointer arithmetic (*(arr + i)) or array indexing (arr[i]).
  • Print each element.

Step 9:

Return to the main() function after displaying the array.

Step 10:

Stop

Program:

#include <stdio.h>

// Function to display array elements void displayArray(int arr[], int size) { printf("Array elements are:\n"); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); }

int main() { int n;

printf("Enter the number of integers: ");
scanf("%d", &n);

if (n <= 0) {
    printf("Number of integers must be positive.\n");
    return 1;
}

int arr[n]; // Array declaration

// Input elements from user
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
}

// Call user-defined function to display array
displayArray(arr, n);

return 0;

}

Output:

m5 5

Result:

Thus, the program was implemented and executed successfully, and the required output was obtained.

About

IAPR-5- Module 5 - FoC

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors