You are on page 1of 114

Welcome C- Language Introduction:C is a general purpose structured programming language.

Its instructions consists of English terms that resembles algrabic expressions, augmented by certain keywords such as if, else, for, while and do. It is a compile oriented case-sensitive language. History of C:C was originally developed in the 1970s by Dennis Ritchie at Bell Telephone labs USA..It was an out growth of two earlier languages called BCPL & B. C was largely confined to use with in Bell Labs until 1978. The C character Set:C was upper case letters A to Z. Lower case letters a to z, digits 0 to 9. and the following special characters %, $. \, -, ! , *, /, <, >, =, #, (, ), _ , [, ], . , ?, &. Blank, etc Identifiers and keywords:Identifiers are names given to various elements such as variables, functions, arrays etc., Identifiers consist of letters, digits and only special character _ (under score). Identifiers sno sname ml tm average_marks Keywords (reserved words) if else for do while

Keywords or reserved words in the programming for a specific task. Data Types:C supports several different types of data each of which may be represented differently with the computers memory. The basic data types are Int Float Char Double integer quantity floating point single character double precision floating 2 bytes 4 bytes 1 byte 8 bytes Range (-32768 to +32767)

Constants:C has four basic types of constants. They are integer constants, floating , point constant, character constant and string constants. Integer Constant:-

Sri

Page2 of 114 An integer constants is a number which consists of a sequence of digits Ex: 45, 234, 8783 etc.

Floating point constant:A floating point constant is a number that contains either a decimal point or an exponent or both. Ex: 59.34, 123.59 Character constant:A character constant is a single character enclosed in single quotation marks. Ex: M, A. String constants:A string constant consists of any number of consecutive characters enclosed in double quotation marks. Ex: ramu, India. Variables:A variable is an identifier this is used to represent some specified type of information with in a designated portion of the program. In simplest form, a variable represents a single data item, that is a numerical quantity or a character constant. DECLARTIONS Variable declarations:A declaration associates a group of variables with a specific data type. All variables must be declared before they can appear in executable statement. A declaration constant of data type followed by one or more variables names, ending with a semi colon(;). Syntax:Data _type variable_name = value; Ex; int a, b, c: int Sno=1854; char sna[20]=ramu. char sco=m. float am= 98.50.

Structure of a c program:Every program consists of one or more functions, one of which must be called main additional function definitions may be precede or follow main(). Each function must contain: 1. A function heading, which consists of the function name,. Followed by an optional list of arguments enclosed in parentheses. 2. A list of argument declaration, if argument included in the heading. 3. A compound statement, which comprises the remainder of the function.

Bye from this page

SISI Computers

Sri

Page3 of 114

Clrscr () function:Syn:- clrscr(); It is used to the clear the screen. Printf () function:Syn:- printf(format, variables); It is used to printf the formatted data on the screen, where format is a string which contains escape sequence characters and conversion characters. Escape sequence characters: \n --------- new line \t --------- tab space \b --------- back space \r --------- form feed \ ---------- for double quotation mark \ ---------- For single quotation mark. \\ ---------- For backslash Conversion Characters:Integer ---------%d Unsigned --------- %u Long integer -------- %ld Unsigned hexa decimal ------- %x Unsigned octal --------- %o Float --------- %f Double --------Character ---------- %c String ---------

%lf %s

Comments:A comment is a non executable statement, which is used only for user reference. These lines ignored by C- complier. Syn:- /* comment */ Function keys to execute a program: F2 save F9 compile and make Ctrl+F9 Execute (run) Alt+F5 Output screen. F3 Load.

Bye from this page

SISI Computers

Sri

Page4 of 114

1. /* this is my first program in c */ main() { clrscr( ); printf ("\n \n \n \t\ t \t Welcome to \"SISI\" \n "); printf ("\n \n \t \t \t For Mr. Surya Kiran, Mr. Suresh and Mr. Prudhvi"); getch(); } Scanf () function:Syn: Scanf (format, variables); It is used to read data form the standard input device (keyboard) and stored into the memory at the specified location. For this we use address of operator (&) proceeded by a numeric variable. 2. /* program to print the sum of 100 and 200 */ main() { int a=100,b=200,c; clrscr(); c=a+b; printf("sum is .. %d",c); getch(); } 3. /* program to print the sum of two accepted numbers */ main() { int a,b,c; clrscr(); printf("enter first number : "); scanf("%d",&a); printf("enter second number : "); scanf("%d",&b); c=a+b; printf("sum is .. %d",c); getch(); } 4./* Programme to Print my details */ main() { int sno= 3456; char name[10]="Deepak"; char sco='m'; float sal=15454.50; clrscr();

Bye from this page

SISI Computers

Sri

Page5 of 114 printf ("employee id number : %d \n", sno); printf ("employee name : %s \n", name); printf ("sex code : %c \n", sco); printf ("salary : %.2f \n", sal); getch(); }

Arthematic Operators: + addition - Subtraction * multipliacation % modulus( remainder) 10+20 = 30 40.10 =30 20*2 = 40 123/10 =12.3 123%10 =3 Assignment Operators Syn:- Variable =Expr; It is used to store the result of an expression into one or more variables where expression is any value, variable, function, and arthematic equation. Syn:- Variable = expr; a = 100; b = 200; c = a+b; d = c; l = strlen(st); Uniary Operators + positive - Negative ++ Increment --Decrement A =10; A++; ++ is used to increment by one, -- is used to decrement by one. Increment and decrement may the post or pre. combined operators : +=, -=,/=,*=,%= ex: a=100; a=a+25; or a+=25; =125; Relation Operators < ,>,==,>=,<=,!=

Bye from this page

SISI Computers

Sri

Page6 of 114

Logically Opearators && - and || - or ! - not. Terinary Operators/ Conditional Operator(?):Syn: ( condition ) ? expr1: expr2: If the condition is true returns the first expression otherwise it returns second expression. 5. . /* program to print the biggest of two accepted numbers */ main() { int a,b,big; clrscr(); Printf("enter any two numbers : "); scanf("%d %d",&a,&b); big = (a>b) ? a : b; printf("big is .. %d",big); getch(); } If statement: Syn:1 If (<expression >) <statement1> Syn:2 If (<expression>) <statement1> else <statement 2> The value of <expression1> is evaluated and if it is non zero, then <statment1> is excuted in the second case <statement 2> is excuted if the expression is 0. And optional else may follow an if statement, but no statements can between on if statement and an else. 6. /* program to print the biggest of two accepted numbers using if condition */ main() { int a,b,big; clrscr(); printf("enter any two numbers : ");

Bye from this page

SISI Computers

Sri scanf("%d %d",&a,&b); if (a>b) big = a; else big = b; printf("big is .. %d",big); getch(); }

Page7 of 114

7. /* program to print the biggest of three accepted numbers */ main() { int a,b,c,big; clrscr(); printf("enter any three numbers : "); scanf("%d %d %d",&a,&b,&c); if (a>b && a>c) big = a; else if (b>c) big = b; else big = c; printf("big is .. %d",big); getch(); } 8. /* program to print the biggest of four accepted numbers */ main() {int a,b,c,d,big; clrscr(); printf("enter four numbers:"); scanf("%d %d %d %d",&a,&b,&c,&d); if (a>b && a>c && a>d) big=a; else if (b>c && b>d) big=b; else if (c>d) big=c; else big=d; printf("big number is:%d",big); getch(); } 9. /* program to print whether the given number is odd number or even number*/

Bye from this page

SISI Computers

Sri main() { int n; clrscr(); printf("enter any number:"); scanf("%d",&n); if (n%2==0) printf("it is even number:"); else printf("it is odd number:"); getch(); }

Page8 of 114

10. /* program to check whether the given number is positive or negative or zero */ main() { int n; clrscr(); printf("enter any number:"); scanf("%d",&n); if(n>0) printf("it is positive number:"); else if (n<0) printf("it is negative number:"); else printf("it is zero:"); getch(); } Switch Statement: Syn: switch(expr) { case value: statement; break; case value: statement; break; : : default: statement; } It is used to compare the expression with values , if it is euqal then it excute related statement and teriminate statement until the break statement. The default statement will be excuted where there is a no match.

Bye from this page

SISI Computers

Sri

Page9 of 114

