UNIT - 5 FUNCTIONS AND POINTERS
FUNCTION <ul><li>Functions is a sub-program that contains one or more statements and it performs some task when called. </...
Types Functions User-Defined Functions  Pre-Defined Functions
Pre-Defined Functions  <ul><li>The pre-defined functions or library functions are built-in functions. </li></ul><ul><li>Th...
User-Defined Functions  <ul><li>The functions defined by the user for their requirement are called user-defined functions....
Advantage of User-Defined Functions  <ul><li>The length of the source program can be reduced. </li></ul><ul><li>It is easy...
Elements of User-Defined Function <ul><li>Function declaration </li></ul><ul><li>Function definition </li></ul><ul><li>Fun...
Function <ul><li>Syntax </li></ul><ul><li>datatype  function_name (parameters list)  </li></ul><ul><li>{ </li></ul><ul><li...
How Function Works  <ul><li>Once a function is called the control passes to the called function. </li></ul><ul><li>The wor...
 
Parameters <ul><li>Actual Parameter </li></ul><ul><li>These  are the parameters transferred from the calling function to t...
 
return Statement <ul><li>The  return  statement may or may not send some values to the calling function. </li></ul><ul><li...
Function Prototypes <ul><li>Function with no arguments and no return values. </li></ul><ul><li>Function with arguments and...
Function with no arguments and no return values <ul><li>Here no data  transfer take place between the calling function and...
 
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main()  //calling function <...
Output <ul><li>Enter two number:3 </li></ul><ul><li>4 </li></ul><ul><li>Sum is:7 </li></ul>
Function with arguments and no return values <ul><li>Here data  transfer take place between the calling function and the c...
 
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ <...
Output <ul><li>Enter two number:2 </li></ul><ul><li>4 </li></ul><ul><li>Sum is:6 </li></ul>
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ <...
Output <ul><li>Enter two number:2 </li></ul><ul><li>4 </li></ul><ul><li>Sum is:6 </li></ul>
Function with arguments and return values <ul><li>Here data  transfer take place between the calling function and the call...
 
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ <...
Output <ul><li>Enter two number:6 </li></ul><ul><li>7 </li></ul><ul><li>Sum is:13 </li></ul>
Function with no arguments and with return values <ul><li>Here data  transfer take place between the called function and t...
 
<ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul...
Output <ul><li>Enter two number:5 </li></ul><ul><li>8 </li></ul><ul><li>Sum is:13 </li></ul>
Parameter Passing Methods <ul><li>Call by value </li></ul><ul><li>Call by reference </li></ul>
Call by value <ul><li>Actual argument passed to the formal argument. </li></ul><ul><li>Any changes to the formal argument ...
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ <...
<ul><li>change(x,y); </li></ul><ul><li>printf(&quot;

Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} </l...
Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Fuction -->x=6,y=5 ...
Call by reference <ul><li>Instead of passing value, the address of the argument will be passed. </li></ul><ul><li>Any chan...
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ <...
<ul><li>change(&x,&y); </li></ul><ul><li>printf(&quot;

Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} <...
Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Function -->x=6,y=5...
Recursion <ul><li>It is a process of calling the same function itself again and again until some condition is satisfied. <...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
<ul><li>int rec(int x) </li></ul><ul><li>{ </li></ul><ul><li>int f; </li></ul><ul><li>if(x==1) </li></ul><ul><li>return(1)...
Example: Working of 3!
Tower of Honoi 1 2 3 3 2 3 1 3 2 3
Tower of Honoi 1 2 3 3 2 3 1 3 2 3
Library Function <ul><li>It is pre-defined function. </li></ul><ul><li>The library function provides functions like mathem...
Example <ul><li>sqrt(x): </li></ul><ul><li>It is used to find the square root of  x </li></ul><ul><li>Example:  sqrt(36)  ...
<ul><li>rand(): </li></ul><ul><li>It is used to generate a random number. </li></ul><ul><li>sin(x): </li></ul><ul><li>It i...
<ul><li>toascii(x): </li></ul><ul><li>It is used to find the ASCII value of  x </li></ul><ul><li>Example:  toascii(a)  is ...
Example: <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<math.h> </li></ul><ul><l...
<ul><ul><li>printf(&quot;
The ceiling of 6.7 is %f&quot;,ceil(6.7)); </li></ul></ul><ul><ul><li>printf(&quot;
The floor of...
Output: <ul><li>Enter the number:6 </li></ul><ul><li>The squareroot of 6 is 2.449490 </li></ul><ul><li>The value of 6 powe...
Array <ul><li>An Array is a collection of similar data items, that are stored under a common name. </li></ul><ul><li>Types...
One-Dimensional array  Array Declaration <ul><li>Syntax: </li></ul><ul><li>data_type array_name[size]; </li></ul><ul><li>E...
Array initialization <ul><li>At compile time </li></ul><ul><li>At run time </li></ul>
At compile time <ul><li>Syntax: </li></ul><ul><li>data_type array_name[size]={variables}; </li></ul><ul><li>Example:  int ...
At Run time <ul><li>Array can also initialize at the run time. </li></ul><ul><li>Example: </li></ul><ul><li>while(i<10) </...
<ul><li>Example: </li></ul><ul><li>scanf(“%d%d”,&a[0],&a[1]); </li></ul>
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
Output <ul><li>Enter the inputs:3 </li></ul><ul><li>6 </li></ul><ul><li>The value in x[0] is 3 </li></ul><ul><li>The value...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
Output <ul><li>The value in x[0] is a </li></ul><ul><li>The value in x[1] is b </li></ul><ul><li>The value in x[2] is c </...
Two-Dimensional array  Array Declaration <ul><li>Syntax: </li></ul><ul><li>data_type array_name[row_size] [col_size]; </li...
Array Initialization <ul><li>Syntax: </li></ul><ul><li>data_type array_name[row_size] [col_size];={variables}; </li></ul><...
<ul><li>int x[2][2]={  {1,50}, </li></ul><ul><li>{2,75} </li></ul><ul><li>}; </li></ul><ul><li>(or) </li></ul><ul><li>int ...
row  0 row  1 Col 0   Col 1 2 1 75 50
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
Output <ul><li>The value in x[0][0] is 1 </li></ul><ul><li>The value in x[0][1] is 50 </li></ul><ul><li>The value in x[1][...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
Output <ul><li>The value in x[0][0] is 1 </li></ul><ul><li>The value in x[0][1] is 50 </li></ul><ul><li>The value in x[1][...
Matrix Addition  <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul...
<ul><li>step2: </li></ul><ul><li>printf(&quot;
 Enter the elements of matrix A 
&quot;); </li></ul><ul><li>for(i=0;i<r1;i+...
<ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c1;j++) </li></ul><ul><li>{ </li></ul><ul><li>c[...
Output <ul><li>Enter the size of matrix A: 2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the size of matrix B: 2 </li></ul...
Matrix Multiplication <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></u...
<ul><li>step2: </li></ul><ul><li>printf(&quot;
 Enter the elements of matrix A 
&quot;); </li></ul><ul><li>for(i=0;i<r1;i+...
<ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>{ </li></ul><ul><li>c[...
Output <ul><li>Enter the size of matrix A:2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the size of matrix B:2 </li></ul><...
<ul><li>Enter the size of matrix A:2 </li></ul><ul><li>3 </li></ul><ul><li>Enter the size of matrix B:3 </li></ul><ul><li>...
<ul><li>20  32 </li></ul><ul><li>50  80 </li></ul>
Passing array to Function <ul><li>Here an array is transferred as parameter to a function. </li></ul><ul><li>void main()  ...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void add(int,int b[]); </li></ul><...
<ul><li>void add(int x,int b[]) </li></ul><ul><li>{ </li></ul><ul><li>int sum=0,i; </li></ul><ul><li>for(i=0;i<x;i++) </li...
Output <ul><li>Enter the Number: 5 </li></ul><ul><li>Enter the Values: 1 </li></ul><ul><li>2 </li></ul><ul><li>3 </li></ul...
Array of Characters <ul><li>In array the characters are terminated by the null (‘�’) character. </li></ul><ul><li>Example:...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
Output <ul><li>a  b  c  d </li></ul>
Multi Dimensional Array <ul><li>Syntax </li></ul><ul><li>datatype array_name [size1][size2]….[size n] </li></ul><ul><ul><l...
<ul><li>Example: </li></ul><ul><ul><li>int a[3][3][3]; </li></ul></ul>Col 0   Col 1  Col 2 row  0 row  1 row  2 X[0][0] X[...
String Functions <ul><li>strlen() </li></ul><ul><li>It is used to find the length of the string. </li></ul><ul><li>syntax:...
<ul><li>strcmp() </li></ul><ul><li>It is used to compare two strings. </li></ul><ul><li>syntax: </li></ul><ul><li>strcmp(s...
<ul><li>strncpy() </li></ul><ul><li>It used to copy ‘n’ characters of one string to another.  </li></ul><ul><li>strstr() <...
<ul><li>strncmp() </li></ul><ul><ul><li>It is used to compare ‘n’ character of two strings. </li></ul></ul><ul><li>strcmpi...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><...
<ul><li>else </li></ul><ul><li>printf(&quot;
string1 is greater than string2:%d&quot;,i); </li></ul><ul><li>getch(); </li>...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><...
Output <ul><li>The string is :itdept </li></ul><ul><li>The string after conversion to uppercase :ITDEPT </li></ul><ul><li>...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><...
<ul><li>Output: </li></ul><ul><li>The string is :it </li></ul>
String Palindrome <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li>...
<ul><li>for(i=0,j=len-1;i<len/2;i++,j--) </li></ul><ul><li>{ </li></ul><ul><li>  if(str[i]!=str[j]) </li></ul><ul><li>  { ...
Enumerated Data Type <ul><li>It is user defined data type. </li></ul><ul><li>The user can create their  own data type and ...
Example <ul><li>#include <conio.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ ...
Example <ul><li>#include <conio.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ ...
Structure <ul><li>A  Structure  is a collection of different data items, that are stored under a common name. </li></ul><u...
<ul><li>Example: </li></ul><ul><li>struct stud </li></ul><ul><li>{ </li></ul><ul><li>int sno; </li></ul><ul><li>char name[...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct stud </li></ul><ul><li>{ </...
<ul><li>printf(&quot;
Enter the student regno,name,m1,m2,m3:&quot;); scanf(&quot;%d%s%d%d%d&quot;,&s.regno,&s.name,&s.m1,&...
Output <ul><li>Enter the student regno,name,m1,m2,m3:100 </li></ul><ul><li>aaa </li></ul><ul><li>87 </li></ul><ul><li>98 <...
Structure assignment <ul><li>It is possible to assign one structure information to another structure of same type using si...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
Output <ul><li>The value of y.a is10 </li></ul>
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct stud </li></ul><ul><li>{ </...
<ul><li>for(i=0;i<n;i++) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;
Enter the student regno,name,m1,m2,m3:&quot;)...
<ul><li>  s[i].grade='A'; </li></ul><ul><li>else if(s[i].avg>=50) </li></ul><ul><li>  s[i].grade='B'; </li></ul><ul><li>el...
<ul><li>Enter the no.of students:2 </li></ul><ul><li>Enter the student regno,name,m1,m2,m3:101 </li></ul><ul><li>aaa </li>...
Union <ul><li>An  Union  is a collection of different data items, that are stored under a common name. Here same memory is...
<ul><li>Example: </li></ul><ul><li>  union  result </li></ul><ul><li>{ </li></ul><ul><li>int mark;  </li></ul><ul><li>floa...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>union stud </li></ul><ul><li>{ </l...
<ul><li>c.a=256; </li></ul><ul><li>printf(&quot;
c.a value is%d&quot;,c.a); </li></ul><ul><li>printf(&quot;
c.b[0] value i...
<ul><li>256 = 00000010 00000000 </li></ul>Higher bit  Lower bit 00000000  00000010  c.a  -  2 Byte c.b[0]  1 Byte c.b[0]  ...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct student </li></ul><ul><li>{...
<ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;
The size of struct is %d&quot;,sizeof(s)); </li></...
Structure & Union int  int  char 2 Byte   2Byte   1Byte 2 Byte structure int ,int, char  union
Preprocessor <ul><li>It is a program that processes the source program before compilation. </li></ul><ul><li>It operates u...
File Inclusion <ul><li>It is used to include some file that contains functions or some definitions. </li></ul><ul><li>Synt...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include &quot;addition.txt&quot; ...
addition.txt <ul><li>int add(int a,int b) </li></ul><ul><li>{ </li></ul><ul><li>return(a+b); </li></ul><ul><li>} </li></ul>
Output <ul><li>Enter the numbers:7 </li></ul><ul><li>4 </li></ul><ul><li>The Value is 11 </li></ul>
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include &quot;fact.c&quot; </li><...
fact.c <ul><li>int rec(int x) </li></ul><ul><li>{ </li></ul><ul><li>int f; </li></ul><ul><li>if(x==1) </li></ul><ul><li>re...
Output <ul><li>Enter the number:5 </li></ul><ul><li>The factorial of 5! is 120 </li></ul>
Macro Substitution <ul><li>It is used to define and use integer, string, or identifier in the source program </li></ul><ul...
Simple Macro <ul><li>It is used to define some constants </li></ul><ul><li>Syntax </li></ul><ul><li># define identifier st...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define pi 3.14 </li></ul><ul><li>...
Argumented Macro <ul><li>It is used to define some complex forms in the source program. </li></ul><ul><li>Syntax: </li></u...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define cube(n) (n*n*n) </li></ul>...
Nested Macro <ul><li>Here one macro is used by another macro. </li></ul><ul><li>Eg: </li></ul><ul><li>#define a 3 </li></u...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define a 3 </li></ul><ul><li>#def...
Conditional Inclusion <ul><li>It is used to include some conditional statements. </li></ul>
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define a 3 </li></ul><ul><li>#ifd...
Pointers <ul><li>Pointer is a variable that contains the memory address of another variable. </li></ul>
<ul><li>Example: </li></ul><ul><li>  x=5 </li></ul><ul><li>  x  Variable  </li></ul><ul><li>  1002  Address </li></ul>5 Va...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
Pointer Declaration <ul><li>Syntax </li></ul><ul><li>data-type *pointer-name; </li></ul><ul><li>data-type  - Type of the d...
Accessing Variable through Pointer <ul><li>If a pointer is declared and assigned to a variable, then the variable can be a...
<ul><li>Example </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li...
Output <ul><li>The Value of x = 5 </li></ul><ul><li>The Address of x = 8758 </li></ul><ul><li>The Value of a = 8758 </li><...
<ul><li>Example: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </l...
5001 10 8000 a y 5001 Variable Value Address
Output <ul><li>The Value of y = 10 </li></ul><ul><li>The Address of y = 5001 </li></ul><ul><li>The Value of a = 5001 </li>...
Null Pointer <ul><li>A pointer is said to be null pointer if zero is assigned to the pointer. </li></ul><ul><li>Example </...
Pointer to Pointer <ul><li>Here one pointer stores the address of another pointer variable. </li></ul><ul><li>Example: </l...
5001 10 8000 a x 5001 Variable Value Address 8000 9000 b
<ul><li>Example </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li...
Output <ul><li>The Value of a = 10 </li></ul><ul><li>The Address of a = 5001 </li></ul><ul><li>The Value of b = 5001 </li>...
Pointers and Functions <ul><li>Call by Value </li></ul><ul><li>Call by Reference </li></ul>
Call by value <ul><li>Actual argument passed to the formal argument. </li></ul><ul><li>Any changes to the formal argument ...
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ <...
<ul><li>change(x,y); </li></ul><ul><li>printf(&quot;

Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} </l...
Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Function -->x=6,y=5...
 
Call by reference <ul><li>Instead of passing value, the address of the argument will be passed. </li></ul><ul><li>Any chan...
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ <...
<ul><li>change(&x,&y); </li></ul><ul><li>printf(&quot;

Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} <...
Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Function -->x=6,y=5...
Pointer to Array <ul><li>The elements of the array can also be accessed through a pointer. </li></ul><ul><li>Example </li>...
<ul><li>Example: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </l...
8744 2 9000 b a[0] 8744 Variable Value Address
Output <ul><li>The Value of a[0] = 2 </li></ul><ul><li>The Address of a[0] = 8744 </li></ul><ul><li>The Value of b = 8744 ...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
a[0]  a[1] a[2]   a[3]   a[4] 8724  8726  8728   8730  8732  Array Value Address 10 9 7 3 2
Output <ul><li>The Value of a[0] = 2 </li></ul><ul><li>The Address of a[0] = 8724 </li></ul><ul><li>The Value of a[1] = 3 ...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </...
Output <ul><li>The Sum is  15 </li></ul>
Pointer and Structures <ul><li>Syntax: </li></ul><ul><li>struct structure_name </li></ul><ul><li>{ </li></ul><ul><li>struc...
<ul><li>Example: </li></ul><ul><li>struct stud </li></ul><ul><li>{ </li></ul><ul><li>int sno; </li></ul><ul><li>char name[...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct stud </li></ul><ul><li>{ </...
<ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float tot,avg; </li></ul><ul><li>t=&s; </li></ul><ul><li>printf(...
Output <ul><li>Enter the student regno,name,m1,m2,m3:1 </li></ul><ul><li>aaa </li></ul><ul><li>76 </li></ul><ul><li>89 </l...
Command Line Argument <ul><li>It allows the user to pass some information to the program while running the program. </li><...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main(int argc,char argv[]) </...
Output <ul><li>C:	c>a </li></ul><ul><li>The Argument is C:TCA.EXE </li></ul>
String Palindrome <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li>...
<ul><li>if(strcmp(s1,s2)==0) </li></ul><ul><li>printf(&quot;
 The string is palindrome&quot;); </li></ul><ul><li>else </li...
Developing a ‘C’ Program <ul><li>The Program development life cycle is considered as a sequence of events by the programme...
Program Design <ul><li>Analysing the problem </li></ul><ul><li>Algorithm development </li></ul><ul><li>Selection of condit...
Program Coding <ul><li>Documentation </li></ul><ul><li>Statement construction </li></ul><ul><li>Input and output format et...
Program Testing <ul><li>It is the process of executing the program with sample data </li></ul>
 
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define p 3.14 </li></ul><ul><li>v...
 
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct stud </li></ul><ul><li>{ </...
<ul><li>printf(&quot;
Enter the student regno,name,m1,m2,m3:&quot;); </li></ul><ul><li>for(i=0;i<2;i++) </li></ul><ul><li>...
<ul><li>Enter the student regno,name,m1,m2,m3:100 </li></ul><ul><li>aaa </li></ul><ul><li>78 </li></ul><ul><li>67 </li></u...
<ul><li>#include <string.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>int main(void) </li></ul><ul><li>{ </li...
Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><...
<ul><li>The character is  D </li></ul><ul><li>The character is  e </li></ul><ul><li>The character is  p </li></ul><ul><li>...
 
Structure
 
 
 
Lab Exercise C Programs
Function-with arg & return <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </...
Output <ul><li>Enter two number:6 </li></ul><ul><li>7 </li></ul><ul><li>Sum is:13 </li></ul>
Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ <...
<ul><li>change(&x,&y); </li></ul><ul><li>printf(&quot;

Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} <...
Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Function -->x=6,y=5...
Factorial-Recursive Fn <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></...
<ul><li>int rec(int x) </li></ul><ul><li>{ </li></ul><ul><li>int f; </li></ul><ul><li>if(x==1) </li></ul><ul><li>return(1)...
Example: Working of 3!
Matrix Multiplication <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></u...
<ul><li>step2: </li></ul><ul><li>printf(&quot;
 Enter the elements of matrix A 
&quot;); </li></ul><ul><li>for(i=0;i<r1;i+...
<ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>{ </li></ul><ul><li>c[...
Output <ul><li>Enter the size of matrix A:2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the size of matrix B:2 </li></ul><...
 
Lab Ex:9,Finding area, circumference of circle <ul><li>#include<stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><u...
<ul><li>area=3.14*r*r; </li></ul><ul><li>c=2*3.14*r; </li></ul><ul><li>printf(&quot; 
The area is :%f&quot;,area); </li></...
Lab Ex:9,Conversion of Celsius to Fahrenheit  <ul><li>#include<stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul...
<ul><li>Enter the value of c:35 </li></ul><ul><li>The fahrenheit is :95.000000 </li></ul>
Lab Ex:11,Arithmetic operations <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main(...
<ul><li>printf(&quot;
The values of A + B:%d&quot;,c); </li></ul><ul><li>printf(&quot;
The values of A - B:%d&quot;,d); </...
Output <ul><li>Enter the values of A and B:6 </li></ul><ul><li>3 </li></ul><ul><li>The values of A + B:9 </li></ul><ul><li...
Lab.Ex13,Largest among 3 nos <ul><li>#include<stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main ( ...
<ul><li>if((a>b)&&(a>c)) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; 
A is Big&quot;); </li></ul><ul><li>} </li></...
Output <ul><li>Enter the value of a:5 </li></ul><ul><li>Enter the value of b:7 </li></ul><ul><li>Enter the value of c:3 </...
String Palindrome <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li>...
<ul><li>for(i=0,j=len-1;i<len/2;i++,j--) </li></ul><ul><li>{ </li></ul><ul><li>  if(str[i]!=str[j]) </li></ul><ul><li>  { ...
Lab.Ex:14,Quadratic Equation <ul><li>#include<stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>#include<mat...
<ul><li>if(d>=0) </li></ul><ul><li>{ </li></ul><ul><li>r1=(-b+sqrt(d))/(2*a); </li></ul><ul><li>r2=(-b-sqrt(d))/(2*a); </l...
Output <ul><li>Enter the value of a:1 </li></ul><ul><li>Enter the value of b:4 </li></ul><ul><li>Enter the value of c:4 </...
Upcoming SlideShare
Loading in …5
×

Unit 5 Foc

3,847 views

Published on

ARRAYS,FUNCTIONS AND POINTERS

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,847
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
Downloads
395
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Unit 5 Foc

  1. 1. UNIT - 5 FUNCTIONS AND POINTERS
  2. 2. FUNCTION <ul><li>Functions is a sub-program that contains one or more statements and it performs some task when called. </li></ul>
  3. 3. Types Functions User-Defined Functions Pre-Defined Functions
  4. 4. Pre-Defined Functions <ul><li>The pre-defined functions or library functions are built-in functions. </li></ul><ul><li>The user can use the functions, but cannot modify the function. </li></ul><ul><li>Example: sqrt() </li></ul>
  5. 5. User-Defined Functions <ul><li>The functions defined by the user for their requirement are called user-defined functions. </li></ul><ul><li>Whenever it is needed, The user can modify the function. </li></ul><ul><li>Example: sum(a,b) </li></ul>
  6. 6. Advantage of User-Defined Functions <ul><li>The length of the source program can be reduced. </li></ul><ul><li>It is easy to locate error. </li></ul><ul><li>It avoid coding of repeated instructions. </li></ul>
  7. 7. Elements of User-Defined Function <ul><li>Function declaration </li></ul><ul><li>Function definition </li></ul><ul><li>Function call </li></ul>
  8. 8. Function <ul><li>Syntax </li></ul><ul><li>datatype function_name (parameters list) </li></ul><ul><li>{ </li></ul><ul><li>local variable declaration; </li></ul><ul><li>………………………… </li></ul><ul><li>body of the function; </li></ul><ul><li>………………………… </li></ul><ul><li>return(expression); </li></ul><ul><li>} </li></ul>
  9. 9. How Function Works <ul><li>Once a function is called the control passes to the called function. </li></ul><ul><li>The working of calling function is temporarily stopped. </li></ul><ul><li>When the execution of called function is completed then the control return back to the calling function and execute the next statement. </li></ul>
  10. 11. Parameters <ul><li>Actual Parameter </li></ul><ul><li>These are the parameters transferred from the calling function to the called function. </li></ul><ul><li>Formal Parameter </li></ul><ul><li>These are the parameters which is used in the called function. </li></ul>
  11. 13. return Statement <ul><li>The return statement may or may not send some values to the calling function. </li></ul><ul><li>Syntax: </li></ul><ul><li>return; (or) </li></ul><ul><li>return(expression); </li></ul>
  12. 14. Function Prototypes <ul><li>Function with no arguments and no return values. </li></ul><ul><li>Function with arguments and no return values. </li></ul><ul><li>Function with arguments and return values. </li></ul><ul><li>Function with no arguments and with return values. </li></ul>
  13. 15. Function with no arguments and no return values <ul><li>Here no data transfer take place between the calling function and the called function. </li></ul><ul><li>These functions act independently, i.e. they get input and display output in the same block. </li></ul>
  14. 17. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() //calling function </li></ul><ul><li>{ </li></ul><ul><li>void add(void); </li></ul><ul><li>add(); </li></ul><ul><li>} </li></ul><ul><li>void add() //called function </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c; </li></ul><ul><li>printf(&quot; Enter two number:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul><ul><li>c=a+b; </li></ul><ul><li>printf(&quot; Sum is:%d&quot;,c); </li></ul><ul><li>} </li></ul>
  15. 18. Output <ul><li>Enter two number:3 </li></ul><ul><li>4 </li></ul><ul><li>Sum is:7 </li></ul>
  16. 19. Function with arguments and no return values <ul><li>Here data transfer take place between the calling function and the called function. </li></ul><ul><li>It is a one way data communication , i.e. the called program receives data from calling program but it does not return any value to the calling program. </li></ul>
  17. 21. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b; </li></ul><ul><li>void add(int,int); </li></ul><ul><li>printf(&quot; Enter two number:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul><ul><li>add(a,b); </li></ul><ul><li>} </li></ul><ul><li>void add(int x,int y) //function with arguments </li></ul><ul><li>{ </li></ul><ul><li>int z; </li></ul><ul><li>z=x+y; </li></ul><ul><li>printf(&quot; Sum is:%d&quot;,z); </li></ul><ul><li>} </li></ul>
  18. 22. Output <ul><li>Enter two number:2 </li></ul><ul><li>4 </li></ul><ul><li>Sum is:6 </li></ul>
  19. 23. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b; </li></ul><ul><li>void add(int a,int b); </li></ul><ul><li>printf(&quot; Enter two number:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul><ul><li>add(a,b); </li></ul><ul><li>} </li></ul><ul><li>void add(int x,int y) //function with arguments </li></ul><ul><li>{ </li></ul><ul><li>int z; </li></ul><ul><li>z=x+y; </li></ul><ul><li>printf(&quot; Sum is:%d&quot;,z); </li></ul><ul><li>} </li></ul>
  20. 24. Output <ul><li>Enter two number:2 </li></ul><ul><li>4 </li></ul><ul><li>Sum is:6 </li></ul>
  21. 25. Function with arguments and return values <ul><li>Here data transfer take place between the calling function and the called function as well as between called function and calling function . </li></ul><ul><li>It is a two way data communication , i.e. the called program receives data from calling program and it return some value to the calling program. </li></ul>
  22. 27. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c; </li></ul><ul><li>int add(int,int); </li></ul><ul><li>printf(&quot; Enter two number:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul><ul><li>c=add(a,b); </li></ul><ul><li>printf(&quot; Sum is:%d&quot;,c); </li></ul><ul><li>} </li></ul><ul><li>int add(int x,int y) </li></ul><ul><li>{ </li></ul><ul><li>int z; </li></ul><ul><li>z=x+y; </li></ul><ul><li>return(z); </li></ul><ul><li>} </li></ul>
  23. 28. Output <ul><li>Enter two number:6 </li></ul><ul><li>7 </li></ul><ul><li>Sum is:13 </li></ul>
  24. 29. Function with no arguments and with return values <ul><li>Here data transfer take place between the called function and the calling function. </li></ul><ul><li>It is a one way data communication , i.e. the called program does not receives data from calling program but it return some value to the calling program. </li></ul>
  25. 31. <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int add(),d; </li></ul><ul><li>d=add(); </li></ul><ul><li>printf(&quot; Sum is:%d&quot;,d); </li></ul><ul><li>} </li></ul><ul><li>int add() //function wit no argument </li></ul><ul><li>{ int a,b,c; </li></ul><ul><li>printf(&quot; Enter two number:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul><ul><li>c=a+b; </li></ul><ul><li>return(c); </li></ul><ul><li>} </li></ul>
  26. 32. Output <ul><li>Enter two number:5 </li></ul><ul><li>8 </li></ul><ul><li>Sum is:13 </li></ul>
  27. 33. Parameter Passing Methods <ul><li>Call by value </li></ul><ul><li>Call by reference </li></ul>
  28. 34. Call by value <ul><li>Actual argument passed to the formal argument. </li></ul><ul><li>Any changes to the formal argument does not affect the actual argument. </li></ul>
  29. 35. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x,y,change(int,int); </li></ul><ul><li>printf(&quot; Enter value of x:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&x); </li></ul><ul><li>printf(&quot; Enter value of y:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&y); </li></ul>
  30. 36. <ul><li>change(x,y); </li></ul><ul><li>printf(&quot; Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} </li></ul><ul><li>int change(int a,int b) </li></ul><ul><li>{ </li></ul><ul><li>int c; </li></ul><ul><li>c=a; </li></ul><ul><li>a=b; </li></ul><ul><li>b=c; </li></ul><ul><li>printf(&quot; Values in the Fuction -->x=%d,y=%d&quot;,a,b); </li></ul><ul><li>} </li></ul>
  31. 37. Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Fuction -->x=6,y=5 </li></ul><ul><li>Values in the Main()-->x=5,y=6 </li></ul>
  32. 38. Call by reference <ul><li>Instead of passing value, the address of the argument will be passed. </li></ul><ul><li>Any changes to the formal argument will affect the actual argument. </li></ul>
  33. 39. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x,y,change(int*,int*); </li></ul><ul><li>printf(&quot; Enter value of x:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&x); </li></ul><ul><li>printf(&quot; Enter value of y:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&y); </li></ul>
  34. 40. <ul><li>change(&x,&y); </li></ul><ul><li>printf(&quot; Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} </li></ul><ul><li>int change(int *a,int *b) </li></ul><ul><li>{ </li></ul><ul><li>int c; </li></ul><ul><li>c=*a; </li></ul><ul><li>*a=*b; </li></ul><ul><li>*b=c; </li></ul><ul><li>printf(&quot; Values in the Function -->x=%d,y=%d&quot;,*a,*b); </li></ul><ul><li>} </li></ul>
  35. 41. Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Function -->x=6,y=5 </li></ul><ul><li>Values in the Main()-->x=6,y=5 </li></ul>
  36. 42. Recursion <ul><li>It is a process of calling the same function itself again and again until some condition is satisfied. </li></ul><ul><li>Syntax: </li></ul><ul><li>func1() </li></ul><ul><li>{ </li></ul><ul><li>……… .. </li></ul><ul><li>func1(); </li></ul><ul><li>} </li></ul>
  37. 43. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a; </li></ul><ul><li>int rec(int); </li></ul><ul><li>printf(&quot; Enter the number:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&a); </li></ul><ul><li>printf(&quot;The factorial of %d! is %d&quot;,a,rec(a)); </li></ul><ul><li>} </li></ul>
  38. 44. <ul><li>int rec(int x) </li></ul><ul><li>{ </li></ul><ul><li>int f; </li></ul><ul><li>if(x==1) </li></ul><ul><li>return(1); </li></ul><ul><li>else </li></ul><ul><li>f=x*rec(x-1); </li></ul><ul><li>return(f); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Enter the number:5 </li></ul><ul><li>The factorial of 5! is 120 </li></ul>
  39. 45. Example: Working of 3!
  40. 46. Tower of Honoi 1 2 3 3 2 3 1 3 2 3
  41. 47. Tower of Honoi 1 2 3 3 2 3 1 3 2 3
  42. 48. Library Function <ul><li>It is pre-defined function. </li></ul><ul><li>The library function provides functions like mathematical, string manipulation etc,. </li></ul>
  43. 49. Example <ul><li>sqrt(x): </li></ul><ul><li>It is used to find the square root of x </li></ul><ul><li>Example: sqrt(36) is 6 </li></ul><ul><li>abs(x): </li></ul><ul><li>It is used to find the absolute value of x </li></ul><ul><li>Example: abs(-36) is 36 </li></ul><ul><li>pow(x,y): </li></ul><ul><li>It is used to find the value of x y </li></ul><ul><li>Example: pow(5,2) is 25 </li></ul><ul><li>ceil(x): </li></ul><ul><li>It is used to find the smallest integer greater than or equal to x </li></ul><ul><li>Example: ceil(7.7) is 8 </li></ul>
  44. 50. <ul><li>rand(): </li></ul><ul><li>It is used to generate a random number. </li></ul><ul><li>sin(x): </li></ul><ul><li>It is used to find the sine value of x </li></ul><ul><li>Example: sin(30) is 0.5 </li></ul><ul><li>cos(x): </li></ul><ul><li> It is used to find the cosine value of x </li></ul><ul><li>Example: cos(30) is 0.86 </li></ul><ul><li>tan(x): </li></ul><ul><li> It is used to find the tan value of x </li></ul><ul><li>Example: tan(30) is 0.577 </li></ul>
  45. 51. <ul><li>toascii(x): </li></ul><ul><li>It is used to find the ASCII value of x </li></ul><ul><li>Example: toascii(a) is 97 </li></ul><ul><li>toupper(x): </li></ul><ul><li>It is used to convert lowercase character to uppercase. </li></ul><ul><li>Example: toupper(‘a’) is A </li></ul><ul><li> toupper(97) is A </li></ul><ul><li>tolower(x): </li></ul><ul><li>It is used to convert uppercase character to lowercase. </li></ul><ul><li>Example: tolower(‘A’) is a </li></ul>
  46. 52. Example: <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<math.h> </li></ul><ul><li>#include<ctype.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x,y=2; </li></ul><ul><ul><li>printf(&quot; Enter the number:&quot;); </li></ul></ul><ul><ul><li>scanf(&quot;%d&quot;,&x); </li></ul></ul><ul><ul><li>printf(&quot; The squareroot of %d is %f&quot;,x,sqrt(x)); </li></ul></ul><ul><li>printf(&quot; The value of %d power%dis%f &quot;,x,y,pow(6,2)); </li></ul>
  47. 53. <ul><ul><li>printf(&quot; The ceiling of 6.7 is %f&quot;,ceil(6.7)); </li></ul></ul><ul><ul><li>printf(&quot; The floor of 6.7 is %f&quot;,floor(6.7)); </li></ul></ul><ul><ul><li>printf(&quot; The absolute value of -6 is %d&quot;,abs(-6)); </li></ul></ul><ul><ul><li>printf(&quot; The value of sin 45 is %f&quot;,sin(45)); </li></ul></ul><ul><ul><li>printf(&quot; The uppercase of 'a' is %c&quot;,toupper('a')); </li></ul></ul><ul><ul><li>printf(&quot; The uppercase of 97 is %c&quot;,toupper(97)); </li></ul></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  48. 54. Output: <ul><li>Enter the number:6 </li></ul><ul><li>The squareroot of 6 is 2.449490 </li></ul><ul><li>The value of 6 power 2 is 36.000000 </li></ul><ul><li>The ceiling of 6.7 is 7.000000 </li></ul><ul><li>The floor of 6.7 is 6.000000 </li></ul><ul><li>The absolute value of -6 is 6 </li></ul><ul><li>The value of sin 45 is 0.850904 </li></ul><ul><li>The uppercase of 'a' is A </li></ul><ul><li>The uppercase of 97 is A </li></ul>
  49. 55. Array <ul><li>An Array is a collection of similar data items, that are stored under a common name. </li></ul><ul><li>Types </li></ul><ul><ul><li>One-Dimensional array </li></ul></ul><ul><ul><li>Two-Dimensional array </li></ul></ul><ul><ul><li>Multi-Dimensional array </li></ul></ul>
  50. 56. One-Dimensional array Array Declaration <ul><li>Syntax: </li></ul><ul><li>data_type array_name[size]; </li></ul><ul><li>Example: int x[3]; </li></ul>X[0] X[1] X[2] x
  51. 57. Array initialization <ul><li>At compile time </li></ul><ul><li>At run time </li></ul>
  52. 58. At compile time <ul><li>Syntax: </li></ul><ul><li>data_type array_name[size]={variables}; </li></ul><ul><li>Example: int x[3]={5,3,7}; </li></ul>X[0] X[1] X[2] x 7 3 5
  53. 59. At Run time <ul><li>Array can also initialize at the run time. </li></ul><ul><li>Example: </li></ul><ul><li>while(i<10) </li></ul><ul><li>{ </li></ul><ul><li>if(i<5) </li></ul><ul><li>sum[i]=0; </li></ul><ul><li>else </li></ul><ul><li>sum[i]=sum[i]+i; </li></ul><ul><li>} </li></ul>
  54. 60. <ul><li>Example: </li></ul><ul><li>scanf(“%d%d”,&a[0],&a[1]); </li></ul>
  55. 61. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x[2],i; </li></ul><ul><li>printf(&quot; Enter the inputs:&quot;); </li></ul><ul><li>for(i=0;i<2;i++) </li></ul><ul><li>scanf(&quot;%d&quot;,&x[i]); </li></ul><ul><li>for(i=0;i<2;i++) </li></ul><ul><li>printf(&quot; The value in x[%d] is %d&quot;,i,x[i]); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  56. 62. Output <ul><li>Enter the inputs:3 </li></ul><ul><li>6 </li></ul><ul><li>The value in x[0] is 3 </li></ul><ul><li>The value in x[1] is 6 </li></ul>
  57. 63. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i; </li></ul><ul><li>char x[5]={'a','b','c','d','e'}; </li></ul><ul><li>clrscr(); </li></ul><ul><li>for(i=0;i<5;i++) </li></ul><ul><li>printf(&quot; The value in x[%d] is %c&quot;,i,x[i]); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  58. 64. Output <ul><li>The value in x[0] is a </li></ul><ul><li>The value in x[1] is b </li></ul><ul><li>The value in x[2] is c </li></ul><ul><li>The value in x[3] is d </li></ul><ul><li>The value in x[4] is e </li></ul>
  59. 65. Two-Dimensional array Array Declaration <ul><li>Syntax: </li></ul><ul><li>data_type array_name[row_size] [col_size]; </li></ul><ul><li>Example: int x[3][2]; </li></ul>X[0][0] X[1][0] X[2][0] Col 0 Col 1 row 0 row 1 row 2 X[0][1] X[1][1] X[2][1]
  60. 66. Array Initialization <ul><li>Syntax: </li></ul><ul><li>data_type array_name[row_size] [col_size];={variables}; </li></ul><ul><li>Example: int x[2][2]={1,50,2,75}; </li></ul>
  61. 67. <ul><li>int x[2][2]={ {1,50}, </li></ul><ul><li>{2,75} </li></ul><ul><li>}; </li></ul><ul><li>(or) </li></ul><ul><li>int x[ ][2]={ {1,50}, </li></ul><ul><li>{2,75} </li></ul><ul><li>}; </li></ul>
  62. 68. row 0 row 1 Col 0 Col 1 2 1 75 50
  63. 69. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,j; </li></ul><ul><li>int x[2][2]={ {1,50}, </li></ul><ul><li>{2,75} </li></ul><ul><li>}; </li></ul><ul><li>clrscr(); </li></ul><ul><li>for(i=0;i<2;i++) </li></ul><ul><li>for(j=0;j<2;j++) </li></ul><ul><li>printf(&quot; The value in x[%d][%d] is %d&quot;,i,j,x[i][j]); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  64. 70. Output <ul><li>The value in x[0][0] is 1 </li></ul><ul><li>The value in x[0][1] is 50 </li></ul><ul><li>The value in x[1][0] is 2 </li></ul><ul><li>The value in x[1][1] is 75 </li></ul>
  65. 71. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,j; </li></ul><ul><li>int x[][2]={ {1,50},{2,75},{3,65}}; </li></ul><ul><li>clrscr(); </li></ul><ul><li>for(i=0;i<=2;i++) </li></ul><ul><li>for(j=0;j<2;j++) </li></ul><ul><li>printf(&quot; The value in x[%d][%d] is %d&quot;,i,j,x[i][j]); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  66. 72. Output <ul><li>The value in x[0][0] is 1 </li></ul><ul><li>The value in x[0][1] is 50 </li></ul><ul><li>The value in x[1][0] is 2 </li></ul><ul><li>The value in x[1][1] is 75 </li></ul><ul><li>The value in x[2][0] is 3 </li></ul><ul><li>The value in x[2][1] is 65 </li></ul>
  67. 73. Matrix Addition <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,j,k,r1,r2,c1,c2; </li></ul><ul><li>int a[5][5],b[5][5],c[5][5]; </li></ul><ul><li>clrscr(); </li></ul><ul><li>step1: </li></ul><ul><li>printf(&quot; Enter the size of matrix A:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&r1,&c1); </li></ul><ul><li>printf(&quot; Enter the size of matrix B: &quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&r2,&c2); </li></ul><ul><li>if((c1==c2)&&(r1==r2)) </li></ul><ul><li>goto step2; </li></ul><ul><li>else </li></ul><ul><li>goto step1; </li></ul>
  68. 74. <ul><li>step2: </li></ul><ul><li>printf(&quot; Enter the elements of matrix A &quot;); </li></ul><ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c1;j++) </li></ul><ul><li>{ </li></ul><ul><li>scanf(&quot;%d&quot;,&a[i][j]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; Enter the elements of matrix B &quot;); </li></ul><ul><li>for(i=0;i<r2;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>{ </li></ul><ul><li>scanf(&quot; %d&quot;,&b[i][j]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  69. 75. <ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c1;j++) </li></ul><ul><li>{ </li></ul><ul><li>c[i][j]=0; </li></ul><ul><li> c[i][j]=c[i][j]+a[i][j]+b[i][j]; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; The resultant matrix after addition of A & B is &quot;); </li></ul><ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c1;j++) </li></ul><ul><li>printf(&quot;%d &quot;,c[i][j]); </li></ul><ul><li>printf(&quot; &quot;); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  70. 76. Output <ul><li>Enter the size of matrix A: 2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the size of matrix B: 2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the elements of matrix A </li></ul><ul><li>2 </li></ul><ul><li>2 </li></ul><ul><li>2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the elements of matrix B </li></ul><ul><li>3 </li></ul><ul><li>3 </li></ul><ul><li>3 </li></ul><ul><li>3 </li></ul><ul><li>The resultant matrix after addition of A&B is </li></ul><ul><li>5 5 </li></ul><ul><li>5 5 </li></ul>
  71. 77. Matrix Multiplication <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,j,k,r1,r2,c1,c2; </li></ul><ul><li>int a[5][5],b[5][5],c[5][5]; </li></ul><ul><li>clrscr(); </li></ul><ul><li>step1: </li></ul><ul><li>printf(&quot; Enter the size of matrix A &quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&r1,&c1); </li></ul><ul><li>printf(&quot; Enter the size of matrix B &quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&r2,&c2); </li></ul><ul><li>if(c1==r2) </li></ul><ul><li>goto step2; </li></ul><ul><li>else </li></ul><ul><li>goto step1; </li></ul>
  72. 78. <ul><li>step2: </li></ul><ul><li>printf(&quot; Enter the elements of matrix A &quot;); </li></ul><ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c1;j++) </li></ul><ul><li>{ </li></ul><ul><li>scanf(&quot;%d&quot;,&a[i][j]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; Enter the elements of matrix B &quot;); </li></ul><ul><li>for(i=0;i<r2;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>{ </li></ul><ul><li>scanf(&quot; %d&quot;,&b[i][j]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  73. 79. <ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>{ </li></ul><ul><li>c[i][j]=0; </li></ul><ul><li>for(k=0;k<c1;k++) </li></ul><ul><li>{ </li></ul><ul><li>c[i][j]=c[i][j]+a[i][k]*b[k][j]; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>printf(&quot;%d &quot;,c[i][j]); </li></ul><ul><li>printf(&quot; &quot;); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  74. 80. Output <ul><li>Enter the size of matrix A:2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the size of matrix B:2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the elements of matrix A </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>Enter the elements of matrix B </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>The resultant matrix is </li></ul><ul><li>32 32 </li></ul><ul><li>32 32 </li></ul>
  75. 81. <ul><li>Enter the size of matrix A:2 </li></ul><ul><li>3 </li></ul><ul><li>Enter the size of matrix B:3 </li></ul><ul><li>2 </li></ul><ul><li>Enter the elements of matrix A </li></ul><ul><li>1 </li></ul><ul><li>2 </li></ul><ul><li>3 </li></ul><ul><li>4 </li></ul><ul><li>5 </li></ul><ul><li>6 </li></ul><ul><li>Enter the elements of matrix B </li></ul><ul><li>2 </li></ul><ul><li>4 </li></ul><ul><li>6 </li></ul><ul><li>8 </li></ul><ul><li>2 </li></ul><ul><li>4 </li></ul>
  76. 82. <ul><li>20 32 </li></ul><ul><li>50 80 </li></ul>
  77. 83. Passing array to Function <ul><li>Here an array is transferred as parameter to a function. </li></ul><ul><li>void main() void fun(n,b[]) </li></ul><ul><li>{ { </li></ul><ul><li>void fun(int,int); int x,b[5]; </li></ul><ul><li>int a[5],n; ………….. </li></ul><ul><li>…………… ………… .. </li></ul><ul><li>fun(n,a); </li></ul><ul><li>…………… } </li></ul><ul><li>} </li></ul>
  78. 84. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void add(int,int b[]); </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a[5],i,n; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot; Enter the Number: &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&n); </li></ul><ul><li>printf(&quot; Enter the Values: &quot;); </li></ul><ul><li>for(i=0;i<n;i++) </li></ul><ul><li>scanf(&quot;%d&quot;,&a[i]); </li></ul><ul><li> add(n,a); </li></ul><ul><li>} </li></ul>
  79. 85. <ul><li>void add(int x,int b[]) </li></ul><ul><li>{ </li></ul><ul><li>int sum=0,i; </li></ul><ul><li>for(i=0;i<x;i++) </li></ul><ul><li>sum=sum+b[i]; </li></ul><ul><li>printf(&quot; The sum is: %d&quot;,sum); </li></ul><ul><li>} </li></ul>
  80. 86. Output <ul><li>Enter the Number: 5 </li></ul><ul><li>Enter the Values: 1 </li></ul><ul><li>2 </li></ul><ul><li>3 </li></ul><ul><li>4 </li></ul><ul><li>5 </li></ul><ul><li>The sum is: 15 </li></ul>
  81. 87. Array of Characters <ul><li>In array the characters are terminated by the null (‘’) character. </li></ul><ul><li>Example: char a[]={a,b,c}; </li></ul> c b a
  82. 88. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i=0; </li></ul><ul><li>char a[]=&quot;abcd&quot;; </li></ul><ul><li>clrscr(); </li></ul><ul><li>while(a[i]!='') </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; %c&quot;,a[i]); </li></ul><ul><li>i++; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  83. 89. Output <ul><li>a b c d </li></ul>
  84. 90. Multi Dimensional Array <ul><li>Syntax </li></ul><ul><li>datatype array_name [size1][size2]….[size n] </li></ul><ul><ul><li>datatype - type of the data. </li></ul></ul><ul><ul><li>array_name -name of the array. </li></ul></ul><ul><ul><li>size -size of the array. </li></ul></ul>
  85. 91. <ul><li>Example: </li></ul><ul><ul><li>int a[3][3][3]; </li></ul></ul>Col 0 Col 1 Col 2 row 0 row 1 row 2 X[0][0] X[1][0] X[2][0] X[0][1] X[1][1] X[2][1] X[0][2] X[1][2] X[2][2]
  86. 92. String Functions <ul><li>strlen() </li></ul><ul><li>It is used to find the length of the string. </li></ul><ul><li>syntax: </li></ul><ul><li>strlen(string) </li></ul><ul><li>strcpy() </li></ul><ul><ul><li>It is used to copy one string to another. </li></ul></ul><ul><li>syntax: </li></ul><ul><li>strcpy(string1,string2) </li></ul><ul><li>strcat() </li></ul><ul><li>It is used to combine two strings. </li></ul><ul><li>syntax: </li></ul><ul><li>strcat(string1,string2) </li></ul>
  87. 93. <ul><li>strcmp() </li></ul><ul><li>It is used to compare two strings. </li></ul><ul><li>syntax: </li></ul><ul><li>strcmp(string1,string2) </li></ul><ul><ul><ul><ul><li>Returns 0 if two strings are equal. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Return value <0 if s1 is less than s2. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Return value >0 if s1 is greater than s2. </li></ul></ul></ul></ul><ul><li>strrev() </li></ul><ul><li> It used to reverse a string. </li></ul><ul><li>syntax: </li></ul><ul><li>strrev(string) </li></ul><ul><li>strlwr(), strupr() </li></ul><ul><li>It used to change the case of a string. </li></ul><ul><li>syntax: </li></ul><ul><li>strlwr(string) </li></ul><ul><li>strupr(string) </li></ul>
  88. 94. <ul><li>strncpy() </li></ul><ul><li>It used to copy ‘n’ characters of one string to another. </li></ul><ul><li>strstr() </li></ul><ul><ul><li>It is used to determine the first occurrence of a given string in another string. </li></ul></ul><ul><li>strncat() </li></ul><ul><ul><li>It Appends source string to destination string upto specified length. </li></ul></ul><ul><li>strspn() </li></ul><ul><ul><li>It is used t find upto what length two strings are identical. </li></ul></ul>
  89. 95. <ul><li>strncmp() </li></ul><ul><ul><li>It is used to compare ‘n’ character of two strings. </li></ul></ul><ul><li>strcmpi() </li></ul><ul><ul><li>It is used to compare two strings without regarding the case. </li></ul></ul><ul><li>strnicmp() </li></ul><ul><ul><li>It is used to compare first ‘n’ characters of two strings without regarding the case. </li></ul></ul><ul><li>strchr() </li></ul><ul><ul><li>It is used to determine the first occurrence of a given character in a string. </li></ul></ul><ul><li>strrchr() </li></ul><ul><ul><li>It is used to determine the last occurrence of a given character in a string. </li></ul></ul>
  90. 96. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char a[]=&quot;college&quot;; </li></ul><ul><li>int b; </li></ul><ul><li>clrscr(); </li></ul><ul><li>b=strlen(a); </li></ul><ul><li>printf(&quot; The length of the string is %d&quot;,b); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The length of the string is 7 </li></ul>
  91. 97. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char a[]=&quot;IT&quot;; </li></ul><ul><li>char b[]=&quot;Dept&quot;; </li></ul><ul><li>clrscr(); </li></ul><ul><li>strcpy(a,b); </li></ul><ul><li>printf(&quot; The string is %s&quot;,a); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The string is Dept </li></ul>
  92. 98. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char a[]=&quot;IT&quot;; </li></ul><ul><li>char b[]=&quot;Dept&quot;; </li></ul><ul><li>clrscr(); </li></ul><ul><li>strcat(a,b); </li></ul><ul><li>printf(&quot; The string is %s&quot;,a); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The string is ITDept </li></ul>
  93. 99. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char a[]=&quot;itdept&quot;; </li></ul><ul><li>char b[]=&quot;it&quot;; </li></ul><ul><li>int i; </li></ul><ul><li>clrscr(); </li></ul><ul><li>i=strcmp(a,b); </li></ul><ul><li>if(i==0) </li></ul><ul><li>printf(&quot; strings are equal:%d&quot;,i); </li></ul><ul><li>else if(i<0) </li></ul><ul><li>printf(&quot; string1 is less than string2:%d&quot;,i); </li></ul>
  94. 100. <ul><li>else </li></ul><ul><li>printf(&quot; string1 is greater than string2:%d&quot;,i); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>string1 is greater than string2:100 </li></ul>
  95. 101. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char a[]=&quot;itdept&quot;; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot; The string is :%s&quot;,a); </li></ul><ul><li>strupr(a); </li></ul><ul><li>printf(&quot; The string after conversion to uppercase :%s&quot;,a); </li></ul><ul><li>strlwr(a); </li></ul><ul><li>printf(&quot; The string after conversion to lowercase :%s&quot;,a); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  96. 102. Output <ul><li>The string is :itdept </li></ul><ul><li>The string after conversion to uppercase :ITDEPT </li></ul><ul><li>The string after conversion to lowercase :itdept </li></ul>
  97. 103. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char a[]=&quot;Dept&quot;; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot; The string is %s&quot;,strrev(a)); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The string is tpeD </li></ul>
  98. 104. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char a[]=&quot;itdept&quot;; </li></ul><ul><li>char b[15]; </li></ul><ul><li>int i=0; </li></ul><ul><li>clrscr(); </li></ul><ul><li>strncpy(b,a,2); </li></ul><ul><li>b[2]=''; </li></ul><ul><li>printf(&quot; The string is :%s&quot;,b); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  99. 105. <ul><li>Output: </li></ul><ul><li>The string is :it </li></ul>
  100. 106. String Palindrome <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int len,i,j; </li></ul><ul><li>char str[15]; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot; Enter the string:&quot;); </li></ul><ul><li>scanf(&quot;%s&quot;,str); </li></ul><ul><li>len=strlen(str); </li></ul>
  101. 107. <ul><li>for(i=0,j=len-1;i<len/2;i++,j--) </li></ul><ul><li>{ </li></ul><ul><li> if(str[i]!=str[j]) </li></ul><ul><li> { </li></ul><ul><li> printf(&quot; The String is not a palindrome&quot;); </li></ul><ul><li> getch(); </li></ul><ul><li> exit(0); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; The String is a palindrome&quot;); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Enter the string:abcba </li></ul><ul><li>The String is a palindrome </li></ul>
  102. 108. Enumerated Data Type <ul><li>It is user defined data type. </li></ul><ul><li>The user can create their own data type and define some values to the variables. </li></ul><ul><li>Syntax: </li></ul><ul><li>enum tag_name </li></ul><ul><li>{ </li></ul><ul><li>enum1; </li></ul><ul><li>enum2; </li></ul><ul><li>……… . </li></ul><ul><li>}; </li></ul>
  103. 109. Example <ul><li>#include <conio.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>enum week {sun,mon,tue,wed,thr,fri,sat}; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot; Monday=%d&quot;,mon); </li></ul><ul><li>printf(&quot; Saturday=%d&quot;,sat); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Monday=1 </li></ul><ul><li>Saturday=6 </li></ul>
  104. 110. Example <ul><li>#include <conio.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>enum week {sun=10,mon,tue,wed,thr,fri,sat}; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot; Monday=%d&quot;,mon); </li></ul><ul><li>printf(&quot; saturday=%d&quot;,sat); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Monday=11 </li></ul><ul><li>saturday=16 </li></ul>
  105. 111. Structure <ul><li>A Structure is a collection of different data items, that are stored under a common name. </li></ul><ul><li>Syntax: </li></ul><ul><li>struct structure_name </li></ul><ul><li>{ </li></ul><ul><li>structure element1; </li></ul><ul><li>structure element2; </li></ul><ul><li>…………………… . </li></ul><ul><li>}; </li></ul>
  106. 112. <ul><li>Example: </li></ul><ul><li>struct stud </li></ul><ul><li>{ </li></ul><ul><li>int sno; </li></ul><ul><li>char name[10]; </li></ul><ul><li>int mark; </li></ul><ul><li>}; </li></ul><ul><li>struct stud s; </li></ul>
  107. 113. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct stud </li></ul><ul><li>{ </li></ul><ul><li>int regno; </li></ul><ul><li>char name[10]; </li></ul><ul><li>int m1; </li></ul><ul><li>int m2; </li></ul><ul><li>int m3; </li></ul><ul><li>}; </li></ul><ul><li>struct stud s; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float tot,avg; </li></ul>
  108. 114. <ul><li>printf(&quot; Enter the student regno,name,m1,m2,m3:&quot;); scanf(&quot;%d%s%d%d%d&quot;,&s.regno,&s.name,&s.m1,&s.m2,&s.m3); </li></ul><ul><li>tot=s.m1+s.m2+s.m3; </li></ul><ul><li>avg=tot/3; </li></ul><ul><li>printf(&quot; The student Details are:&quot;); </li></ul><ul><li>printf(&quot; %d %s %f %f&quot;,s.regno,s.name,tot,avg); </li></ul><ul><li>} </li></ul>
  109. 115. Output <ul><li>Enter the student regno,name,m1,m2,m3:100 </li></ul><ul><li>aaa </li></ul><ul><li>87 </li></ul><ul><li>98 </li></ul><ul><li>78 </li></ul><ul><li>The student Details are: </li></ul><ul><li>100 aaa 263.000000 87.666664 </li></ul>
  110. 116. Structure assignment <ul><li>It is possible to assign one structure information to another structure of same type using simple assignment statement. </li></ul>
  111. 117. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li> struct </li></ul><ul><li> { </li></ul><ul><li> int a; </li></ul><ul><li> }x,y; </li></ul><ul><li> clrscr(); </li></ul><ul><li> x.a=10; </li></ul><ul><li> y=x; </li></ul><ul><li> printf(&quot;The value of y.a is%d&quot;,y.a); </li></ul><ul><li> getch(); </li></ul><ul><li>} </li></ul>
  112. 118. Output <ul><li>The value of y.a is10 </li></ul>
  113. 119. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct stud </li></ul><ul><li>{ </li></ul><ul><li>int regno; </li></ul><ul><li>char name[10],grade; </li></ul><ul><li>int m1,m2,m3; </li></ul><ul><li>float avg,tot; </li></ul><ul><li>} s[10]; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,n; </li></ul><ul><li>printf(&quot; Enter the no.of students:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&n); </li></ul>
  114. 120. <ul><li>for(i=0;i<n;i++) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; Enter the student regno,name,m1,m2,m3:&quot;); </li></ul><ul><li>scanf(&quot;%d%s%d%d%d&quot;,&s[i].regno,&s[i].name,&s[i].m1, </li></ul><ul><li>&s[i].m2,&s[i].m3); </li></ul><ul><li>s[i].tot=s[i].m1+s[i].m2+s[i].m3; </li></ul><ul><li>s[i].avg=s[i].tot/3; </li></ul><ul><li>if(s[i].m1<35||s[i].m2<35||s[i].m3<35) </li></ul><ul><li>s[i].grade='f'; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>if(s[i].avg>=75) </li></ul><ul><li> s[i].grade='d'; </li></ul><ul><li>else if(s[i].avg>=60) </li></ul>
  115. 121. <ul><li> s[i].grade='A'; </li></ul><ul><li>else if(s[i].avg>=50) </li></ul><ul><li> s[i].grade='B'; </li></ul><ul><li>else if(s[i].avg>=35) </li></ul><ul><li> s[i].grade='C'; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; STUDENT MARK LIST &quot;); </li></ul><ul><li>printf(&quot; REGNO NAME TOTAL Avg GRADE&quot;); </li></ul><ul><li>for(i=0;i<n;i++) </li></ul><ul><li>printf(&quot; %d %s %f %f %c&quot;,s[i].regno,s[i].name,s[i].tot,s[i].avg,s[i].grade); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  116. 122. <ul><li>Enter the no.of students:2 </li></ul><ul><li>Enter the student regno,name,m1,m2,m3:101 </li></ul><ul><li>aaa </li></ul><ul><li>89 </li></ul><ul><li>98 </li></ul><ul><li>78 </li></ul><ul><li>Enter the student regno,name,m1,m2,m3:102 </li></ul><ul><li>bbb </li></ul><ul><li>59 </li></ul><ul><li>68 </li></ul><ul><li>76 </li></ul><ul><li>STUDENT MARK LIST </li></ul><ul><li>REGNO NAME TOTAL Avg GRADE </li></ul><ul><li>101 aaa 265.000000 88.333336 d </li></ul><ul><li>102 bbb 203.000000 67.666664 A </li></ul>
  117. 123. Union <ul><li>An Union is a collection of different data items, that are stored under a common name. Here same memory is shared by its members. </li></ul><ul><li>Syntax: </li></ul><ul><li>union union _name </li></ul><ul><li>{ </li></ul><ul><li> union element1; </li></ul><ul><li> union element2; </li></ul><ul><li>………………… </li></ul><ul><li>}; </li></ul>
  118. 124. <ul><li>Example: </li></ul><ul><li> union result </li></ul><ul><li>{ </li></ul><ul><li>int mark; </li></ul><ul><li>float avg; </li></ul><ul><li>char grade; </li></ul><ul><li>}; </li></ul><ul><li>union result s; </li></ul>
  119. 125. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>union stud </li></ul><ul><li>{ </li></ul><ul><li>int a; </li></ul><ul><li>char b[2]; </li></ul><ul><li>}; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>union stud c; </li></ul>
  120. 126. <ul><li>c.a=256; </li></ul><ul><li>printf(&quot; c.a value is%d&quot;,c.a); </li></ul><ul><li>printf(&quot; c.b[0] value is%d&quot;,c.b[0]); </li></ul><ul><li>printf(&quot; c.b[1] value is%d&quot;,c.b[1]); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>c.a value is256 </li></ul><ul><li>c.b[0] value is0 </li></ul><ul><li>c.b[1] value is1 </li></ul>
  121. 127. <ul><li>256 = 00000010 00000000 </li></ul>Higher bit Lower bit 00000000 00000010 c.a - 2 Byte c.b[0] 1 Byte c.b[0] 1 Byte c.b[0] c.b[1]
  122. 128. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct student </li></ul><ul><li>{ </li></ul><ul><li>int a; </li></ul><ul><li>int b; </li></ul><ul><li>char c; </li></ul><ul><li>}s; </li></ul><ul><li>union student1 </li></ul><ul><li>{ </li></ul><ul><li>int a; </li></ul><ul><li>int b; </li></ul><ul><li>char c; </li></ul><ul><li>}s1; </li></ul>
  123. 129. <ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; The size of struct is %d&quot;,sizeof(s)); </li></ul><ul><li>printf(&quot; The size of union is %d&quot;,sizeof(s1)); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The size of struct is 5 </li></ul><ul><li>The size of union is 2 </li></ul>
  124. 130. Structure & Union int int char 2 Byte 2Byte 1Byte 2 Byte structure int ,int, char union
  125. 131. Preprocessor <ul><li>It is a program that processes the source program before compilation. </li></ul><ul><li>It operates under the following directives </li></ul><ul><ul><ul><ul><li>File Inclusion </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Macro substitution </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Conditional inclusion </li></ul></ul></ul></ul>
  126. 132. File Inclusion <ul><li>It is used to include some file that contains functions or some definitions. </li></ul><ul><li>Syntax: </li></ul><ul><li>#include<filename> (or) </li></ul><ul><li>#include“filename” </li></ul><ul><li>Eg: #include<stdio.h> </li></ul><ul><li> #include “ex.c” </li></ul>
  127. 133. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include &quot;addition.txt&quot; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b; </li></ul><ul><li>printf(&quot; Enter the numbers:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul><ul><li>printf(&quot;The Value is %d&quot;,add(a,b)); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  128. 134. addition.txt <ul><li>int add(int a,int b) </li></ul><ul><li>{ </li></ul><ul><li>return(a+b); </li></ul><ul><li>} </li></ul>
  129. 135. Output <ul><li>Enter the numbers:7 </li></ul><ul><li>4 </li></ul><ul><li>The Value is 11 </li></ul>
  130. 136. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include &quot;fact.c&quot; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a; </li></ul><ul><li>printf(&quot; Enter the number:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&a); </li></ul><ul><li>printf(&quot;The factorial of %d! is %d&quot;,a,rec(a)); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  131. 137. fact.c <ul><li>int rec(int x) </li></ul><ul><li>{ </li></ul><ul><li>int f; </li></ul><ul><li>if(x==1) </li></ul><ul><li>return(1); </li></ul><ul><li>else </li></ul><ul><li>f=x*rec(x-1); </li></ul><ul><li>return(f); </li></ul><ul><li>} </li></ul>
  132. 138. Output <ul><li>Enter the number:5 </li></ul><ul><li>The factorial of 5! is 120 </li></ul>
  133. 139. Macro Substitution <ul><li>It is used to define and use integer, string, or identifier in the source program </li></ul><ul><li>The three forms of macros are </li></ul><ul><ul><ul><ul><li>Simple Macro </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Argumented Macro </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Nested Macro </li></ul></ul></ul></ul>
  134. 140. Simple Macro <ul><li>It is used to define some constants </li></ul><ul><li>Syntax </li></ul><ul><li># define identifier string/integer </li></ul><ul><li>Eg: </li></ul><ul><ul><li>#define pi 3.14 </li></ul></ul><ul><ul><li>#define CITY “chennai” </li></ul></ul>
  135. 141. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define pi 3.14 </li></ul><ul><li>#define CITY &quot;chennai&quot; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;The Value is %f&quot;,2*pi); </li></ul><ul><li>printf(&quot; The Value CITY is %s&quot;,CITY); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The Value is 6.280000 </li></ul><ul><li>The Value CITY is chennai </li></ul>
  136. 142. Argumented Macro <ul><li>It is used to define some complex forms in the source program. </li></ul><ul><li>Syntax: </li></ul><ul><li>#define identifier (v1,v2,….) string/integer </li></ul><ul><li>Eg: </li></ul><ul><li>#define cube(n) (n*n*n) </li></ul>
  137. 143. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define cube(n) (n*n*n) </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;The Value of 3 cube is %d&quot;,cube(3)); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The Value of 3 cube is 27 </li></ul>
  138. 144. Nested Macro <ul><li>Here one macro is used by another macro. </li></ul><ul><li>Eg: </li></ul><ul><li>#define a 3 </li></ul><ul><li>#define sq a*a </li></ul>
  139. 145. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define a 3 </li></ul><ul><li>#define sq a*a </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;The Value is %d&quot;,sq); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The Value is 9 </li></ul>
  140. 146. Conditional Inclusion <ul><li>It is used to include some conditional statements. </li></ul>
  141. 147. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define a 3 </li></ul><ul><li>#ifdef a </li></ul><ul><li>#define c a+5 </li></ul><ul><li>#endif </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; The value C is %d&quot;,c); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>The value C is 8 </li></ul>
  142. 148. Pointers <ul><li>Pointer is a variable that contains the memory address of another variable. </li></ul>
  143. 149. <ul><li>Example: </li></ul><ul><li> x=5 </li></ul><ul><li> x Variable </li></ul><ul><li> 1002 Address </li></ul>5 Value
  144. 150. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x=5; </li></ul><ul><li>printf(&quot; The Address of x = %u&quot;,&x); </li></ul><ul><li>printf(&quot; The Value of x = %d&quot;,x); </li></ul><ul><li>} </li></ul><ul><li>Output </li></ul><ul><li>The Address of x = 8714 </li></ul><ul><li>The Value of x = 5 </li></ul>
  145. 151. Pointer Declaration <ul><li>Syntax </li></ul><ul><li>data-type *pointer-name; </li></ul><ul><li>data-type - Type of the data to which the pointer points. </li></ul><ul><li>pointer-name - Name of the pointer </li></ul><ul><li>Example: int *a; </li></ul>
  146. 152. Accessing Variable through Pointer <ul><li>If a pointer is declared and assigned to a variable, then the variable can be accessed through the pointer. </li></ul><ul><li>Example: </li></ul><ul><li>int *a; </li></ul><ul><li>x=5; </li></ul><ul><li>a=&x; </li></ul>
  147. 153. <ul><li>Example </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x=5; </li></ul><ul><li>int *a; </li></ul><ul><li>a=&x; </li></ul><ul><li>printf(&quot; The Value of x = %d&quot;,x); </li></ul><ul><li>printf(&quot; The Address of x = %u&quot;,&x); </li></ul><ul><li>printf(&quot; The Value of a = %d&quot;,a); </li></ul><ul><li>printf(&quot; The Value of x = %d&quot;,*a); </li></ul><ul><li>} </li></ul>
  148. 154. Output <ul><li>The Value of x = 5 </li></ul><ul><li>The Address of x = 8758 </li></ul><ul><li>The Value of a = 8758 </li></ul><ul><li>The Value of x = 5 </li></ul>
  149. 155. <ul><li>Example: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int y=10; </li></ul><ul><li>int *a; </li></ul><ul><li>a=&y; </li></ul><ul><li>printf(&quot; The Value of y = %d&quot;,y); </li></ul><ul><li>printf(&quot; The Address of y = %u&quot;,&y); </li></ul><ul><li>printf(&quot; The Value of a = %d&quot;,a); </li></ul><ul><li>printf(&quot; The Address of a = %u&quot;,&a); </li></ul><ul><li>} </li></ul>
  150. 156. 5001 10 8000 a y 5001 Variable Value Address
  151. 157. Output <ul><li>The Value of y = 10 </li></ul><ul><li>The Address of y = 5001 </li></ul><ul><li>The Value of a = 5001 </li></ul><ul><li>The Address of a = 8000 </li></ul>
  152. 158. Null Pointer <ul><li>A pointer is said to be null pointer if zero is assigned to the pointer. </li></ul><ul><li>Example </li></ul><ul><li>int *a,*b; </li></ul><ul><li>a=b=0; </li></ul>
  153. 159. Pointer to Pointer <ul><li>Here one pointer stores the address of another pointer variable. </li></ul><ul><li>Example: </li></ul><ul><li>int x=10,*a,**b; </li></ul><ul><li>a=&x; </li></ul><ul><li>b=&a; </li></ul>
  154. 160. 5001 10 8000 a x 5001 Variable Value Address 8000 9000 b
  155. 161. <ul><li>Example </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a=10; </li></ul><ul><li>int *b,**c; </li></ul><ul><li>b=&a; </li></ul><ul><li>c=&b; </li></ul><ul><li>printf(&quot; The Value of a = %d&quot;,a); </li></ul><ul><li>printf(&quot; The Address of a = %u&quot;,&a); </li></ul><ul><li>printf(&quot; The Value of b = %d&quot;,b); </li></ul><ul><li>printf(&quot; The Address of b = %u&quot;,&b); </li></ul><ul><li>printf(&quot; The Value of c = %d&quot;,c); </li></ul><ul><li>printf(&quot; The Address of c = %u&quot;,&c); </li></ul><ul><li>} </li></ul>
  156. 162. Output <ul><li>The Value of a = 10 </li></ul><ul><li>The Address of a = 5001 </li></ul><ul><li>The Value of b = 5001 </li></ul><ul><li>The Address of b = 8000 </li></ul><ul><li>The Value of c = 8000 </li></ul><ul><li>The Address of c = 9000 </li></ul>
  157. 163. Pointers and Functions <ul><li>Call by Value </li></ul><ul><li>Call by Reference </li></ul>
  158. 164. Call by value <ul><li>Actual argument passed to the formal argument. </li></ul><ul><li>Any changes to the formal argument does not affect the actual argument. </li></ul>
  159. 165. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x,y,swap(int,int); </li></ul><ul><li>printf(&quot; Enter value of x:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&x); </li></ul><ul><li>printf(&quot; Enter value of y:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&y); </li></ul>
  160. 166. <ul><li>change(x,y); </li></ul><ul><li>printf(&quot; Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} </li></ul><ul><li>int swap(int a,int b) </li></ul><ul><li>{ </li></ul><ul><li>int c; </li></ul><ul><li>c=a; </li></ul><ul><li>a=b; </li></ul><ul><li>b=c; </li></ul><ul><li>printf(&quot; Values in the Function -->x=%d,y=%d&quot;,a,b); </li></ul><ul><li>} </li></ul>
  161. 167. Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Function -->x=6,y=5 </li></ul><ul><li>Values in the Main()-->x=5,y=6 </li></ul>
  162. 169. Call by reference <ul><li>Instead of passing value, the address of the argument will be passed. </li></ul><ul><li>Any changes to the formal argument will affect the actual argument. </li></ul>
  163. 170. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x,y,change(int*,int*); </li></ul><ul><li>printf(&quot; Enter value of x:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&x); </li></ul><ul><li>printf(&quot; Enter value of y:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&y); </li></ul>
  164. 171. <ul><li>change(&x,&y); </li></ul><ul><li>printf(&quot; Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} </li></ul><ul><li>int change(int *a,int *b) </li></ul><ul><li>{ </li></ul><ul><li>int c; </li></ul><ul><li>c=*a; </li></ul><ul><li>*a=*b; </li></ul><ul><li>*b=c; </li></ul><ul><li>printf(&quot; Values in the Function -->x=%d,y=%d&quot;,*a,*b); </li></ul><ul><li>} </li></ul>
  165. 172. Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Function -->x=6,y=5 </li></ul><ul><li>Values in the Main()-->x=6,y=5 </li></ul>
  166. 173. Pointer to Array <ul><li>The elements of the array can also be accessed through a pointer. </li></ul><ul><li>Example </li></ul><ul><li>int a[3]={2,3,7}; </li></ul><ul><li>int *b; </li></ul><ul><li>b=a; </li></ul>
  167. 174. <ul><li>Example: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a[3]={2,3,7}; </li></ul><ul><li>int *b; </li></ul><ul><li>b=a; </li></ul><ul><li>printf(&quot; The Value of a[0] = %d&quot;,a[0]); </li></ul><ul><li>printf(&quot; The Address of a[0] = %u&quot;,&a[0]); </li></ul><ul><li>printf(&quot; The Value of b = %d&quot;,b); </li></ul><ul><li>} </li></ul>
  168. 175. 8744 2 9000 b a[0] 8744 Variable Value Address
  169. 176. Output <ul><li>The Value of a[0] = 2 </li></ul><ul><li>The Address of a[0] = 8744 </li></ul><ul><li>The Value of b = 8744 </li></ul>
  170. 177. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a[5]={2,3,7,9,10}; </li></ul><ul><li>int i; </li></ul><ul><li>for(i=0;i<5;i++) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; The Value of a[%d] = %d&quot;,i,a[i]); </li></ul><ul><li>printf(&quot; The Address of a[%d] = %u&quot;,i,&a[i]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  171. 178. a[0] a[1] a[2] a[3] a[4] 8724 8726 8728 8730 8732 Array Value Address 10 9 7 3 2
  172. 179. Output <ul><li>The Value of a[0] = 2 </li></ul><ul><li>The Address of a[0] = 8724 </li></ul><ul><li>The Value of a[1] = 3 </li></ul><ul><li>The Address of a[1] = 8726 </li></ul><ul><li>The Value of a[2] = 7 </li></ul><ul><li>The Address of a[2] = 8728 </li></ul><ul><li>The Value of a[3] = 9 </li></ul><ul><li>The Address of a[3] = 8730 </li></ul><ul><li>The Value of a[4] = 10 </li></ul><ul><li>The Address of a[4] = 8732 </li></ul>
  173. 180. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a[5]={1,2,3,4,5}; </li></ul><ul><li>int i,sum=0; </li></ul><ul><li>int *b; </li></ul><ul><li>b=a; </li></ul><ul><li>for(i=0;i<5;i++) </li></ul><ul><li>{ </li></ul><ul><li>sum=sum + *b; </li></ul><ul><li>b++; //b=b+1 </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; The Sum is %d&quot;,sum); </li></ul><ul><li>} </li></ul>
  174. 181. Output <ul><li>The Sum is 15 </li></ul>
  175. 182. Pointer and Structures <ul><li>Syntax: </li></ul><ul><li>struct structure_name </li></ul><ul><li>{ </li></ul><ul><li>structure element1; </li></ul><ul><li>structure element2; </li></ul><ul><li>…………………… . </li></ul><ul><li>}variable,*ptr; </li></ul>
  176. 183. <ul><li>Example: </li></ul><ul><li>struct stud </li></ul><ul><li>{ </li></ul><ul><li>int sno; </li></ul><ul><li>char name[10]; </li></ul><ul><li>int mark; </li></ul><ul><li>}; </li></ul><ul><li>struct stud *s; </li></ul>
  177. 184. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct stud </li></ul><ul><li>{ </li></ul><ul><li>int regno; </li></ul><ul><li>char name[10]; </li></ul><ul><li>int m1; </li></ul><ul><li>int m2; </li></ul><ul><li>int m3; </li></ul><ul><li>}; </li></ul><ul><li>struct stud s; </li></ul><ul><li>struct stud *t; </li></ul>
  178. 185. <ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float tot,avg; </li></ul><ul><li>t=&s; </li></ul><ul><li>printf(&quot; Enter the student regno,name,m1,m2,m3:&quot;); </li></ul><ul><li>scanf(&quot;%d%s%d%d%d&quot;,&s.regno,&s.name,&s.m1,&s.m2,&s.m3); </li></ul><ul><li>tot=s.m1+s.m2+s.m3; </li></ul><ul><li>avg=tot/3; </li></ul><ul><li>printf(&quot; The student Details are:&quot;); </li></ul><ul><li>printf(&quot; %d %s %f %f&quot;,s.regno,s.name,tot,avg); </li></ul><ul><li>printf(&quot; %d %s %f %f&quot;,t->regno,t->name,tot,avg); </li></ul><ul><li>} </li></ul>
  179. 186. Output <ul><li>Enter the student regno,name,m1,m2,m3:1 </li></ul><ul><li>aaa </li></ul><ul><li>76 </li></ul><ul><li>89 </li></ul><ul><li>76 </li></ul><ul><li>The student Details are: </li></ul><ul><li>1 aaa 241.000000 80.333336 </li></ul><ul><li>1 aaa 241.000000 80.333336 </li></ul>
  180. 187. Command Line Argument <ul><li>It allows the user to pass some information to the program while running the program. </li></ul>
  181. 188. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main(int argc,char argv[]) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; The Argument is %s&quot;,argv[0]); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>  </li></ul>
  182. 189. Output <ul><li>C: c>a </li></ul><ul><li>The Argument is C:TCA.EXE </li></ul>
  183. 190. String Palindrome <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char s1[15],s2[15]; </li></ul><ul><li>printf(&quot; enter the string:&quot;); </li></ul><ul><li>scanf(&quot;%s&quot;,s1); </li></ul><ul><li>strcpy(s2,s1); </li></ul><ul><li>strrev(s1); </li></ul>
  184. 191. <ul><li>if(strcmp(s1,s2)==0) </li></ul><ul><li>printf(&quot; The string is palindrome&quot;); </li></ul><ul><li>else </li></ul><ul><li> printf(&quot; The string is not a palindrome&quot;); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>enter the string: aba </li></ul><ul><li>The string is palindrome </li></ul>
  185. 192. Developing a ‘C’ Program <ul><li>The Program development life cycle is considered as a sequence of events by the programmer to develop the program. </li></ul><ul><li>The Program development life cycle contains the following phase </li></ul><ul><ul><li>Program Design </li></ul></ul><ul><ul><li>Program Coding </li></ul></ul><ul><ul><li>Program Testing </li></ul></ul>
  186. 193. Program Design <ul><li>Analysing the problem </li></ul><ul><li>Algorithm development </li></ul><ul><li>Selection of conditional and control structure etc,. </li></ul>
  187. 194. Program Coding <ul><li>Documentation </li></ul><ul><li>Statement construction </li></ul><ul><li>Input and output format etc,. </li></ul>
  188. 195. Program Testing <ul><li>It is the process of executing the program with sample data </li></ul>
  189. 197. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#define p 3.14 </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>#ifdef p </li></ul><ul><li>printf(&quot; Pentium&quot;); </li></ul><ul><li>#else </li></ul><ul><li>printf(&quot; Celeron&quot;); </li></ul><ul><li>#endif </li></ul><ul><li>printf(&quot; the value is %f&quot;,2*p); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Pentium </li></ul><ul><li>the value is 6.280000 </li></ul>
  190. 199. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct stud </li></ul><ul><li>{ </li></ul><ul><li>int regno; </li></ul><ul><li>char name[10]; </li></ul><ul><li>int m1; </li></ul><ul><li>int m2; </li></ul><ul><li>int m3; </li></ul><ul><li>}; </li></ul><ul><li>struct stud s; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float tot,avg; </li></ul><ul><li>int i; </li></ul>
  191. 200. <ul><li>printf(&quot; Enter the student regno,name,m1,m2,m3:&quot;); </li></ul><ul><li>for(i=0;i<2;i++) </li></ul><ul><li>{ </li></ul><ul><li>scanf(&quot;%d%s%d%d%d&quot;,&s.regno,&s.name,&s.m1,&s.m2,&s.m3); </li></ul><ul><li>tot=s.m1+s.m2+s.m3; </li></ul><ul><li>avg=tot/3; </li></ul><ul><li>} </li></ul><ul><li>for(i=0;i<2;i++) </li></ul><ul><li>printf(&quot;%d %s %f %f&quot;,s.regno,s.name,tot,avg); </li></ul><ul><li>} </li></ul>
  192. 201. <ul><li>Enter the student regno,name,m1,m2,m3:100 </li></ul><ul><li>aaa </li></ul><ul><li>78 </li></ul><ul><li>67 </li></ul><ul><li>98 </li></ul><ul><li>101 </li></ul><ul><li>bbb </li></ul><ul><li>80 </li></ul><ul><li>90 </li></ul><ul><li>75 </li></ul><ul><li>bbb 245.000000 81.666664 </li></ul><ul><li>101bbb 245.000000 81.666664 </li></ul>
  193. 202. <ul><li>#include <string.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>int main(void) </li></ul><ul><li>{ </li></ul><ul><li>char string[15]; </li></ul><ul><li>char *ptr, c = 'r'; </li></ul><ul><li>strcpy(string, &quot;This is a string&quot;); </li></ul><ul><li>ptr = strchr(string, c); </li></ul><ul><li>if (ptr) </li></ul><ul><li>printf(&quot;The character %c is at position: %d &quot;, c, ptr-string); </li></ul><ul><li>else </li></ul><ul><li>printf(&quot;The character was not found &quot;); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  194. 203. Example <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char a[]=&quot;Dept&quot;; </li></ul><ul><li>int i=0; </li></ul><ul><li>clrscr(); </li></ul><ul><li>while(a[i]!='') </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; The character is %c&quot;,a[i]); </li></ul><ul><li>i++; </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  195. 204. <ul><li>The character is D </li></ul><ul><li>The character is e </li></ul><ul><li>The character is p </li></ul><ul><li>The character is t </li></ul>
  196. 206. Structure
  197. 210. Lab Exercise C Programs
  198. 211. Function-with arg & return <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c; </li></ul><ul><li>int add(int,int); </li></ul><ul><li>printf(&quot; Enter two number:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul><ul><li>c=add(a,b); </li></ul><ul><li>printf(&quot; Sum is:%d&quot;,c); </li></ul><ul><li>} </li></ul><ul><li>int add(int x,int y) </li></ul><ul><li>{ </li></ul><ul><li>int z; </li></ul><ul><li>z=x+y; </li></ul><ul><li>return(z); </li></ul><ul><li>} </li></ul>
  199. 212. Output <ul><li>Enter two number:6 </li></ul><ul><li>7 </li></ul><ul><li>Sum is:13 </li></ul>
  200. 213. Example <ul><li>#include <stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int x,y,change(int*,int*); </li></ul><ul><li>printf(&quot; Enter value of x:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&x); </li></ul><ul><li>printf(&quot; Enter value of y:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&y); </li></ul>
  201. 214. <ul><li>change(&x,&y); </li></ul><ul><li>printf(&quot; Values in the Main()-->x=%d,y=%d&quot;,x,y); </li></ul><ul><li>} </li></ul><ul><li>int change(int *a,int *b) </li></ul><ul><li>{ </li></ul><ul><li>int c; </li></ul><ul><li>c=*a; </li></ul><ul><li>*a=*b; </li></ul><ul><li>*b=c; </li></ul><ul><li>printf(&quot; Values in the Function -->x=%d,y=%d&quot;,*a,*b); </li></ul><ul><li>} </li></ul>
  202. 215. Output <ul><li>Enter value of x:5 </li></ul><ul><li>Enter value of y:6 </li></ul><ul><li>Values in the Function -->x=6,y=5 </li></ul><ul><li>Values in the Main()-->x=6,y=5 </li></ul>
  203. 216. Factorial-Recursive Fn <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a; </li></ul><ul><li>int rec(int); </li></ul><ul><li>printf(&quot; Enter the number:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&a); </li></ul><ul><li>printf(&quot;The factorial of %d! is %d&quot;,a,rec(a)); </li></ul><ul><li>} </li></ul>
  204. 217. <ul><li>int rec(int x) </li></ul><ul><li>{ </li></ul><ul><li>int f; </li></ul><ul><li>if(x==1) </li></ul><ul><li>return(1); </li></ul><ul><li>else </li></ul><ul><li>f=x*rec(x-1); </li></ul><ul><li>return(f); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Enter the number:5 </li></ul><ul><li>The factorial of 5! is 120 </li></ul>
  205. 218. Example: Working of 3!
  206. 219. Matrix Multiplication <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,j,k,r1,r2,c1,c2; </li></ul><ul><li>int a[5][5],b[5][5],c[5][5]; </li></ul><ul><li>clrscr(); </li></ul><ul><li>step1: </li></ul><ul><li>printf(&quot; Enter the size of matrix A &quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&r1,&c1); </li></ul><ul><li>printf(&quot; Enter the size of matrix B &quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&r2,&c2); </li></ul><ul><li>if(c1==r2) </li></ul><ul><li>goto step2; </li></ul><ul><li>else </li></ul><ul><li>goto step1; </li></ul>
  207. 220. <ul><li>step2: </li></ul><ul><li>printf(&quot; Enter the elements of matrix A &quot;); </li></ul><ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c1;j++) </li></ul><ul><li>{ </li></ul><ul><li>scanf(&quot;%d&quot;,&a[i][j]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; Enter the elements of matrix B &quot;); </li></ul><ul><li>for(i=0;i<r2;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>{ </li></ul><ul><li>scanf(&quot; %d&quot;,&b[i][j]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  208. 221. <ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>{ </li></ul><ul><li>c[i][j]=0; </li></ul><ul><li>for(k=0;k<c1;k++) </li></ul><ul><li>{ </li></ul><ul><li>c[i][j]=c[i][j]+a[i][k]*b[k][j]; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>for(i=0;i<r1;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=0;j<c2;j++) </li></ul><ul><li>printf(&quot;%d &quot;,c[i][j]); </li></ul><ul><li>printf(&quot; &quot;); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  209. 222. Output <ul><li>Enter the size of matrix A:2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the size of matrix B:2 </li></ul><ul><li>2 </li></ul><ul><li>Enter the elements of matrix A </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>Enter the elements of matrix B </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>4 </li></ul><ul><li>The resultant matrix is </li></ul><ul><li>32 32 </li></ul><ul><li>32 32 </li></ul>
  210. 224. Lab Ex:9,Finding area, circumference of circle <ul><li>#include<stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main ( ) </li></ul><ul><li>{ </li></ul><ul><li>int r; </li></ul><ul><li>float area,c; </li></ul><ul><li>clrscr( ); </li></ul><ul><li>printf(&quot; Enter the value of r:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&r); </li></ul>
  211. 225. <ul><li>area=3.14*r*r; </li></ul><ul><li>c=2*3.14*r; </li></ul><ul><li>printf(&quot; The area is :%f&quot;,area); </li></ul><ul><li>printf(&quot; The circumference is :%f&quot;,c); </li></ul><ul><li>getch( ); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Enter the value of r:7 </li></ul><ul><li>The area is :153.860001 </li></ul><ul><li>The circumference is :43.959999 </li></ul>
  212. 226. Lab Ex:9,Conversion of Celsius to Fahrenheit <ul><li>#include<stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main ( ) </li></ul><ul><li>{ </li></ul><ul><li>float c,f; </li></ul><ul><li>clrscr( ); </li></ul><ul><li>printf(&quot; Enter the value of c:&quot;); </li></ul><ul><li>scanf(&quot;%f&quot;,&c); </li></ul><ul><li>f=(c*1.8)+32; </li></ul><ul><li>printf(&quot; The Fahrenheit is :%f&quot;,f); </li></ul><ul><li>getch( ); </li></ul><ul><li>} </li></ul>
  213. 227. <ul><li>Enter the value of c:35 </li></ul><ul><li>The fahrenheit is :95.000000 </li></ul>
  214. 228. Lab Ex:11,Arithmetic operations <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c,d,e,f; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot; Enter the values of A and B:&quot;); </li></ul><ul><li>scanf(&quot;%d%d&quot;,&a,&b); </li></ul><ul><li>c=a+b; </li></ul><ul><li>d=a-b; </li></ul><ul><li>e=a*b; </li></ul><ul><li>f=a/b; </li></ul>
  215. 229. <ul><li>printf(&quot; The values of A + B:%d&quot;,c); </li></ul><ul><li>printf(&quot; The values of A - B:%d&quot;,d); </li></ul><ul><li>printf(&quot; The values of A * B:%d&quot;,e); </li></ul><ul><li>printf(&quot; The values of A / B:%d&quot;,f); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  216. 230. Output <ul><li>Enter the values of A and B:6 </li></ul><ul><li>3 </li></ul><ul><li>The values of A + B:9 </li></ul><ul><li>The values of A - B:3 </li></ul><ul><li>The values of A * B:18 </li></ul><ul><li>The values of A / B:2 </li></ul>
  217. 231. Lab.Ex13,Largest among 3 nos <ul><li>#include<stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main ( ) </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c; </li></ul><ul><li>clrscr( ); </li></ul><ul><li>printf(&quot; Enter the value of a:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&a); </li></ul><ul><li>printf(&quot; Enter the value of b:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&b); </li></ul><ul><li>printf(&quot; Enter the value of c:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&c); </li></ul>
  218. 232. <ul><li>if((a>b)&&(a>c)) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; A is Big&quot;); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li> if(b>c) </li></ul><ul><li>printf(&quot; B is Big&quot;); </li></ul><ul><li> else </li></ul><ul><li>printf(&quot; C is Big&quot;); </li></ul><ul><li>} </li></ul><ul><li>getch( ); </li></ul><ul><li>} </li></ul>
  219. 233. Output <ul><li>Enter the value of a:5 </li></ul><ul><li>Enter the value of b:7 </li></ul><ul><li>Enter the value of c:3 </li></ul><ul><li>B is Big </li></ul>
  220. 234. String Palindrome <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int len,i,j; </li></ul><ul><li>char str[15]; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot; Enter the string:&quot;); </li></ul><ul><li>scanf(&quot;%s&quot;,str); </li></ul><ul><li>len=strlen(str); </li></ul>
  221. 235. <ul><li>for(i=0,j=len-1;i<len/2;i++,j--) </li></ul><ul><li>{ </li></ul><ul><li> if(str[i]!=str[j]) </li></ul><ul><li> { </li></ul><ul><li> printf(&quot; The String is not a palindrome&quot;); </li></ul><ul><li> getch(); </li></ul><ul><li> exit(0); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>printf(&quot; The String is a palindrome&quot;); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Enter the string:abcba </li></ul><ul><li>The String is a palindrome </li></ul>
  222. 236. Lab.Ex:14,Quadratic Equation <ul><li>#include<stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>#include<math.h> </li></ul><ul><li>void main ( ) </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c,d,r1,r2; </li></ul><ul><li>clrscr( ); </li></ul><ul><li>printf(&quot; Enter the value of a:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&a); </li></ul><ul><li>printf(&quot; Enter the value of b:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&b); </li></ul><ul><li>printf(&quot; Enter the value of c:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&c); </li></ul><ul><li>d=b*b-4*a*c; </li></ul>
  223. 237. <ul><li>if(d>=0) </li></ul><ul><li>{ </li></ul><ul><li>r1=(-b+sqrt(d))/(2*a); </li></ul><ul><li>r2=(-b-sqrt(d))/(2*a); </li></ul><ul><li>printf(&quot; The roots are %d,%d&quot;,r1,r2); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot; The roots are imaginary&quot;); </li></ul><ul><li>} </li></ul><ul><li>getch( ); </li></ul><ul><li>} </li></ul>
  224. 238. Output <ul><li>Enter the value of a:1 </li></ul><ul><li>Enter the value of b:4 </li></ul><ul><li>Enter the value of c:4 </li></ul><ul><li>The roots are -2,-2 </li></ul>

×