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

FUNCTIONS

The document discusses functions and pointers in C programming, detailing the structure and purpose of functions, including function declaration, definition, and calling. It explains the concepts of call by value and call by reference for passing parameters, as well as the return statement and its significance. Examples are provided to illustrate these concepts, including the use of built-in functions and recursion.

Uploaded by

vini t
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

FUNCTIONS

The document discusses functions and pointers in C programming, detailing the structure and purpose of functions, including function declaration, definition, and calling. It explains the concepts of call by value and call by reference for passing parameters, as well as the return statement and its significance. Examples are provided to illustrate these concepts, including the use of built-in functions and recursion.

Uploaded by

vini t
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CS8251- PROGRAMMING IN C

UNIT III-FUNCTIONS AND POINTERS  After the called function is executed, the control is
returned back to the calling function.
Introduction to functions: Function prototype, function definition,
function call, Built-in functions (string functions, math functions) – Why functions are needed?
Recursion – Example Program: Computation of Sine series,
Scientific calculator using built-in functions, Binary Search using
recursive functions

3.1 FUNCTIONS

 C enables programmers to break up a program into segments


commonly known as functions, facilitates each function to be Fig: Functions calling another functions
written and tested separately and it is independent of the others.
 Every function in the program is supposed to perform a well-
defined task.
 Every function interfaces to the outside world in terms of how
information is transferred to it and how results generated by it
are transmitted back.
 This interface is specified by the function name.
 main() calls a function named funcl(). Therefore, main() is known
as the calling function and func1 is known as the called function.

 The main() function calls other functions for dividing the


entire code into smaller sections. This approach is referred to
as the top-down approach.
 Understanding, coding, and testing multiple separate functions
are far easier than doing it for one big function.

 When the compiler encounters a function call (calling


function) , instead of executing the next statement in the
calling function, the control jumps to the statements that
are part of the called function.

1
CS8251- PROGRAMMING IN C
 Placing the function declaration statement prior to its
header file, enables the compiler to make a check on the
arguments used while calling that function.
 The general format for declaring a function that accepts some
arguments and returns some value as a result can be given as:

return data_type function_name(data_type variablel,


data_type variable2, ...);

 Here, function_name is a valid name for the function.


 Naming a function follows the same rules as naming
Fig: Function func1() called twice from main() variables.
 A function should have a meaningful name that must specify
 A function f that uses another function g is known as the calling the task that the function will perform.
function and g is known as the called function.  The function name is used to call it for execution in a
 The inputs that a function takes are known as program.
arguments/parameters.  Every function must have a different name that indicates
 When a called function returns some result back to the calling the particular job that the function does.
function, it is said to return that result.  return data _type specifies the data type of the value that
 The calling function may or may not pass parameters to the will be returned to the calling function as a result ofthe
called function. If the called function accepts arguments, the processing performed by the called function.
calling function will pass parameters, else it will not do so.  data_type variablel, data_type variable2 is a list of
 Function declaration is a declaration statement that identifies a variables of specified data types.
function with its name, a list of arguments that it accepts, and  These variables are passed from the calling function to the
the type of data it returns. called function. They are also known as arguments or
 Function definition consists of a function header that identifies parameters that the called function accepts to perform its
the function, followed by the body of the function containing the task.
executable code for that function.  After the declaration of every function, there should be a
semicolon.
3.1.1 Function declaration/Function prototype  If the semicolon is missing, the compiler will generate an
error message.
 Before using a function, the compiler must know about the  The function declaration is global. Therefore, the declared
number of parameters and the type of parameters that the function can be called from any point in the program.
function expects to receive and the data type of the value that  A function cannot be declared within the body of another
it will return to the calling function. function.

2
CS8251- PROGRAMMING IN C
Swaps or interchanges
Return data type Converts a character to void swap(inta, int the value of integer
uppercase. The function b); variables aand b received
char convert_to_ receives a character as an as arguments. The
uppercase (char ch); argument, converts it into function returns no value,
upper case and returns therefore the return type
the converted character is void
back to the calling
function. The function is used to
void print(void); print information on
Calculates average of screen. The function
two integer numbers a neither accepts any value
Function name
and b received as as argument nor returns
float avg (int a, int arguments. The function any value. Therefore the
b); returns a floating point return type is void and
value. the argument list contains
void data type
int find_largest(int Finds the largest of three
a, int b, int c); nos a ,b and c arguments.
An integer value which is  A function having void as its return type cannot return any
Data type of the largest of the 3 nos is value.
variable returned to the calling fn.  A function having void as its parameter list cannot accept
any value. So the function declared as
Double multiply Multiplies two floating void print(void) ;
(float a, float b); point numbers a and b or
Variable 1 That are received as void print();
arguments and returns a does not accept any input/arguments from the calling function.
double value.  If the function declaration does not specify any return
type, then by default, the function returns an integer
value. Therefore when a function is declared as
int sum(int a, int b);

