Implement a C program to demonstrate call by value and call by reference by swapping two integers using separate functions.
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.
Start
Include the standard input-output library: #include<stdio.h>.
Declare two functions:
swapv(int, int)for swapping using call by valueswapr(int *, int *)for swapping using call by reference
In the main() function, declare two integer variables a and b and initialize them with values (e.g., 10 and 20).
Print the values of a and b before calling swapv().
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.
Print the values of a and b before calling swapr().
Call the function swapr(&a, &b) using the addresses of a and b.
Print the values of a and b after the swapr() function call to show that call by reference successfully swaps the original values.
Inside swapv(x, y) function:
- Step 10.1: Swap the values of
xandyusing a temporary variable. - Step 10.2: Print the swapped values (formal parameters).
Inside swapr(*x, *y) function:
- Step 11.1: Swap the values pointed to by
xandy. - Step 11.2: Print the swapped values (affects actual parameters).
Stop
#include <stdio.h>
void swapv(int x, int y) { int temp; temp = x; x = y; y = temp; printf("Inside swapv (Call by Value): a = %d, b = %d\n", x, y); }
void swapr(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; printf("Inside swapr (Call by Reference): a = %d, b = %d\n", *x, *y); }
int main() { int a = 10, b = 20;
printf("Before swapv: a = %d, b = %d\n", a, b);
swapv(a, b);
printf("After swapv: a = %d, b = %d\n\n", a, b);
printf("Before swapr: a = %d, b = %d\n", a, b);
swapr(&a, &b);
printf("After swapr: a = %d, b = %d\n", a, b);
return 0;
}
Thus, the program was implemented and executed successfully, and the required output was obtained.
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.
To implement a C program that uses a recursive function to generate and display the Fibonacci series for a given number of terms.
Start
Include the standard input-output library: #include<stdio.h>.
Declare a recursive function fibo(int x) that returns the Fibonacci number at position x.
In the main() function, declare variables n and i.
Prompt the user to enter a positive integer n.
Read the value of n.
Display a message indicating that the Fibonacci series of n terms will be printed.
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
Define the recursive function fibo(x) as follows:
- Step 9.1: If
x == 0orx == 1, returnx. - Step 9.2: Otherwise, return
fibo(x - 1) + fibo(x - 2).
Stop
int main() { int n, i;
printf("Enter the number of terms for Fibonacci series: ");
scanf("%d", &n);
if (n <= 0) {
printf("Please enter a positive integer.\n");
return 0;
}
printf("Fibonacci series of %d terms:\n", n);
for (i = 0; i < n; i++) {
printf("%d ", fibo(i));
}
printf("\n");
return 0;
}
Thus, the program was implemented and executed successfully, and the required output was obtained.
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.
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.
Start
Include the standard input-output library: #include<stdio.h>.
Declare a recursive function printEvenOdd(int cur, int limit) to print numbers from cur to limit with a step of 2.
In the main() function, declare two integer variables: lowerLimit and upperLimit.
Prompt the user to enter the lower limit of the range.
Read and store the lower limit.
Prompt the user to enter the upper limit of the range.
Read and store the upper limit.
Display a message indicating that the even/odd numbers in the given range will be printed.
Call the recursive function printEvenOdd(lowerLimit, upperLimit).
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.
Stop
#include <stdio.h>
void printEvenOdd(int cur, int limit) { if (cur > limit) { // Terminate recursion return; } else if (cur == limit) { // Last number, no trailing comma printf("%d", cur); } else { // Print current number followed by comma printf("%d, ", cur); } // Recursive call with step of 2 printEvenOdd(cur + 2, limit); }
int main() { int lowerLimit, upperLimit;
// Input lower limit
printf("Enter the lower limit: ");
scanf("%d", &lowerLimit);
// Input upper limit
printf("Enter the upper limit: ");
scanf("%d", &upperLimit);
if (lowerLimit > upperLimit) {
printf("Invalid range. Lower limit should be less than or equal to upper limit.\n");
return 0;
}
printf("The sequence is:\n");
printEvenOdd(lowerLimit, upperLimit);
printf("\n");
return 0;
}
Thus, the program was implemented and executed successfully, and the required output was obtained.
Implement a C program that dynamically allocates memory using calloc(), accepts integer inputs from the user, computes their sum, and prints the sum.
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.
Start
Include the standard input-output library: #include<stdio.h>.
a. Declare a pointer ptr to int.
b. Declare integers n, i, and sum (initialize sum = 0).
Read the integer n from the user (the number of integers to be stored).
Use the calloc() function to allocate memory for n integers:
ptr = calloc(n, sizeof(int))
If ptr is not NULL, continue to the next step; otherwise, memory allocation failed (the program exits).
For each i from 0 to n - 1:
a. Read an integer from the user.
b. Store it at memory location ptr + i.
For each i from 0 to n - 1:
a. Access the value stored at ptr + i.
b. Add it to sum.
Print the value of sum.
Call free(ptr); to release the memory allocated by calloc().
Stop
#include <stdio.h> #include <stdlib.h> // Required for calloc() and free()
int main() { int *ptr; // Pointer for dynamic memory int n, i, sum = 0;
// Read the number of integers
printf("Enter the number of integers: ");
scanf("%d", &n);
// Allocate memory for n integers using calloc
ptr = (int *)calloc(n, sizeof(int));
// Check if memory allocation was successful
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Read integers from the user
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", ptr + i);
}
// Compute the sum
for (i = 0; i < n; i++) {
sum += *(ptr + i);
}
// Print the sum
printf("Sum of the entered integers: %d\n", sum);
// Free the allocated memory
free(ptr);
return 0;
}
Thus, the program was implemented and executed successfully, and the required output was obtained.
Implement a C program that reads a set of integers into an array and displays the array elements using a user-defined function.
To implement a C program that reads integers into an array and displays the elements using a user-defined function.
Start
Include the standard input-output library: #include<stdio.h>.
Declare the function prototype: void displayArray(int *arr, int size);
In the main() function, declare an integer array of size 5 and a loop variable.
Prompt the user to enter the required number of integers.
Read the integers from the user and store them in the array using a loop.
Call the displayArray function, passing the array and its size as arguments.
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.
Return to the main() function after displaying the array.
Stop
#include <stdio.h>
void displayArray(int *arr, int size);
int main() { int arr[5]; // Array of size 5 int i;
// Input elements from the user
printf("Enter 5 integers:\n");
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
// Call the user-defined function to display the array
printf("The elements of the array are:\n");
displayArray(arr, 5);
return 0;
}
// Function to display array elements void displayArray(int *arr, int size) { int i; for (i = 0; i < size; i++) { printf("%d ", *(arr + i)); // Using pointer arithmetic } printf("\n"); }
Thus, the program was implemented and executed successfully, and the required output was obtained.