11. /* write a program to accept any no between1 to3 and printf the numbers in words*/ main() { int a; clrscr(); printf("enter any number:"); scanf("%d",&a); switch(a) { case 1: printf("enter one"); break; case 2: printf("enter two"); break; case 3: printf("enter three"); break; default: printf("invalid number"); } getch(); } 12. /*program to accept any week number between 1 to 7 and print the name of week */ main() { int a; clrscr(); printf("enter any number:"); scanf("%d",&a); switch(a) { case 1: printf("sunday");break; case 2: printf("monday");break; case 3: printf("tuesday");break; case 4: printf("wednesday");break; case 5: printf("thrusday");break; case 6: printf("friday");break; case 7: printf("saturday");break; default: printf("invalid number"); } getch(); } 13. /*program to accept any month number between 1 to 12 and print no.of days in it*/ main() { int a; clrscr(); printf("enter any month number:"); scanf("%d",&a);

Bye from this page

SISI Computers

Sri switch(a) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31 days");break; case 4: case 6: case 9: case 11:printf("30 days");break; case 2: printf("29 days");break; default: printf("invalid number"); } getch(); }

Page10 of 114

Loops In C 1. While loop 2. Do while loop 3. For loop 1. While loop: Syn: while (<expression>) <statement > <statements> is occurated repeatedly as long as the value of <expression > remains non zero. The test takes place before each excution of the <statement> 14. /*programm to print numbers from 1 to 100 */ main() { int i=1; clrscr(); while(i<=100) { printf("%d\t",i); i++; } getch(); } 15. /* program to print odd numbers from 1 to 100*/ main() { int i=1;

Bye from this page

SISI Computers

Sri clrscr(); while(i<=100) { printf("%d\t",i); i=i+2; } getch(); }

Page11 of 114

16. /* program to print even numbers from 1 to 100*/ main() { int i=2; clrscr(); while(i<=100) { printf("%d\t",i); i=i+2; } getch(); } 17. /* program to print sum of natural numbers from 1 to 10*/ main() { int s=0,i=1; clrscr(); while(i<=10) { s=s+i; i++; } printf("sum of natural numbers: %d",s); getch(); } 18. /*program to print sum of odd numbers from 1 to 10*/ main() { int s=0,i=1; clrscr(); while(i<=10) { s=s+i; i=i+2; }

Bye from this page

SISI Computers

Sri printf("sum of odd numbers:%d",s); getch(); }

Page12 of 114

19./* program to print sum of even numbers from 1 to 10*/ main() { int s=0,i=2; clrscr(); while(i<10) { s=s+i; i=i+2; } printf("sum of even numbers:%d",s); getch(); } 20./* program to print sum of odd, sum of even, no.of odd, no.of even, sum of natural numbers from 1 to 10*/ main() { int so,se,no,ne,sn,i=1; so=se=no=ne=sn=0; clrscr(); while(i<=10) { if(i%2==0) { se=se+i; ne++; } else { so=so+i; no++; } sn=sn+i; i++; } printf("sum of odd: %d\n",so); printf("sum of even: %d\n",se); printf("no.of odd: %d\n",no); printf("no.of even: %d\n",ne); printf("sum of natural numbers: %d\n",sn); getch(); }

Bye from this page

SISI Computers

Sri

Page13 of 114

2.Do while loop: Syn: Do <statement> while <expression>; <statements>is excuted repaeatedly as long as the value of <expressions> remains nonzero. The test takes place after each excution of the <statement> 21. /* program to print numbers form 1 to 100 by using do while loop*/ main() { int i=1; clrscr(); do { printf("%d\t",i); i++; } while(i<=100); getch(); } 3.For loop: syn: for([<expr1>];[<expre2];[expre3];) <statement> <statement> is excuteed repeatedly until the value of <expr2> is 0. Before the first interation <expr1> of evaluated. This is usually used to intialised variable for the loop. After each iteration fof the loop ,<expr3> is evaluated. This is usually to increment a loop counter. All the expression are optional. If <expr2> is left out it is assumed to be 1. 22. /* program to print the numbers from 1 to 100 using for loop */ main() { int i; clrscr(); for(i=1;i<=100;i++) printf("%d\t",i); getch(); } 23. /* program to print a number from 100 to 1*/ main() { int i; clrscr(); for(i=100;i>=1;i--) printf("%d\t",i); getch(); }

Bye from this page

SISI Computers

Sri

Page14 of 114

24. /*program to accept any number and print its mathematical table*/ main() { int n,i; clrscr(); printf("enter any number:"); scanf("%d",&n); for(i=1;i<=20;i++) printf("%d x %d = %d \n",n,i,n*i); getch(); } 25. /* program to print 20 mathematical tables from 1 to 20 */ main() { int n,i; clrscr(); for(n=1;n<=20;n++) { for(i=1;i<=20;i++) printf("%d x %d = %d\n",n,i,n*i); getch(); clrscr(); } getch(); } Break Statement:Syn: break; The break statement causes control to pass to the statement following the inner most enclosing while, do,for or switch statement . 26. /* program for example of break statement*/ main() { int a; clrscr(); for(a=1;a<=100;a++) { printf("%d\t",a); if(a==50) break; } getch(); }

Bye from this page

SISI Computers

Sri

Page15 of 114

Continue Statement:Syn: Continue; The causes control to pass to the end of the inner most enclosing while,do, for statement at switch point the loop continuation condition is revaluated. 27. /*program to print 1 to 100 except number 50 to 75*/ main() { int a; clrscr(); for(a=1;a<=100;a++) { if(a>=50 && a<=75) continue; printf("%d\t",a); } getch(); } Go To Statement Syn: goto satatement; Control is unconditionally transferred to location of local lable specified by <identifier> 28. /*program by using goto statement*/ main() { int a=1; clrscr(); sisi: printf("%d\t",a); a++; if(a<=1000) goto sisi; getch(); } 29./*program to accept any number and print no.of digits in it*/ main() { int a,b=0; clrscr(); printf("enter any number:"); scanf("%d",&a); while(a>0) { a=a/10; b++; }

Bye from this page

SISI Computers

Sri printf("number of digits:%d",b); getch(); }

Page16 of 114

30. /*program to accept any number and sum of digits in it*/ main() { int a,r,s=0; clrscr(); printf("enter any number:"); scanf("%d",&a); while(a>0) { r=a%10; s=s+r; a=a/10; } printf("sum of digits:%d",s); getch(); } 31. /*program to accept any number and print number in reverse order*/ main() { int a,b,c=0; clrscr(); printf("enter any number:"); scanf("%d",&a); while(a>0) { b=a%10; c=c*10+b; a=a/10; } printf("reversed number:%d",c); getch(); } 32. /*program to accept any number and check whether the accepted number is palindrome number or not*/ main() { int n,r,s=0,t; clrscr(); printf("enter any number:"); scanf("%d",&n);

Bye from this page

SISI Computers

Sri t=n; while(n>0) { r=n%10; s=s*10+r; n=n/10; } if(s= =t) printf("palindrome number"); else printf("not a palindrome number"); getch(); }

Page17 of 114

33. /*program to accept any number and check whether the accepted number is armstrong number or not*/ Definition: Any number, sum of each digits cube is same number Ex: 1,153,370,371,407 main() { int n,r,s=0,t; clrscr(); printf("enter any number:"); scanf("%d",&n); t=n; while(n>0) { r=n%10; s=s+r*r*r; n=n/10; } if(s==t) printf("armstrong number"); else printf("not an armstrong number"); getch(); }

34. /*program to accept any number whether the accepted number is prime number or not*/ Definition: Any number which is divisible by 1 and itself only. Ex: 2,3,5,7 main()

Bye from this page

SISI Computers

Sri { int n,i,s=0; clrscr(); printf("enter any number:"); scanf("%d",&n); for(i=1;i<=n;i++) if(n%i==0) s++; if(s==2) printf("prime number"); else printf("not a prime number"); getch(); }

Page18 of 114

35. /*program to accept any number and check whether the accepted number is perfect number or not*/ Definition: Any number, sum of factors below that number is the same number. Ex: 6,28,496. main() { int n,i,s=0; clrscr(); printf("enter any number:"); scanf("%d",&n); for(i=1;i<n;i++) if(n%i==0) s=s+i; if(s==n) printf("perfect number"); else printf("not a perfect number"); getch(); } 36. /*program to accept any number and print its factorial value*/ main() { int n,i,s=1; clrscr(); printf("enter any number:"); scanf("%d",&n); for(i=1;i<=n;i++) s=i*s; printf("factorial value is:%d",s);

Bye from this page

SISI Computers

Sri getch(); }

Page19 of 114

37. /*program to accept any number and check whether the number is strong number or not*/ Strong Number : Any number sum of each digit factorial value is the same number Ex: 145. main() { int n,i,s=0,f=1,r,t; clrscr(); printf("enter any number:"); scanf("%d",&n); t=n; while(n>0) { r=n%10; f=1; for(i=1;i<=r;i++) f=f*i; s=s+f; n=n/10; } if(s==t) printf("strong number"); else printf("not a strong number"); getch(); } 38. /*program to print palidrome numbers from 1 to 1000*/ main() { int n,i,s,r; clrscr(); for(i=1;i<=1000;i++) { n=i; s=0; while(n>0) { r=n%10; s=s*10+r;

Bye from this page

SISI Computers

Sri n=n/10; } if(s==i) printf("%d\t",i); } getch(); }

Page20 of 114

39./*progrom to print armstrong numbers from 1 to 1000*/ main() { int n,i,s,r; clrscr(); for(i=1;i<=1000;i++) { n=i; s=0; while(n>0) { r=n%10; s=s+r*r*r; n=n/10; } if(s==i) printf("%d\t",i); } getch(); } 40. /*program to print prime numbers from 1 to 1000*/ main() { int n,i,s; clrscr(); for(n=1;n<=1000;n++) { s=0; for(i=1;i<=n;i++) if(n%i==0) s++; if(s==2) printf("%d\t",n); } getch(); }

Bye from this page

SISI Computers

Sri

Page21 of 114

41. /*program to print perfect numbers from 1 to 1000*/ main() { int n,i,s; clrscr(); for(n=1;n<=100;n++) { s=0; for(i=1;i<n;i++) if(n%i ==0) s=s+i; if(s==n) printf("%d\t",n); } getch(); } 42. /*program to print strong numbers from 1 to 1000*/ main() { int n,i,s,f,r,j; clrscr(); for(i=1;i<=1000;i++) { n=i; s=0; while(n>0) { r=n%10; f=1; for(j=1;j<=r;j++) f=f*j; s=s+f; n=n/10; } if(s==i) printf("%d\t",i); } getch(); } ARRAYS A group of similar type of data is stored in a single memory location is a single variable is called array. These are 1. Single Dimension 2. Double Dimension

Bye from this page

SISI Computers

Sri

Page22 of 114 3. Multi Dimension array An array value can be refernce with index number, so an array element is called subscript variable.

43. /* program to accept ten employee salary in an array and print them*/ main() { int sal[10],i; clrscr(); for(i=0;i<10;i++) { printf("enter employee salary : "); scanf("%d",&sal[i]); } for(i=0;i<10;i++) printf("%d\t",sal[i]); getch(); } 44. /*program to accept an ten employee in array and print the total salary*/ main() { int sal[10],i,total=0; clrscr(); for(i=0;i<10;i++) { printf("enter employee salary:"); scanf("%d",&sal[i]); total=total+sal[i]; } printf("total sal....%d",total); getch(); } 45. /*program to accept an employee salary in an array and print the highest salary and lowest salary*/ main() { int sal[10],i, l=32767,h=0; clrscr(); for(i=0;i<10;i++) { printf("enter employee salary:"); scanf("%d",&sal[i]); if(sal[i]>h) h=sal[i]; if(sal[i]<l) l=sal[i];

Bye from this page

SISI Computers

Sri } printf("highest salary is .....%d\n",h); printf("lowest salary is ......%d\n",l); getch(); }

Page23 of 114

46. /*program to accept to create an array of 15cells input value upto 10 cells and store sum of odd values into the 11th cell and sum of even values into 12th cell and no.of odd values into the 13th cell and no.of even values into the 14th cell and sum of all values into the 15th cell print that array*/ 10 9 0 14 8 1 18 4 2 22 6 3 26 1 4 30 3 5 34 7 6 38 2 7 42 10 8 46 5 9 50 25 10 54 30 11 58 5 12 62 5 13 66 55 14

main() { int a[15],i; a[10]=a[11]=a[12]=a[13]=a[14]=0; clrscr(); for(i=0;i<10;i++) { printf("enter anu number:"); scanf("%d",&a[i]); if(a[i]%2==0) { a[11]+=a[i]; a[13]++; } else { a[10]+=a[i]; a[12]++; } a[14]+=a[i]; } for(i=0;i<15;i++) { printf("%5d",a[i]); } getch(); }

Bye from this page

SISI Computers

Sri

Page24 of 114

Gotoxy () Function Syn: gotoxy(x,y) Its position cursor in text window where x represents the column and y represents the row. There are 80 cloumns and 25 rows on the screen. /* example program for gotoxy() */ Main() { clrscr(); gotoxy(36,12); printf(welcome); getch(); } 47. /* re-write the above progrm using gotoxy*/ main() { int a[15],i; a[10]=a[11]=a[12]=a[13]=a[14]=0; clrscr(); for(i=0;i<10;i++) { gotoxy(i*4+10,12); scanf("%d",&a[i]); if(a[i]%2==0) { a[11]+=a[i]; a[13]++; } else { a[10]+=a[i]; a[12]++; } a[14]+=a[i]; gotoxy(50,12); printf("%d",a[10]); gotoxy(54,12);printf("%d",a[11]); gotoxy(58,12);printf("%d",a[12]); gotoxy(62,12);printf("%d",a[13]); gotoxy(66,12);printf("%d",a[14]); } getch(); }

Bye from this page

SISI Computers

Sri Sorting Array

Page25 of 114

Sorting means arranging the cell values in ascending or descending order. Ascending means lowest value comes first and highest value goes last. Descending means highest value comes first and lowest value goes last. 48. /*progamm to accept an array 10cells input values in it and sort that array ascending order*/ main() { int a[10],i,j,t; clrscr(); for(i=0;i<10;i++) { printf("enter any number:"); scanf("%d",&a[i]); } printf("\n \n unsorted array\n"); for(i=0;i<10;i++) printf("%d\t",a[i]); for(i=0;i<9;i++) for(j=i+1;j<10;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } printf("\n \n sorted array\n"); for(i=0;i<10;i++) printf("%d\t",a[i]); getch(); } Random() Function: Syn: random(num); It returns an integer between 0 and (n-1). Randomize Function: Syn: randomize(); It intialise the random number greater for a random value . Delay() Funciton: Syn: delay(milliseconds); It suspends the program excution for interval.

Bye from this page

SISI Computers

Sri

Page26 of 114

Sleep() Function: Syn: sleep(seconds); It suspends the program excution for an interval. (seconds). 49. /*rewrite the above program for random values*/ #include<stdlib.h> main() { int a[10],i,j,t; clrscr(); randomize(); printf("\n \n unsorted array\n"); for(i=0;i<10;i++) { a[i]=random(100); printf("%d\t",a[i]); delay(50000); } for(i=0;i<9;i++) for(j=i+1;j<10;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } printf("\n \n sorted array\n"); for(i=0;i<10;i++) { printf("%d\t",a[i]); delay(50000); } getch(); } 50. /*rewrite the above array program using gotoxy with random values*/ #include<stdlib.h> main() { int a[10],i,j,t; clrscr(); randomize(); gotoxy(30,8); printf("unsorted array"); for(i=0;i<10;i++)

Bye from this page

SISI Computers

Sri { a[i]=random(100); gotoxy(i*5+10,10); printf("%d",a[i]); sleep(1); } for(i=0;i<9;i++) for(j=i+1;j<10;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } gotoxy(30,12); printf("sorted array"); for(i=0;i<10;i++) { gotoxy(i*5+10,17);printf("%d",a[i]); sleep(1); } getch(); }

Page27 of 114

Searching an element in an Array: There are two methods to search an element one is linear search method. Second one is binary search method. Linear search is used to search an element sequentially from the first cell to last cell one by one for sorted and unsorted arrays where as binary search is used to search an element randomly for sorted arrays only. 51. /*program to create no.of 10 cells input values in it and also accept any number to search then search whether the accepted number is found in the array or not using linear search method*/ #include<stdlib.h> main() { int a[10],i,n; clrscr(); randomize(); gotoxy(30,8); printf("array values are...."); for(i=0;i<10;i++) { a[i]=random(100); gotoxy(i*5+10,10);

Bye from this page

SISI Computers

Sri

Page28 of 114

printf("%d",a[i]); sleep(1); } gotoxy(30,12);printf("enter any number to search:"); scanf("%d",&n); for(i=0;i<10;i++) if(a[i]==n) break; gotoxy(30,15); if(i<10) printf("%d is found in the array at %d cell ",n,i+1); else printf("%d is not found in the array ",n); getch(); } 52. /*rewrite the above program using binary serach method*/ #include<stdlib.h> main() { int a[10],i,n,j,t,b,m; clrscr(); randomize(); gotoxy(30,8); printf("array values are..."); for(i=0;i<10;i++) { a[i]=random(100); gotoxy(i*5+10,10); printf("%d",a[i]); sleep(1); } for(i=0;i<9;i++) for(j=i+1;j<10;j++) if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } gotoxy(30,12); printf("sorted array values are.."); for(i=0;i<10;i++) { gotoxy(i*5+10,14); printf("%d",a[i]);

Bye from this page

SISI Computers

Sri

Page29 of 114

sleep(1); } gotoxy(30,16); printf("enter any number to search:"); scanf("%d",&n); b=0; t=9; m=4; while (b<=t && a[m]!=n) { m=(t+b)/2; if(n>a[m]) b=m+1; else if(n<a[m]) t=m-1; } gotoxy(30,18); if(a[m]==n) printf("%d is found in the array at %d cell",n,m+1); else printf("%d is not found in the array",n); getch(); } Double Dimension Arrays:53./*program to create an array of 5*5 matrix input values in it and print that array in middle of the screen*/ main() { int a[5][5],i,j; clrscr(); for(i=0;i<5;i++) { for(j=0;j<5;j++) {printf("enter any number:"); scanf ("%d",&a[i][j]); }} clrscr(); for(i=0;i<5;i++) for(j=0;j<5;j++) { gotoxy(j*3+30,i*2+9); printf("%d",a[i][j]); delay(50000); }

Bye from this page

SISI Computers

Sri getch(); }

