APIC Ch-2 Theory+Practicals-1
APIC Ch-2 Theory+Practicals-1
Introduction:
1) Function Declaration:
returnTypefunctionName(parameterList);
2) Function Definition:
returnTypefunctionName(parameterList);
{
Actual code….
}
3) Function Call:
functionName(parameters);
Advantages of Functions:
Types of Functions in C:
C String Functions:
Output:
C Math Functions:
#include<stdio.h>
#include <math.h>
int main()
{
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
return 0;
}
Output:
4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12
i) Scanf() Function:
Ctype.h:
Library Functions:
Character Classes:
if(i>j)
{
greaterNum=i;
}
else
{
greaterNum is=j;
}
//returning the result
return greaterNum;\
Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, result;
int addition(int, int); //function declaration
clrscr();
Arguments Parameters
1) The values that are The variables that are
declared within the defined when the function
function, when the is declared are known as
function is called are parameters.
known as an argument.
2) These are used in function These are used in the
call statement to send function header of the
value from the coming called function to receive
function to the receiving the value from the
function. argument.
3) During the time of call Parameters are local
each argument is always variables which are
assigned to the parameter assigned values of the
in the function definition. argument when the
function is called.
4) They are known as actual They are known as formal
parameters. parameters.
Function Mechanism:
#include<stdio.h>
#include<conio.h>
{
int n1,n2 ;
void swap(int,int);
clrscr();
n1=10;
n2=20;
swap(n1,n2);
printf(“n1=%d, n2=%d”,n1,n2);
getch();
}
1) Call by reference:
#include<stdio.h>
#include<conio.h>
{
int n1,n2 ;
void swap(int*,int*);
clrscr();
n1=10;
n2=20;
swap(&n1,&n2);
printf(“n1=%d, n2=%d”,n1,n2);
getch();
}
Recursive Functions In C:
O/P:
Advantages of Recursion:
Disdvantages of Recursion:
Ch-2 Practicals:
#include<stdio.h>
#include<conio.h>
void sum();
void main()
{
clrscr();
sum();
getch();
}
void sum()
{
int a,b,c;
printf("Enter the value of A:");
scanf("%d",&a);
printf("Enter the value of B:");
scanf("%d",&b);
c=a+b;
printf("Sum of A & B: %d",c);
}
O/P:
Sum of A & B: 3
#include<stdio.h>
#include<conio.h>
float area();
int main()
{
float result = area();
printf("The area of the circle is: %f\n", result);
return 0;
}
float area()
{
float radius, result;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
result = 3.14 * radius * radius;
return result;
}
O/P:
Enter the radius of the circle: 2
The area of the circle is: 12.560000
P-2.3: Write a C program to return sum of first n numbers using user
defined functions.
O/P:
Enter the value of n: 3
The sum of first 3 numbers is: 6
P-2.4: Write a C program to swap two variable values using call by
value.
#include <stdio.h>
#include<conio.h>
void swap(int, int);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
O/P:
Enter the value of x and y
10
20
Before Swapping
x = 10
y = 20
After Swapping
x = 20
y = 10
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
swap(&a,&b);
printf("After swapping values in main:\na=%d\nb=%d\n",a,b);
}
void swap (int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("After swapping values in function:\n a=%d\nb=%d\n",*a,*b);
}
O/P:
#include <stdio.h>
#include<conio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number: ");
scanf("%d",&n);
f =fact(n);
printf("Factorial of %d is: %d",n,f);
return 0;
}
int fact(int n)
{
int temp;
if (n==0)
{
return 1;
}
else
{
temp=n*fact(n-1);
}
return temp;
}
O/P:
Enter the number: 5
Factorial of 5 is: 120
P-2.7: Write a C program to find out maximum number out of three
number using functions.
#include <stdio.h>
#include<conio.h>
int max(int, int, int);
int main()
{
int a, b, c;
printf("Enter three numbers: \n");
scanf("%d%d%d", &a, &b, &c);
return 0;
}
else
return z;
}
}
O/P:
#include <stdio.h>
#include<conio.h>
int main()
{
int i, n, sum=0;
printf("Enter value of n: ");
scanf("%d", &n);
return 0;
}
O/P:
Enter value of n: 5
Sum of odd numbers = 9
P-2.9: Write a C program to illustrate the use of various built-in string
functions.
#include <stdio.h>
#include<conio.h>
#include <string.h>
int main()
{
char str1[20] = "Hello";
char str2[20] = "World";
char str3[40];
clrscr();
getch();
return 0;
}
O\P:
Length of String1: 5
#include <stdio.h>
#include <string.h>
int main()
{
char string[20];
int i, len;
int flag = 0;
len = strlen(string);
if (flag)
{
printf("%s is not a palindrome", string);
}
else
{
printf("%s is a palindrome", string);
}
return 0;
}
O/P:
#include <stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
double x, y;
return 0;
}
O/P: