0% found this document useful (0 votes)
49 views40 pages

APIC Ch-2 Theory+Practicals-1

1) Functions in C allow programmers to break programs into smaller, reusable parts to perform specific tasks. There are three parts to a function: the declaration, definition, and call. 2) The declaration specifies the return type and parameters. The definition contains the actual code or logic. The call executes the function. 3) Some key advantages of functions are modularity, reusability, and dividing large programs into smaller, more manageable pieces.

Uploaded by

Vekariya Kartik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views40 pages

APIC Ch-2 Theory+Practicals-1

1) Functions in C allow programmers to break programs into smaller, reusable parts to perform specific tasks. There are three parts to a function: the declaration, definition, and call. 2) The declaration specifies the return type and parameters. The definition contains the actual code or logic. The call executes the function. 3) Some key advantages of functions are modularity, reusability, and dividing large programs into smaller, more manageable pieces.

Uploaded by

Vekariya Kartik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Ch-2 Functions in C

Introduction:

 Function is a sub part of a program used to perform a


specific task and is executed individually.
 Every C program must contain at least one function
called main(). However a program may also contain other
functions.
 Every function in C has the following:
1) Function Declaration (Function Prototype)
2) Function Definition
3) Function Call

1) Function Declaration:

The function declaration tells the compiler about function


name, the datatype of the return value and parameters.
The function declaration is also called a function
prototype. The function declaration is performed before
the main function or inside the main function or any other
function.

 Function declaration syntax:

returnTypefunctionName(parameterList);
2) Function Definition:

 The function definition provides the actual code of that


function. The function definition is also known as the
body of the function. The actual task of the function is
implemented in the function definition. That means the
actual instructions to be performed by a function are
written in function definition. The actual instructions of a
function are written inside the braces ‘{}’. The function
definition is performed before the main function or after
the main function.

 Function definition syntax:

returnTypefunctionName(parameterList);
{
Actual code….
}

3) Function Call:

The function call tells the compiler when to execute the


function definition. When a function call is executed the
execution control jumps to the function definition where
the actual code gets executed and returns to the same
function call once the execution completes. The function
call is performed inside the main function or any other
function or inside the function itself.
 Function call syntax:

functionName(parameters);

Advantages of Functions:

 Using functions we can implement modular


programming.
 Functions make the program more readable and
understandable.
 Using functions the program implementation becomes
easy.
 Once a function is created it can be used many times
(code reusability).
 Using functions larger programs can be divided into
smaller modules.

Types of Functions in C:

 In C programming language based on


providing the function definition functions
are divided into two types those are as
follows:
 System Defined Function
 User Defined Function
 System Defined Function:

 The C programming language provides pre-defined


functions make programming easy. These predefined
functions are known as system defined functions. The
system defined functions is defined as follows:

 The function whose definition is defined by the system is


called as system defined functions.

 The system defined functions are also called as Library


functions or Standard functions or Predefined functions.
The implementation of system defined functions is
already defined by the system. In C all the system defined
functions are defined inside the header files like stdio.h,
conio.h, math.h, string.h, etc., For example the functions
printf() & scanf() are defined functions in the program we
must include the respective header file using #include
statement. For example, if we use a system defined
function as sqrt() in the program we must include the
header file called math.h because the function sqrt() is
defined in math.h.
Points to be remembered:

 System defined functions are declared in the header


files.
 System defined functions are implemented in .dll
files. (DLL stands for Dynamic Link Library).
 To use system defined functions the respective header
file must be included.

C String Functions:

1) Strlen(string_name): returns the length of string


name.
2) Strcpy(destination, source):Copies the content of
source string to destination string.
3) Strcat(first_string, second_string): Concates or joins
first string with second string. The result of the string
is stored in first string.
4) Strcmp(first_string, second_string): Compares first
string with second string. If both the srings are same,
it returns 0.
5) Strrev(string): returns reverse string.
6) Strlwr(string): returns string characters in lowercase.
7) Strupr(string): returns string characters in uppercase.

 The strlen() function returns the length of the given


string it doesn’t count null character ‘\0’.
#include<stdio.h>
#include <string.h>
int main()
{
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}

Output:

Length of string is: 10

C Math Functions:

 C programming allows us to perform mathematical


operations through the functions defined in <math.h>
header file. The <math.h> header file contains various
methods for performing mathematical operations such
as sqrt(), pow(), ceil(), floor(), etc.

 There are various methods in <math.h> header file.