Page30 of 114

54. /* program to print 5*5 matrix in random values*/ #include<stdlib.h> main() { int a[5][5],i,j; clrscr(); randomize(); for(i=0;i<5;i++) for(j=0;j<5;j++) { a[i][j]=random(100); gotoxy(j*3+30,i*2+9); printf("%d",a[i][j]); sleep(1); } getch(); } 55. /*programm to print a 5*5 unit matrix*/ #include<stdlib.h> main() { int a[5][5],i,j; clrscr(); for(i=0;i<5;i++) for(j=0;j<5;j++) { if(i==j) a[i][j]=1; else a[i][j]=0; gotoxy(j*3+30,i*2+9); printf("%d",a[i][j]); } getch(); } 56. /*program to print anti-diagonals or once and rest of all zeros in 5*5 matrix*/ #include<stdlib.h> main() {

Bye from this page

SISI Computers

Sri int a[5][5],i,j; clrscr(); for(i=0;i<5;i++) for(j=0;j<5;j++) { if(i+j==4) a[i][j]=1; else a[i][j]=0; gotoxy(j*3+30,i*2+9); printf("%d",a[j][i]); } getch(); }

Page31 of 114

57. /*program to accept any array in the following format*/ 0 0 3 0 0 0 0 3 0 0 3 3 3 3 3 0 0 3 0 0 0 0 3 0 0

#include<stdlib.h> main() { int a[5][5],i,j; clrscr(); for(i=0;i<5;i++) for(j=0;j<5;j++) { if(i==2||j==2) a[i][j]=3; else a[i][j]=0; gotoxy(j*3+30,i*2+9); printf("%d",a[i][j]); delay(50000); } getch(); } 58. /* program to create three arrays of 3*3 matrix input values into the first two arrays and store its sum into the third array. Print three arrays on the screen*/ #include<stdlib.h>

Bye from this page

SISI Computers

Sri main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); gotoxy(3,7); printf("first array"); gotoxy(23,7); printf("second array"); gotoxy(53,7); printf("third array"); for(i=0;i<3;i++) for(j=0;j<3;j++) { gotoxy(j*3+5,i*2+9); scanf("%d",&a[i][j]); gotoxy(j*3+25,i*2+9); scanf("%d",&b[i][j]); c[i][j]=a[i][j]+b[i][j]; gotoxy(j*3+55,i*2+9); printf("%d",c[i][j]); } getch(); }

Page32 of 114

59. /*Rewrite the above program using matrix multiplication */ #include<stdlib.h> main() { int a[3][3],b[3][3],c[3][3],i,j,k; clrscr(); gotoxy(3,7);printf("first array"); gotoxy(23,7);printf("second array"); for(i=0;i<3;i++) for(j=0;j<3;j++) { gotoxy(j*3+5,i*2+9); scanf("%d",&a[i][j]); gotoxy(j*3+25,i*2+9); scanf("%d",&b[i][j]); } gotoxy(53,7);printf("third array"); for(i=0;i<3;i++) for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) c[i][j]+=a[i][k]*b[k][j]; gotoxy(j*3+55,i*2+9);

Bye from this page

SISI Computers

Sri printf("%d",c[i][j]); } getch(); }

Page33 of 114

60. /*program to create two arrays of 5*5matrix input values into the 1 st arrays and transpose its value into the 2nd array . Print 2 arrays on this screen*/ main() { int a[5][5],b[5][5],i,j; clrscr(); gotoxy(5,2);printf("first array"); gotoxy(40,12);printf("second array"); for(i=0;i<5;i++) for(j=0;j<5;j++) { gotoxy(j*3+5,i*2+4); scanf("%d",&a[i][j]); b[j][i]=a[i][j]; gotoxy(i*3+40,j*2+14); printf("%d",b[j][i]); } getch(); } 61. /* program to create an array of 5*5 matrix input values into 4*4 matrix and store its sum in corresponding last rows and columns*/ #include<stdlib.h> main() { int a[5][5],i,j; clrscr(); for(i=0;i<5;i++) a[i][4]=a[4][i]=0; for(i=0;i<4;i++) for(j=0;j<4;j++) { gotoxy(j*3+30,i*2+9); scanf("%d",&a[i][j]); a[i][4]+=a[i][j]; a[4][j]+=a[i][j]; a[4][4]+=a[i][j]; gotoxy(42,i*2+9);printf("%d",a[i][4]); gotoxy(j*3+30,17);printf("%d",a[4][j]); gotoxy(42,17);printf("%d",a[4][4]);

Bye from this page

SISI Computers

Sri } getch(); }

Page34 of 114

62. /*program to create an array of 5*5 matrix input values input and sort that array in ascending order row-wise*/ #include<stdlib.h> main() { int a[5][5],i,j,k,t; clrscr(); randomize(); gotoxy(5,2);printf("original array"); for(i=0;i<5;i++) for(j=0;j<5;j++) { gotoxy(j*3+5,i*2+4); a[i][j]=random(100); printf("%d",a[i][j]); sleep(1); } for(i=0;i<5;i++) for(j=0;j<5;j++) for(k=j+1;k<5;k++) if(a[i][j]>a[i][k]) { t=a[i][j]; a[i][j]=a[i][k]; a[i][k]=t; } gotoxy(40,12);printf("sorted array"); for(i=0;i<5;i++) for(j=0;j<5;j++) { gotoxy(j*3+40,i*2+14); printf("%d",a[i][j]); sleep(1); } getch(); } STRINGS A string is nothing but a group of characters .In a character array which is teriminated with null character. We can use the following functions to manipulate strings.

Bye from this page

SISI Computers

Sri

Page35 of 114

1. strlen() function: syn: strlen(st); It returns the length of the given string. 63. /* example for strlen() function */ main() { char st[50]; int i; clrscr(); printf(enter any string : ); scanf(%s,st); printf(length is .. %d \n,strlen(st)); for(i=0;i<strlen(st);i++) printf(%c \n,st[i]); getch(); } 2.strcpy()function: syn: strcpy(dest,source); It copies a source string to the destination string variable. 3.strrev()function: syn: strrev(st) It reverses the all the characters in string except for the terminating null. 4. strcmp()function: syn: strcmp(s1,s2); It compares s2 to s1 and returns a value less than zero if s1 is less than s2; = = 0, if s1 is the same as s2 ;> 0 if s1 is greater than s2 perform a signed comprassion . 64. /* program to accept any strong and check weather the accepted number string is palindrome or not*/ main() { char st[50],rs[50]; clrscr(); printf("enter any string:"); scanf("%s",st); strcpy(rs,st); strrev(rs); if(strcmp(st,rs)==0) printf("palindrome word"); else printf(" not a palindrome word");

Bye from this page

SISI Computers

Sri getch(); }

Page36 of 114

5.strcat() function: syn: strcat(dest,soruce); It appends source string to destination strings variable 6. strche()function: syn: strchr(st,ch); It finds a character in a string and returns a pointer first occurrence of the character C is str; if C does not occurs in str, strchr returns null. 7. strupr()function: syn: strupr(st) It converts the given string into upper case. 65. /* program accept any string and convert it into upper case*/ main() { char st[50]; clrscr(); printf("enter any string:"); scanf("%s",st); strupr(st); printf("converted string is ..%s",st); getch(); } 8. strlwr()function: syn: strlwr(st); It converts given string into lower case. 9. puts()function: syn: puts(st); It prints a string including new line character(\n). 10.gets()function; syn: gets(st); It reads a string including blanks. 66. /* programm to accept any string and convert it into lower case*/ main() { char st[50]; clrscr(); puts("enter any string:"); gets(st);

Bye from this page

SISI Computers

Sri

Page37 of 114

strlwr(st); printf("converted string is ........%s",st); getch(); } ASCII:American stand code for information and interchange. It is the standard code for information of characters and interchange into the machine code. They are 256 ASCII characters form 0 to 255 in that 0 to 31 cntrl characters. 32 to 126 normal and printable character and 127 to 256 graph characters . 32- space 33-! 34-|| 35-# 36-$ 37-% 38-? 39-/ 40-( 41-) 42-* 43-+ 44- 45- _ 46-. 47-\ 48-0 65 to 90 - A to Z 97 to 122 - a to z 48 to 57 - 0 to 9

67./* program to print all ASCII Characters*/ main() { int i; clrscr(); for(i=1;i<255;i++) { printf("%d ---> %c\n",i,i); if(i%23==0) getch(); } getch(); } 68. /*program to accept any sentence and convert it into lowercase without using strlwr() function*/

Bye from this page

SISI Computers

Sri main() { char st[50]; int i; clrscr(); puts("enter any sentence:"); gets(st); for(i=0;i<strlen(st);i++) if (st[i]>= 65 && st[i]<=90) st[i]+=32; printf("converted string is.....%s",st); getch(); }

Page38 of 114

69. /* program to accept any sentence and convert it into upper case without using strupr() function*/ main() { char st[50]; int i; clrscr(); puts("enter any string:"); gets(st); for(i=0;i<strlen(st);i++) if(st[i]>=97 && st[i]<=122) st[i]-=32; printf("converted string: %s",st); getch(); } 70. /* program to accept any sentence and printf no.of caps,no.of smalls, no.of digits,no.of special characters, no.of vowels,no.of constants, no.of alphabets, no.of spaces*/ main() { int i, nc,ns,nd,nsp,nv,nco,na,nspace; char st[50]; nc=ns=nd=nsp=nv=nco=na=nspace=0; clrscr(); puts("enter any sentence:"); gets(st); for(i=0;i<strlen(st);i++) { if(st[i]>=65 && st[i]<=90)nc++; else if(st[i]>=97 && st[i]<=122)ns++; else if(st[i]>=48 && st[i]<=57)nd++;

Bye from this page

SISI Computers

Sri

Page39 of 114

else if (st[i]==32)nspace++; else nsp++; if(strchr("AEIOU aeiou", st[i]))nv++; } na=nc+ns; nco=na-nv; printf("number of caps:%d\n",nc); printf("number of smalls:%d\n",ns); printf("number of digits:%d\n",nd); printf("number of specials:%d\n",nsp); printf("number of vowels:%d\n",nv); printf("number of constants:%d\n",nco); printf("number of alphabets:%d\n",na); printf("number of spaces:%d\n",nspace); getch(); } Logical Funtions( Macros):isalnum (c) Ture if c is a letter or digit isalpha ( c) Ture if c is a letter isdigit ( c) Ture if c is a digit iscontrl ( c) True if c is delete character Or Ordinary control character isascii ( c) Ture if c is a valid ascii character isprint ( c) Ture if c is printable character isgraph ( c) like is print except that the space character is excluded. is lower (c) true if c is an lower case. isupper ( c) true if c is an upper case. ispunct ( c) True if c is a punctuation character isspace ( c) True if c is a space ,tab or for feed. isxdigit ( c) True if c is hexadecimal digit toupper ( c) converts c is the range of [a- z] tocharacters [A-Z] tolower ( c) converts c in the range[A-Z] to characters [a-z] toascii ( c) converts c greater than 127 to the range 0 127 by cleaning all but the lower 7bits . 71. /* rewrite the above programm using logical functions*/ #include<ctype.h> main() { int i,nc,ns,nd,nsp,nv,nco,na,nspace; char st[50]; nc=ns=nd=nsp=nv=nco=na=nspace=0; clrscr(); puts("enter any string:"); gets(st);

Bye from this page

SISI Computers

Sri

Page40 of 114

for(i=0;i<strlen(st);i++) { if(isupper(st[i]))nc++; else if(islower(st[i]))ns++; else if(isdigit(st[i]))nd++; else if(isspace(st[i]))nspace++; else nsp++; if(strchr("AEIOU aeiou",st[i]))nv++; } na=nc+ns; nco=na-nv; printf("number of caps:%d\n",nc); printf("number of smalls:%d\n",ns); printf("number of digits:%d\n",nd); printf("number of specials:%d\n",nsp); printf("number of vowels:%d\n",nv); printf("number of constants:%d\n",nco); printf("number of alphabets:%d\n",na); printf("number of spaces:%d\n",nspace); getch(); } 72. /*write a programm to accept any sentence and printf no.of words in it*/ main() { char st[50]; int i,n=0; clrscr(); puts("enter any sentence:"); gets(st); for(i=0;i<strlen(st);i++) if(st[i]==' ')n++; printf("number of words:%d\n",n+1); getch(); } 73. /*Rewrite the above programm but there are more than one space between two words*/ main() { char st[60]; int i,n=0; clrscr(); puts("enter any string:"); gets(st);

Bye from this page

SISI Computers

Sri for(i=0;i<strlen(st);i++) if(st[i]==' ' && st[i+1]!= ' ') if(st[0]!=' ') n++; printf("number of words : %d\n", n); getch(); }

Page41 of 114

