Call by value
#include <stdio.h>
void swap(int a,int b);
int main() {
int m,n;
printf("enter values for a and b:");
scanf("%d %d",&m,&n);
printf("the values before swapping are m=%d n=%d \n",m,n);
swap(m,n); //Actual parameter
printf("the values after swapping are m=%d n=%d \n",m,n);
}
void swap(int a, int b) //formal parameter
{
int temp;
temp=a;
a=b;
b=temp;
printf("After swapping within Function: a = %d, b = %d\n", a, b);
}
Call by reference
#include <stdio.h>
void swap(int *a,int *b);
int main() {
int m,n;
printf("enter values for a and b:");
scanf("%d %d",&m,&n);
printf("the values before swapping are m=%d n=%d \n",m,n);
swap(&m,&n); //Actual parameter
printf("the values after swapping are m=%d n=%d \n",m,n);
}
void swap(int *a, int *b) //formal parameter
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("After swapping within Function: *a = %d, *b = %d\n", *a, *b);
}
Passing array to UDF
1. Calculate the sum of elements in an array.
#include<stdio.h>
int sum(int arr[], int size);
int main() {
int size,i;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int arraySum = sum(arr, size);
printf("The sum of elements in the array is: %d\n", arraySum);
return 0;
}
int sum(int arr[], int size) {
int sum = 0,i;
for (i = 0; i < size; i++) {
sum =sum+ arr[i];
}
return sum;
}
Sort the elements of an array in ascending order
#include<stdio.h>
void ascending(int arr[], int size);
int main() {
int size, i;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
ascending(arr, size);
return 0;
}
void ascending(int arr[], int size) {
int i, j, temp;
for (i = 0; i < size - 1; i++) {
for (j = i+1; j < size ; j++) {
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("The array in ascending order is: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
Find the smallest element in an array.
#include<stdio.h>
int smallest(int arr[], int size);
int main() {
int size,i;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of the array:\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int smallestElement = smallest(arr, size);
printf("The smallest element in the array is: %d\n", smallestElement);
return 0;
}
int smallest(int arr[], int size) {
int smallest = arr[0],i;
for ( i = 1; i < size; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
return smallest;
}
Recursion program
1. Factorial Using Recursion
#include <stdio.h>
int factorial(int n)
int main() {
int num;
printf("Enter the numbers:");
scanf("%d",&num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
2. Fibonacci Series Using Recursion
#include <stdio.h>
int fibonacci(int n);
int main() {
int n,i;
printf("Enter the number:");
scanf("%d",&n);
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
int fibonacci(int n) {
if (n == 0 || n == 1) {
return n;
}
else{
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
3. Sum of natural number
#include <stdio.h>
int sum(int n);
int main() {
int n ;
printf("Enter the number:");
scanf("%d",&n);
printf("Sum = %d", sum(n));
return 0;
}
int sum(int n) {
if (n == 0)
return n;
else
return n + sum(n - 1);
}
4. GCD (Greatest Common Divisor) Using Recursion
#include <stdio.h>
int gcd(int a, int b);
int main() {
int a, b;
printf("Enter the numbers:");
scanf("%d%d",&a,&b);
printf("GCD = %d", gcd(a, b));
return 0;
}
int gcd(int a, int b) {
if (b== 0)
return a;
else
return gcd(b, a % b);
}
Factor of a number
#include <stdio.h>
int Factors(int num, int factors[]);
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
int factors[n];
int count = Factors(n, factors);
printf("Factors of %d are: ", n);
for (int i = 0; i < count; i++) {
printf("%d ", factors[i]);
}
return 0;
}
int Factors(int num, int factors[]) {
int count = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
factors[count++] = i;
}
}
return count;
}