0% found this document useful (0 votes)
25 views

C Program Functions

The document contains code snippets for: 1) A function that returns the maximum of two numbers by comparing them and returning the larger value. 2) A recursive function to calculate the factorial of a number by multiplying it by the factorial of the previous smaller number. 3) A non-recursive function to calculate the factorial of a number by using a for loop to iteratively multiply the number by integers from 1 to itself. 4) Functions to swap the values of two variables, first passing the variables by value then passing their addresses by reference.

Uploaded by

Deepa Thilak
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

C Program Functions

The document contains code snippets for: 1) A function that returns the maximum of two numbers by comparing them and returning the larger value. 2) A recursive function to calculate the factorial of a number by multiplying it by the factorial of the previous smaller number. 3) A non-recursive function to calculate the factorial of a number by using a for loop to iteratively multiply the number by integers from 1 to itself. 4) Functions to swap the values of two variables, first passing the variables by value then passing their addresses by reference.

Uploaded by

Deepa Thilak
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Maximum of two numbers


#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main () {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}

2. factorial using recursion


#include <stdio.h>
long int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, fact(n));
return 0;
}
long int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}

3. factorial without recursion

#include <stdio.h>
int fact(int n);

int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, fact(n));
return 0;
}
int fact(int n)
{
int factt=1;
for(int i=1;i<=n;i++)
{
factt=factt*i;

}
return factt;
}

4. swaping

#include<stdio.h>
// function prototype, also called function declaration
void swap(int a, int b);

int main()
{
int m = 22, n = 44;
// calling swap function by value
printf(" values before swap m = %d \nand n = %d", m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}

#include<stdio.h>
// function prototype, also called function declaration
void swap(int *a, int *b);
int main()
{
int m = 22, n = 44;
// calling swap function by reference
printf("values before swap m = %d \n and n = %d",m,n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n values after swap a = %d \nand b = %d", *a, *b);
}

You might also like