3
CS8251- PROGRAMMING IN C
3.1.2 Function Definition 3.1.3 Function call

 When a function is defined, space is allocated for that function  The function call statement invokes the function.
in the memory.  When a function is invoked the compiler jumps to the called
 A function definition comprises two parts: function to execute the statements that are part of that function.
- Function header  Once the called function is executed, the program control passes
- Function body back to the calling function.
 The syntax of a function definition can be given as:  Function call statement has the following syntax:
return data_type function_name(data_type variable1, function_name (variablel, variable2, ... );
data_type variable2,….)  When the function declaration is present before the function
{ call, the compiler can check if the correct number and type of
………….. arguments are used in the function call and the returned value, if
statements any, is being used reasonably.
………….  Function name and the number and type of arguments in the
return(variable); function call must be same as that given in the function
} declaration and function header of the function definition.
 The number of arguments and the order of arguments in the  If the parameters passed to a function are more than what it is
function header must be same as that given in the function specified to accept then then the extra arguments will be
declaration statement. discarded.
 While return data_type function_name(data_type variablel,  If the parameters passed to a function are less than what it is
data_typevariable2, .. ) is known as the function header, the rest specified to accept then the unmatched arguments will be
of the portion comprising of program statements within { } is the initialized to some garbage value.
function body which contains the code to perform the specific task.  Names (and not the types) of variables in function declaration,
 The function header is same as function declaration. function call, and header of function definition may vary.
 The only difference between the two is that a function header is not  If the data type of the argument passed does not match with that
followed by a semicolon. specified inthe function declaration then either the unmatched
 The list of variables in the function header is known as the formal argument will be initialized to some garbage value or a compile
parameter list. The parameter list may have zero or more time error will be generated.
parameters of any data type.  Arguments may be passed in the form of expressions to the
 The function body contains instructions to perform the desired called function.
computation in a function.  In such cases, arguments are first evaluated and converted to
 The argument names in the function declaration and function the type of formal parameter and then the body of the
definition need not be the same. function gets executed.
 However, the data types of the arguments specified in function  The parameter list must be separated with commas.
declaration must match with that given in function definition.

4
CS8251- PROGRAMMING IN C
 If the return type of the function is not void, then the value 3.1.4 Return statement
returned by the called function may be assigned to some
variable as shown in the following statement.  The return statement is used to terminate the execution of a
variable_name = function_name(variablel, variable2, ...); function and returns control to the calling function.
 The control passes from the called function to the calling
Eg: Add two integers using functions. function when the return statement is encountered.
 A return statement may or may not return a value to the calling