74. /* write a programm to accept any sentence and print words separately*/ main() { char st[60],ss[50]; int i,j=0; clrscr(); puts("enter any string:"); gets(st); strcat(st," "); for(i=0;i<strlen(st);i++) if(st[i]!=' ') ss[j++]=st[i]; else {ss[j]='\0'; j=0; puts(ss); } getch(); } 75. /*programm to accept any sentence and print biggest word and smallest word*/ main() { char st[60],ss[60],bst[60],sst[60]; int i,j=0,b=0,s=60; clrscr(); puts("enter any sentence:"); gets(st); strcat(st," "); for(i=0;i<strlen(st);i++) if(st[i]!=' ') ss[j++]=st[i]; else { ss[j]='\0'; j=0; if(strlen(ss)<b) { b=strlen(ss);

Bye from this page

SISI Computers

Sri

Page42 of 114

strcpy(bst,ss); } if(strlen(ss)<s) { s=strlen(ss); strcpy(sst,ss); } } printf("biggest wordis...%s... its length... %d\n",bst,b); printf("smallest word is..%s..itslength..%d\n",sst,s); getch(); } 76. /* program to accept any sentence and print no.of palindrome words*/ main() { char st[60],ss[60],rs[60]; int i,j=0,n=0; clrscr(); puts("enter any sentence:"); gets(st); strcat(st," "); for(i=0;i<strlen(st);i++) if(st[i]!=' ') ss[j++]=st[i]; else { ss[j]='\0'; j=0; strcpy(rs,ss); strrev(rs); if(strcmp(ss,rs)==0)n++; } printf("no.of palindrome words:%d\n",n); getch(); } 77. /*program to accept any company name and print its acronym name */ main() { char st[60],ss[60],rs[60]; int i,j=0,k=0; clrscr(); puts("enter any sentence:"); gets(st); strcat(st," ");

Bye from this page

SISI Computers

Sri

Page43 of 114

for(i=0;i<strlen(st);i++) if(st[i]!=' ') ss[j++]=st[i]; else { ss[j]='\0'; j=0; if(strcmp(ss,"and")!=0 && strcmp(ss,"of")!=0 && strcmp(ss,"in")!=0) { rs[k++]=toupper(ss[0]); rs[k++]='.'; } } rs[k]='\0'; printf(" acroname is...> %s",rs); getch(); } 1. sound () function: Syn: sound(frequency); Sound : Turns pc speaker on at specified frequency. Where frequency is hertz (cycle per sound) 2 nosound() function: syn: nosound() It turns speaker off. 3.kbhit() function: syn: kbhit() It checks for recent keystores. 78. #include<stdlib.h> main() { randomize(); clrscr(); while(!kbhit()) { textcolor(random(16)); textbackground(random(8)); clrscr(); gotoxy(30,12); cprintf("welcome to the world of music"); sound(random(50000)); delay(5000); }

Bye from this page

SISI Computers

Sri nosound(); } Tex color()function: Syn: Textcolor(newcolor);

Page44 of 114

It selects new character color in text mode. Where new color is a color number between 0 to 15. 0-Black 1-Blue 9-Lightblue 2-Green 10-Ligthgreen 3-Cyan 11-Lightcyan 4-Red 12-Lightred 5-Magenta 13-Light magenta 6-Brown 14-Yellow 7-Lightgray 15-White 8-darkgray 128-Blink Text Background: Syn: Textcolor(newcolor); It selects new text background color. 79.main() { int i; clrscr(); for(i=0;i<15;i++) { textcolor(i); textbackground(15-i); cprintf("this is the color text and this code is %d\n\r",i); } getch(); } 80. /* w.a.p to accept any string and print string top to bottom charcter by charater*/ #include <stdlib.h> main() { char st[50]; int i,j,p; clrscr(); randomize(); puts("enter any string"); gets (st);

Bye from this page

SISI Computers

Sri p=(80-strlen(st))/2; clrscr(); gotoxy(p,1); puts(st); for(i=0;i<strlen(st);i++,p++) if(st[i]!=' ') for(j=2;j<=25;j++) { gotoxy(p,j); printf("%c",st[i]); gotoxy(p,j-1); printf(" "); gotoxy(1,1); sound(random(500)); delay(50000); nosound(); } getch(); }

Page45 of 114

81. /* w.a.p to drag charater from left to right of the screen*/ #include <stdlib.h> main() { char st[60]; int i,j,p=80; clrscr(); puts("enter any string ;"); gets(st); gotoxy(1,12); printf("%s",st); for(i=strlen(st)-1;i>=0;i--,p--) if(st[i]!=' ') for(j=i+2;j<=p;j++) { gotoxy(j,12); printf("%c", st[i]); gotoxy(j-1,12); printf(" "); sound(random(5000)); delay(50000); nosound(); } getch(); }

Bye from this page

SISI Computers

Sri

Page46 of 114

82. /* w.a.p to print character in spiral form */ main() { i,c1,c2,r1,r2,k; randomize(); for(k=5;k=6;k++) { clrscr(); randome c1=1; c2=80; r1=1; r2=24; while (r1<=r2) { for(i=c1;i<=c2;i++) { gotoxy(i,r1); printf(%c,k); delay(500); } for(i=r1+1;i<=r2;i++) { gotoxy(c2,i); printf(%c,k); delay(500); } for(i=r2-1;I>=r1;i--) { gotoxy(c1,i); printf(%c,k); delay(500); } for(i=r2-1;i>r1;i--) { gotoxy(c1,i); printf(%c,k); delay(500); } c1++; c2--; r1++; r2--; } } getch(); } Functions A Function is a small program module which exist in the current program and it can be called by its name. Function returns a value. Any function does not return a value then that function is called void function. Syntax for function declaration :Returns_type function_name (arguments_types); Syntax for function defination :Returns_type function_name (arguments) { statements;

Bye from this page

SISI Computers

Sri return expr; } 83. /* Example of function */ int sum(int,int); main() { int a,b; clrscr(); printf("enter two numbers : "); scanf("%d %d",&a,&b); printf("sum is .. %d",sum(a,b)); getch(); } int sum (int x, int y) { int z; z=x+y; return z; }

Page47 of 114

84. /* w.a.p to display n natural nos. using function*/ void disp(int n) { int i; for(i=1;i<=n;i++) printf("%d\t",i); } main() { int n; clrscr(); printf("enter any numbers "); scanf("%d", &n); disp(n); getch(); } 85. /* example for void function to display a line*/ void line() { int i;

Bye from this page

SISI Computers

Sri for(i=1;i<=80;i++) printf("-"); }

Page48 of 114

main() { clrscr(); line(); line(); printf("\t \t \t welcome to SISI computer \n"); line(); line(); getch(); } 86. /*w.a function to reverse an interger no.*/ int reV(int n) { int r,s=0; while(n>0) { r=n%10; s=s*10+r; n=n/10; } returns; } main() { int n; clrscr(); printf("enter any no. "); scanf("%d", &n); printf("reversed no. is ...%d", rev(n)); getch(); } 87. /* write a logical function palindrom or not*/ int palin(int n) {int r,s=0,t=n; while(n>0) { r=n%10; s=s*10+r; n=n/10;

Bye from this page

SISI Computers

Sri } if(s==t) return 1; else return 0; } main() { int n; clrscr(); printf("enter any number"); scanf("%d", &n); if(palin(n)); printf("palindrom number"); printf("not a palindrom"); getch(); }

Page49 of 114

88. /* write a logical function for artmstrong or not*/ int arm(int n) { int r,s=0,t=n; while (n>0) { r=n%10; s=s+r*r*r; n=n/10; } if(s==t) return 1; else return 0; } main() { int n; clrscr(); printf("enter any number "); scanf("%d",&n); if(arm(n)) printf("armstrong number"); else printf("not a armstrong"); getch(); }

Bye from this page

SISI Computers

Sri

Page50 of 114

