This document discusses functions in C programming. It begins by asking questions about functions and their syntax and benefits. It then provides examples of built-in library functions like sqrt, pow, and isupper. The document demonstrates how to write user-defined functions with a factorial function as an example. It also provides examples using functions like calculating factorials, binomial coefficients, GCD, prime checking, and printing prime numbers within a range.
2. What is a function?
Write the Syntax of Function?
What are the benefits of using a function?
Is main a user-defined or library function?
Explain the difference between call by value
and call by reference(address)?
mohammed.sikander@cranessoftware.com 2
3. Name some Library (built-in) functions used
by you till now?
mohammed.sikander@cranessoftware.com 3
5. SYNOPSIS
#include <math.h>
double pow(double x, double y);
mohammed.sikander@cranessoftware.com 5
int main( )
{
int x , y;
printf(“Enter two numbers : “);
scanf(“ %d %d” , &x, &y);
int res = pow ( x , y);
printf(“ %d “ , res);
}
6. #include <ctype.h>
int isupper(int c);
It checks whether the given character is uppercase or not. Returns o is not
non-zero otherwise
mohammed.sikander@cranessoftware.com 6
#include <ctype.h>
int main( )
{
char c;
printf(“Enter a character : “);
scanf(“ %c”,&c);
int res = isupper(c) ;
if(res == 0)
printf(“Not a Upper Case Character “);
else
printf(“UPPERCASECharacter “);
}
7. Now we have learned how to call a function.
Now lets write our (user-defined) function.
mohammed.sikander@cranessoftware.com 7
9. Write a factorial function which accepts a number and returns the
factorial of it.
Eg: Input Output
5 120
3 6
mohammed.sikander@cranessoftware.com 9
int factorial( int num)
{
int res = 1;
for(i = 2 ; i <= num ; i++)
res = res * i;
return res;
}
int main( )
{
int res = factorial(5);
printf(“ %d n“ , res);
res = factorial(3);
printf(“ %d n“ , res);
}
10. int main( )
{
int n , r;
printf(“Enter the value of n and r : “);
scanf(“ %d %d”,&n,&r);
int nfact = factorial(n);
int rfact = factorial(r);
int nrfact = factorial(n - r);
int ncr = nfact / (rfact * nrfact);
int npr = nfact / nrfact;
printf(“ %d C %d = %d n“ , n , r , ncr);
printf(“ %d P %d = %d n“ , n , r , npr);
}
mohammed.sikander@cranessoftware.com 10
11. int gcd(int a,int b)
{
int rem;
while(b != 0)
{
rem = a % b;
a = b;
b = rem;
}
return a;
}
mohammed.sikander@cranessoftware.com 11
int main()
{
int a , b;
printf(“Enter two numbers : “);
scanf(“ %d %d”,&a,&b);
int res = gcd(a , b);
printf(“GCD(%d , %d) = %d”,a , b , res);
int lcm = a * b / res;
printf(“LCM(%d ,%d) = %d”,a , b , lcm);
}
12. #include <stdbool.h>
bool isprime(int num)
{
int count = 0;
for(int i = 2 ; i < num - 1 ; i++)
{
int rem = num % i;
if(rem == 0)
count++;
}
return count == 0?true:false;
}
mohammed.sikander@cranessoftware.com 12
int main( )
{
int num ;
bool result;
printf("Enter a number : ");
scanf(" %d" , &num);
if(res == true)
printf("Prime ");
else
printf("Not Prime ");
}
13. mohammed.sikander@cranessoftware.com 13
int main( )
{
int n1 , n2;
bool result;
printf("Enter the start and end range : ");
scanf(" %d %d" , &n1 , n2);
for(int i = n1 ; i <= n2 ; i++)
{
bool res = isprime( i );
if(res == true)
printf(“ %d “ , i);
}
}