The commonly used functions of <math.h> header
file are given below:

1) Ceil(number): rounds up the given number it


returns the integer value which is greater than or
equal to given number.
2) Floor(number): rounds down the given number it
returns the integer value which is less than or equal
to given number.
3) Sqrt(number): returns the square root of given
number.
4) Pow(base, exponent): returns the power of the
given number.
5) Abs(number): returns the absolute value of given
number.

Let’s see a simple example of math functions found


in <math.h> header file:

#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

Console Input Output functions:

In C language it has a collection of functions that can


be used in a program with the required number of
arguments written in parenthesis. Console Input
Output functions also include in the user program by
using the header file <stdio.h> which stands for
Standard Input Output header. Keyboard and screen
together is say console.

There are major two types of console input output


functions in C programming language:
1) Formatted Input Output Functions
2) Character Input Output Functions
1) Formatted Input Output Functions:

i) Scanf() Function:

It is used to read/input values of variables using


the standard input device such as keyboard. It
has the following syntax:
scanf(“Format String”, &var1, &var2,…&varn);
Where Format String is the control string
which represents the format specifications, the
symbol & which represent the memory address
where the variable value is to be stored.

Data type Format Meaning


1) Int %d Represents a decimal integer value
%u Represents an unsigned integer value
%o Represents an unsigned octal value
%x Represents an unsigned hexadecimal
value
2) float %f Represents a floating value
%e Represents a floating value in decimal
or exponential form
3) char %c Represents a single character value
%s Represents a string value of characters
ii) printf() Function:

It is used to print/display values of variables


using the standard output device such as
monitor. It has the following syntax:
printf(“Format String”, var1, var2,…varn);
2) Character Input Output Functions:
i) getch() Function:
 It is used to read a character from the keyboard
and it does not accept the enter key press. The
following syntax is for getch() function:
ch=getch();where ‘ch’ is the character variable.
 Example:
char ctr;
ctr=getch();
ii) getchar() Function:
It is used to read one character at a time from
the standard input device such as keyboard. It
has the following syntax:
ch=getchar();//where ‘ch’ is the character variable.
iii) putchar() Function:
It is used to display one character at a time on
the monitor screen. It has the following syntax:
putchar(ch);//where ‘ch’ is the character variable.
Example:
char c= ‘M’;
putchar(c);
iv) putch() Function:
It is used to display one character on the monitor
screen. It has the following syntax:
putch(ch);//where ‘ch’ is the character variable.
Example:
char c= ‘s’;
putch(c);
v) getche() Function:
It is used to read a character from the keyboard
without expecting the enter key press It has the
following syntax:
ch=getche();//where ‘ch’ is the character variable.
Example:
char ctr;
ctr=getche();
vi) gets() Function:
It is used to read a string of characters including
white spaces. It has the following syntax:
gets(str);//where ‘str’ is the character string variable.
Example:
char str[15];
gets(str);
vii) puts() Function:
It is used to display a character string on the
monitor screen. It has the following syntax:
puts(str);//where ‘str’ is a string(array of characters).
Example:
char str[20]= “Hello World”;
puts(str);
vii) clrscr() Function:
It is used to display clear the monitor screen. It
has the following syntax:
clrscr();
Note: The header file <conio.h> must include
using the function in a program.

 Ctype.h:

 The ctype.h header file of the C standard library


declares several functions that are useful for testing
and mapping characters.
 All functions accepts int as a parameter whose value
must be EOF or representable as an unsigned
character.
 All the functions return non zero (true) if the
argument c satisfies the condition described and zero
(false) if not.

 Library Functions:

1) int isalnum(int c): This function checks whether the


passed character is alphanumeric.
2) int isalpha(int c): This function checks whether the
passed character is alphabetic.
3) int iscntrl(int c): This function checks whether the
passed character is control character.
4) int isdigit(int c): This function checks whether the
passed character is decimal digit.
5) int isgraph(int c): This function checks whether the
passed character has graphical representation using
local.
6) int islower(int c): This function checks whether the
passed character is lowercase letter.
7) int isprint(int c): This function checks whether the
passed character is printable.
8) int ispunct(int c): This function checks whether the
passed character is a punctuation character.
9) int isupper(int c): This function checks whether the
passed character is uppercasecase letter.
10) int isxdigit(int c): This function checks whether the
passed character is hexadecimal digit.
 The library also contains two conversion functions
that accepts and returns an “int”.