89. /* w.a.p to accept any number and check whether the accepted number is perfect number or not using logical function*/ int per (int n) { int r,s=0,i; for(i=1;i<n;i++) if(n%i==0) s=s+i; if(s==n) return 1; else return 0; } main() { int n; clrscr(); printf("enter any number ;"); scanf(%d",&n); if(per(n)) printf("perfect number"); else printf("not a perfect number "); getch(); } 90. /*write a logical function for prime number or not*/ int pri (int n) { int r,i,s=0; for(i=1;i<=n;i++) if(n%i==0) s++; if(s==2) return 1; else return 0; } main() { int n; clrscr(); printf("enter any number:"); scanf("%d",&n);

Bye from this page

SISI Computers

Sri if(pri(n)) printf("prime number"); else printf("not a prime number"); getch(); }

Page51 of 114

91./* write a logical function for strong number or not */ int strong(int n) { int s=0,i,r,j,t=n,f; while(n>0) { r=n%10; f=1; for(i=1;i<=r;i++) f=f*i; s=s+f; n=n/10; } if (s==t) return 1; else return 0; } main() { int n; clrscr(); printf("enter any number : "); scanf("%d",&n); if (strong(n)) printf("strong number"); else printf("not a strong"); getch(); } Recursive Function A function calls itself is nothing but recursion. When recursion tecnique is used in function program every time when the function is called by itself. The values will be pushed into memory stark and at last when the given condition is false the stack values are poped LIFO and the given expression will be evaluated and then the function is teriminated.

Bye from this page

SISI Computers

Sri

Page52 of 114

92./* Example for recurssive function to display n natural numbers */ void recur(int n) { if(n>1) recur(n-1); printf("%d\t",n); } main() { int n; clrscr(); printf("enter any number: "); scanf("%d",&n); recur(n); getch(); } 93. /* write a recurssive function to display sum of n natural numbers*/ int sum(int n) { if(n>1) return n+sum(n-1); else return 1; } main() { int n; clrscr(); printf("enter any number:"); scanf("%d",&n); printf("sum is ...%d", sum(n)); getch(); } 94. /* write a recurssive function for factorial value*/ int fact (int n) { if(n>1) return n* fact(n-1); else return 1; } main() { int n; clrscr(); printf("enter any number:"); scanf("%d",&n);

Bye from this page

SISI Computers

Sri printf("fact is...%d",fact(n)); getch(); }

Page53 of 114

95. /* write a recurssive function for ab*/ float power (float a,int b) { if(b>0) return a* power (a,b-1); else if (b<0) return (1/a) * power(a,b+1); else return 1; } main() { float a; int b; clrscr(); printf("enter base value:"); scanf("%f",&a); printf("enter exponent value:"); scanf("%d",&b); printf("power(%.2f,%d)=%.2f\n",a,b,power(a,b)); getch(); } Storage Classes There are four storage classes available 1. Automatic or Local 2. Exteranl or Global 3. Static 4. Register

1. Automatic: By default of all variables are automatic. Auotmatic variables are also called
as local varaiables. These variables are created automatically when the functions is called and destroyed automatically when the funcition is teriminated. Automatical variables are visible in a particular function is which those are declared, and life time also with in function. We can declared automatic variables auto keyword, however this is optional

Bye from this page

SISI Computers

Sri

Page54 of 114

2. External: 3. Static:

Unlike automatic variables extend variables are declared outside the function and these variables are visible in entire program starting from its declaration point. And its life time also throught the program. These variables are declared like automatic variables in a function using static keyword. These variables visibility is same as automatic variables visibility in a function in which those are declared, but life time is global variables life time. Even the function is teriminated the variable stays in existence and does not lose its value when the function is called next time then it retains its value. Static variables are instead only one time that is first time the function is called static.

96. /* Example for static variable */ int sum(int n) { static int s=0; s=s+n; return s; } main() { int n; clrscr(); do { printf("enter any number 0 to stop : "); scanf("%d",&n); if(n!=0) printf("sum is .. %d \n",sum(n)); } while(n!=0); getch(); }

4. Registers:

We can tell the complier that variable should be kept in one of the machines register instead of keeping in the memory, since a register access is much faster than the memory access, keeping the frequently accessed variables in the register will be lead to faster execution of program. Pointers

A pointer is a variable which hold the address of the another variables rather than a value. Pointer variables are declared using astric(*) indirection operator after accessing a variable address it is possible to assign a value, read a value and display the value of that variable. Pointer variables are used in allocating memory and deallocating memory that is dynamic alloction. This concept is useful in construction of editors, complier, linkers, loaders, assemblers and even operating systems using data structures.

Bye from this page

SISI Computers

Sri

Page55 of 114

To allocate memory we use malloc() function and to deallocte memory we use free() function. Malloc()function: Syn:- malloc(size): It allocates main memory, where size is in bytes and returns a pointer to the newly allocated block or null if not enough space exists for new block if size ==0 it returns null. Free()function: Syn:- free(block); It frees block allocated with malloc. Size of() function: Syn:- sizeof(expression) or sizeof(type); It returns the size in bytes of the given expression or type. 97. /* example for pointer variables */ main() { int a,*p; a=100; p=&a; clrscr(); printf("value of a is .... %d\n",a); printf("value of a is .... %d\n",*p); printf("address of a is ... %ld \n",&a); printf("value of p is ... %ld \n",p); *p+=25; printf("value of a is ... %d \n",a); getch(); } 98. /* write a programme to interchange values of variables using pointer function (call by reference) */ void swap (int *x, int *y) { int t; t=*x; *x=*y; *y=t; } main() { int a,b; clrscr(); printf("enter value for a : "); scanf("%d",&a);

Bye from this page

SISI Computers

Sri

Page56 of 114

printf("enter value for b : "); scanf("%d",&b); printf("values after interchanging \n"); swap(&a,&b); printf("value of a is....%d\n",a); printf("value of b is .....%d\n",b); getch(); } 99. /* example for call by value and call by reference*/ void tot_avg(int a ,int b,int c,int *d,float *e) { *d = a+b+c; *e = *d/3; } main() { int sno,m1,m2,m3,tm; char sna[20]; float am; clrscr(); printf("enter student number,name,marks,3papers:"); scanf("%d %s %d %d %d",&sno,&sna,&m1,&m2,&m3); tot_avg(m1,m2,m3,&tm,&am); printf("total marks:%d\n",tm); printf("avrage amarks:%.2f\n",am); getch(); } 100. /* write aprogramm to dispaly characters vertically using pointer*/ void disp(char *s) { while(*s!='\0') { printf("%c \n",*s); s++; } } main() { char st[50]; clrscr(); puts("enter any sentence:"); gets(st); disp(st);

Bye from this page

SISI Computers

Sri getch(); }

Page57 of 114

101. /*write a function equalent to strlen*/ int len(char *s) { char*s1=s; while(*s!='\0') s++; return s-s1; } main() { char st[50]; clrscr(); puts("enter any sentence:"); gets(st); printf("length is ..%d",len(st)); getch(); } 102. /*write a function equalent to strcat*/ void cat (char *s,char *s1) { while(*s!='\0') s++; while(*s1!='\0') *s++=*s1++; *s='\0'; } main() { char st[50],st1[50]; clrscr(); puts("enter first string:"); gets(st); puts("enter second string:"); gets(st1); cat(st,st1); printf("concatenated string is ..%s",st); getch(); } 103. /*write a function equalent to strcmp if both strings are equal it returns one otherwise its zero*/ int cmp (char *s,char *s1) {

Bye from this page

SISI Computers

Sri while(*s!='\0') if(*s++ != *s1++) break; if(*s!=*s1) return 0; else return 1; } main() { char st[50],st1[50]; clrscr(); puts("enter first string:"); gets(st); puts("enter second string:"); gets(st1); if(cmp(st,st1)) printf("both strings are equal:"); else printf("both strings are not equal:"); getch(); }

Page58 of 114

Structures And Unions Struct:Syn: struct [<struct type name >]{ [<type<variable-name>];} [<structure- variables>]; A struct, Like a union groups variables into a single record. The <struct type name> is an optional tag name that refers to the structure type the <structure variables> are the data definitions and are also optional. Though both are optional, one of the two must appear. Elements in the record are defined by using a <type> followed by <variable type> separated by commas. Different variable types can be separated by a semicolon. To access elements in a structure you use a record selector (.) To declare additional variables of the same type, they use the keyword struct followed by the variables names Union: Syn:- union [<union-type-name>] { <type> <variable name >; } [<union variable>]; A union is similar to a struct except it allows you to define variable that share storage space . Turbo c will allocate enough storage in a number to accommodate the largets element in the union.

Bye from this page

SISI Computers

Sri

Page59 of 114

104 ./* programm to demonstrate the size of the structure and the size of the union*/ struct srec1 { int sno,m1,m2,m3,tm; char sna[20]; float am; }st1; union srec2 { int sno,m1,m2,m3,tm; char sna[20]; float am; }st2; main() { clrscr(); printf("the size of the structure is ..%d\n",sizeof(st1)); printf("the size of the union is ...%d\n",sizeof(st2)); getch(); } 105. /*w A P to create an accept emp no,name salary for five employee in a structured array*/ struct emp-rec { int empo,sal; char ena[20]; } main() { struct emp-rec emp[5];int i; clrscr(); for(i=0;i<5;i++) { printf("enter employee number"); scanf("%d",&emp[i] empno); printf("enter employee name"); scanf("%s", emp[i]ena); printf("enter employee salary"); scanf(%d", &emp[i],sal); } for(i=0;i<5;i++) printf("%d\t%s\t%d\n",emp[i].empno,emp[i].ena,emp[i].sal); getch(); }

Bye from this page

SISI Computers

Sri

Page60 of 114

106. /*program to create an accept emp no,name,salary for five employee in a standard array */ struct emp_rec { int empno,sal; char ena[20]; } main() { struct emp_rec emp[5]; int i; clrscr(); for(i=0;i<5;i++) { printf("enter employee number: "); scanf("%d",&emp[i].empno); printf("enter employee name:"); scanf("%s",emp[i].ena); printf("enter employee salary:"); scanf("%d",&emp[i].sal); } for(i=0;i<5;i++) printf("%d \t %s\t %d\n",emp[i].empno,emp[i].ena,emp[i].sal); getch(); }

FILES A file is nothing but a collection of data which is stored inot the seconds storage device and which can be accessed any time or any number of times. A file is nothing but a collection of records, a record is nothing but a collection of fields and a field is nothing but an information. To work with files we must select the buffer. A buffer is a temporary storage which can perform all it transactions.

Printer

Disk
Bye from this page

Buffer *fp

Monitor
SISI Computers

Sri

Page61 of 114

KB
Syntax for Buffer:FILE* file pointer; Fopen() function:Syn:- fopen(file name,mode); It is used to open a file with the sepecified mode where mode is R - open for reading W create for writing A append , open for writing at end of file. + - add symbol to allow read /write access b open in binary mode. T open in text mode fclose() function:Syn:- fclose(fp); It closes a open file fcloseall() functions:Syn:- fcloseall(); It closes all open files. There are two types of files :1. text files 2. data files. 1. Text files:- It is used to read and write of text information either in character by character or in line by line. Character i/o :- it is used to read and wirte text information character by character using the following functions. fgetc ()function:Syn:- fgetsc (fp); It reads a character from a file fputc () function:Syn:- It writes a character to a file.

Bye from this page

SISI Computers

Sri

Page62 of 114

feof() function:Syn:- feof(fp); It returns true if file pointer is in end of file poisition other wise it returns false. 107. /* write a program to accept any file and print its contents character by character */ #include<stdio.h> main() { char fname[20],ch; FILE *fp; clrscr(); printf("enter any file name : "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) printf("file not found \n"); else while(!feof(fp)) { ch=fgetc(fp); printf("%c",ch); delay(5000); } fclose(fp); getch(); } 108. /* write a program to accept any file and print number of caps, number of smalls, number of digits, number of special chars, number of vowels, number of consonents, number of alphabets, number of spaces. */

#include<stdio.h> #include<ctype.h> main() { char fname[20],ch; FILE *fp; int nc,ns,nd,nspace,nsp,na,nv,nco; clrscr(); nc=ns=nd=nspace=nsp=na=nv=nco=0; printf("enter any file name"); scanf("%s",fname);

Bye from this page

SISI Computers

Sri

Page63 of 114

fp=fopen(fname,"r"); if(fp==NULL) printf("file not found\n"); else { while(!feof(fp)) { ch=fgetc(fp); printf("%c",ch); delay(5000); if(isupper(ch))nc++; else if(islower(ch))ns++; else if(isdigit(ch))nd++; else if(isspace(ch))nspace++; else nsp++; if(strchr("aeiouAEIOU",ch))nv++; } na=nc+ns; nco=na-nv; printf("number of capitals %d\n",nc); printf("number of smalls %d\n",ns); printf("number of digit %d\n",nd); printf("number of space %d\n",nspace); printf("number of special %d\n",nsp); printf("number of vowels %d\n",nv); printf("number of alphabets %d\n",na); printf("consonents %d\n",nco); fclose(fp); } getch(); } 109. /* write aprogram to accept any file and copy its conents into an another accepted file character by character */ #include<stdio.h> main() { char fname[20],fname1[20],ch; FILE *fp,*dp; clrscr(); printf("enter any file name"); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) printf("file not found \n"); else

Bye from this page

SISI Computers

Sri

Page64 of 114 { printf("enter another file to copy"); scanf("%s",fname1); dp=fopen(fname1,"w"); while(!feof(fp)) { ch=fgetc(fp); fputc(ch,dp); } printf("file copied"); fcloseall(); } getch(); }

String i/o:- It is used to read and write data line by line. For this we use fputs(), fgets() function. Syn:- fgets(st,n,fp); It reads of string of n number of characters from a file fputs () function:Syn:- fputs(st,fp); It writes a string into the file 110. /* write a program to accept any file name and print its contents line by line */ #include<stdio.h> main() { char fname[20],st[80]; FILE *fp; clrscr(); printf("enter any file name"); scanf("%s",&fname); fp=fopen(fname,"r"); if (fp==NULL) printf("file not found\n"); else while(!feof(fp)) { fgets(st,80,fp); printf("%s",st); delay(50000); } fclose (fp); getch();

Bye from this page

SISI Computers

Sri }

Page65 of 114

111. /* write a program to accept any file name and print number of lines and words in it */ #include<stdio.h> main() { char fname[20],st[80]; FILE *fp; int nl,nw,i; clrscr(); nl=nw=0; printf("Enter any file name "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) printf("File not found \n"); else { while(!feof(fp)) { fgets(st,80,fp); printf("%s",st); nl++; delay(10000); for(i=0;i<=strlen(st);i++) if(st[i]==' ' && st[i+1]!=' ') nw++; if(st[0]!=' ') nw++; } clrscr(); printf("\n number of lines=%d\n",nl); printf("number of words=%d\n",nw); fclose(fp); } getch(); } 112. /* writea program to accept any file and copy its conentes into another accepted file line by line */ #include<stdio.h> main()

Bye from this page

SISI Computers

Sri { char fname[20],fname1[20],st[80]; FILE *fp,*dp; clrscr(); printf("enter any file name"); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) printf("file not found \n"); else { printf("enter another file to copy"); scanf("%s",fname1); dp=fopen(fname1,"w"); while(!feof(fp)) { fgets(st,80,fp); fputs(st,dp); delay(5000); } printf("file copied"); } fcloseall(); getch() } /* Command line Argumnets :-

Page66 of 114

C allows sending arguments from command line to main function. The main() function receives three type of paremeters. They are argc, argv, env. 1. argc (argument count) :- It is an integer parameters which counts total number of parameters passed from the command line. 2. argv (argument value) :- It is character pointer array parameter which contains all argument values. 3. env (environment variables) :- It is also a character pointer parameters which contains all operating system variables */ 113. /* example for command line arguments*/ main(int argc,char *args[],char *env[])

Bye from this page

SISI Computers

Sri

Page67 of 114

{ int i; printf("total arguments:%d\n",argc); for(i=0;i<argc;i++) printf("args[%d],%s\n",i,args[i]); printf("all operating system variables \n"); for(i=0;env[i];i++) printf("env[%d],%d\n",i,env[i]); } 114. /* write a program equivalent dos type command*/ #include<stdio.h> main(int argc ,char *argv[]) { FILE *fp; char ch; if(argc<2) {puts("required parameter missing\n"); exit(0); } if(argc>2) { puts("too many parameters\n"); exit(0); } fp=fopen(argv[1],"r"); if(fp==NULL) printf("file not found"); else { while(!feof(fp)) { ch=fgetc(fp); printf("%c",ch); delay(5000); } } fclose(fp); } 115. /* rewrite above program to display contents more than one file * # include <stdio.h> main(int argc, char *argv[]) { FILE *fp; char st[80];

Bye from this page

SISI Computers

Sri

Page68 of 114 int i; if (argc<2) { printf("Required parameters missing "); exit(0); } for(i=1;i<argc;i++) { fp=fopen(argv[i],"r"); if (fp==NULL) { printf("file not found - %s \n",argv[i]); getch(); } else {printf("contents of %s \n",argv[i]); while(!feof(fp)) { fgets(st,80,fp); printf("%s",st); delay(5000); } getch(); } } fclose(fp); }

116. /* write a program equivalent dos copy command */ #include<stdio.h> main(int argc,char *argv[]) { FILE *fp,*dp; char ch; clrscr(); if(argc<3) { printf("Required parameter missing "); exit(0); } if(argc>3) { printf("Too many parameters "); exit(0); }

Bye from this page

SISI Computers

Sri

Page69 of 114 fp=fopen(argv[1],"r"); if(fp==NULL) { printf("File not found %s\n",argv[1]); getch(); } else { dp=fopen(argv[2],"w"); while(!feof(fp)) { ch=fgetc(fp); fputc(ch,dp); delay(5000); } printf("File copied "); } fcloseall(); getch(); }

/* Findfirst and Findnext functions:findfirst() function:syn:- findfirst(filename, ffblk, attrib); syn:- findnext(ffblk); findfirst searches a disk directory for files and findnext continues findfirst search. On success these functions return 0. where file name is the file sepcification path can contains dos wild card characters ? (matches one character) and * (matches a series of characters). ffblk is DOS file control block structure. struct ffblk { char ff_reserved[21]; char ff_attrib; unsigned ff_ftime; unsigned ff_fdate; long ff_fsize; char ff_name[13];

Bye from this page

SISI Computers

Sri

Page70 of 114

}; attrib is MS-DOS file attributes. FA_RDONLY - Read only attributes. FA_HIDDEN - Hiddon files. FA_SYSTEM - System files. FA_LABEL - Volume lable FA_DIREC - Directorys FA_ARCH - Archive files. */ 117. /* example for findfirst and find next functions */