#include <stdio.h> function.
int sum(int a, int b);  The syntax of return statement can be given as
int main() return<expression>;
{  Here expression is placed in between angular brackets because
intnuml, num2, total = 0; specifying an expression is optional.
printf("\n Enter the first number:”)  The value of expression, if present, is returned to the calling
scanf ("%d", &numl); function.
printf("\n Enter the second number: “);  However, in case expression is omitted, return value of the
scanf ("%d", &num2); function is undefined.
total = sum(numl, num2);  Functions that have no return statement, the control automatically
printf ("\n Total = %d", total); returns to the calling function after the last statement of the called
return 0; function is executed.
}  The return statement, like the break statement, is used to cause a
premature termination of the function.
int sum(int a, int b)
{ A function have more than one return statement
int result;
result = a + b; #include <stdio.h>
return result; #include <conio.h>
} int check_relation(int a, int b);
Output int main()
{
Enter the first number: 20 int a=3, b=5, res;
Enter the second number: 30 clrscr () ;
Total = 50 res = check_relation (a, b);

if(res==0)
printf("\n EQUAL");
if(res==l)
5
CS8251- PROGRAMMING IN C
printf("\n a is greater than b");  If the called function is supposed to modify the value of the
if(res==-l) parameters passed to it, then the change will be reflected only in
printf("\n a is less than b"); the called function.
getch();  In the calling function no change will be made to the value of
return 0; the variables. This is because all the changes were made to the
} copy of the variables and not to the actual variables.

int check_relation(int a, int b) Eg:// Call by value without return statement


{
if(a==b) #include <stdio.h>
return 0; void add(int n);
else if(a>b) int main()
return 1; {
else return -1; int num = 2;
} printf("\n The value of num before calling the function = %d", num);
add(num);
Output: printf ("\n The value of num after caJ the f'unot ion = %d", num);
a is less than b return 0;
}
3.2 PASSING PARAMETERS TO FUNCTIONS void add (int n)
{
 When a function is called, the calling function may have to n = n + 10;
pass some values to the called function. printf("\n The value of num in the calling function = %d", n) ;
 There are two ways in which arguments or parameters can be }
passed to the called function. They include:
- Call by value in which values of variables are passed by Output
the calling function to the called function. The value of numbefore calling the function=2
- Call by reference in which address of variables are The value of num in the called function = 12
passed by the calling function to the called function. The value of num after calling the function=2

3.2.1 Call by Value 3.2.1 Eg:// Call by value with return statement

 In the call by-value method, the called function creates new #include <stdio.h>
variables to store the value of the arguments passed to it. int add (int n);
 Therefore, the called function uses a copy of the actual int main()
arguments to perform its intended task. {
6
CS8251- PROGRAMMING IN C
int num = 2; - So although the called function may modify the value of the
printf ("\n The value of num calling the function = %d”, num); variables, these variables remain unchanged in the calling
num= add (num); function.
printf ("\n The value of num after calling the function = %d", nurn);
return 0; Eg: Call by Reference
}
int add(int n) #include <stdio.h>
{ void add (int *n);
n = n + 10; int main ()
printf ("\n The value of num in the called function = %d", n) ; {
return n; int num = 2;
} printf("\n The value of num before calling the function = %d",num);
Output: add (&num);
The valueof number before calling the function = 2 printf("\n The value of num after calling the function = %d", num) ;
The value of numin the called function = 12 return 0;
The valueof numafter calling the function = 12 }
void add(int *n)
While passing arguments toa function using the call-by-value {
method: *n = *n + 10;
 When arguments are passed by value, the called function printf("\n The value of num in the calledfunction = %d", *n) ;
creates new variables of the same data type as arguments }
passed to it.
 The values of the arguments passed by the function are copied Output
into the newly created variables. The value of numbefore calling the function = 2
 Arguments are called by value when the called function does not The value of num in the called function = 12
need to modify the values of the original in the calling function. The value of num after calling the function = 12
 Values of the variables in the calling function remain unaffected
when the arguments are passed using call by value technique.  When the calling function passes arguments to the
 call-by-value method of passing arguments mustbe used only in calledfunction using call-by-value method, the only way
two cases: toreturn the modified value of the argument to the caller is
- The called function does not need to modify the value of the actual explicitly using the return statement.
parameter.  The better option when a function wants to modify the value of
- It simply uses the value of to perform its task. the argument is to pass arguments using call-by-reference
- The called function should only temporarily modify the value of technique.
the variables and not permanently.  In call by reference, we declare the function parameters
as references rather than normal variables.
7
CS8251- PROGRAMMING IN C
 When this is done any changes made by the function to the swap(x, y);
arguments it receives are visible in the calling function printf("After Swapping\nx = %d\ny = %d\n", x, y);
 To indicate that an argument is passed using call by reference, return 0;
an asterisk (*) is placed after the type in the parameter list. }
 This way, changes made to the parameter in the called function void swap(int a, int b)
will then be reflected in the calling function. {
 Hence, in call-by-reference method, a function receives an implicit int temp;
reference to the argument, rather than a copy of its value. temp = b;
 Therefore, the function can modify the value of the variable and b = a;
that change will be reflected in the calling function as well. a = temp;
printf("Values of a and b is %d %d\n",a,b);
Advantages }

 Since arguments are not copied into new variables, itprovides Eg: Swapping of two values by call by reference
greater time and space efficiency.
 The called function can change the value of the argumentand the #include <stdio.h>
change is reflected in the calling function. void swap(int*, int*);
 A return statement can return only one value. In casewe need to int main()
return multiple values, pass those argumentsby reference. {
int x, y;
Disadvantages printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
 The side-effect of using this technique is that when an argument is printf("Before Swapping\nx = %d\ny = %d\n", x, y);
passed using call by address, it becomes difficult to tell whether swap(&x, &y);
that argument is meant for input, output, or both. printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
Eg: Swapping of two values by call by value }
void swap(int *a, int *b)
#include <stdio.h> {
void swap(int, int); int temp;
int main() temp = *b;
{ *b = *a;
int x, y; *a = temp;
printf("Enter the value of x and y\n"); }
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
8
CS8251- PROGRAMMING IN C
Program on function to swap the value of two variables( call by
value & by reference) Output:
In main(), a = 1 and b = 2
#include <stdio.h> In function(Call By Value Method) a=2 and b= 1
void swap_call_by_val(int, int); In main(), a = 1 and b = 2
void swap_call_by_ref(int *, int *); In main(), c = 3 and d = 4
In function(Call By Reference Method) c=4 and d = 3
int main() In main(), c = 4 and d = 3
{
int a=l, b=2, c=3, d=4;
printf("\n In main 0 , a = %d and b %d",a, b); Eg: Greatest of 3 nos by using functions
swap_call_by_val(a, b); #include<stdio.h>
printf ("\n In main( ), a = %d and b = %d",a, b); int greater(int a, int b, int c);
printf ("\n In main (),c = %d and d =%d", c , d) int main()
swap_call_by_ref (&c, &d) ; {
printf("\n In main() , c = %d and d =%d", c , d); intnuml, num2, num3, large;
return 0; printf("\n Enter the first number :”);
} scanf("%d", &numl);
void swap_call_by_val(int a, int b) printf ("\n Enter the second number :”);
{ scanf ("%d", &num2);
int temp; printf("\n Enter the third number :”);
temp = a; scanf ("%d", &num3);
a = b; large = greater(numl, num2,num3);
b = temp; printf("\n Largest number = %d”,large);
printf("\n In function (Call By ValueMethod) a = %d and b = %d", return 0;
a, b); }
} int greater(int a, int b, int c)
void swap_call_by_ref(int*c, int*d) {
{ if(a>b && a>c)
int temp; return a;
temp = *c; if (b>a && b>c)
*c = *d; return b;
*d = temp; else
printf("\n In function (Call By ReferenceMethod) c = %d and d = return c;
%d",*c,*d); }
}
9
CS8251- PROGRAMMING IN C
Output: printf("\nEnter size of an array: ");
scanf("%d", &n);
Enter the first number : 45 printf("\nEnter elements of an array:\n");
Enter the second number: 23 for(i=0; i<n; i++)
Enter the third number: 34 scanf("%d", &a[i]);
Largest number = 45 selection_sort();
printf("\n\nAfter sorting:\n");
Eg: area of a circle using functions for(i=0; i<n; i++)
#include<stdio.h> printf("\n%d", a[i]);
float cal_area(float r); getch();
int main () }
{ void selection_sort()
float area, radius; {
printf ("\n Enter the radius of the circle: "); int i, j, min, temp;
scanf("%f", &radius); for (i=0; i<n; i++)
area = cal_area (radius) ; {
printf("\n Area of the circle with radius %f = %f", radius, area) ; min = i;
return 0; for (j=i+1; j<n; j++)
} {
float cal_area(float radius) if (a[j] < a[min])
{ min = j;
return (3.14 * radius * radius); }
} temp = a[i];
a[i] = a[min];
Output: a[min] = temp;
Enter the radius of the circle: 7 }
Area of the circle with radius 7=153.83 }

Eg: Selection sort Output:


Enter size of an array:5
#include <stdio.h> Enter elements of an array:
void selection_sort(); 56
int a[30], n; 18
void main() 26
{ 11
int i; 22
10
CS8251- PROGRAMMING IN C
After sorting: printf (" \n Factorial of a number is %d”,factorial) ;
11 return 0;
18 }
22 int fact(int n)
26 {
56 if (n==1)
return 1;
3.3 RECURSIVE FUNCTIONS else
return (n*fact(n-1));
 A recursive function is defined as a function that calls }
itself to solve a smaller version of its task until a final o/p:
call is made which does not require a call to itself. Enter the number: 5
 Base case, in which the problem is simple enough to be Factorial of a number is 120
solved directly without making any further calls to the
same function . Eg: GCD by using recursion
 Recursive case, in which first the problem at hand is
divided into simpler sub-parts. Second the function #include<stdio.h>
calls itself but with sub-parts of the problem obtained in int gcd(int, int) ;
the first step. Third, the result is obtained by combining int main()
the solutions of simpler sub-parts. {
 Every recursive function must have a base case and a int numl, num2, res;
recursive case. printf("\n Enter the two numbers: ”);
 The base case of a recursive function acts as the terminating scanf (“%d %d", &numl, &num2) ;
condition. So, in the absence of an explicitly-defined base res = gcd(numl,num2) ;
case, a recursive function would call itself indefinitely. printf("\n GCD 0f %d and %d = %d", numl, num2, res) ;
return 0;
Eg: Factorial using recursion }

#include <stdio.h> int gcd(int x, int y)


int fact(int) ; {
int main() int rem;
{ rem =x%y;
int num, factorial; if (rem==0)
printf ("\n Enter the number ”); return y;
scanf ( "%d", &num); else
factorial = fact(num) ; return (gcd(y, rem));
11
CS8251- PROGRAMMING IN C
} 0 1 1 2 3

o/p: Eg: Sum of n natural nos using recursion


Enter the two numbers: #include <stdio.h>
82 6 int sum(int n);
GCD of 82 and 6 =2 int main()
{
Eg: Fibonacci using recursion int num;
printf("Enter a positive integer:");
#include <stdio.h> scanf("%d",&num);
int fib(int); printf("Sum = %d",sum(num));
int main() return 0;
{ }
int n, i = 0, res;
printf("Enter the number of terms\n"); int sum(int n)
scanf("%d" ,&n) ; {
printf("Fibonacci series\n"); if(n != 0)
for(i = 0; i < n; i++ ) return n + sum(n-1);
{ else
res = fib(i); return n;
printf("%d\t" ,res) ;
} }
return 0; o/p:
} Enter a positive number : 5
int fib(int n) Sum = 15
{
if (n==0) Eg:Binary search using recursion
return 0; #include <stdio.h>
else if(n==1) int main(void)
return 1; {
else int a[100];
return(fib(n-1) + fib(n-2) ); int len, pos, search_item;
} printf("Enter the length of the array\n");
o/p: scanf("%d", &len);
Enter the number of terms: 5 printf("Enter the array elements\n");
Fibonacci series
12
CS8251- PROGRAMMING IN C
for (int i=0; i<len; i++)
Eg: Scientific Calculator using built in functions
scanf("%d", &a[i]);
printf("Enter the element to search\n"); #include<stdio.h>
scanf("%d", &search_item); #include<conio.h>
pos = binarysearch(a,0,len-1,search_item); #include<math.h>
if (pos < 0 ) int main(void)
printf("Cannot find the element %d in the array.\n", {
search_item); int choice, i, a, b;
else
float x, y, result;
printf("The position of %d in the array is %d.\n", search_item,
clrscr();
pos+1);
do
return 0;
{
}
printf(“\nSelect your operation (0 to exit):\n”);
printf(“1. Addition\n2. Subtraction\n3.Multiplication\n4. Division\n”);
int binarysearch(int a[], int low, int high, int x)
printf(“5. Square root\n6. X ^ Y\n7. X ^ 2\n8. X ^ 3\n”);
{
printf(“9. 1 / X\n10. X ^ (1 / Y)\n11. X ^ (1 / 3)\n”);
int mid = (low + high) / 2;
printf(“12. 10 ^ X\n13.X!\n14. %\n15.log10(x)\n16. Modulus\n”);
if (low > high) return -1;
printf(“17. Sin(X)\n18.Cos(X)\n19. Tan(X)\n20. Cosec(X)\n”);
if (a[mid] == x) return mid;
printf(“21. Cot(X)\n22. Sec(X)\n”);
if (a[mid] < x)
printf(“Choice: “);
return binarysearch(a, mid + 1, high, x);
scanf(“%d”, &choice);
else
if(choice == 0) exit(0);
return binarysearch(a, low, mid-1, x);
switch(choice)
}
{
case 1:
o/p: printf(“Enter X: “);
scanf(“%f”, &x);
Enter the length of the array 5 printf(“\nEnter Y: “);
Enter the array elements scanf(“%f”, &y);
10 result = x + y;
20 printf(“\nResult: %f”, result);
30 break;
40 case 2:
50 printf(“Enter X: “);
The position of 40 in the array is 4. scanf(“%f”, &x);
13
CS8251- PROGRAMMING IN C
printf(“\nEnter Y: “); case 7:
scanf(“%f”, &y); printf(“Enter X: “);
result = x – y; scanf(“%f”, &x);
printf(“\nResult: %f”, result); result = pow(x, 2);
break; printf(“\nResult: %f”, result);
case 3: break;
printf(“Enter X: “); case 8:
scanf(“%f”, &x); printf(“Enter X: “);
printf(“\nEnter Y: “); scanf(“%f”, &x);
scanf(“%f”, &y); result = pow(x, 3);
result = x * y; printf(“\nResult: %f”, result);
printf(“\nResult: %f”, result); break;
break; case 9:
case 4: printf(“Enter X: “);
printf(“Enter X: “); scanf(“%f”, &x);
scanf(“%f”, &x); result = pow(x, -1);
printf(“\nEnter Y: “); printf(“\nResult: %f”, result);
scanf(“%f”, &y); break;
result = x / y; case 10:
printf(“\nResult: %f”, result); printf(“Enter X: “);
break; scanf(“%f”, &x);
case 5: printf(“\nEnter Y: “);
printf(“Enter X: “); scanf(“%f”, &y);
scanf(“%f”, &x); result = pow(x, (1/y));
result = sqrt(x); printf(“\nResult: %f”, result);
printf(“\nResult: %f”, result); break;
break; case 11:
printf(“Enter X: “);
case 6: scanf(“%f”, &x);
printf(“Enter X: “); y = 3;
scanf(“%f”, &x); result = pow(x, (1/y));
printf(“\nEnter Y: “); printf(“\nResult: %f”, result);
scanf(“%f”, &y); break;
result = pow(x, y); case 12:
printf(“\nResult: %f”, result); printf(“Enter X: “);
break; scanf(“%f”, &x);
14
CS8251- PROGRAMMING IN C
result = pow(10, x); printf(“Enter X: “);
printf(“\nResult: %f”, result); scanf(“%f”, &x);
break; result = sin(x * 3.14159 / 180);
case 13: printf(“\nResult: %.2f”, result);
printf(“Enter X: “); break;
scanf(“%f”, &x); case 18:
result = 1; printf(“Enter X: “);
for(i = 1; i <= x; i++) scanf(“%f”, &x);
{ result = cos(x * 3.14159 / 180);
result = result * i; printf(“\nResult: %.2f”, result);
} break;
printf(“\nResult: %.f”, result); case 19:
break; printf(“Enter X: “);
case 14: scanf(“%f”, &x);
printf(“Enter X: “); result = tan(x * 3.14159 / 180);
scanf(“%f”, &x); printf(“\nResult: %.2f”, result);
printf(“\nEnter Y: “); break;
scanf(“%f”, &y); case 20:
result = (x * y) / 100; printf(“Enter X: “);
printf(“\nResult: %.2f”, result); scanf(“%f”, &x);
break; result = 1 / (sin(x * 3.14159 / 180));
case 15: printf(“\nResult: %.2f”, result);
printf(“Enter X: “); break;
scanf(“%f”, &x); case 21:
result = log10(x); printf(“Enter X: “);
printf(“\nResult: %.2f”, result); scanf(“%f”, &x);
break; result = 1 / tan(x * 3.14159 / 180);
case 16: printf(“\nResult: %.2f”, result);
printf(“Enter X: “); break;
scanf(“%d”, &a); case 22:
printf(“\nEnter Y: “); printf(“Enter X: “);
scanf(“%d”, &b); scanf(“%f”, &x);
result = a % b; result = 1 / cos(x * 3.14159 / 180);
printf(“\nResult: %d”, result); printf(“\nResult: %.2f”, result);
break; break;
case 17: default:
15
CS8251- PROGRAMMING IN C
printf(“\nInvalid Choice!”); o/p:
}
} while(choice); Enter the value for x : 90
Enter the value for n : 2
getch(); The value of Sin(1.570795) = 1.0045
return 0;
}
Eg: Computation of sine series

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i, n;
float x, sum, t;
clrscr();

printf(" Enter the value for x : ");


scanf("%f",&x);

printf(" Enter the value for n : ");


scanf("%d",&n);
x=x*3.14159/180;
t=x;
sum=x;
for(i=1;i<n+1;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf(" The value of Sin(%f) = %.4f",x,sum);
getch();
}

16

You might also like