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>
// 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;
}
# Result
:
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
#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;
}
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>
// 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;
}
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>
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;
}
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>
// 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;
}
Thus, the program was implemented and executed successfully, and the required output was obtained.