#include<dir.h> #include<dos.h> main() { int i,c=0; struct ffblk st; clrscr(); i=findfirst("f*.?",&st,FA_ARCH); while(i==0) { printf("%s---->%d\n",st.ff_name,st.ff_fsize); i=findnext(&st); c++; if(c%24==0) getch(); } printf("%d files",c); getch(); } 118. /* write a program to display contents from more than one file using command line arguments the parameters can include dos wild card characters (* and ?) for a group of files */ # include <stdio.h> # include <dir.h> # include <dos.h> void disp(char fnam[]) { FILE *fp; char ss[80]; clrscr(); fp=fopen(fnam,"r"); if (fp==NULL) { printf("file not found");

Bye from this page

SISI Computers

Sri getch(); } else {

Page71 of 114

printf("%s contents ......",fnam); while(!feof(fp)) { fgets(ss,80,fp); printf("%s",ss); delay(50000); } } getch(); fclose(fp); } main(int argc, char *argv[]) { int i,n; struct ffblk st; if (argc<2) { printf("Required parameters missing "); exit(0); } for(i=1;i<argc;i++) { if(strchr(argv[i],'*') || strchr(argv[i],'?')) {n=findfirst(argv[i],&st,FA_ARCH); while(n==0) { disp(st.ff_name); n=findnext(&st); } } else { disp(argv[i]); } } } DATA FILES Formatted I/o:It is used to read and write formatted data type into a file or a from a file . It is used f scanf (), fpritn(), f function .

Bye from this page

SISI Computers

Sri

Page72 of 114

fscanf() function: Syn:- fscanf(fp,format,variables); It is used to read the formatted data from a file fprintf() function:Syn:- fprintf(fp,format,variables); It is used to write the formatted data to a file. 119. /* Data creation program for student.dat which contains the following structure. Student number, name, marks in 3 papers */ #include<stdio.h> main() { int sno,m1,m2,m3; char name[20],ch; FILE *fp; clrscr(); fp=fopen("student.dat","w"); do { printf("\n Enter student no "); scanf("%d",&sno); printf("Enter student name "); scanf("%s",name); printf("Enter marks in 3 subjects "); scanf("%d %d %d",&m1,&m2,&m3); fprintf(fp,"%d %s %d %d %d\n",sno,name,m1,m2,m3); printf("Any more student (y/n)"); ch=getche(); }while(ch=='y'); fclose(fp); } 120. /* write a program to process student.dat and print each student marks statement in your own format */ #include<stdio.h> main() { int sno,m1,m2,m3,tm; char name[20],res[30],div[30]; float am; FILE *fp; clrscr(); fp=fopen("student.dat","r");

Bye from this page

SISI Computers

Sri

Page73 of 114

while(!feof(fp)) { fscanf(fp,"%d %s %d %d %d\n",&sno,name,&m1,&m2,&m3); tm=m1+m2+m3; am=tm/3; if(m1>=50 && m2>=50 && m3>=50) { strcpy(res,"PASS"); if(am>=60) strcpy(div,"FIRST CLASS"); else strcpy(div,"SECOND CLASS"); } else { strcpy(res,"FAIL"); strcpy(div,"NILL"); } clrscr(); gotoxy(25,5);printf("SAI INFOVISION SOFTWARE INSTITUTE "); gotoxy(25,6);printf("___________________________________"); gotoxy(25,7);printf("Student number = %d",sno); gotoxy(25,8);printf("Student name = %s",name); gotoxy(25,9);printf("Student marks = %d\t%d\t%d",m1,m2,m3); gotoxy(25,10);printf("Student Total marks = %d",tm); gotoxy(25,11);printf("Student Average marks = %.2f",am); gotoxy(25,12);printf("Student result = %s",res); gotoxy(25,13);printf("Student division = %s",div); getch(); } fclose(fp); } 121. /* write a program to create a data file called emp.dat which contains the following record structure. emplyee number, name, territory number, office number and annual salary, there are 3 territories (1,2,3) in each territory there are two offices. */ #include<stdio.h> main() { FILE *fp; int eno,tno,ono; char ena[20],ch; float sal; clrscr(); fp=fopen("emp.dat","w");

Bye from this page

SISI Computers

Sri do {

Page74 of 114

printf("enter employee no"); scanf("%d",&eno); printf("enter employee name"); scanf("%s",ena); printf("enter territory no (1/2/3):"); scanf("%d",&tno); printf("enter office no(1/2):"); scanf("%d",&ono); printf("enter annual salary:"); scanf("%f",&sal); fprintf(fp,"%d %s %d %d %f\n",eno,ena,tno,ono,sal); printf("enter any more(y/n)"); ch=getche(); } while(toupper(ch)=='Y'); fclose(fp); } 122. /* write a program to process emp.dat and print employee report in a list format using the following conditions. territory number 1 1 2 2 3 3 office number 1 2 1 2 1 2 bonus 6% 5% 5% 4% 4 3%

At the end of the report print total bonus amount */ #include<stdio.h> void line() { int i; for(i=1;i<=80;i++) printf("-"); } main() { int eno,tno,ono,s=1,r=8; char ena[20]; float asal,bamt,tbamt=0;

Bye from this page

SISI Computers

Sri

Page75 of 114 FILE *fp; clrscr(); fp=fopen("emp.dat","r"); gotoxy(28,2); printf("SAI INFORMATION TECHNOLOGY"); gotoxy(35,3); printf("VIJAYAWADA"); gotoxy(1,5); line(); gotoxy(1,6); printf("slno"); gotoxy(10,6);printf("eno"); gotoxy(20,6); printf("name"); gotoxy(40,6);printf("tno"); gotoxy(50,6); printf("ono"); gotoxy(60,6);printf("asal"); gotoxy(70,6); printf("bonus"); gotoxy(1,7);line(); while(!feof(fp)) { fscanf(fp,"%d %s %d %d %f\n",&eno,ena,&tno,&ono,&asal); if(tno==1) { if(ono==1) bamt=asal*.06; else bamt=asal*.05; }

else if(tno==2) { if(ono==1) bamt=asal*.05; else bamt=asal*.04; } else { if(ono==1) bamt=asal*.04; else bamt=asal*.03; } tbamt=tbamt+bamt; gotoxy(1,r); printf("%d",s); gotoxy(10,r);printf("%d",eno); gotoxy(20,r); printf("%s",ena); gotoxy(40,r);printf("%d",tno);

Bye from this page

SISI Computers

Sri

Page76 of 114 gotoxy(50,r); printf("%d",ono); gotoxy(60,r);printf("%.2f",asal); gotoxy(70,r); printf("%.2f\n",bamt); s++; r++; getch(); } line(); gotoxy(50,++r);printf("Total Bonus amount"); gotoxy(70,r);printf("%.2f\n",tbamt); line(); fclose(fp); getch();

} 123. /* write a program to create a data file called sales.dat which contains the following record structure. Sales man number, name, number of items sold, item code (d- disk/ t- tape), size code (a/b/c), type of customer (w-whole sale, o-others ) */ #include<stdio.h> main() { FILE *fp; int sno,nis; char sna[20],ic,sc,tc,ch; clrscr(); fp=fopen("sales.dat","w"); do { printf("\n enter salesman number "); scanf("%d",&sno); printf("enter salesman name : "); scanf("%s",&sna); printf("enter number of items sold : "); scanf("%d",&nis); printf("enter item code (d/t) : "); ic = getche(); printf("\n enter size code (a/b/c) : "); sc=getche(); printf("\n enter type of customer (w/o) : "); tc=getche(); fprintf(fp,"%d %s %d %c %c %c\n",sno,sna,nis,ic,sc,tc); printf("\n enter any more(y/n) : "); ch=getche(); }

Bye from this page

SISI Computers

Sri while(toupper(ch)=='Y'); fclose(fp); }

Page77 of 114

124. /* write a program to process data file called sales.dat and print a report in the following format. d t a 900 1200 b 600 900 c 300 600

10% discound for wholesale customers and nill for others. #include<stdio.h> void line() { int i; for(i=0;i<80;i++) printf("-"); } main() { int sno,nis,s=1,r=8; char sna[20],ic,sc,tc; float samt,dis,net,tsamt=0; FILE *fp; clrscr(); fp=fopen("sales.dat","r"); gotoxy(28,2); printf("SAI INFORMATION TECHNOLOGY"); gotoxy(35,3); printf("VIJAYAWADA"); gotoxy(1,5); line(); gotoxy(1,6); printf("slno"); gotoxy(5,6);printf("sno"); gotoxy(13,6); printf("name"); gotoxy(25,6);printf("nis"); gotoxy(33,6); printf("code"); gotoxy(38,6);printf("size"); gotoxy(43,6); printf("type"); gotoxy(48,6); printf("samt"); gotoxy(60,6); printf("dis"); gotoxy(70,6); printf("net"); gotoxy(1,7);line(); while(!feof(fp)) {

Bye from this page

SISI Computers

Sri

Page78 of 114 fscanf(fp,"%d %s %d %c %c %c\n",&sno,sna,&nis,&ic,&sc,&tc); if (ic=='d') { if(sc=='a') samt=nis*900; else if (sc=='b') samt=nis*600; else if (sc=='c') samt = nis * 300; } else if( ic=='t') { if(sc=='a') samt=nis*1200; else if (sc=='b') samt=nis*900; else if (sc=='c') samt = nis * 600; } if (tc=='w') dis=samt*0.1; else dis=0; net=samt-dis; tsamt=tsamt+net; gotoxy(1,r); printf("%d",s); gotoxy(5,r);printf("%d",sno); gotoxy(13,r); printf("%s",sna); gotoxy(25,r);printf("%d",nis); gotoxy(33,r); printf("%c",ic); gotoxy(38,r);printf("%c",sc); gotoxy(43,r); printf("%c",tc); gotoxy(48,r); printf("%9.2f",samt); gotoxy(60,r); printf("%9.2f",dis); gotoxy(70,r); printf("%9.2f\n",net); s++; r++; } line(); gotoxy(50,++r);printf("Total sales amount"); gotoxy(70,r);printf("%9.2f\n",tsamt); line(); fclose(fp); getch();

Bye from this page

SISI Computers

Sri

Page79 of 114

RANDOM ACCESS FILES Steps to read a record in random order :1. Send the file pointer to the end of last record using fseek() function 2. Find out the total size of the file using ftell() function. 3. Calculate the total number of records that is total size of files / record size. 4. Read a valid record number 5. Send the file pointer given to the first byte poisition and read the record. 1.fseek() function:syn:- fseek (fp,offset, whence); It positions the file pointer of a file where off set is the new position relative to the position specified by whence where whence SEEK_SET - seeks from beginning pf file SEEK_CUR - seeks from current position. SEEK_END - seeks from end of file. 2. ftell() function:syn:- ftell( fp) ; It returns the current file pointer position on success. 125. /* write a program to process stud.dat data file in random access */ #include<stdio.h> struct srec { int sno,m1,m2,m3; char sna[20]; }st; main() { FILE *fp; int tm,rno,nor,fs; char res[20],div[20]; float am; clrscr(); fp=fopen("stud.dat","r"); fseek(fp,0,SEEK_END); fs=ftell(fp); nor = fs/sizeof(st); do { printf("\n enter record number between 1 to %d 0 to stop : ",nor); scanf("%d",&rno); if (rno>=1 && rno <=nor) { fseek(fp,(rno-1)*sizeof(st),SEEK_SET); fread(&st,sizeof(st),1,fp);

Bye from this page

SISI Computers

Sri

Page80 of 114

tm=st.m1+st.m2+st.m3; am=tm/3; if(st.m1>=50 && st.m2>=50 && st.m3>=50) { strcpy(res,"pass"); if(am>=60) strcpy(div,"first"); else strcpy(div,"second"); } else { strcpy(res,"fail"); strcpy(div,"nill"); } clrscr(); gotoxy(35,2); printf("SISI-CMTES"); gotoxy(35,3); printf("vijayawada"); gotoxy(28,5); printf("student report"); gotoxy(28,6); printf("================"); gotoxy(25,8); printf("stdent number %d",st.sno); gotoxy(25,9); printf("student name %s",st.sna); gotoxy(25,10); printf("marks in 3 papers %d %d %d",st.m1,st.m2,st.m3); gotoxy(25,11); printf("total marks %d",tm); gotoxy(25,12); printf("average marks %.2f",am); gotoxy(25,13); printf("result %s",res); gotoxy(25,14); printf("division %s",div); getch(); } else if (rno!=0) { printf("\n Invalid record number \n"); getch(); } }while(rno); fclose(fp); } Block I/o: It is used to read and write records block by block (a group of records ) for this be used fread() and fwrite() function. Syn:- fread( addr of obj , size of obj, number of records, fp); It is used to read a records from a file, where object is a structure variable Fwrite () function:-

Bye from this page

SISI Computers

Sri

Page81 of 114 Syn:- fwrites (addr of obj, size of obj, Number of records , fp); It is used to write a group of records to a file 126. /* data creation program for stud.dat using block i/o */ # include <stdio.h> struct srec { int sno,m1,m2,m3; char sna[20]; }st;