1) int tolower(int c): This function converts


uppercase letters to lowercase.
2) int toupper(int c): This function converts
lowercase letters to uppercase.

Character Classes:

1) Digits: This is a set of whole


numbers{0,1,2,3,4,5,6,7,8,9}.
2) Hexadecimal digits: This is a set of hexadecimal
digits
{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,a,b,c,d,e,f}.
3) Lowercase digits: This is a set of lowercase letters
{a,b,c,d,e,f,g,h,I,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}.
4) Uppercase digits: This is a set of uppercase
letters{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,
U,V,W,X,Y,Z}.
5) Letters: This is a set of lowercase and uppercase
letters.
6) Alphanumeric characters: This is a set digits,
lowercase and uppercase letters.
7) Punctuation characters: This is a set of punctuation
characters.
! “ # $ % * ‘ () * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
8) Graphical characters: This is a set of alphanumeric
and punctuation characters.
9) Space characters: This is a set of tab, newline,
vertical tab, form feed, carriage return, and space.
10) Printable characters: This is a set of alphanumeric
characters, punctuation characters and space
characters.
11) Control characters: In ASCII, these characters
have octal codes 000 through 037, and 177 (DEL).
12) Blank characters: These are space and tabs.
13) Alphabetic Characters: This is a set of lowercase
and uppercase letters.

User Defined Functions:

In C programming language users can also create


their own functions. The functions that are created
by users are called as user defined functions. The
user defined functions is defined as follows:
The function whose definition is defined by the user
is called as user defined function.
That means the function that is implemented by user
is called as user defined function. For example the
function main is implemented by user so it is called
as user defined function.
In C every user defined function must be declared
and implemented. Whenever we make functions call,
the function definition gets executed.
There can be four different types of user defined
functions, they are:
1. Function with no arguments and no return value.
2. Function with no arguments and a return value.
3. Function with arguments and no return value.
4. Function with arguments and a return value.

1. Function with no arguments and no return


value:

Such functions can either be used to display


information or they are completely dependent
on user inputs.
Below is an example of a function which takes
2 numbers as input from user and display which
is the greater number.
#include<stdio.h>
void greatNum(); //function declaration
int main()
{
greatNum(); //function call
return();
}

void greatNum(); //function definition


int i,j;

printf(“Enter 2 numbers that you want to


compare….”);
scanf(“%d%d”,&i,&j);
if(i>j)
{
printf(“The greatest number is: %d”,i);
}
else
{
printf(“The greater number is: %d”,j);
}
}

2. Function with no arguments and no return


value:

We have modified the above example to make the


function greatNum()return the number which is
greater amongst the numbers.
#include<stdio.h>
int greatNum(); //function declaration
int main()
{
int result;
result=greatNum(); //function call
printf(“The greater number is:
%d”,result);
return 0;
}

int greatNum(); //function definition


{
int i,j,greatNum;
printf(“Enter 2 numbers that you want to
compare….”);
scanf(“%d%d”, &i,&j);

if(i>j)
{
greaterNum=i;
}
else
{
greaterNum is=j;
}
//returning the result
return greaterNum;\

3. Function with no arguments and no return


value:

We are using the same function as example again


and again to demonstrate that to solve a problem
there can be many different ways. This time, we
have modified the above example to make the
function greatNum() take two int values as
argument, but it will not be returning anything.
#include<stdio.h>
void greatNum(int x, int y); //function declaration
int main()
{
int i,j;
printf(“Enter 2 numbers that you want to
compare….”);
scanf(“%d%d”,&i,&j);
greatNum(i,j);//function call
return 0;
}

void greatNum(int x, int y);//function


declaration
{
if(x>y)
{
printf(“The greatest number is: %d”, x);
}
else
{
printf(“The greater number is: %d”, y);
}
}

4. Function with arguments and a return value:

This is the best type, as this makes the function


completely independent of inputs and outputs and
only the logic is defined inside the function body.
#include<stdio.h>
int greatNum(int a, int b); //function declaration
int main()
{
int i,j,result;
printf(“Enter 2 numbers that you want to
compare….”);
scanf(“%d%d”,&i,&j);
result=greatNum(i,j);//function call
printf(“The greater number is: %d”, result);
return 0;
}

int greatNum(int x, int y);//function definition


{
if(x>y)
{
return x;
}
else
{
return y;
}
}

 Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, result;
int addition(int, int); //function declaration
clrscr();

printf(“Enter 2 numbers that you want to


compare….”);
scanf(“%d%d”,&num1, &num2);
result=addition(num1, num2);//function call
printf(“Sum= %d”,result);
getch ();
}