main() { char ch; FILE *fp; fp=fopen("stud.dat","w"); clrscr(); do { printf("\n enter studuent number : "); scanf("%d",&st.sno); printf("enter student name : "); scanf("%s",st.sna); printf("enter marks in 3 papers : "); scanf("%d %d %d",&st.m1,&st.m2,&st.m3); fwrite(&st,sizeof(st),1,fp); printf("any more students (y/n) "); ch=getche(); }while(toupper(ch)=='Y'); fclose(fp); } 127. /* write a program to process stud.dat using block i/o */ #include<stdio.h> struct srec { int sno,m1,m2,m3; char sna[20]; }st; main() { FILE *fp; int tm; char res[20],div[20]; float am; clrscr(); fp=fopen("stud.dat","r"); while(!feof(fp)) {

Bye from this page

SISI Computers

Sri

Page82 of 114

fread(&st,sizeof(st),1,fp); tm=st.m1+st.m2+st.m3; am=tm/3; if(st.m1>=50 && st.m2>=50 && st.m3>=50) { strcpy(res,"pass"); if(am>=60) strcpy(div,"first"); else strcpy(div,"second"); } else { strcpy(res,"fail"); strcpy(div,"nill"); } clrscr(); gotoxy(35,2); printf("SISI-CMTES"); gotoxy(35,3); printf("vijayawada"); gotoxy(28,5); printf("student report"); gotoxy(28,6); printf("================"); gotoxy(25,8); printf("student number %d",st.sno); gotoxy(25,9);printf("student name %s",st.sna); gotoxy(25,10); printf("marks in 3 papers %d %d %d",st.m1,st.m2,st.m3); gotoxy(25,11); printf("total marks %d",tm); gotoxy(25,12); printf("average marks %.2f",am); gotoxy(25,13); printf("result %s",res); gotoxy(25,14); printf("division %s",div); getch(); } fclose(fp); } /* data creation program for cust.dat using block i/o for a group of records */ #include<stdio.h> main() { FILE *fp; struct crec { int cno,samt; char cna[20]; }cust[5];

Bye from this page

SISI Computers

Sri int i; clrscr(); fp=fopen("cust.dat","w"); for(i=0;i<5;i++) { printf("\n enter customer number:"); scanf("%d",&cust[i].cno); printf("enter customer name :"); scanf("%s",cust[i].cna); printf("enter sales amount : "); scanf("%d",&cust[i].samt); } fwrite(&cust,sizeof(cust),5,fp); fclose(fp); }

Page83 of 114

128. /* data process program for cust.dat using block i/o for a group of records */ #include<stdio.h> main() { FILE *fp; struct crec { int cno,samt; char cna[20]; }cust[5]; int i; clrscr(); fp=fopen("cust.dat","r"); fread(&cust,sizeof(cust),5,fp); fclose(fp); for(i=0;i<5;i++) { printf("customer number :%d\n",cust[i].cno); printf("customer name :%s\n",cust[i].cna); printf("sales amount :%d\n\n",cust[i].samt); } getch(); } Data Structures :Data structures refers usually a structure of a graph or

Bye from this page

SISI Computers

Sri

Page84 of 114

a group of nodes where the data can be shared. Arrays, Structures, unions are known as static data structures because it is deficult to maintain disk files, C provides dynamic data structures which can be created, manipulated and maintained using pointer address method. Because it is very easy to alter address rather than disk files. Using dynamic data structures, simple to complex applications, editors, linkers, loaders, ascemblers and even operationg systems can build. The following are the different tyes of dynamic data structures: 1. stacks 2. queues 3. linked lists. a) linear linked lists b) doubly linked lists c) circular lists 4. Trees 5. Graphs Linear Linked lists:A Linear Linked List isa dynamic data structure where in this list each node contains some data and a pointer variable. The pointer points either next node or previous node. so that the list is connected in order then the same list can be traversed either FIFO (First in first out) or LIFO (Last in first out) method. 129. /* write a program to create a linear linked list and display the list in FIFO method */ #include<alloc.h> #include<stdlib.h> struct node { int n; struct node *next; } main() { struct node *f,*l,*c; int k; clrscr(); f=l=c=NULL; do { printf("Enter any number 0 to stop "); scanf("%d",&k); if(k!=0)

Bye from this page

SISI Computers

Sri {

Page85 of 114

c = (struct node *) malloc( sizeof(struct node)); c->n=k; if (f==NULL) f=c; if(l==NULL) { l=c; l->next=NULL; } else { l->next = c; l=c; l->next=NULL; } } } while(k); printf("List output is \n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } getch(); } 130. /* write a program to create a linear linked list and display the list in LIFO method */ #include<alloc.h> #include<stdlib.h> struct node { int n; struct node *prev; }; main() { struct node *l,*c; int k; clrscr(); l=c=NULL; do

Bye from this page

SISI Computers

Sri {

Page86 of 114

printf("Enter any number 0 to stop "); scanf("%d",&k); if(k!=0) { c=(struct node *) malloc(sizeof(struct node)); c->n=k; if(l==NULL) { l=c; l->prev=NULL; } else { c->prev = l; l=c; } } } while(k); printf("List output is \n"); c=l; while(c!=NULL) { printf("%d\t",c->n); c=c->prev; } getch(); } 131. /* write a program to accept any file name and print its contents in reverse order */ #include<alloc.h> #include<stdlib.h> #include<stdio.h> struct node { char st[80]; struct node *prev; } main() { struct node *l,*c; char fname[20],ss[80];

Bye from this page

SISI Computers

Sri

Page87 of 114 FILE *fp; clrscr(); l=c=NULL; printf("Enter any file name "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL) printf("File not found "); else while(!feof(fp)) { fgets(ss,80,fp); c=(struct node *) malloc(sizeof(struct node)); strcpy(c->st,ss); if(l==NULL) { l=c; l->prev=NULL; } else { c->prev =l; l=c; } } c=l; while(c!=NULL) { printf("%s",c->st); c=c->prev; delay(50000); } getch();

} 132. /* write a program to create a linear linked list in sorted order */ #include<alloc.h> #include<stdlib.h> #include<stdio.h> struct node { int n; struct node *next;

Bye from this page

SISI Computers

Sri }; main() {

Page88 of 114

struct node *f,*b,*a,*l,*c; int k; clrscr(); f=l=c=NULL; clrscr(); do { printf("Enter any number 0 to stop"); scanf("%d",&k); if(k!=0) { c=(struct node *)malloc(sizeof(struct node)); c->n=k; if(f==NULL) f=c; if(l==NULL) { l=c; l->next=NULL; } else { if(c->n < f->n) { c->next=f; f=c; } else if(c->n > l->n) { l->next=c; l=c; l->next=NULL; } else { a=f; while(a->n < c->n) { b=a; a=a->next; } b->next=c; c->next=a;

Bye from this page

SISI Computers

Sri } } } }while(k!=0); printf("List output is \n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } getch(); }

Page89 of 114

133. /* write a progam to delete a node in linear linked list */ #include<alloc.h> #include<stdlib.h> #include<stdio.h> struct node { int n; struct node *next; }; main() { struct node *f,*b,*a,*l,*c; int k; clrscr(); f=l=c=NULL; clrscr(); do { printf("Enter any number 0 to stop "); scanf("%d",&k); if(k!=0) { c=(struct node *) malloc(sizeof(struct node)); c->n=k; if(f==NULL) f=c; if(l==NULL) { l=c; l->next=NULL;

Bye from this page

SISI Computers

Sri } else {

Page90 of 114

if(c->n < f->n) { c->next =f; f=c; } else if(c->n > l->n) { l->next=c; l=c; l->next=NULL; } else { a=f; while(a->n < c->n) { b=a; a=a->next; } b->next=c; c->next=a; } } } }while(k!=0); printf("List output is \n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } do { printf("\nEnter any number to delete 0 to stop "); scanf("%d",&k); if(k!=0) { c=f; while(c->n!=k && c!=NULL) { b=c;

Bye from this page

SISI Computers

Sri

Page91 of 114 c=c->next; } if(c==NULL) printf("Value not found in the list \n"); else { if(c==f) f=f->next; else b->next=c->next; free(c); printf("Node has been deleted \n"); c=f; while(c!=NULL) { printf("%d \t",c->n); c=c->next; } printf("\n"); } } else { c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } } }while(k); getch();

} 134. /* write a program to satisfy the following menu options in linear linked list. MAIN MENU 1. CREATE 2. INSERT 3. DELETE 4. LISTING 5. EXITING Enter your choice (1-5) : c

Bye from this page

SISI Computers

Sri

Page92 of 114

Doubly linked list :In doubly linked list, each node contains two pointers, one points next node address and another points previous node address, so that it is possible to travels the list both in FIFO and LIFO methods. In order to terminate the list, last node next points to null, and first node previous points to null. 135. /* write a program to create a doubly linked list and display the list in LIFO and FIFO methods */ #include<alloc.h> #include<stdlib.h> #include<stdio.h> struct node { int n; struct node *next; }*f=NULL,*l=NULL,*c=NULL,*c,*b,*a; int k; void create() { clrscr(); do { printf("Enter any number 0 to stop "); scanf("%d",&k); if(k!=0) { c=(struct node *) malloc(sizeof(struct node)); c->n=k; if(f==NULL) f=c; if(l==NULL) { l=c; l->next=NULL; } else { if(c->n < f->n) { c->next =f; f=c; } else if(c->n > l->n) {

Bye from this page

SISI Computers

Sri

Page93 of 114 l->next=c; l=c; l->next=NULL; } else { a=f; while(a->n < c->n) { b=a; a=a->next; } b->next=c; c->next=a; } }

} }while(k!=0); } void listing() { clrscr(); printf("List output is \n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } getch(); } void delnode() { clrscr(); do { printf("\nEnter any number to delete 0 to stop "); scanf("%d",&k); if(k!=0) { c=f; while(c->n!=k && c!=NULL) { b=c; c=c->next; }

Bye from this page

SISI Computers

Sri

Page94 of 114 if(c==NULL) printf("Value not found in the list \n"); else { if(c==f) f=f->next; else b->next=c->next; free(c); printf("Node has been deleted \n"); c=f; while(c!=NULL) { printf("%d \t",c->n); c=c->next; } printf("\n"); } } else { c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } } }while(k); getch();

} main() { int ch; do { clrscr(); gotoxy(35,10);printf("MAIN MENU"); gotoxy(35,11);printf("========"); gotoxy(35,13);printf("1.CREATE"); gotoxy(35,14);printf("2.INSERT"); gotoxy(35,15);printf("3.DELETE"); gotoxy(35,16);printf("4.LISTING"); gotoxy(35,17);printf("5.EXITING"); gotoxy(35,19);printf("Enter U R choice (1-5):"); scanf("%d",&ch);

Bye from this page

SISI Computers

Sri switch(ch) { case 1: case 2:create();break; case 3:delnode();break; case 4:listing();break; } }while(ch<5); }

Page95 of 114

136. /* write a program to create a doubly linked list in sorted order */ struct node *next,*prev; #include<alloc.h> #include<stdlib.h> #include<stdio.h> struct node { int n; struct node *next; }*f=NULL,*l=NULL,*c=NULL,*c,*b,*a; int k; void create() { clrscr(); do { printf("Enter any number 0 to stop "); scanf("%d",&k); if(k!=0) { c=(struct node *) malloc(sizeof(struct node)); c->n=k; if(f==NULL) f=c; if(l==NULL) { l=c; l->next=NULL; } else { if(c->n < f->n) {

Bye from this page

SISI Computers

Sri

Page96 of 114 c->next =f; f=c; } else if(c->n > l->n) { l->next=c; l=c; l->next=NULL; } else { a=f; while(a->n < c->n) { b=a; a=a->next; } b->next=c; c->next=a; } }

} }while(k!=0); } void listing() { clrscr(); printf("List output is \n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } getch(); } void delnode() { clrscr(); do { printf("\nEnter any number to delete 0 to stop "); scanf("%d",&k); if(k!=0) { c=f;

Bye from this page

SISI Computers

Sri

Page97 of 114 while(c->n!=k && c!=NULL) { b=c; c=c->next; } if(c==NULL) printf("Value not found in the list \n"); else { if(c==f) f=f->next; else b->next=c->next; free(c); printf("Node has been deleted \n"); c=f; while(c!=NULL) { printf("%d \t",c->n); c=c->next; } printf("\n"); } } else { c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } } }while(k); getch();

} main() { int ch; do { clrscr(); gotoxy(35,10);printf("MAIN MENU"); gotoxy(35,11);printf("========"); gotoxy(35,13);printf("1.CREATE"); gotoxy(35,14);printf("2.INSERT");

Bye from this page

SISI Computers

Sri

Page98 of 114

gotoxy(35,15);printf("3.DELETE"); gotoxy(35,16);printf("4.LISTING"); gotoxy(35,17);printf("5.EXITING"); gotoxy(35,19);printf("Enter U R choice (1-5):"); scanf("%d",&ch); switch(ch) { case 1: case 2:create();break; case 3:delnode();break; case 4:listing();break; } }while(ch<5); }

} main() { struct node *f,*l,*c,*t; int k; clrscr(); f=l=c=NULL; clrscr(); do { printf("enter any number 0 to stop"); scanf("%d",&k); if(k!=0) { c=(struct node *) malloc(sizeof(struct node)); c->n=k; if(f==NULL) { f=c; f->prev=NULL; } if(l==NULL) { l=c; l->next=NULL; } else { if(c->n < f->n) {

Bye from this page

SISI Computers

Sri

Page99 of 114 c->next=f; f->prev=c; f=c; f->prev=NULL; } else if(c->n > l->n) { l->next=c; c->prev=l; l=c; l->next=NULL; } else { t=f; while(t->n < c->n) t=t->next; t->prev->next=c; c->prev=t->prev; c->next=t; t->prev=c; } }

} }while(k); printf("\nlist forward output is\n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } printf("\nlist Back ward output is\n"); c=l; while(c!=NULL) { printf("%d\t",c->n); c=c->prev; } getch(); } 137. /* write a program to create a doubly linked list in sorted order and delete a node */

Bye from this page

SISI Computers

Sri

Page100 of 114

#include<alloc.h> #include<stdlib.h> struct node { int n; struct node *next,*prev; } main() { struct node *f,*l,*c,*t; int k; clrscr(); f=l=c=NULL; clrscr(); do { printf("enter any number 0 to stop"); scanf("%d",&k); if(k!=0) { c=(struct node *)malloc(sizeof(struct node)); c->n=k; if(f==NULL) { f=c; f->prev=NULL; } if(l==NULL) { l=c; l->next=NULL; } else { if(c->n < f->n) { c->next=f; f->prev=c; f=c; f->prev=NULL; } else if(c->n > l->n) { l->next=c; c->prev=l; l=c; l->next=NULL; }

Bye from this page

SISI Computers

Sri else { t=f;

Page101 of 114

while(t->n < c->n) t=t->next; t->prev->next=c; c->prev=t->prev; c->next=t; t->prev=c; } } } }while(k); printf("\nlist forward output is\n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } printf("\nlist Back ward output is\n"); c=l; while(c!=NULL) { printf("%d\t",c->n); c=c->prev; } do { printf("\n enter any no to delete a node 0 to stop"); scanf("%d",&k); if(k!=0) { c=f; while(c->n!=k && c!=NULL) c=c->next; if(c==NULL) printf("value not foundin the list\n"); else { if(c==f) { f=f->next; f->prev=NULL; }

Bye from this page

SISI Computers

Sri

Page102 of 114 else if(c==l) { l=l->prev; l->next=NULL; } else { c->next->prev=c->prev; c->prev->next=c->next; } free(c); printf("Node has been deleted \n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } }

} }while(k); getch(); } 138. /* write a program to satisfy the following menu options in doubly linked list MAIN MENU --------1. CREATE 2. INSERT 3. DELETE 4. LIST FORWARD. 5. LIST BACKWARD. 6. EXIT MENU. Enter your choice (1-6) : */ #include<alloc.h> #include<stdlib.h> struct node { int n; struct node *next,*prev; } *f=NULL,*l=NULL,*c,*t; int k;

Bye from this page

SISI Computers

Sri

Page103 of 114

void create() { clrscr(); do { printf("enter any number 0 to stop"); scanf("%d",&k); if(k!=0) { c=(struct node*)malloc(sizeof(struct node)); c->n=k; if(f==NULL) { f=c; f->prev=NULL; } if(l==NULL) { l=c; l->next=NULL; } else { if(c->n < f->n) { c->next=f; f->prev=c; f=c; f->prev=NULL; } else if(c->n > l->n) { l->next=c; c->prev=l; l=c; l->next=NULL; } else { t=f; while(t->n < c->n) t=t->next; t->prev->next=c; c->prev=t->prev; c->next=t; t->prev=c;

Bye from this page

SISI Computers

Sri }

Page104 of 114

} } }while(k); } void listf() { clrscr(); printf("list forward output is\n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; }getch(); } void listb() { clrscr(); printf("\n list backward output is\n"); c=l; while(c!=NULL) { printf("%d\t",c->n); c=c->prev; } getch(); } void delnode() { clrscr(); do { printf("\n enter any number to delete 0 to stop"); scanf("%d",&k); if(k!=0) { c=f; while(c->n!=k && c!=NULL) c=c->next; if(c==NULL) printf("value not found list\n"); else { if(c==f)

Bye from this page

SISI Computers

Sri

Page105 of 114 { f=f->next; f->prev=NULL; } else if(c==l) { l=l->prev; l->next=NULL; } else { c->next->prev=c->prev; c->prev->next=c->next; } free(c); printf("Node has been deleted\n"); c=f; while(c!=NULL) { printf("%d\t",c->n); c=c->next; } } } }while(k); getch(); } main() { int ch; do { clrscr(); gotoxy(35,10); printf("main menu"); gotoxy(35,11); printf("---------"); gotoxy(35,13); printf("1.CREATE"); gotoxy(35,14); printf("2.INSERT"); gotoxy(35,15); printf("3.DELETE"); gotoxy(35,16); printf("4.LIST FORWORD"); gotoxy(35,17); printf("5.LIST BACKWARD"); gotoxy(35,18); printf("6.EXIT"); gotoxy(35,20); printf("enter your choice (1-6)"); scanf("%d",&ch); switch(ch) { case 1:

Bye from this page

SISI Computers

Sri

Page106 of 114 case 2:create();break; case 3:delnode();break; case 4:listf();break; case 5:listb();break; } }while(ch<6); }

139. /* write a program to sort data file in ascending order on nummeric field */ #include<alloc.h> #include<stdio.h> struct node { int num,n1,n2,n3; char name[20]; struct node *next,*prev; }; main() { struct node *f,*l,*c,*t; int m1,m2,m3,sno; char sna[20]; FILE *fp; clrscr(); f=l=c=NULL; clrscr(); fp=fopen("student.dat","r"); while(!feof(fp)) { fscanf(fp,"%d %s %d %d %d\n",&sno,sna,&m1,&m2,&m3); c=(struct node *) malloc(sizeof(struct node)); c->num=sno; strcpy(c->name,sna); c->n1=m1; c->n2=m2; c->n3=m3; if(f==NULL) { f=c; f->prev=NULL; } if(l==NULL) { l=c;

Bye from this page

SISI Computers

Sri

Page107 of 114 l->next=NULL; } else if(c->num < f->num) { c->next = f; f->prev= c; f=c; f->prev=NULL; } else if(c->num > l->num) { l->next = c; c->prev = l; l=c; l->next = NULL; } else { t=f; while(t->num < c->num) t=t->next; t->prev->next=c; c->prev=t->prev; t->prev=c; c->next=t; } } c=f; printf("Rno\t Name\t\t Sub1 \t Sub2 \t Sub3\n");

while(c!=NULL) { printf("%2d\t%9s\t %d\t %d\t %d\n",c->num,c->name,c->n1,c->n2,c->n3); c=c->next; } getch(); } Binary Tree :Like doubly linked list, in binary tree, each node contains two pointers which are called left and right pointers. The first node what we have created is "Root" node, and the subsequent nodes must be connected either left to the root node or right to the root node. If the value already exists in tree display error. Tree can be

Bye from this page

SISI Computers

Sri traversed using 3 methods.

Page108 of 114

1. Preorder : process node, go left, and go right. 2. Inorder : go left, Process node,and go right. 3. Post order: go left, go right , process node. 140. /* example for binary tree */ #include<alloc.h> #include<stdlib.h> struct node { int n; struct node *left,*right; }; struct node *create(struct node *rt,int k) { if(rt==NULL) { rt=(struct node *) malloc(sizeof(struct node)); rt->n=k; rt->left=rt->right=NULL; } else if(k<rt->n) rt->left=create(rt->left,k); else if(k>rt->n) rt->right=create(rt->right,k); else printf("Duplicate values \n"); return rt; } void preorder(struct node *rt) { if(rt!=NULL) { printf("%d\t",rt->n); preorder(rt->left); preorder(rt->right); } } void inorder(struct node *rt) {

Bye from this page

SISI Computers

Sri if(rt!=NULL) { inorder(rt->left); printf("%d\t",rt->n); inorder(rt->right); } } void postorder(struct node *rt) { if(rt!=NULL) { postorder(rt->left); postorder(rt->right); printf("%d\t",rt->n); } } main() { int k; struct node *root=NULL; clrscr(); do {

Page109 of 114

printf("Enter any number 0 to stop "); scanf("%d",&k); if(k!=0) root=create(root,k); }while(k); printf("\n preorder output\n "); preorder(root); printf("\n inorder output\n "); inorder(root); printf("\n post order output \n"); postorder(root); getch(); } 141. /* write a program to accept any file and print its character frequency */ #include<alloc.h> #include<stdio.h> #include<stdlib.h> int s=0; struct node {

Bye from this page

SISI Computers

Sri char ch; int n; struct node *left,*right;

Page110 of 114

}; struct node *create(struct node *rt,char ch1) { if(rt==NULL) { rt=(struct node *) malloc(sizeof(struct node)); rt->ch=ch1; rt->n=1; rt->left=rt->right=NULL; } else if(ch1<rt->ch) rt->left=create(rt->left,ch1); else if(ch1>rt->ch) rt->right=create(rt->right,ch1); else rt->n++; return rt; } void inorder(struct node *rt) { if(rt!=NULL) { inorder(rt->left); printf("%c ------> %d\n",rt->ch,rt->n); s++; if(s%23==0) getch(); delay(500000); inorder(rt->right); } } main() { FILE *fp; char ch,fname[20]; struct node *root=NULL; clrscr(); printf("Enter any file name "); scanf("%s",fname); fp=fopen(fname,"r"); if(fp==NULL)

Bye from this page

SISI Computers

Sri

Page111 of 114 printf("File not found "); else { while(!feof(fp)) { ch=fgetc(fp); root=create(root,ch); } inorder(root); } getch();

} Graphics 142. #include <graphics.h> main() { int gd=DETECT,gm; clrscr(); initgraph (&gd,&gm," "); printf("maximum x value is %d\n",getmaxx()); printf("maximum y value is %d\n",getmaxy()); setcolor(14); circle(320,240,50); getch(); closegraph(); } 143. #include<graphics.h> #include<stdlib.h> main() { int gd=DETECT,gm,i; clrscr(); randomize(); initgraph(&gd,&gm," "); while(!kbhit()) { for(i=1;i<240;i++) {setcolor(random(16)); circle(320,240,i); delay(5000); } for(i=240;i>=1;i--) { setcolor(0);

Bye from this page

SISI Computers

Sri circle(320,240,i); delay(5000); } } getch(); closegraph(); } 144. #include<graphics.h> #include<stdlib.h> main() { int gd=DETECT,gm,i; clrscr(); randomize(); initgraph(&gd,&gm," "); while(!kbhit()) { setcolor(random(16)); circle(random(640),random(480),20); delay(5000); } getch(); closegraph(); }

Page112 of 114

145. #include<graphics.h> #include<stdlib.h> main() { int gd=DETECT,gm,i; clrscr(); randomize(); initgraph(&gd,&gm," "); while(!kbhit()) { setcolor(random(15)); line(random(500),random(480),random(640),random(480)); delay(5000); } getch(); closegraph(); } 146. #include<graphics.h> #include<stdlib.h>

Bye from this page

SISI Computers

Sri main() { int gd=DETECT,gm,i; clrscr(); initgraph(&gd,&gm," "); ellipse(320,240,0,360,100,150); setfillstyle(1,4); fillellipse(320,240,150,100); getch(); closegraph(); }

Page113 of 114

147. #include<graphics.h> #include<stdlib.h> main() { int gd=DETECT,gm,i; clrscr(); randomize(); initgraph(&gd,&gm," "); while(!kbhit()) { setfillstyle(random(10),random(16)); fillellipse(random(640),random(480),20,20); delay(5000); } getch(); closegraph(); } 148. #include<graphics.h> #include<stdlib.h> main() { int gd=DETECT,gm,i,a[2000]; int ar[18]={50,50,70,50,70,40,100,40,100,50,120,50,120,70,50,70,50,50}; initgraph(&gd,&gm," "); drawpoly(9,ar); fillellipse(70,70,8,8); fillellipse(100,70,8,8); getimage(45,40,125,85,a); while(!kbhit()) { for(i=45;i<=590;i++) {

Bye from this page

SISI Computers

Sri putimage(i,40,a,COPY_PUT); delay(5000); } for(i=590;i>=45;i--) {putimage(i,40,a,COPY_PUT); delay(5000); } } getch(); closegraph(); } 149. #include<graphics.h> #include<stdlib.h> main() { int gd=DETECT,gm; initgraph(&gd,&gm,""); outtextxy(0,100,"welcome to"); settextstyle(4,0,9); outtextxy(100,200,"Sasi & Rani"); getch(); closegraph(); }

Page114 of 114

150. #include<graphics.h> #include<stdlib.h> main() { int gd=DETECT,gm; int a[18]={50,50,70,50,70,40,100,40,50,120.50,120,70,50,70,50,50}; initgraph(&gd,&gm," "); drawpoly(9,ar); circle(70,70,8); circle(100,70,8); getch(); closegraph(); }

Bye from this page

SISI Computers

You might also like