int addition(int a, int b); //function definition


{
return a+b;
}

In the about example program the function


declaration statement “int addition(int,int) tells
the compiler that there is a function with the name
addition which takes two integer values as
parameters and returns an integer value. The
function call statement takes the execution control
to the addition() definition along with the values
of num1, num2. Then function definition executes
the code written inside it and comes back to the
function call along with return value.
In the concept of functions the function call is
known as “Calling Function” and the function
definition is known as “Called Function”.
 Write difference between Parameters
and Arguments:

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:

There are two methods to pass parameters from


calling function to called function and they are as
follows:
1) Call by value
2) Call by Reference
1) Call by value:

In call by value parameter passing method, the


copy of actual parameter values are copied to
formal parameters and these formal parameters
are used in called function.
The changes made on the formal parameter does
not affect the values of actual parameter that
means after the execution control comes back to
the calling function the actual parameter values
remain same.
For example consider the following program:

#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();
}

void swap(int a, int b)


{
int temp;
temp=a; a=b; b=temp;
}

1) Call by reference:

In call by reference parameter passing method


the memory location address of the actual
parameter is copied to formal parameters.
This address is used to access the memory
location of the actual parameters is called
function.
In this method of parameter passing, the formal
parameters must be pointer variables.
The changes made on the formal parameter
affect the values of actual parameters.
For example consider the following program:

#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();
}

void swap(int* a, int* b)


{
int temp;
temp=*a; *a=*b; *b=temp;
}

 Recursive Functions In C:

In C programming language, function calls can be


made from the main function, other functions or from
the same function itself.
The recursive is defined as follows:
A function called by itself is called recursive
function.
For example consider the following program:
#include<stdio.h>
#include<conio.h>
{
int fact,n;
printf(“Enter any positive integer: ”);
scanf(“%d”,&n);
fact=factorial(n);
printf(“\n Factorial of %d is %d\n”,n,fact);
return 0;
}
int factorial(int n)
{
int temp;
if(n==0)
return 1;
else
temp=n*factorial(n-1);//recursive function call
return temp;

O/P:

Enter any positive integer: 5


Factorial of 5 is: 120

 Advantages of Recursion:

1) The code maybe easier to write.


2) To solve such problems which are naturally recursive
such as tower of Hanoi.
3) Reduce unnecessary calling of function.
4) Extremely useful when applying the same solution.
5) Recursion reduces the length of code.
6) It is very useful in solving the data structure problem.
7) Stacks evolutions and infix, prefix, postfix
evaluations etc.

 Disdvantages of Recursion:

1) Recursive functions are generally slower than non


recursive functions.
2) It may require a lot of memory space to hold
intermediate results on the systems stacks.
3) Hard to analyze or understand the code.
4) It is not more efficient in terms of space and time
complexity.
5) The computer may run out of memory if the
recursive calls are not properly checked.

Ch-2 Practicals:

P-2.1: Write a C program to sum two given integer numbers using


function

#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:

Enter the value of A: 2

Enter the value of B: 1

Sum of A & B: 3

P-2.2: Write a C program to area of circle using function.

#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.

// Online C compiler to run C program online


#include <stdio.h>
#include<conio.h>
void num(int n);
void main()
{
int n;
clrscr();
printf ("Enter the value of n: ");
scanf ("%d", &n);
num(n);
getch();
}
void num(int n)
{
int sum=0,i;
for(i=1;i<=n;i++)
{
sum+=i;
}
printf("The sum of first %d numbers is: %d",n,sum);
}

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);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);


swap(x, y);
return 0;
}

void swap(int a, int b)


{
int temp;
temp = b;
b = a;
a = temp;
printf("After Swapping\na = %d\nb = %d\n", a, b);
}

O/P:
Enter the value of x and y
10
20
Before Swapping
x = 10
y = 20
After Swapping
x = 20
y = 10

P-2.5: Write a C program to swap two variable values using call by


reference.

#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;

printf("Enter the value of a and b: \n");


scanf("%d%d",&a,&b);

printf("Before swapping the values in main:\n a=%d\nb=%d\n",a,b);

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:

Enter the value of a and b:


10
20
Before swapping the values in main:
a=10
b =0
After swapping values in function:
a=0
b=10
After swapping values in main:
a=0
b=10

P-2.6: Write a C program to find factorial of given number using


recursion.

#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);

printf("Maximum number out of %d, %d and %d is %d\n", a, b, c,


max(a, b, c));

return 0;
}

int max(int x, int y, int z)


{
if(x > y && x > z)
{
return x;
}
else
{
if(y > z)
return y;

else
return z;
}
}
O/P:

Enter three numbers:


10
11
20
Maximum number out of 10, 11 and 20 is 20

P-2.8: Write a C program to sum of odd numbers between 1 to n where n


is entered through keyboard.

#include <stdio.h>
#include<conio.h>
int main()
{
int i, n, sum=0;
printf("Enter value of n: ");
scanf("%d", &n);

for(i=1; i<=n; i+=2)


{
sum += i;
}

printf("Sum of odd numbers = %d", sum);

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();

//strlen - calculates the length of a string


printf("Length of String1: %d\n\n", strlen(str1));

//strcat - concatenates two strings


strcat(str1, str2);
printf("Concatenated string: %s\n\n", str1);

//strcpy - copies a string to another string


strcpy(str3, str1);
printf("Copied string: %s\n\n", str3);

//strcmp - compares two strings


if(strcmp(str1,str2)==0)
{
printf("Strings are equal.\n\n");
}
else
{
printf("Strings are not equal.\n\n");
}

//strrev - reverses the strings


printf("Enter a string: %s");
gets(str3);
strrev(str3);
printf("After reversing: %s\n\n", str3);

//strlwr - lowercase the characters of string


printf("Enter a string: %s");
gets(str3);
printf("Lowercase string: %s\n\n",strlwr(str3));

//strupr - uppercase the characters of string


printf("Enter a string: %s");
gets(str3);
printf("Uppercase string: %s\n\n",strupr(str3));

getch();
return 0;
}

O\P:

Length of String1: 5

Concatenated string: HelloWorld

Copied string: HelloWorld

Strings are not equal.

Enter a string: Haashim


After reversing: mihsaaH

Enter a string: HAASHIM


Lowercase string: haashim

Enter a string: mirza


Uppercase string: MIRZA
P-2.10: Write a C program to find out given string is palindrome or not.

#include <stdio.h>
#include <string.h>
int main()
{
char string[20];
int i, len;
int flag = 0;

printf("Enter a string: ");


scanf("%s", string);

len = strlen(string);

for(i=0;i < len ;i++)


{
if(string[i] != string[len-i-1])
{
flag = 1;
break;
}
}

if (flag)
{
printf("%s is not a palindrome", string);
}
else
{
printf("%s is a palindrome", string);
}
return 0;
}
O/P:

Enter a string: Wow


Wow is a palindrome

P-2.11: Write a C program to illustrate the use of various built-in math


functions.

#include <stdio.h>
#include<conio.h>
#include <math.h>
int main()
{
double x, y;

//Demonstrating square root function


printf("Enter a number to find the square root: ");
scanf("%lf", &x);
y = sqrt(x);
printf("Square root of %.2lf is %.2lf\n\n",x,y);

//Demonstrating power function


printf("Enter a number to raise to a power: ");
scanf("%lf", &x);
printf("Enter the power to raise the number to: ");
scanf("%lf", &y);
double result = pow(x, y);
printf("%.2lf raised to the power of %.2lf is %.2lf\n\n",x,y, result);

//Demonstrating absolute value function


printf("Enter a number to find the absolute value: ");
scanf("%lf", &x);
y = fabs(x);
printf("Absolute value of %.2lf is %.2lf\n\n",x,y);

//Demonstrating ceil function


printf("Enter a number to round: ");
scanf("%lf", &x);
y = ceil(x);
printf("Rounded value of %.2lf is %.2lf\n\n",x,y);

//Demonstrating floor function


printf("Enter a number to round: ");
scanf("%lf", &x);
y = floor(x);
printf("Rounded value of %.2lf is %.2lf\n\n",x,y);

return 0;
}

O/P:

Enter a number to find the square root: 4


Square root of 4.00 is 2.00

Enter a number to raise to a power: 2


Enter the power to raise the number to: 3
2.00 raised to the power of 3.00 is 8.00

Enter a number to find the absolute value: -12.3


Absolute value of -12.30 is 12.30

Enter a number to round: 3.2


Rounded value of 3.20 is 4.00

Enter a number to round: 4.5


Rounded value of 4.50 is 4.00

You might also like