Anda di halaman 1dari 96

C Language Introduction ________________________ Languages : ___________ A Set Of Statements Is Called A Language.

There Are Four Types Of Languages According To Their Time. I generation languages: _______________________ These languages are machine languages. To write programs in these languages the system technology must be required. The data is Non-portable. That means a program written in a system does not work in another systems. II Generation Languages : ________________________ These Are Assembly Languages. These Are Also system oriented that means to write any program in a system that system's technology must be required and data is non-portable. But they used MNEMONIC words in programs. That means they used a single word instead of more words. III Generation Languages : _________________________ In these languages programs are witten in general english language. There is no need to know the system technology and the data can be transfered anywhere. IV Generation languages : _________________________ These languages are defined in any one of the above languages. These are also called as packages. Here I & II Generation languages are called Low Level Languages and III & IV generation languages are called High Level Languages. For high level languages we have to use translaters to translate the source co de written in general english language into machine language. These translaters are two types. 1) Interpreters 2) Compilers. 1) Interpreters :

These translaters translate the source code step by step into machine language until any error. If there is any error it stops and shows some messag e. After correction it can continue. Ex: BASIC, DBase III+, .... 2) Compilers :

These translaters translate the entire source code into machine language when it is error-free and creates an object file in machine

language. If there is any error it shows the list of error. After debugging it creates the object file. Ex: COBOL, C, C++, ... C LANGUAGE __________ The language 'C' was designed by Dennis Ritchie at AT & T Bell Laboratories. The standardised C was released in 1979. The 'C' language is used to develop WHAT IS C --------C is a programming language developed at AT & T's Bell Laboratories of U.S.A in 1972. It was designed and written by Dennis Ritchie. HISTORICAL DEVELOPMENT OF C ---------------------------By 1960 a hoarde of computer languages had come into existence, almost each for a specific purpose. For example, COBOL was being used for Commercial Applications, FORTRAN for Engineering and Scientific Applications and so on. At this stage people started thinking that instead of learning and using so many languages, each for a different purpose, why not use only one language, each for a different purpose, why not use only one

i) ii) iii) iv) v)

Scientific applications, Business applications, Graphical applications (Ex: WINDOWS ), System programs, Operating Systems (Ex: UNIX) , ...

Character Set : alphabets digits ==> special symbols constants, variables, keywords statements, ==> Programs instructions

Constants : The unchangeable quantities are called Constants.The constants are generally two types. 1) Character constants : ____________________ a) Characters b) Strings 2) Ex: 'a', '5', '+', ' ', ... Ex: "abc", "435", 'rama", ...

Numeric Constants : a) integers Ex: 435, -657, 65535, -32768,...

b) Real numbers i) Fractional form Ex: 435.67, 345.00054, ... ii) Exponential form Ex: 0.02e3, 1.17e-38, ... Variables : __________ The quantities which can be changed during the execution of program are called Variables. A variable can be considered as the name of the cell which can hold the constants. To use any variable it must be declared with its data type before the first executable statement and they can be initialised. Naming the variable is very important. 1) The variable name must be start with either alphabets or an underscore and may contain alphabets, digits, hyphen or underscore. 2) The maximum length of a variable is 8 characters. But some compilers can accept upto 32 characters. 3) There must not be any blank spaces or special symbols in a variable name. 4) A variable name must not be a keyword. Ex: valid ----eno empname emp_name Keywords : _________ These are predefined words. There are 32 keywords in C language. These keywords can not be used as user-defined variables. Operators : __________ There are 42 operators in C language. 1) Arithmetic Operators : + - * / Example : 100 + 40 ==> 100 - 40 ==> 100 * 40 ==> 100 / 40 ==> 100 % 40 ==> 40 % 100 ==> 2) Assigning Operators : % 140 60 4000 2 20 40 = invlid -----emp name emp(name 45abc

(variable) = (constant) / (variable) / (expression) ; Example :

a = 5 b = a c = a + b -2 3) Multiple operators : += Example : a a a a a = = = = = a a a a a + * / % 3 3 3 3 3 ==> ==> ==> ==> ==> a a a a a += -= *= /= %= 3 3 3 3 3 ++ --= *= /= %=

4) Unary Operators : Ex : a = a + 1 a = a - 1

==> a += 1 ==> a ++ ==> ++ a ==> a -= 1 ==> a -- ==> -- a

5) Relational Operators : == > < >= <= !=

6) Logical Operators : && && - AND - OR ! - NOT AND T T F F T F T F = = = = T F F F T T F F T F T F OR = = = = T T T F F T NOT F = T = T F !

, . : ; < > # { [ ( ) ] } ......

Structure of a 'C' program : preprocessor commands global declarations main() { local declarations ; statements ; } function(arguments)

{ local declarations ; statements ; } * The 'C' program has a free formated structure. * Every statement must be terminated with a semicolon ; * A program is a collection of functions. There may be a lot of functions but at least one function must be there that is main(), where the execution starts. * C has case sensitivity. All the keywords are defined in lower case. * So better to write entire program in lower case. Preprocessor commands : ---------------------The commands which start with a hash(#) symbol are called Preprocessor commands. Eample : # include <stdio.h> # include "conio.h" # define PI 3.14159 Global declarations : _____________________ To use any variable it must be declared with its data type before the first executable statement. The variables which declared in a block are available in that block only. To use the variable in the entire program with same effect it must be declared as global. Data Types : __________________________________________________________________ Type Range occupied bytes format string -----------------------------------------------------------------signed char -128 to 127 1 %c unsigned char 0 to 255 1 %c shortsigned int -32768 to 32767 2 %i %d %o %x short unsigned int 0 to 65535 2 %u long signed int -2^31 to 2^31 -1 4 %ld long unsigned int 0 to 2^32 -1 4 %lu float 3.14e-38 to 3.14e38 4 %f %e double 1.17e-308 to 1.17e308 8 %lf long double 1.17e-4932 to1.17e4932 10 %Lf ____________________________________________________________________ Functions : ----------The functions are two types. 1) derived functions. 2) user-defined functions. The derived functions are defined by the 'C' authors. They defined them in the header files. To use the function the header file must be included as preprocessor

statement. 1) clrscr() :This function is used to clear the screen. This function's prototype has defined in the header file CONIO.H ( CONIO ==> Console Input Output ) Syntax : clrscr(); 2) printf() :This function is used to display the text and the values of variables. This function's prototype has defined in the header file STDIO.H To display the variable's value the format string must be used. (STDIO ==> Syntax : Standard Input Output )

printf(" format string ", variables) ; Example : printf(" Hello \t World "); printf(" %d %c", k, j); printf("The marks are %d, %d, %d", m1, m2, m3 ); Note : The function printf() returns an integer value that is the number of arguments given to the statement. Remarks : To write any remarks or comments to the statements they must be enclosed with the symbols /* */ Example : /* Write a program .......... .............so ..and ..so */

Example Programs : 1). My First 'C' Program # include <stdio.h> # include <conio.h> main() { clrscr() ; printf("Hello" ); printf("Softech Computers") ; printf("Welcome") ; }

Save this program (F2) as FIRST.C After compilation(Alt-F9) it creates an object file, and an executable file which can be executed at MS-DOS prompt. By saving a modified file it creates a backup file. FIRST.C FIRST.BAK FIRST.OBJ FIRST.EXE Output : Hello Welcome TURBO C editor : It is a compiler of C program and it can be also used as an general editor. To enter into editor first change into the directory which contains the software and enter the command TC at the command prompt. C:\> CD TC C:\TC> tc Then it opens the editor which contains a menu bar at the top, a status bar at the bottom and a main window to write the programming statements and a sub window which shows the messages. The menu bar contains some menu pads and they can be selected by pressing ALT and the highlighted character in the required menu pad. Then it shows the submenu which contains some bars and they can be selected using arrow keys. The status bar shows online help and the keys information. 1). To write a new program select 'New' command from "File" menu. 2). To save the working program select 'Save' command from "File" menu or press F2 and enter a name. 3) To compile the program select 'Compile to OBJ' command from "compile" menu or press Alt + F9. Then it shows the list of errors or warnings. If the program is error-free then the compiler creates an object file (.OBJ) and an executable file (.EXE). To execute the program select 'Run' command from "Run" menu or press Ctrl + F9. To seee the output of the execution select 'User Screen' command from "Run" menu or press Alt + F5. To close the editor select 'Quit' command from "File" menu or press Alt + X.

4) 5) 6)

Escape Sequences : _________________ \0 \t \l \r \n \a \' \" \\ printf() ______ :Syntax of Printf() printf(); ======== Def :- printf is a function printf the values on the screen Syantax :printf("Message"); printf("format string",var); printf("format string,format string",var1,var2); ==> ==> ==> ==> ==> ==> ==> ==> ==> Null character Tab ( 8 spaces) Line feed Carriage Return New line character ( \n = \l + \r ) Alert (beep sound) Single quotes Double quotes back slash

P . N . R / 100 main() { int p; int n; int r; int si; p=1000; r=2; n=12; si=p*n*r/100; printf("%d%d%d",p,n,r); printf("%d",si); } Diffrent Type Useges of C program ----------------------------------1. main() { int a; int b; int c; clrscr(); a=10; b=20; c=a+b; printf("%d",c);

} 2. main() { int a,b,c; a=10; b=20; c=a+b; printf("%d",c); } 3. main() { int a=10,b=20,c; c=a+b; printf("%d",c); } 4. main() { int a=10,b=20,c=a+b; printf("%d",c); } 5. main() { int a=10,b=20; printf("%d",a+b); } 6. main(){int a=10,b=20;printf("%d",a+b);} program to compute 619+348 -------------------------main(){ int a=1; float area=69.25; clrscr(); printf("%4d\n",a); a=10; printf("%4d\n",a); a=100; printf("%4d\n",a); a=1000; printf("%4d\n",a); printf("%.2f",area); } Ex Programs : 2) Using Escape Sequences # include <stdio.h> # include <conio.h> main() { clrscr() ;

printf("Hello \t ") ; printf("Softech \n") ; printf("Welcome ") ; } Output : Hello Softech Welcome 3) # include <stdio.h> # include <conio.h> main() { clrscr() ; printf("Hello \t Softech \n Welcome ") ; } Output : Hello Softech Welcome

4)

Using Variables # include <stdio.h> # include <conio.h> main() { int k = char j = clrscr() printf("\n printf("\n } Output : The value of k is 65 65 A 101 42 The value of j is 42 42 * 52 2a

65 ; '*' ; ; The value of k is %i %d %c %o %x", k, k, k, k, k ) ; The value of j is %i %d %c %o %x", j, j, j, j, j ) ;

5)

Formatting the output # include <stdio.h> # include <conio.h> main() { int a, b, c ; clrscr() ; a = 6 ; b = 23456; c = 678 ;

printf("\n %05d \t %d", a, a ) ; printf("\n %05d \t %d", b, b ) ; printf("\n %5d \t %d", c, c ) ; } Output : 00006 23456 678 6 23456 678

6)

Arithmetic Operations # include <stdio.h> # include <conio.h> main() { int a, b, c, d, e, f ; clrscr() ; a = 100 ; b = 40 ; c = a + b ; d = a - b ; e = a * b ; f = a / b ; printf("The given values are %d, %d", a, b ) ; printf("\n The addition is %d", c) ; printf("\n The subtraction %d", d) ; printf("\n The product is %d", e) ; printf("\n The division %d", f) ; printf("\n The reminder is %d", a%b) ; } Output : The The The The The The given values are 100, 40 addition is 140 subtraction 60 product is 4000 division 2 reminder is 20

Notes : The Arithmetic operations are three types depend on the types of the operands in the expression. operand1 integer integer real Ex Programs : 7) Type casting # include <stdio.h> # include <conio.h> operand2 integer real real result integer real real

main() { int m1, m2, m3, tot; float avg ; clrscr() ; m1 = 65; m2 = 66; m3 = 68; tot = m1 + m2 + m3 ; avg = tot / 3.0 ; avg = (float) tot / 3 ; printf("The three subjects marks are %d,%d,%d",m1,m2,m3); printf("\n The total %d \t Average %f", tot, avg ) ; } Output : The three subjects marks are 65, 66, 68 The total 199 Average 66.33 8) Formatting the output of floating point values # include <stdio.h> # include <conio.h> main() { float bas, da, hra, pf, net ; clrscr() ; bas = 5000; da = bas * 20 / 100 ; hra = bas * 30 / 100 ; pf = bas * 5 / 100 ; net = bas + da + hra - pf ; printf("The Basic Salary %f", bas) ; printf("\n Da %.1f\tHra%010.3f\tPf%5.0f",da,hra,pf); printf("\n Net Salary %10.2f", net) ; } Output : The Basic Salary 5000.000000 Da 1000.0 Hra 001500.000 Net Salary 7250.00 9) Program to demonstrate the # include <stdio.h> # include <conio.h> main() { int k = 5 ; clrscr() ; printf("\n %d", k ++ ; printf("\n %d", printf("\n %d", printf("\n %d", Pf 00250

Increment / Decrement operators

k) ; ++k) ; k++); k) ;

k -- ; printf("\n %d", k--) ; printf("\n %d", --k) ; k = ++k + ++k + ++k ; printf("\n %d", k) ; k = 5 ; k = k++ + ++k + ++k + k++ + k++ ; printf("\n %d", k) ; getch() ; } scanf() : _________ This function is used to accept the values for the variables while executing the program from keyboard. This function's prototype has defined in the header file STDIO.H The function printf() returns an integer value that is the number of arguments given to the statement. Reading data items from the standard Input into variables Standard Input Statement Syntax : scanf(<format>,<address of variable>); scanf("format ",&var); scanf("format ",&var1,&var2,&var3); scanf("formatstring" , &(variables) ); Note : To accept two or more values with a single scanf() they can be seperated by space or tab or enter key. Format: string containg the type of the data Exampel : "%d" address of variable Note : it is the physical location where the variable has been created. To know the address of a variable we can use the address operator &. Example: scanf("%d",&i); scanf("%d",&a); scanf("%d%d%d",&a,&b); main() { int a=1; float area=69.25; clrscr(); printf("%4d\n",a); a=10; printf("%4d\n",a); a=100; printf("%4d\n",a);

a=1000; printf("%4d\n",a); printf("%.2f",area); } Adding two no using scanf(); main() { int a,b,c; clrscr(); printf("Enter a value "); scanf("%d",&a); printf("Enter b value "); scanf("%d",&b); c=a+b; printf("Addition %d ",c); } Student marks program :main() { int sno,m1,m2,m3,tot,avg; char sname[10]; clrscr(); printf("\nEnter student no"); scanf("%d",&sno); printf("\nEnter student name "); scanf("%s",sname); printf("\nEnter three subject maks"); scanf("%d%d%d",&m1,&m2,&m3); tot=m1+m2+m3; avg=tot/3; printf("\nStudent no %d",sno); printf("\nStudent name %s",sname); printf("\nThree subject makrs %d\t%d\t%d",m1,m2,m3); printf("\nTota marks %d",tot); printf("\nAverage maks %d",avg); }

getch() : _______ This function is used to accept a single character for the variable while executing the program. But this function does not display the entered character. This function's prototype has defined in the header file CONIO.H Note : -----To see the entered character the function Syntax: (variable) = getch() ; Example : char c; c = getch();

getche() can be Used.

Example Programs : 9) Program to demonstrate the difference between the functions scanf(), getche(), getch() # include <stdio.h> # include <conio.h> main() { char k ; clrscr(); printf("Enter any character ") ; scanf("%c", &k) ; printf("You entered the character %c", k) ; printf("\n\n Enter any character ") ; k = getche(); printf("\n You entered the character %c", k) ; printf("\n\n Enter any character ") ; k = getch() ; printf("\n You entered the character %c", k) ; getch(); } Output : Enter any character abcdef You entered the chracter a Enter any character g You entered the chracter g Enter any character You entered the chracter d 10) Write a program to calculate the total, average of a student's three subjects marks # include <stdio.h> # include <conio.h> main() { int m1, m2, m3, tot; float avg ; clrscr() ; printf("Enter three subjects marks \n") ; scanf("%d%d%d", &m1, &m2, &m3 ) ; tot = m1 + m2 + m3 ; avg = tot / 3.0 ; avg = (float) tot / 3 ; printf("The three subjects marks are %d,%d,%d",m1,m2,m3); printf("\n The total %d \t Average %f", tot, avg ) ; } Output : Enter three subjects marks

65 66 68 The three subjects marks are 65, 66, 68 The total 199 Average 66.33 11) Write a program to accept an employee's basic salary, calculate da, hra, pf, net salary and print all Notes : Conditional Statements : In C language the conditional statement returns zero when the condition is false. Otherwise it returns a non-zero(1) value when the condition is true. Ex Program : # include <stdio.h> # include <conio.h> main() { int k = 5 ; clrscr() ; printf("\n printf("\n printf("\n printf("\n printf("\n getch() ; } output %d", %d", %d", %d", %d", k ); k<10) ; k>10) ; k+(k==5) ); k=10) ; 5 1 0 6 10

Notes : There are three types of conditional statements in 'C'. I) if II) switch II) conditional operators 1). if ... else : Syntax : if (condition) { (statements); } else { (statements) ; } Ex Program : 13) Write a program to check whether the if (condition) { (statements); or }

given number is zero or not # include <stdio.h> # include <conio.h> main() { int k; clrscr() ; printf("Enter any number ") ; scanf("%d", &k) ; if(k==0) printf("The number is zero ") ; else printf("The number is not zero ") ; getch() ; }

14) Write a program to check the given number is positive or negative # include <stdio.h> # include <conio.h> main() { int k ; clrscr() ; printf("Enter any number ") ; scanf("%d", &k) ; if(k==0) printf("The number is zero ") ; else if(k>0) printf("The number is Positive "); else printf("The number is Negative ") ; getch() ; } 15) Write a program to find the big number in the given two numbers. # include <stdio.h> # include <conio.h> main() { int a, b ; clrscr() ; printf("Enter any two numbers \n") ; scanf("%d%d", &a, &b) ; if(a==b) printf("\n Given both are equl ") ; else

{ printf("\n The big is ") ; if(a>b) printf("%d", a) ; else printf("%d", b) ; } getch() ; } 16) Write a program to find the biggest number in the given three numbers # include <stdio.h> # include <conio.h> main() { int a, b, c ; clrscr() ; printf("Enter any three numbers \n") ; scanf("%d%d%d", &a, &b, &c) ; if(a==b && a==c) printf("Given all are equal ") ; else { printf("\n The biggest is ") ; if(a>b && a>c) printf("%d", a) ; else if(b>c) printf("%d", b); else printf("%d", c); } getch() ; } 17 Write a program to find the smallest number in the given five numbers # include <stdio.h> # include <conio.h> main() { int a, b, c, d, e, t ; clrscr() ; printf("Enter any five numbers \n") ; scanf("%d%d%d%d%d", &a, &b, &c, &d, &e ) ; if(a==b && a==c && a==d && a==e) printf("\n Given all are equal ") ; else { t = a ; if(t>b) t = b; if(t>c) t = c ; if(t>d) t = d ; if(t>e) t = e ;

printf("\n The biggest is %d", t) ; } getch( ); } program to display the positive value of a number main() { int n; clrscr(); printf("\n Enter any negitive no"); scanf("%d",&n); if(n<0) /* n=-n;*/ n*=-1; printf("%d",n); } 18) Write a program to find the biggest and smallest numbers in the given five numbers # include <stdio.h> # include <conio.h> main() { int a, b, c, d, e, x, y ; clrscr() ; printf("Enter any five numbers \n") ; scanf("%d%d%d%d%d", &a, &b, &c, &d, &e ) ; if(a==b && a==c && a==d && a==e) printf("\n Given all are equal ") ; else { x = a ; y = a ; if(x<b) x = b; else y = b; if(x<c) x = c ; else if(y>c) y = c ; if(x<e) x = e ; else if(y>d) y = d ; if(x<e) x = e ; else if(y>e) y = e ; printf("\n The biggest is %d", x) ; printf("\n The smallest is %d", y) ; } getch( );

} 19) Write a program to accept three subjects marks of a student, calculate total, average find the result, division and print all details # include <stdio.h> # include <conio.h> main() { int m1, m2, m3, tot ; float avg ; clrscr() ; printf("Enter three subjects marks \n") ; scanf("%d%d%d", &m1, &m2, &m3 ) ; tot = m1 + m2 + m3 ; avg = (float) tot / 3 ; printf("The three subjects marks %d, %d, %d", m1, m2, m3 ) ; printf("\n The Total %d \t Average %03.2f", tot, avg ) ; if(m1<35 m2<35 m3<35) { printf("\n Result is Fail ") ; printf("\t division is NIl ") ; } else { printf("\n Result is Pass \t") ; if(avg>=60) printf("Division is I class ") ; else if(avg>=50) printf("Division is II class") ; else printf("Division is III class") ; } getch() ; } 20) Write a Employee Salary Program Employee name Basic Salary . Conditions :bas<500 hra da 5% 8% bas>=500 and bas<1000 8% 10% bas>=1000 10% 12% Accept Employee no,

pf 2% 3% 4%

esi 1% 2% 3%

Calculate Gross and Net Salary Print all Details

main() { int eno,bas,hra,da,net,pf,esi,gross;

char ename[10]; printf("\n Enter employee No"); scanf("%d",&eno); printf("\n Enter employee name "); scanf("%s",ename); printf("\n Enter employee basic Salary "); scanf("%d",&bas); if(bas<500) { hra=bas*5/100; da =bas*8/100; pf= bas*2/100; esi=bas*1/100; } else if(bas>=500 && bas<1000) { hra=bas*8/100; da=bas*10/100; pf=bas*3/100; esi=bas*2/100; } else if(bas>=1000) { hra=bas*10/100; da=bas*12/100; pf=bas*4/100; esi=bas*3/100; } gross=bas+hra+da; net=gross-(pf+esi); printf("\n printf("\n printf("\n printf("\n printf("\n printf("\n printf("\n printf("\n printf("\n getch(); } Employee No Employee Name Employee Basic H.R.A D.A P.F E.S.I Gross N.E.T Salary %d ",eno); %s",ename); %d",bas); %d",hra); %d",da); %d",pf); %d",esi); %d",gross); %d",net);

Notes : 2) switch... case : Syntax:

switch(variable) { case (value) : (statements) ; case (value) : (statements) ; default : (statements) ; } break : This keyword stops the execution in the given block and come out. Generally this is used in switch..case statements and looping Statements. Ex Programs : 21) # include <stdio.h> # include <conio.h> main() { int k ; clrscr() ; printf("Enter any number " ); scanf("%d", &k) ; switch(k) { case 0 : printf("\n Number is zero " ); case 1 : case 2 : case 3 : case 4 : printf("\n Number is less than five ") ; break ; case 5 : printf("\n Number is five " ); break ; default : printf("\n Number is greater than five ") ; } getch() ; } 22) # include <stdio.h> # include <conio.h> main() { char k; clrscr() ; printf("Enter any one of the alphabets " ); k = getche() ; switch(k) { case 'a' : case 'A' :

printf("\n A for Active ") ; break ; case 'b' : case 'B' : printf("\n B for Brave ") ; break ; case 'c' : case 'C' : printf("\n C for Courage "); break ; case 'd' : case 'D' : printf("\n D for Dare ") ; break ; default : printf("\n You are timid ") ; } getch() ; } 23) # include <stdio.h> # include <conio.h> main() { int a, b, k ; clrscr() ; printf("Enter two numbers \n") ; scanf("%d%d", &a, &b) ; printf("\n\n 1. Addition ") ; printf("\n 2. Subtraction ") ; printf("\n 3. Multiplication ") ; printf("\n 4. Division ") ; printf("\n\n Select your choice ") ; scanf("%d", &k); printf("\n") ; switch(k) { case 1 : printf(" The addition %d", a+b) ; break ; case 2 : printf(" The subtraction %d", a-b) ; break ; case 3 : printf(" The multiplication %d", a*b); break ; case 4 : printf(" The division %d", a/b); break ; default : printf(" Invalid choice "); } getch() ; } Notes : 3) Conditional Expressions : Syntax : ( ? : ; )

(condition) ? (statement1) : (statement2) ; Ex Programs : 24) Write a program to check whether the given number is zero or not. # include <stdio.h> # include <conio.h> main() { int k; clrscr() ; pritnf("Enter any number ") ; scanf("%d", &k); (k==0) ? printf("Number is zero ") : pritnf("Number is not zero ") ; getch() ; }

25) Write a program to find the biggest number in the given three numbers # include <stdio.h> # include <conio.h> main() { int a, b, c, t ; clrscr() ; printf("Enter any three numbers \n") ; scanf("%d%d%d", &a, &b, &c); t = (a>b) ? a : b ; printf("The biggest is %d", (t>c)?t:c ); getch() ; } Notes : gotoxy() : This function locates the cursor position to the given place on the screen. This function's prototype has defined in the header file CONIO.H Syntax: gotoxy(column, row) ; Generally in MS-DOS mode the screen contains 80 columns and 25 rows. Ex Programs : 26) # include <stdio.h> # include <conio.h> main() { clrscr() ; gotoxy(20, 3) ; printf("Hello "); gotoxy(70, 5); printf("Welcome") ;

gotoxy(35,12); printf("Welcome "); gotoxy(50,20); printf("To smile "); getch() ; }

Notes : goto : This command changes the execution control to the given statement. Syntax: goto (label) ; (label) : (statements) ; Example Programs : 27) # include <stdio.h> # include <conio.h> main() { clrscr() ; printf("Hello ") ; printf("World ") ; goto abc ; printf("Go out ") ; xyz : printf("To smile ") ; goto end ; abc : printf("Welcome ") ; goto xyz ; end : getch() ; } Output : Hello World Welcome To smile Note : Looping Statements : Repeating a block of statements number of times is called Looping.There are three types of looping statements defined in C language. 1) do..while 2) while 3) for. Note : The keyword 'goto' cn be also used to repeat a block of

statements number of times. Example Programs : 28) # include <stdio.h> main() { abc : printf("Welcome ") ; goto abc ; } 29) /* Program to display the first 10 natural numbers */ # include <stdio.h> # include <conio.h> main() { int k ; clrscr() ; k = 1 ; abc : printf("%d ", k) ; k++ ; if(k<=10) goto abc ; getch() ; }

Notes : 1) do ... while() : Syntax : do { (statements); } while(condition) ; Ex Programs : Without loop natural numbers main() { int n=1; clrscr(); printf("\n%d",n); n=n+1; printf("\n%d",n); n=n+1; printf("\n%d",n); n=n+1; printf("\n%d",n); n=n+1; printf("\n%d",n); }

30)

/* Write a program to display the first 10 natural numbers

*/

# include <stdio.h> # include <conio.h> main() { int k ; clrscr() ; k = 1 ; do { printf("%d ", k) ; k++ ; }while(k<=10) ; getch() ; } 31) /* Write a program to display the even numbers upto the given number an d find the sum of them */ # include <stdio.h> # include <conio.h> main() { int k, n, s ; clrscr() ; printf("Enter any number ") ; scanf("%d", &n) ; k = 2 ; s = 0; do { printf("%d ", k) ; s += k ; k += 2 ; }while (k<=10) ; printf("\n The sum is %d", s) ; getch() ; } Notes : There is a draw-back in do.. while() staement. It executes the conditional state ment after executing the statement. 2) while() : Syntax: while(condition) { (statements); } Ex Programs : 32) /* Write a program to display the first 10 natural numbers */

# include <stdio.h> # include <conio.h> main() { int k ; clrscr() ; k = 1 ; while(k<=10) { printf("%d ", k) ; k++ ; } getch() ; } 33) /* Write a program to display the even numbers upto the given number an d find the sum of them */ # include <stdio.h> # include <conio.h> main() { int k, n, s ; clrscr() ; printf("Enter any number ") ; scanf("%d", &n) ; k = 2 ; s = 0; while (k<=10) { printf("%d ", k) ; s += k ; k += 2 ; } printf("\n The sum is %d", s) ; getch() ; }

0000000 program to generate natural number series upto 20. main(){ int i; i=1; clrscr(); while(i<20){ printf("%d\n",i); i++; } }

program to compute the biggest of a set of numbers. main(){ int a; int sum; printf("Enter all the numbers and a 0 at the end"); sum=0; scanf("%d",&a); sum=a; while(a!=0){ scanf("%d",&a); sum+=a; } printf("sum = %d",sum); } Write a program Generate Natural Numbers 1 to 100 using While Loop

main() { int n=1; clrscr(); while(n<=100) { printf("\t%d",n); n=n+1; } getch(); } Write a program print name 100 times

main() { int n=1; clrscr(); while(n<=100) { printf("\tRam Kumar"); n=n+1; } getch(); } Write program Generate Natural numbers 1 to 100 in Revers order main() { int n=100; while(n>=1) { printf("\t%d",n); n=n-1;

} getch(); } W.A.P Generate natural numbers upto Given no 1 to n<100 1 to t=25 main() { int n=1,t; printf("\n Enter any no"); scanf("%d",&t); while(n<=t) { printf("\t%d",n); n=n+1; } getch(); }

main() { int n,t; printf("\n Enter Starting no"); scanf("%d",&n); printf("\n Enter Ending no "); scanf("%d",&t); while(n<=t) { printf("\t %d",n); n=n+1; } getch(); } W.A.P Sum of natural numbers upto 10 s=s+n 0 4 5 -----15 -----3 6 10 15 s = s + n 1 = 0 + 1 1 + 2 3 3 6 4 10 5 1 2 3

Sum of Natural Numbers up to Given no Sum of Odd Sum of Even

main() { int n=1,s=0; clrscr(); while(n<=5) { s=s+n; n=n+1; } printf("\n Sum of Natural numbers %d",s); } W.A.P Accept no Find Foctorial Value 5*4*3*2*1=120 1*1= 1 1*2= 2 2*3= 6 6*4= 24 24*5=120 main() { int n=1,ft=1,f; printf("\n Enter any no"); scanf("%d",&f); while(n<=f) { ft=ft*n; n=n+1; } printf("\n Factorial Value %d",ft); } Write a program Generate Table 1 to 10 given table 1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 2 * 2 * 2 * 1 2 3 = 1 = 2 = 6

main() { int n=1,t,r; printf("\n Enter any table"); scanf("%d",&t); while(n<=10) { r=n*t; printf("\n %d * %d = %d",t,n,r); n=n+1; } }

W.A.P Generate Febonachi 1 1 2 3 5 8 13 21 main() { int s1=1,s2=1,s3,t,n=1; printf("\n Enter any no"); scanf("%d",&t); printf("\n%d",s1); printf("\n%d",s2); while(n<=t) { s3=s1+s2; printf("\n %d",s3); s1=s2; s2=s3; n=n+1; } }

n=n+1 n=n-1 n=n+2 n=n-2

n++ or ++n n-- or --n n+=2 n-=2

000000000

34) /* Write a program to check whether the given number is Prime or not Prime Number : A number which is divisible by 1 and itselft. */ /* I method */

# include <stdio.h> # include <conio.h> main() { int n, k, s; clrscr() ; printf("Enter any number ") ; scanf("%d", &n) ; k = 1; s = 0; while(k<=n) { if(n%k==0) s++; k++ ; } if(s==2) printf("\n Number is Prime " ); else printf("\n Number is not a Prime ") getch( ); } /* II method */ # include <stdio.h> # include <conio.h> main() { int n, k, s; clrscr() ; printf("Enter any number ") ; scanf("%d", &n) ; k = 2; s = 0; while(k<=n/2) { if(n%k==0) { s++; break ; } k++ ; } if(s==0) printf("\n Number is Prime " ); else printf("\n Number is not a Prime ") getch( ); } 35) /* Write a program to find the number of digits, sum of digits, and rev erse order of the given number */ # include <stdio.h> # include <conio.h> main() { long int a, b ; int n, s, r ; clrscr( ); printf("Enter any big number " ); scanf("%ld", &a) ; n = 0; s = 0; b = 0; while(a>0) { n ++ ;

r s b a

= a % 10 ; += r ; = (b*10) + r ; /= 10 ; }

printf("\n The number of digits %d", n) ; printf("\n The sum of digits %d", s) ; printf("\n The Reverse number %lu", b) ; getch( ); } 36) /* Write a program to check whether the given number is Armstrong or not

Armstrong Number : A number which is equal to the sum of the cubes of the digits is called Armstrong Number. Ex: 1, 153, 370, 371, 407 */ # include <stdio.h> # include <conio.h> # include <math.h> main() { int a, b, r, s ; clrscr() ; printf("Enter any number ") ; scanf("%d", &a) ; b = a ; s = 0; while(a>0) { r = a % 10 ; s = pow(r, 3); /* a /= 10; }

s += r * r * r ;

*/

if(b==s) printf("\n The number is an Armstrong ") ; else printf("\n The number is not an Armstrong ") ; getch() ; } Notes : 3) for() : Syntax: for ( initialisation ; condition ; iteration ) { (statements) ; } Ex Programs : 37) /* Write a program to display the odd numbers upto the given number */

# include <stdio.h> # include <conio.h> main() { int n, a ; clrscr() ; printf("Enter any number " ); scanf("%d", &a ); n = 1 ; for ( ; a>=n ; ) { printf("%d ", n); n += 2 ; } */ for(n=1; n<=a ; n+=2) printf("\n %d " , n) ; getch() ; } /*

38) /* Write a program to display the even numbers upto the given number and find the sum of them */ # include <stdio.h> # include <conio.h> main() { int a, n, s ; clrscr() ; printf("Enter any number ") ; scanf("%d", &a) ; /* n = 2 ; s = 0 ; for( ; n<=a ; ) { printf("%d ", n) ; s += n ; n += 2 ; } */ /* s = 0 ; for(n=2; n<=a; n+=2) { printf("%d " , n ); s += n; } */ for(n=2, s=0 ; n<=a ; s+=n, n+=2 )

printf("%d ", n) ; printf("\n The sum is %d", s) ; getch() ; }

39) /* Write a program to find the sum of natural numbers, even numbers, od d numbers upto the given number */ # include <stdio.h> # include <conio.h> main() { int a, n, s, odd, even ; clrscr() ; printf("Enter any number ") ; scanf("%d", &a) ; for(n=1, s=0, odd=0, even=0 ; n<=a ; s+=n, n++) if(n%2==0) even += n; else odd += n; printf("\n The sum of natural numbers %d", s) ; printf("\n The sum of even numbers %d", even ) ; printf("\n The sum of odd numbers %d" , odd ) ; getch() ; } 40) /* Write a program to find the factorial value of the given number

n! = n * (n-1) ! */ # include <stdio.h> # include <conio.h> main() { long int a, f ; clrscr() ; printf("Enter any number ") ; scanf("%ld", &a) ; /* f = 1 ; for( ; a>1 ;) { f *= a ; a -- ; }

*/ for(f=1; a>1; f*=a, a--) ; printf("\n The factorial is %ld" , f) ; getch() ; }

41) /* Write a program to display the multiplication table of the given number using all types of loopings. */ # include <stdio.h> # include <conio.h> main() { int n, k; clrscr() ; printf("Enter any number " ); scanf("%d", &n) ; /* k = 1 ; do { printf("\n %d x %2d = %3d", n, k, n*k ); k ++ ; } while (k<=10) ; */ /* k = 1 ; while( k<= 10 ) { printf("\n %d x %2d = %3d ", n, k, n*k ); k ++ ; } */ for(k=1; k<=10; k++) printf("\n %d x %2d = %3d", n, k, n*k); getch() ; } 42) /* Write a program to display the ASCII chart

ASCII ==> American Standard Code for Information Interchange */ # include <stdio.h> # include <conio.h> main() { int k ; clrscr() ; /* do k = 0 ;

{ printf("\t %d %c", k, k) ; k ++ ; if(k%50==0) getch() ; } while(k<=255 ) ; */ /* k = 0 ; while(k<=255) { printf("\t %d %c", k, k) ; k ++ ;

if(k%50==0) getch() ; } */ for(k=0; k<=255; k++) { printf("\t %d %c", k, k) ; if(k%50==0) getch() ; } getch() ; } 43) /* Write a program to display the multiplication table of the given numb er using all types of Loopings. */ Notes : Nested Loops : Looping in a loop is called Nesting of Loops. Ex Programs : 44) # include <stdio.h> # include <conio.h> main() { int n, k, j ; clrscr() ; printf("Enter any number ") ; scanf("%d", &n) ; for(k=1; k<=n; k++) { printf("\n ") ; for(j=1; j<=k; j++) printf("%d ", j) ; } getch() ; } /* output : Enter any number 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 */ 45) # include <stdio.h> # include <conio.h> main() { int n, k, j ; clrscr() ;

printf("Enter any number ") ; scanf("%d", &n) ; for(k=1; k<=n; k++) { printf("\n ") ; for(j=1; j<=k; j++) printf("%d ", k) ; } getch() ; } /* output : Enter any number 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 46) # include <stdio.h> # include <conio.h> main() { int n, k, j ; clrscr() ; printf("Enter any number ") ; scanf("%d", &n) ; for(k=n; k>=1; k--) { printf("\n ") ; for(j=1; j<n-k; j++) printf(" ") ; for(j=1; j<=k; j++) printf("%d ", j) ; } getch() ; } /* output : Enter 1 2 3 1 2 1 1 */ any number 5 4 5 3 4 2 3 1 2

*/

47)

# include <stdio.h> # include <conio.h> main() { int n,j,k,a; clrscr(); printf("Enter the any number"); scanf("%d",&a); for(n=a;n>0;n--) { printf("\n"); for(k=1;k<=n;k++) printf("%c",64+k); for(k=1;k<=2*(a-n)-1;k++) printf(" "); k=(a==n)?n-1:n; for (;k>=1;k--) printf("%c",64+k); } getch(); } /* Output : Enter any number 5 ABCDEDCBA ABCD DCBA ABC AB A 48) /* Write the given # include # include

CBA BA A

*/

a program to display the multiplication tables upto number */ <stdio.h> <conio.h>

main() { int a, n, k ; clrscr() ; printf("Enter any number ") ; scanf("%d", &a) ; for(n=1; n<=a; n++) { for(k=1; k<=10; k++ ) printf("\n %d x %2d = %3d ", n, k, n*k); getch() ; clrscr() ; } }

49) /* Write a program to display the list of Prime numbers upto the given number */

# include <conio.h> # include <stdio.h> main() { int a, n, k, s ; clrscr() ; printf("Enter any number ") ; scanf("%d", &a ) ; for(n=2; n<=a; n++) { for(k=2, s=0; k<=n/2; k++) if(n%k==0) { s++; break ; } if(s==0) printf("%d ", n) ; } getch() ; } 50) /* Write a program to display the list of Armstrong Numbers upto the gi ven number */ 51) /* Write a program to display the Fibonacci Series upto the given numbe r Fibonacci Series : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, .... In this series every element is the sum of its previous two numbers */ 52)/* Write a program to display a box with the given character, length and width using arrays */

/* Output : Enter box length 10 Enter box width 5 Enter any character * * * * * * * * * * * * * * * * * * * * * * * * * * * */

Notes : Arrays : An array is a collection of similar data type elements. It stores the elements i n contiguous locations. To use any array it must be declared with its size and t hey may be initialized. The size must be a constant. The indexing is start from 0. The arrays are two types. 1) Single Dimensional arrays, 2) Multi Dimensional Arrays. 1) Single Dimensional Arrays : Syntax : (type) (variable) [size] ; Ex: int eno[5] = { 56, 67, 45, 89, 45}; char ena[] = { 'a', 'b', 'c', 'd' } ; float bas[] = { 56.67, 900.45, 567 } ; Ex Programs :

53) /* Write a program to create a 5 cells single dimensional array, store 7 in cells and print all */ # include <conio.h> # include <stdio.h> main() { int k, a[5] ; clrscr() ; printf("Enter any five numbers \n") ; for(k=0; k<=4; k++) a[k] = 7 ; printf("\n The array elements are \n") ; for(k=0; k<5; k++) printf("%d ", a[k]) ; getch() ; } /* Output: 7 7 7 7 7 */

W.A SINGLE dimention array program create 5 cells array store all cell values = 7 7 7 7 7 7

main() { int a[5]; int i; a[0]=7; a[1]=7; a[2]=7; a[3]=7; a[4]=7; printf("\n printf("\t printf("\t printf("\t printf("\t } or

%d",a[0]); %d",a[1]); %d",a[2]); %d",a[3]); %d",a[4]);

main() { int a[5],i; clrscr(); for(i=0;i<5;i++) a[i]=7; for(i=0;i<5;i++) printf("\t%d",a[i]); }

main() { int a[10],i,n=1; for(i=0;i<10;i++) { a[i]=n; n++; } for(i=0;i<10;i++) printf("\t %d",a[i]); } 0 1 2 3 4 5 6 7 8 9 10

54) /* Write a program to accept 5 numbers and print all in reverse order */ # include <conio.h> # include <stdio.h> main() { int k, a[5] ; clrscr() ; printf("Enter any five numbers \n") ; for(k=0; k<=4; k++) scanf("%d", &a[k]) ; printf("\n The array elements in reverse order \n") ; for(k=4; k>=0; k--) printf("%d ", a[k]) ; getch() ; } 55) /* Write a program to accept 5 numbers print all, and find the sum of them */ # include <conio.h> # include <stdio.h> main() { int k, s, a[5] ; clrscr() ; printf("Enter any five numbers \n") ; for(k=0, s=0; k<=4;s+=a[k], k++) scanf("%d", &a[k]) ; printf("\n The array elements are \n") ; for(k=0; k<5; k++) printf("%d ", a[k]) ; printf("\n The sum of elements is %d", s) ; getch() ; }

56) /* Write a program to create 10 cells single dimensional array, accept 9 cells values, assign the sum of them to the last cell and print all */ # include <conio.h> # include <stdio.h> main() { int k, a[10] ; clrscr() ; printf("Enter any nine numbers \n") ; for(k=0,a[9]=0; k<=8;a[9]+=a[k], k++) scanf("%d", &a[k]) ; printf("\n The array elements are \n") ; for(k=0; k<10; k++) printf("%d ", a[k]) ; getch() ; }

Notes : 2) Multi Dimensional Arrays : Ex: int a[5][3], b[4][5][6][7], ..... char na[3][20] = { "abcdefgh", "ramakrishna", "Bhanodaya" }; A multi dimensional array is a collection of another arrays. That means a double dimensional array is a collection of single dimensional arrays. Ex: The array a[5][3] is a collection of 5 single dimensional arrays with size 3. Ex Programs : 57) /* Write a program to create a 5x5 double dimensional array, store 7 in all cells and print them as a matrix */ # include <stdio.h> # include <conio.h> main() { int k, j, a[5][5] ; clrscr() ; for(k=0; k<5; k++) for(j=0; j<5; j++) a[k][j] = 7; for(k=0; k<=4; k++) { printf("\n"); for(j=0; j<=4; j++) printf("%d ", a[k][j] ); } getch() ; } 58) main() {

output: 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

(same as above) for(k=0; k<5; k++) for(j=0; j<5; j++) a[k][j] = (k==j k+j==4) ? 7 : 0; (same as above) } /* Output : 7 0 0 0 7 0 7 0 7 0 0 0 7 0 0 0 7 0 7 0 7 0 0 0 7

*/

59) main() { (same as above) for(k=0; k<5; k++) for(j=0; j<5; j++) a[k][j] = (k==0 k==4 (same as above) } /* Output : 7 7 7 7 7 7 0 0 0 7 7 0 0 0 7 7 0 0 0 7 7 7 7 7 7

j==0

j==4) ? 7 : 0;

*/

60) /* Write a program to display a box with the given character, length and width using arrays */ # include <stdio.h> # include <conio.h> main() { char ch, a[25][80] ; int k, j, len, w ; clrscr() ; printf("Enter the box length "); scanf("%d", &len) ; printf("Enter the box width ") ; scanf("%d", &w ); printf("Enter a character "); ch = getche() ; printf("\n\n") ; for(k=0; k<w; k++) for(j=0; j<len; j++) a[k][j] = (k==0 j==0 k==w-1 for(k=0; k<w; k++) {

j==len-1) ? ch : 32 ;

printf("\n"); for(j=0; j<len; j++) printf("%c ", a[k][j] ); } getch() ; } /* Output : Enter box length 10 Enter box width 5 Enter any character * * * * * * * * * * * * * * * * * * * * * * * * * * * */

61)/* Write a program to find the addition matrix of two 3x3 matrices */ # include <stdio.h> # include <conio.h> main() { int a[3][3], b[3][3], c[3][3], k, j ; clrscr(); printf("Enter 9 numbers for firs array \n") ; for(k=0; k<3; k++) for(j=0; j<3; j++) scanf("%d", &a[k][j] ); printf("\n Enter 9 numbers for second array \n") ; for(k=0; k<3; k++) for(j=0; j<3; j++) scanf("%d", &b[k][j] ); for(k=0; k<3; k++) for(j=0; j<3; j++) c[k][j] = a[k][j] + b[k][j] ; printf("\n The Addition matrix is \n"); for(k=0; k<3; k++) { printf("\n "); for(j=0; j<3; j++) printf("%3d ", c[k][j] ); printf(""); } getch(); }

62) /* Write a program to find the multiplication matrix of two 3x3 matrices */ # include <stdio.h>

# include <conio.h> main() { int a[3][3], b[3][3], c[3][3], k, j, t ; clrscr(); printf("Enter 9 numbers for firs array \n") ; for(k=0; k<3; k++) for(j=0; j<3; j++) scanf("%d", &a[k][j] ); printf("\n Enter 9 numbers for second array \n") ; for(k=0; k<3; k++) for(j=0; j<3; j++) scanf("%d", &b[k][j] ); for(k=0; k<3; k++) for(j=0; j<3; j++) for(t=0, c[k][j]=0 ; t<3; t++) c[k][j] += a[k][t] * b[t][j] ; printf("\n The multiplication matrix is \n"); for(k=0; k<3; k++) { printf("\n "); for(j=0; j<3; j++) printf("%3d ", c[k][j] ); printf(""); } getch(); }

SORT'S LINER */ #include<stdio.h> #include<conio.h> main() { int a[5],i,j,temp; printf("\n Enter 5 values "); for(i=0;i<5;i++) scanf("%d",&a[i]); for(i=0;i<5;i++) for(j=i+1;j<5;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } ----SORTING

for(i=0;i<5;i++) printf("\t %d",a[i]); }

Babbule Sort ___________ _ main() { int a[5],i,j,temp; printf("\n Enter 5 values"); for(i=0;i<5;i++) scanf("%d",&a[i]); for(i=0;i<5;i++) for(j=0;j<4-i;j++) if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } for(i=0;i<5;i++) printf("\t %d ",a[i]); }

Notes: STRINGS : A string is an array of characters. It ends with a null character. ( '\0' ==> N ull character ) Ex : char na1[6] = { 'a', 'b','c', 'd', 'e', '\0' } ; char na2[6] = "abcde" ; char names[][] = { "rama", "krishna", "abcd" } ; char str1[20], str2[40] ; Note : The format string for a string variable is %s . Ex programs : 63) # include <stdio.h> # include <conio.h> main() { char str[80]; clrscr(); printf("Enter any string "); scanf("%s", str) ; printf("\n You entered the string %s", str) ; getch() ; }

/* Output : Enter any string udaya bhanu You entered the string udaya

*/

Notes : scanf() function can accept the string values. But it does not allow spaces in t he string. To avoid this problem gets() can be used. gets() : This function is used to accept the value for a string variable. This function's prototype has defined in the header file STDIO.H. Syntax : gets(varaible) ; Ex : gets(str) ; puts() : This function is used to display the string value of the variable. This function 's prototype has defined in the header file STDIO.H Syntax : puts(string) ; Ex : puts("The string is "); puts(str) ; Ex Programs : 64) # include <stdio.h> # include <conio.h> main() { char str[80] ; clrscr() ; printf("Enter any string ") ; gets(str) ; printf(" You entered "); puts(str); getch() ; } /* Output : Enter any string udaya bhanu You entered the string udaya bhanu */ 65) /* Write a program to find the length of a string */ # include <stdio.h> # include <conio.h> main() { char str[80]; int k; clrscr() ; printf("Enter any string "); gets(str) ; /* k = 0; while(str[k]!='\0') k++ ; */ for(k=0; str[k] !='\0'; k++) ;

printf("\n The length is %d", k); getch() ; } 66)/* Write a program to change the given string into upper case # include <stdio.h> # include <conio.h> main() { char str[80] ; clrscr() ; int k ; */

printf("Enter any string \n") ; gets(str) ; for(k=0; str[k]!='\0'; k++) if(str[k]>=97 && str[k]<=122) str[k] -= 32 ; /* if( str[k] >='a' && str[k] <='z' ) str[k] -= 'a' - 'A'; */ printf("\n In upper case %s", str) ; getch() ; } /* Output : Enter any string Udaya Bhanu In upper case UDAYA BHANU

*/ */

67)/* Write a program to change the given string into lower case 68)/* Write a program to change the given string into Sentence case that means the first character into upper case and the remaining into lower case */ # include <stdio.h> # include <conio.h> main() { char str[80] ; int k ; clrscr() ; printf("Enter any string ") ; gets(str) ; if(str[0]>='a' && str[0]<='z' ) str[0] -= 'a' - 'A' ; for(k=1; str[k]!='\0'; k++) if(str[k]>=65 && str[k]<=90) str[k] += 32 ; printf("\n In sentence case %s", str) ; getch() ; } /* Output : Enter any string UDAYA BHANU In upper case Udaya bhanu 69)

*/ */

/* Write a program to change the given string into Title Case

/* Output : Enter any string UDAYA BHANU

In upper case Udaya Bhanu

*/ */

70) /* Write a program to copy a string to another # include <stdio.h> # include <conio.h> main() { char s[80], t[80] ; int k; clrscr() ; printf("Enter the source string to copy ") ; gets(s) ; k=0; while(s[k]!='\0') { t[k] = s[k] ; k++ ; } t[k] = '\0' ; printf("\n The new string is %s", t) ; getch() ; } 71)/* Write a program to concatenate two strings # include <stdio.h> # include <conio.h> main() { char a[80], b[80] ; int k, j ; clrscr() ; */

printf("Enter two strings \n") ; gets(a) ; gets(b); for(k=0;a[k]!='\0'; k++) ; for(j=0; b[j]!='\0'; k++, j++) a[k] = b[j] ; a[k] = '\0' ; printf("\n The concatenated string is %s", a) ; getch() ; } 72)/* Write a program to reverse the given string # include <stdio.h> # include <conio.h> main() { char a[80], b[80] ; int k, j ; clrscr() ; printf("Enter any string ") ; gets(a) ; for(k=0; a[k]!='\0'; k++) ; for(k--, j=0; k>=0; k--, j++) b[j] = a[k]; b[j] = '\0' ; */

printf("\n In reverse order %s", b) ; getch(); } /* Output : Enter any string bhanodaya In upper case ayadonahb */ 73)/* Program to move the given name around the screen # include <stdio.h> # include <conio.h> # include <string.h> main() { int k, r, c, DL; char str[80]; clrscr(); puts("Enter your name "); gets(str); k = strlen(str); DL = 10000; while(!kbhit()) { for(r=1; r<=23; r++) { gotoxy(1,r); printf("%s", str); delay(DL); clrscr(); } for(c=1; c<=80-k; c+=3) { gotoxy(c,23); printf("%s", str); delay(DL); clrscr(); } for(r=23;r>0; r--) { gotoxy(80-k,r); printf("%s", str); delay(DL); clrscr(); } for(c=80-k;c>0; c-=3) { gotoxy(c,1); printf("%s", str); delay(DL); clrscr(); } } } 74)/* Program to fall and replace the given name character by character */ # include <stdio.h> # include <conio.h> # include <string.h> # include <dos.h> main() { char na[80]; */

int r, c, k, n, DL; clrscr(); printf("Enter your name "); gets(na); k = strlen(na); DL = 2000; clrscr(); for(n=1; n<5; n++) { for(c=0; c<k; c++) { for(r=1; r<=15; r++) { gotoxy(c+20,r);printf(" "); gotoxy(c+20,r+1);printf("%c", na[c]); delay(DL); } } for(c=0; c<k; c++) { for(r=16; r>1; r--) { gotoxy(c+20, r); printf(" "); gotoxy(c+20, r-1); printf("%c", na[c]); delay(DL); } } } } 75)/* Program to display the given name as a box # include <stdio.h> # include <conio.h> main() { char str[20], a[25][20]; int k, j, n ; clrscr( ); printf("Enter Your name ") ; gets(str) ; for(n=0; str[n]!='\0'; n++) ; for(k=0; k<n; k++) for(j=0; j<n; j++) if(k==0) a[k][j] = str[j] ; else if(j==0) a[k][j] = str[k]; else if(j==n-1) a[k][j] = str[n-k-1] ; else if(k==n-1) a[k][j] = str[n-j-1] ; else a[k][j] = 32 ; for(k=0; k<n; k++) { printf("\n"); for(j=0; j<n; j++) printf("%c ", a[k][j] ) ; } */

getch() ; } /* Output : Enter Your name Bhanodaya B h a n o d a y a h a n * o d a y a y a d o n a h B

y a d n a h */

Notes : STRING.H functions : To manipulate the strings the C authors designed some functions in the header fi le STRING.H 1) strlen(): This function returns an integer value that is the length of the string. length means the number of characters. Syntax: int strlen(string) ; 2) strupr() : This function changes the given string into uppercase characters. Syntax : strupr(string) ; 3) strlwr() : This function changes the given string into lowercase characters. Syntax : strlwr(string) ; 4) strcpy() : This function copies the string to another string. Syntax : strcpy(target, source) ; 5) strcat() : This function adds two strings. Syntax : strcat(destination, second) ; 6) strrev() : This function change the given string into reverse order. Syntax : strrev(string) ; 7) strcmp() : This function compares two strings and returns zero when both are same. Syntax : int strcmp(first, second) ; Ex Programs : Function are 1. Pree Define Functions 2. User Define Functions Predefined Functions ------------------1. printf();

2. scanf(); 3. clrscr(); 4. getch(); 5. puts(); 6. gets(); 7. strlen(); 8. strupr(); 9. strrev(); 10.strcpy(); 11.strcat(); 12.strcmp();

strupr(); -------Syntax :strupr(var); strupr("name"); Ex :strupr(sname) welcome WELCOME strupr("softech"); SOFTECH Strlwr(); -------Syntax :strlwr(var); strlwr("name"); Ex :strlwr(sname) WELCOME welcome strupr("SOFTECH"); softech

main() { char s1[10]; puts("\n Enter any Lower string"); gets(s1); strupr(s1); printf("\n Upper String %s",s1); strlwr(s1); printf("\n Lower String %s",s1); } Strrev(); ------Syntax :strrev(var); strrev("name"); Ex :-

strrev(sname); strrev("welcome"); main() { char s1[10],s2[10]; puts("\n Enter any string"); gets(s1); strrev(s1); printf("\n Revers of string %s",s1); printf("\n %s",strrev(s1)); } strcpy ------Syntax :strcpy(t,s); strcpy(var,"name"); Ex:strcpy(s1,s2); strcpy(res,"pass"); main() { char s1[10],s2[10]; puts("\n Enter any Strings"); gets(s1); strcpy(s2,s1); printf("\n Copy to Strings %s",s2); } strcat(); -------Syntax :strcat(var1,var2); Ex :strcat(s1,s2); s1=wel s2=come output :welcome main() { char s1[10],s2[10]; puts("\n Enter two strings"); gets(s1); gets(s2); strcat(s1,s2); printf("\n Adding two strings %s",s1); }

Strcmp(); --------

Syntax :strcmp(var1,var2); Ex :strcmp(s1,s2); */

main() { char s1[10],s2[10]; int n; puts("\n Enter any two strings"); gets(s1); gets(s2); n=strcmp(s1,s2); if(n==0) puts("\n Both strings are equal"); else if(n>=1) puts("\n s1 sting is big"); else puts("\n s2 sting is big"); }

77) /* Program to demonstrate the library functions of STRING.H # include <stdio.h> # include <conio.h> # include <string.h> main() { char a[80], b[80] ; clrscr() ; printf("Enter a string ") ; gets(a) ; printf("\n The given string is %s", a); printf("\n The length of string is %d", strlen(a) ) ; printf("\n In Upper case %s", strupr(a)) ; printf("\n In Lower case %s", strlwr(a) ); strcpy(b, a) ; printf("\n The new string is %s", b) ; strrev(b); printf("\n In reverse order %s", b) ;

*/

printf("\n The difference is %d", strcmp(a,b) ); getch(); } /* Output : Enter a string Bhanodaya The given string is Bhanodaya The length of string is 9 In Upper case BHANODAYA In Lower case bhanodaya The new string is bhanodaya In reverse order ayadonahb The difference is 1 */

FUNCTIONS : The function is a piece of code. These functions are used to reduce the repetiti on of coding. The functions are two types. 1) Derived functions, 2) User-defined functions. The derived functions are provided by the C writers and they defined them in hea der files. To use these functions the header file must be included at the top of the program. We can create our own functions. To write any function there are three steps. 1) Functions declarations, 2) Functions calling, 3) Function definition. or 1) Function definition, 2) Function calling. In the function declaration and/or in the definition the name must be follow the return data type, and it should be followed by parenthesis. In the parenthesis there may be arguments with their data types. If the function does not return a ny value it must be declared as void. The default return type is int. Syntax: (datatype) (functionname) (argumentstype) ; /* Function declaration */ functionname(arguments) ; /* Function calling */ (datatype) (functionname) (arguments) { (statements) ; } /* Function definition */

These functions are three types. 1) No arguments with no return value functions, 2) Arguments with no return value functions, 3) Arguments with return value functions. 1) No arguments with no return value functions :

78) # include <stdio.h> # include <conio.h> void first(void) ;

void second(void) ; void third(void) { printf("\n This is in third function ") ; } void fourth(void) { printf("\n This is in fourth function ") ; } void main() { clrscr() ; printf("This is in Main ") ; first() ; second() ; third() ; fourth() ; printf("\n This is in main again ") ; getch() ; } void first(void) { printf("\n This is in first function ") ; } void second(void) { printf("\n This is in second function ") ; } /* Output This is in This is in This is in This is in This is in This is in : Main first function second function third function fourth function mainagain

*/

79) # include <stdio.h> # include <conio.h> void replicate(void) { int k; for(k=1; k<=50; k++) printf("*") ; } void main() { clrscr() ; replicate() ; printf("\n Hello \n") ; replicate() ; printf("\n World \n") ; replicate() ; printf("\n Welcome \n") ;

replicate() ; getch(); } /* Output : Hello World Welcome */

/* 2) Arguments with No Return value functions 80) # include <stdio.h> # include <conio.h> void replicate(int n, int ch) { int k; for(k=1; k<=n; k++) printf("%c", ch) ; } void main() { clrscr() ; replicate(30,'*') ; printf("\n Hello \n") ; replicate(60,'#') ; printf("\n Bhanodaya \n") ; replicate(50,'%') ; printf("\n Welcome \n") ; replicate(40,'@') ; getch(); } /* Output :

*/

Hello Bhanodaya %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Welcome @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */ 81) # include <stdio.h> # include <conio.h> void add(int, int) ; void subtr(int, int) ; void mult(int a, int b) { printf("\n The multiplication %d", a*b) ; } void div(int k, int j) {

printf("\n The division %f", (float)k / j ) ; } void main() { int a, b ; clrscr() ; printf("Enter any two numbers \n") ; scanf("%d%d", &a, &b ) ; add(a, b) ; subtr(a, b) ; mult(a, b) ; div(a, b) ; getch() ; } void add(int m, int n) { int k; k = m - n ; printf("\n The addition is %d", k) ; } void subtr(p, q) int p, q ; { int r ; r = p - q ; printf("\n The subtraction %d" , r) ; }

/* 3. Arguments with Return value functions 82) # include <stdio.h> # include <conio.h> int add(int x, int y) { int z ; z = x + y; return z ; } int subtr(int p, int q) { return p-q ; } int mult(int, int ) ; int div(int, int) ;

*/

int main() { int a, b, c ; clrscr() ; printf("Enter any two numbers \n" ); scanf("%d%d", &a, &b) ; c = add(a, b) ; printf("\n The addition is %d", c ); printf("\n The subtraction

%d", subtr(a,b) ) ; c = mult(a,b) ; printf("\n The multiplication %d", mult(a,b) ); printf("\n The d ivision %d", div(a,b) ); getch() ; return 0; } int mult(int a, int b) { return a*b; } int div(int m, int n) { int p = m / n; return p; }

83) Important : /* Program to demonstrate all types of functions # include <stdio.h> # include <conio.h> /* No arguments with No return value functions */ void looping(void) ; void condition(void) ; /* Arguments with No return value functions */ void add(int, int) ; void mult(int, int) ; /* Arguments with Return value functions */ int subtr(int, int) ; int div(int, int) ; int main() { looping() ; return 0; } void looping() { char ch = 'y' ; while(ch=='y' ch=='Y') { clrscr() ; condition() ; gotoxy(45, 22) ; printf(" Do you want to cotinue (y/n) ") ; ch = getche (); } } void condition() {

*/

int a, b, k; printf("\n Enter any two numbers \n") ; scanf("%d%d", &a, &b) ; gotoxy(30, 5) ; printf("1. Addition " ); gotoxy(30, 6) ; printf("2. Subtraction ") ; gotoxy(30, 7) ; printf("3. Multiplication ") ; gotoxy(30, 8) ; printf("4. Division ") ; gotoxy(30, 10 ); printf("Enter Your choice ") ; scanf("%d", &k) ; gotoxy(10, 15); switch(k) { case 1 : add(a, b) ; break ; case 2 : printf(" The subtraction %d", subtr(a,b) ); break ; case 3 : mult(a,b ); break ; case 4 : printf(" The division %d", div(a,b) ) ; break; default : printf(" Invalid choice ") ; } } void add(int a, int b) { printf("\n The addition %d", a+b) ; } int subtr(int x, int y) { int z = x - y; return z ; } void mult(int p, int q) { int r ; r = p * q ; printf("The multiplication %d", r) ; } int div(int a, int b) { int c ; c = a / b ; return c; } 0000000000 program to generate a series of factorials upto 7 */ fact(int n){ int i,f=1; for(i=2;i<=n;i++) f*=i; printf("The factorial of %d is %d\n",n,f); } main(){ int i;

for(i=1;i<8;i++) fact(i); }

/* Greatest Common Division main() { int a,b,gcd,min,i; printf("\n Enter two no"); scanf("%d%d",&a,&b); if(a>b) min=b; else min=a; gcd=1; for(i=1;i<=min;i++) if(a%i==0&&b%i==0) if(gcd<i) gcd=i; printf("%d",gcd); } another logic */ int gcd(int a,int b) { if(a<b) return gcd(a,b); if(a%b==0) return b; else return gcd(b,a%b); } main() { int a,b,s; printf("\n Enter two no"); scanf("%d%d",&a,&b); s= gcd(a,b); printf("\n G.c.d no %d",s); }

Sum of Natural Numbsers up given no int sum(int x) { if(x>0) return x+sum(x-1); else return 0; } main() {

int n,s; clrscr(); scanf("%d",&n); s=sum(n); printf("\n Sum of naturno upto %d is %d",n,s); } _ move's the diskes move(int a,int b,int c,int n) { if(n>0) { move(a,c,b,n-1); printf("%d->%d\n",a,c); move(b,a,c,n-1); } } main() { int n; clrscr(); scanf("\n%d",&n); move(1,2,3,n); getch(); } 000000000000

Write a program store a value print address values using pointers main() { int a=10; int *ptr; clrscr(); ptr=&a; printf("\n A-value %d",a); printf("\n A-address %u",&a); printf("\n Pointer value %d",*ptr); printf("\n Address of ptr %u",&ptr); printf("\n Address of ptr and value %u",*(&ptr)); printf("\n Address of ptr and vluae and value of ptr and var %d",*(*(&ptr))) ; } display of two float values using pointers main() { float a=10.2; float b=20.4; float *ptr1,*ptr2; ptr1=&a; ptr2=&b; printf("\n a value %.2f",*ptr1); printf("\n b value %.2f",*ptr2); }

addition of two values using pointers main() { int a=10,b=20,c; int *ptr1,*ptr2; ptr1=&a; ptr2=&b; c=*ptr1+*ptr2; printf("\n Add of two values %d",c); } Add of two values using pointers main() { int a=10,b=20; int *ptr1,*ptr2; clrscr(); ptr1=&a; ptr2=&b; *ptr1=*ptr1+10; *ptr2=*ptr2+10; printf("\nA-value %d",a); printf("\n B-value %d",b); } */ main() { int a=10,b=20; int temp; int *ptr1,*ptr2; printf("\n Before swaping"); printf("\n A-value %d\t",a); printf(" B-value %d",b); ptr1=&a; ptr2=&b; temp=*ptr1; *ptr1=*ptr2; *ptr2=temp; printf("\n After swaping values"); printf("\n Avalue %d\t",a); printf("Bvalue %d",b); }_ /*Write a program to print change values main() { int a[3],*ptr; clrscr(); a[0]=3;a[1]=5;a[2]=6; ptr=&a[0]; printf("\n %d %d %d",a[0],a[1],a[2]); printf("\n %u %u %u",&a[0],&a[1],&a[2]); printf("\n pointer values %d %d %d",*ptr,*(ptr+1),*(ptr+2)); }

Write a program to print natural no up to accepted value*/ main() { int i,n,a[10],n1=1,*ptr; ptr=&a[0]; clrscr(); printf("\n Enter any value below 10"); scanf("%d",&n); for(i=0;i<n;i++) { a[i]= n1; n1++; } for(i=0;i<n;i++) { printf("\n"); printf("\t %d",a[i]); printf("\t %u",&a[i]); /*printf("\t %d",*(ptr+i)); */ printf("\t %d",*ptr); ptr++; } } /* main() { int a[5],i; printf("\n Enter 5 values"); for(i=0;i<5;i++) scanf("\n %d",&a[i]); for(i=0;i<5;i++) printf("\t %d",a[i]); }*/ main() { int a[5],*ptr1,*ptr2,i; clrscr(); ptr1=a; ptr2=ptr1; printf("\n Enter five values"); for(i=0;i<5;i++) { scanf("%d",&ptr1[i]); ptr1++; } ptr1=ptr2; for(i=0;i<5;i++) { printf("\t%d",ptr1[i]); ptr1++; } } main() {

int a,b; clrscr(); printf("Enter two numbers \n"); scanf("%d%d",&a,&b); swap(&a,&b); printf("After swaping A = %d , B = %d\n"); getch(); } swap(int *pa,int *pb) { int temp; temp = *pa ; *pa = *pb; *pb = temp; }

sizeof(); sizeof(data_type) sizeof(int); 2

main() { clrscr(); printf("\n printf("\n printf("\n printf("\n printf("\n printf("\n printf("\n } malloc(); a[100]; 50

character_size= %d",sizeof(char)); int_size = %d",sizeof(int)); long_int_size %u",sizeof(long int)); float_size = %d",sizeof(float)); long_float_size = %d",sizeof(long float)); double_size %d",sizeof(double)); long_double_size %u",sizeof(long double));

malloc(sizeof(int *) int *ptr; ptr=malloc(n*sizeof(int *)) */ main() { int i,n; int *ptr ; clrscr(); printf("\n Ente how many cells"); scanf("%d",&n); ptr=malloc(n*sizeof(int *)); for(i=0;i<n;i++)

scanf("%d",&ptr[i]); for(i=0;i<n;i++) printf("%d",ptr[i]); }

STRUCTURES WITH POINTERS */ struct emp { int eno,bas; }; main() { struct emp e={1,2500}; struct emp *ptr; ptr=&e; printf("\n pointer_eno %d",ptr->eno); printf("\n pointer_bas %d",ptr->bas); printf("\n employee no %d",e.eno); printf("\n Employee basic %d",e.bas); }

Notes : PREPROCESSOR COMMANDS : The commands which start with hash (#) are called Preprocessor Commands. Ex : # define PI 3.14159 # include <stdio.h> # include "conio.h" # define A 1.7

define : This command is used to define our own constants and macros. Ex Programs : 84) # include <stdio.h> # include <conio.h> # # # # # # define define define define define define MN main() pf printf cls clrscr() wait getch() sf scanf PI 3.14159

# define msg "Enter the radius of circle " # define area(g) PI*g*g # define per(g) 2*PI*g MN { float r ; cls ; pf(msg) ; sf("%f", &r) ; pf("\n The area of circle is %f", area(r)) ; pf("\n The perameter of circle %f", per(r) ) ; wait ; } Notes : include : This command is used to include the files which contains the definition of functions. There are two types to include the files. 1) # include < name > This type of command includes the file which is located in the specified directory. These specifications are set by selecting the Directories command from Options menu. 2) # include " name " This type of command includes the file which is located in the current directory and/or in the specified directory. Ex Programs : 85) /* Save this progam as SUB.C # include <stdio.h> # include <conio.h> int add(int x, int y) { int z ; z = x + y; return z ; } int subtr(int p, int q) { return p-q ; } /* Save and execute this program as MAIN.C # include "sub.c" int mult(int, int ) ; int div(int, int) ; */

*/

int main() { int a, b, c ; clrscr() ; printf("Enter any two numbers \n" ); scanf("%d%d", &a, &b) ; c = add(a, b) ; printf("\n The addition is %d", c ); printf("\n The subtraction

%d", subtr(a,b) ) ; c = mult(a,b) ; printf("\n The multiplication %d", mult(a,b) ); printf("\n The d ivision %d", div(a,b) ); getch() ; return 0; } int mult(int a, int b) { return a*b; } int div(int m, int n) { int p = m / n; return p; }

Notes : STORAGE CLAUSES : The declared variables are generally stored in memory devices. But by using the storage clauses they can be stored either in memory devices or in CPU registers. These storage clauses are 4 types. The variables initialisation depends on this type. 1) auto, 2) register, 3) static, 4) extern 1) auto : This keyword is an option. It stores the variable in memory device. It assigns a junk(garbage) value to the variable. The variable which declared in a block that is available in that block only. The default type is auto. Ex Programs : 86) # include <stdio.h> # include <conio.h> main() { auto int k; { int k = 5 ; { auto int k = 20 ; clrscr() ; printf("\n %d", k ); } } printf("\n %d", k ); getch() ; } 87) # include <stdio.h> # include <conio.h> main()

output :

20 5 456

{ int k ; clrscr() ; printf("%d \n", k) ; for(k=1; k<=5; k++) display() ; getch() ; } display() { auto int k = 20 ; printf(" %d " , k); k += 3 ; } /* Output : 4567 20 20 20 20

20

*/

Notes : 2) register : This keyword stores the variable in CPU registers. It assigns a junk(garbage) va lue to the variable. In CPU registers it can't store more and big values like f loats, doubles,..etc. It can store Only chars and integers. These variables are also local that means the variables which declared in a block are available in t hat block only. Generally these variables are used to generate the looping statements. Ex Programs 88) # include <stdio.h> # include <conio.h> main() { register int k ; clrscr() ; printf("%d \n", k ); for(k=1; k<=100; k++) printf("%d ", k) ; getch() ; }

Notes : 3) static : This keyword stores the variable in memory device. It initialises the variable as 0. The scope of variable is local that means the variables which declared in a block are available in that block only. But it does not destroy the variable's value when end that block. It takes the previous value when the controller entered into that block again. Ex Programs : 89)

# include <stdio.h> # include <conio.h> main() { static int k ; clrscr() ; printf("%d \n", k) ; for(k=1; k<=5; k++) display() ; getch() ; } display() { static int k = 20 ; printf(" %d " , k); k += 3 ; } /* Output : 0 20 23 26 29 32 Notes : 4) extern : This keyword stores the variable in memory device. It initialises the variable as 0. The scope of variable is global. This variable is declared before the main() . The variable's value can be changed in the functions. Ex Programs : 90) # include <stdio.h> # include <conio.h> int k ; main() { clrscr() ; printf("\n %d", k) ; k = 5 ; disp1() ; disp2() ; printf("\n %d", k) ; getch() ; } disp1() { printf("\n %d", k ); k += 3 ; } disp2() { int k = 20 ; printf("\n %d", k ); disp3() ;

*/

} disp3() { printf("\n %d", k ); } /* Output 0 5 20 8 8 */

Notes : STRUCTURES : A structure is a collection of variety of data type elements. This is created wi th the keyword struct. struct (name) { (elements declaration); } ; struct (name) (variable) ; (variable).(element) Ex : 1) struct employee { int eno; char ename[80]; float bas; }; struct employee emp = { 5, "rama", 5600 } ; 2) struct employee { int eno; char ename[80]; float bas; } emp = { 5, "rama", 5600 } ; 3) struct { int eno; char ename[80]; float bas; } emp = { 5, "rama", 5600 } ; Ex Programs : 91) # include <stdio.h> # include <conio.h> main() { struct book { int pages; char title[40] ; float price ; } ;

struct book bk = { 500, "Let us C", 175 } ; clrscr() ; printf("\n The title of book %s" , bk.title ); printf("\n The number of pages %d " , bk.pages ) ; printf("\n The price of book %.2f" , bk.price ) ; getch( ); } 92) # include <stdio.h> # include <conio.h> main() { struct book { int pages; char title[40] ; float price ; } ; struct book bk ; clrscr() ; printf("Enter the number of pages of book ") ; scanf("%d" , &bk.pages ) ; printf("Enter the book title ") ; scanf("%s", bk.title ) ; printf("Enter the cost of book ") ; scanf("%f", &bk.price); printf("\n The title of book %s" , bk.title ); printf("\n The number of pages %d " , bk.pages ) ; printf("\n The price of book %.2f" , bk.price ) ; getch( ); }

93) /* Array of Structures */ # include <stdio.h> # include <conio.h> main() { struct book { int pages; char title[40] ; float price ; } ; int k ; struct book bk[3] = { { 500, "Let us C", 175 }, { 350, "Graphics under C", 235 } , { 800, "Datastructures through C and C++ ", 350 } } ; clrscr() ; for(k=0; k<3; k++) { printf("\n\n The title of book %s" , bk[k].title ); printf("\n The number of pages %d" , bk[k].pages ) ;

printf("\n The price of book %.2f" , bk[k].price ) ; getch( ); } } 94) /* Structures to Functions # include <stdio.h> # include <conio.h> struct book { int pages; char title[40] ; float price ; } ; main() { struct book bk = { 500, "Let us C", 175 } ; clrscr() ; display(bk.pages, bk.title, bk.price ); dispstrct(bk) ; getch( ); } display(int pg, char na[], float pr) { printf("\n The title of book %s", na); printf("\n The number of pages %d", pg); printf("\n The price of book %f", pr ); } dispstrct(struct book b) { printf("\n The title of book %s", b.title); printf("\n The number of pages %d", b.pages); printf("\n The price of book %f", b.price ); } 95) /* Structures in structure # include <stdio.h> # include <conio.h> */ */

struct book { int pages; char name[40]; float price ; } ; struct pens { int qty; char name[40]; float price ; } ; struct shop { char name[40], street[40] ; struct book bk ; struct pens pn ; } ; main() { struct shop sh = { "Udaya Book world ", "Chintal ", { 1200, "Test your skills in C", 175 } , { 50, "Reynolds", 12 }

} ; clrscr() ; printf("\n The shop name %s" , sh.name ) ; printf("\n The shop address %s ", sh. street); printf("\n\n The book title %s" , sh.bk.name ) ; printf("\n The number of pages in the book %d",sh.bk.pages ) ; printf("\n The cost of each book %.2f" , sh.bk.p rice ) ; printf("\n\n The name of pen %s" , sh.pn.name ) ; printf("\n The quantity of pen s %d" , sh.pn.qty ) ; printf("\n The cost of each pen %.2f" , sh.pn.price ) ; getch() ; } 96) /* Write a program to interchange the values of two variables */

Notes : POINTERS This topic is the most important in C. To use any variable it must be declared with its data type before the first exec utable statement. By declaring a variable the compiler reserves the required spa ce in memory between 64 kb and 128 kb. Every cell has a unique address in memo ry. This address is a number,that is an integer. The variables stored in the memory can be accessed with their addresses using po inters.To access with pointers we have to use two new operators. & ==> Address of * ==> Value at address of Ex : int a = 5; a ==> 5 &a ==> 65500 *(&a) ==> 5 These address value can be stored in another variable. But that must be a pointe r variable of the same type to the variable. Ex Programs : 97) # include <stdio.h> # include <conio.h> main() { int k, *p, **q ; clrscr() ; k = 7; p = &k; q = &p; printf("The value of k is %d", k) ; printf("\n The address of k is %u", &k); pri ntf("\n The value at address %u is %d", &k, *(&k) ) ; printf("\n The value of P is %u", p) ; printf("\n The value at address %u is %u" , &p, *(&p) ) ; printf("\n The value at address %u is %d", p, *p ); printf("\n The value of q is %u", q) ; printf("\n The value at address of %u is %u", &q, *(&q) ) ; printf("\n The value at address %u is %u", q, *q); printf("\n The integer value is %d", *(*q) ) ; getch() ; } /* Output :

The The The The The The The The The The

value of k is 7 address of k is 65524 value at address 65524 is value of P is 65524 value at address 65522 is value at address 65524 is value of q is 65522 value at address of 65520 value at address 65522 is integer value is 7

7 65524 7 is 65522 65524 */

98) # include <stdio.h> # include <conio.h> main() { int a, *ap ; float b, *bp ; char c, *cp ; clrscr() ; printf("\n The size of int is %d and pointer is %d", sizeof(a), sizeof(ap) ) ; p rintf("\n The size of float is %d and Pointer is %d", sizeof(b),sizeof(bp) ) ; p rintf("\n The size of char is %d and Pointer is %d", sizeof(c), sizeof(cp) ) ; getch() ; } Notes : Pointers in Functions : The functions can be send some arguments to the definition and may take a return value. If the value of these variables changed in the functions that does not effected to the function calling. To change the value after calling the function the values can be send by referen ce. That means the address of the variable can be send to change the value. Ex Programs : 98) /* Write a program to interchange the values of two variables using functions A) CALL BY VALUE : */ # include <stdio.h> # include <conio.h> main() { int a=20, b=30 ; clrscr() ; printf("The initial values are printf("a = %d \t b = %d", a, swap(a,b) ; printf("\n\n After swapping in printf(" a = %d \t b = %d", a, getch() ; } swap(int x, int y) { int t ; \n") ; b) ; main \n") ; b) ;

t = x; x = y ; y = t ; printf("\n\n After swapping \n"); printf(" x = %d, y = %d", x, y); } /* Output : a = 20 x = 30 a = 20 B) CALL BY REFERENCE : # include <stdio.h> # include <conio.h> main() { int a=20, b=30 ; clrscr() ; printf("The initial values are printf("a = %d \t b = %d", a, swap(&a,&b) ; printf("\n\n After swapping in printf(" a = %d \t b = %d", a, getch() ; } \n") ; b) ; main \n") ; b) ; b = 30 y = 20 b = 30

*/

swap(int *x, int *y) { int t ; t = *x; *x = *y ; *y = t ; printf("\n\n After swapping \n"); printf(" x = %d, y = %d", *x, *y); } /* Output : a = 20 b = 30 x = 30 b = 30 a = 30 b = 20 */ Notes : Arrays with Pointers : An array is a collection of similar data type elements mentioning with a single variable. The address of first element is considered the address of the array. The array elements can be accessed using the address of array and the index val ue. Ex Programs : q) # include <stdio.h> # include <conio.h> main() { int k, a[5] = { 10, 20, 30, 40, 50 } ; clrscr() ; for(k=0; k<5; k++) { printf("\n the addres of %d element %u %u", k, &a[k], a+k ) ; printf("the value is %d %d %d %d", a[k], *(a+k), *(k+a), k[a] ) ;

} getch () ; } q) # include <stdio.h> # include <conio.h> main() { int *a, n ; clrscr() ; printf("Enter the number of elements you want ") ; scanf("%d", &n) ; accept(a, n) ; display(a, n) ; getch() ; } accept(int *p, int n) { int k; printf("Enter %d numbers \n ", n) ; for(k=0; k<n; k++) scanf("%d", p+k) ; } display(int *p, int n) { int k; printf("\n The elemetns are \n") ; for(k=0; k<n; k++) printf("%d ", *(p+k) ) ; } q) # include <stdio.h> # include <conio.h> # define M 5 main() { int a[M] ; clrscr() ; accept(a, M) ; display(a, M) ; getch() ; } accept(int *p, int k) { int n ; printf("Enter any %d numbers \n", k) ; for(n=0; n<k; n++) scanf("%d", p+n) ; }

display(int d[], int k) { int n ; printf("\n The array elements are \n") ; for(n=0; n<k; n++) printf("%d ", d[n] ) ; } Notes : malloc() : This function is used to allocate the required space in the memory while execut ing the program. This function's prototype was defined in the header file ALLOC .H There is an another function to allocate memory. that is calloc(). The malloc() assigns junk values the allocated space. But calloc() assigns 0 to the allocated space. Syntax: (pointer) = (pointertype) malloc( size * number) ; (pointer) = (pointertype) calloc( size , number ) ; Note : If the compiler failed to allocate the required space this function returns a NU LL value. Ex Programs : q) # include <stdio.h> # include <conio.h> # include <alloc.h> main() { int *p, n, k, s; clrscr() ; printf("Enter the number of elements ") ; scanf("%d", &n) ; p = (int *) malloc(sizeof(int) * n) ; if(p==NULL) { printf("Unable to allocate the required space ") ; exit(0) ; } printf("Enter %d numbers \n", n) ; for(k=0, s=0; k<n;s+=p[k], k++) scanf("%d", p+k ); printf("\n The elements are \n") ; for(k=0; k<n; k++) printf("%d ", p[k] ); printf("\n The sum is %d", s) ; getch() ; } q) /* Write a program to sort the given numbers using Linear Sort technique */ # include <stdio.h> # include <conio.h> # include <stdlib.h>

# include <alloc.h> main() { int *p, n; clrscr() ; printf("Enter the number of elements ") ; scanf("%d", &n) ; p = (int *) malloc(2*n) ; if(p==NULL) { printf("\n Unable to allocate the required space ") ; exit(0); } accept(p, n) ; linear(p, n) ; /* bubble(p, n) ; display(p, n) ; getch() ; } accept(int *p, int n) { int k ; printf("Enter %d numbers \n", n) ; for(k=0; k<n; k++) scanf("%d", p+k) ; } display(int *p, int n) { int k ; printf("\n The elements are " ) ; for(k=0; k<n; k++) printf("%d ", *(p+k) ) ; } /* linear(int a[], int n) { int k, j, t ; for(k=0; k<n; k++) for(j=k; j<n; j++) if(a[k]<a[j]) { t = a[k] ; a[k] = a[j] ; a[j] = t ; } } */ linear(int *a, int n) { int k, j, t ; for(k=0; k<n; k++) for(j=k; j<n; j++) if(*(a+k) < *(a+j) ) { t = *(a+k) ; *(a+k) = *(a+j) ; */

*(a+j) = t ; } }

bubble(int a[], int n) { int t, k, j ; for(k=0; k<n-1; k++) for(j=0; j<n-1; j++) if(a[j] < a[j+1] ) { t = a[j] ; a[j] = a[j+1] ; a[j+1] = t ; } } /* bubble(int *a, int n) { int t, k, j ;

for(k=0; k<n-1; k++) for(j=0; j<n-1; j++) if(*(a+j) < *(a+j+1) ) { t = *(a+j) ; *(a+j) = *(a+j+1) ; *(a+j+1) = t ; } } */

Notes : String Pointers to Functions : Ex Programs : q) /* # # # # Moving the given string as a Banner include include include include <stdio.h> <conio.h> <string.h> <dos.h> */

void main() { char *str = " Bhanodaya is a super hero "; clrscr(); while(!kbhit()) { substr(str); delay(100); gotoxy(20, 12); printf("%s",str); } getch(); }

/* substr(char *str) { char *dst, c; int i=1, j=0; c = *str; while(*(str+i)!='\0') { *(dst+j) = *(str+i); i++; j++; } *(dst+j) = c; *(dst+j+1) = '\0'; strcpy(str,dst); } */ substr(char str[]) { char dst[80], ch ; int k, j ; ch = str[0] ; k=1; j=0; while(str[k]!='\0') { dst[j] = str[k] ; k++; j++; } dst[j++] = ch ; dst[j] = '\0' ; strcpy(str, dst) ; } Notes : Structures with Pointers : The elements of a structure variable can be accessed using a period operator bet ween the variable and the element. To access the elements using the address of t he variable the ' -> ' operator must be used. (variablepoitner) -> (element) Ex Programs : q) # include <conio.h> # include <stdio.h> struct book { int pages; char title[40]; float price ; } ; main() { struct book bk = { 500, "Pointers in C", 175.00 } ; struct book *b ; clrscr() ; b = &bk ; printf("\n The book title %s" , bk.title ) ; printf("\n The number of pages %d" , bk.pages ) ; printf("\n The cost of book %.2f " , bk.price ) ; printf("\n\n The book title %s" , b->title) ; printf("\n The number of pages %d" , b->pages ) ; printf("\n The cost of book %.2f ", b->price ) ; printf("\n\n The size of structure variable %d",sizeof(bk) ); printf("\n The si

ze of structure pointer %d", sizeof(b) ) ; getch() ; } q) /* Structure Pointers to Functions */ # include <conio.h> # include <stdio.h> struct book { int pages; char title[40]; float price ; } ; main() { struct book bk = { 500, "Pointers in C", 175.00 } ; clrscr() ; dispvar(bk) ; disppntr(&bk) ; getch() ; } dispvar(struct { printf("\n The printf("\n The printf("\n The printf("\n The } book bk) book title %s" , bk.title ) ; number of pages %d" , bk.pages ) ; cost of book %.2f " , bk.price ) ; size of structure variable %d" , sizeof(bk) );

disppntr(struct book *b) { printf("\n\n The book title %s" , b->title) ; printf("\n The number of pages %d", b->pages ) ; printf("\n The cost of book %.2f ", b->price ) ; printf("\n The size of structure pointer %d", sizeof(b) ) ; } Notes : FILE HANDLING IN C Using C programs the data files and text files can be manipulated. To use any fi le it must be loaded into memory. In C programs the file pointer where the file stored can be found. fopen(): This function is used to open the required file in the required mode and returns the file pointer where the file stored. If it is unable to open in the given mo de it returns a constant value NULL. Syntax : FILE * (filepointer) ; (filepointer) = fopen("filename", "mode" ); modes : - - - "w" ==> To create the file and store the data "a" ==> To add the data to the file "r" ==> To read the data from the file Ex : FILE *fp; fp = fopen("data.txt", "w"); fclose() :

The file, which opened in memory must be closed to avoid the data corruption. To close the file the function fclose() must be used. Syntax : fclose(filepointer); Ex : fclose(fp) ; fputc(): This function is used to store the characters in the data file opened in the fil epointer. Syntax : fputc(char, filepointer) ; Ex : fputc(ch, fp) ; fgetc() : This function is used to read a character to the variable from the file pointer. Syntax : char variable = fgetc(filepointer) ; Ex : ch = fgetc(fp) ; Ex Programs : q) /* Write a program to create a text file DATA and store some data. */ # include <stdio.h> # include <conio.h> # include <stdlib.h> main() { FILE *fp ; char ch ; fp = fopen("DATA", "w") ; if(fp==NULL) { printf("\n Unable to open the given file ") ; exit(0) ; } while(1) { ch = getchar(); if(ch==EOF) break ; fputc(ch, fp) ; } fclose(fp) ; } q) /* Write a program to read the text from the file DATA # include <stdio.h> # include <conio.h> # include <stdlib.h> main() { FILE *fp ; char ch ; clrscr() ; fp = fopen("DATA", "r") ; */

if(fp==NULL) { printf("\n File not found ") ; exit(0) ; } while(1) { ch = fgetc(fp); if(ch==EOF) break ; putchar(ch) ; } fclose(fp) ; getch() ; }

q)/* Write a program to read the text from the file DATA display in Upper case characters */ # include <stdio.h> # include <conio.h> # include <stdlib.h> main() { FILE *fp ; char ch ; clrscr() ; fp = fopen("data", "r") ; if(fp==NULL) { printf("\n File not found ") ; exit(0) ; } while(1) { ch = fgetc(fp); if(ch==EOF) break ; if(ch>=97 && ch<=122) ch -= 32 ; putchar(ch) ; } fclose(fp) ; getch() ; } q)/* Write a program to read the text from the file DATA copy to another in file in upper case characters */ # include <stdio.h> # include <conio.h> # include <stdlib.h> main() { FILE *fs, *ft ; char ch, *src, *trg ; clrscr() ;

printf("Enter the source file to copy ") ; scanf("%s", src) ; fs = fopen(src, "r") ; if(fs==NULL) { printf("\n Source File not found ") ; exit(0) ; } printf("\n Enter the target file to copy ") ; scanf("%s", trg) ; ft = fopen(trg, "w") ; if(ft==NULL) { printf("\n Unable to open the target file ") ; exit(1) ; } while(1) { ch = fgetc(fs); if(ch==EOF) break ; if(ch>=97 && ch<=122) ch -= 32 ; putchar(ch) ; fputc(ch, ft) ; } fclose(fs) ; fclose(ft) ; getch() ; } Notes : fprintf() : This function stores the data in the file. Syntax : fprintf(filepointer, "format string", variables); Ex : fprintf(fp, "%d %s %f", eno, ena, bas); fscanf() : This function reads the data from the file. Syntax : fscanf(filepointer, "format string", variables); Ex : fscanf(fp, "%d%s%f", &eno, ena, &bas); Ex Programs : q)/* Write a program to create a data file EMP.DAT, accept employee number, name, basic salary and store all in the data file */ # include <conio.h> # include <stdio.h> # include <stdlib.h> main() { FILE *fp ; int eno; char ena[40]; float bas ; char ans='y' ; clrscr() ;

fp = fopen("EMP.DAT", "w") ; if(fp==NULL) { printf("\n Unable to open in the required mode ") ; exit(0) ; } while(ans=='y' ans=='Y') { printf("\n Enter Employee Number ") ; scanf("%d", &eno) ; printf(" Enter Employee Name ") ; scanf("%s", ena) ; printf(" Enter Basic Salary ") ; scanf("%f", &bas) ; fprintf(fp, "\n %d %s %f", eno, ena, bas) ; printf("Do you want to continue ") ; ans = getche() ; } getch(); } q)/* Write a program to read the records from the data file EMP.DAT, calculate da, hra, pf, net salary and print all */ # include <conio.h> # include <stdio.h> # include <stdlib.h> main() { FILE *fp ; int eno; char ena[40]; float bas, da, hra, pf, net ; clrscr() ; fp = fopen("EMP.DAT", "r") ; if(fp==NULL) { printf("\n File not found ") ; exit(0) ; } while( fscanf(fp, "%d%s%f", &eno, ena, &bas) > 0) { da = bas * 20 / 100 ; hra = bas * 30 / 100 ; pf = bas * 10 / 100 ; net = bas + da + hra - pf ; printf("\n\n Employee Number %d", eno) ; printf("\n Employee Name %s", ena) ; printf("\n Basic Salary %.2f", bas) ; printf("\n Da %.2f \t Hra %.2f \t Pf %.2f", da, hra, pf ) ; printf("\n Net Salary %.2f", net) ; getch() ; } }

Notes : fwrite() : This function is used to store the structures in the file in binary mode. Syntax: fwrite ( (pointer to variable), (size of variable), (numberofvariables),(filepointer) ); Ex: fwrite( &emp, sizeof(emp), 1, fp); fread() : This function is used to read the structures from the file in binary level. Syntax: fread ( (pointer to variable), (size of variable), (numberofvariables),(filepointer) ); Ex: Ex Programs : fread(&emp, sizeof(emp), 1, fp);

q) # include <stdio.h> # include <conio.h> struct employee { int eno; char ena[20]; float bas ; } ; main() { struct employee emp ; FILE *fp ; char ans ; clrscr() ; fp = fopen("empbn.dat", "wb") ; if(fp==NULL) { printf("Unable to open the data file ") ; exit(0) ; } do { printf("\n\n Enter employee Number ") ; scanf("%d", &emp.eno) ; printf("Enter Employee Name ") ; scanf("%s", emp.ena) ; printf("Enter Basic Salary ") ; scanf("%f", &emp.bas) ; fwrite(&emp, 1, sizeof(emp), fp) ; printf("Do you want to add more ") ; ans = getche() ; } while(ans=='y' ans=='Y') ; } q) # include <stdio.h> # include <conio.h> struct employee { int eno; char ena[20]; float bas ; } ; main()

{ struct employee emp ; FILE *fp ; clrscr() ; fp = fopen("empbn.dat", "rb") ; if(fp==NULL) { printf("Unable to open the data file ") ; exit(0) ; } while( fread(&emp, 1, sizeof(emp), fp) > 0) { printf("\n\n Employee Number %d", emp.eno) ; printf("\n Employee Name %s", emp.ena) ; printf("\n Basic Salary %.2f", emp.bas) ; getch() ; } } q) # include <stdio.h> # include <conio.h> struct employee { int eno; char ena[20]; float bas ; } ; main() { struct employee emp ; FILE *fs, *ft ; clrscr() ; fs = fopen("empbn.dat", "rb") ; if(fs==NULL) { printf("Unable to open the data file ") ; exit(0) ; } ft = fopen("emp.dat", "w") ; if(ft==NULL) { printf("\n Unable to open the target file ") ; exit(1); } while( fread(&emp, 1, sizeof(emp), fs) > 0) { printf("\n\n Employee Number %d", emp.eno) ; printf("\n Employee Name %s", emp.ena) ; printf("\n Basic Salary %.2f", emp.bas) ; fprintf(ft, "\n %d %s %f", emp.eno, emp.ena, emp.bas); getch() ; } } Notes : COMMAND LINE ARGUMENTS : The arguments can be passed to any function when they are calling.The main() is

also a function and some arguments can be passed to it from the MS-DOS prompt. * Here we have to use the words argv, args[] . * Here argv is the an integer value and it is the number of arguments passed and *args[] is an array of strings contain the arguments Syntax: main(int argv, char *args[]) Ex Programs : q) /* This program demonstrates about the arguments passed to the function main() */ # include <stdio.h> void main(int argv, char *args[]) { int k ; printf("\n The number of arguments given %d", argv) ; printf("\n The arguments are \n") ; for(k=0; k<argv; k++) printf("\n %s", args[k] ) ; }

/* Save this program as ARG.C After compilation it creates an object file ARG.OBJ and an executable file ARG.EXE, which can be executed at MS-DOS prompt [prompt] ARG abc lkj xyz mnb */ q)/* Program to create a file which can create text file # include <stdio.h> # include <stdlib.h> void main(int argv, char *args[]) { char ch; FILE *fp ; if(argv!=2) { printf("\n Invalid number of arguments ") ; exit(0) ; } fp = fopen(args[1], "w") ; if(fp==NULL) { printf("\n Unable to create the given file ") ; exit(1) ; } while(1) { ch = getchar() ; if(ch==EOF) break ; fputc(ch, fp) ; } printf("\n 1. file created \n") ; */

} /* Save this program as CREATE.C After compilation it creates an executable file CREATE.EXE, which can be executed at MS-DOS prompt. [prompt] CREATE (filename) */ q) /* Program to create a file which can read a text file */

# include <stdio.h> # include <stdlib.h> void main(int argv, char *args[]) { char ch; FILE *fp ; if(argv!=2) { printf("\n Invalid number of arguments ") ; exit(0) ; } fp = fopen(args[1], "r") ; if(fp==NULL) { printf("\n File not found - %s", args[1] ) ; exit(1) ; } while(1) { ch = fgetc(fp) ; if(ch==EOF) break ; putchar(ch) ; } }

/* Save this program as SHOW.C After compilation it creates an executable SHOW.EXE [prompt] SHOW (filename) */ q)/* Program to create a file which can copy the text from a file to another */ # include <stdio.h> # include <stdlib.h> void main(int argv, char *args[]) {

FILE *fs, *ft ; char ch ; if(argv!=3) { printf("Invalid number of arguments "); exit(0) ; } fs = fopen(args[1], "r") ; if(fs==NULL) { printf("\n Unable to open the source file ") ; exit(1) ; } ft = fopen(args[2],"w") ; if(ft==NULL) { printf("\n This target file %s not found ", args[2] ) ; exit(2) ; } while( (ch=fgetc(fs)) != EOF) fputc(ch, ft) ; fclose(fs) ; fclose(ft) ; printf("\n 1. File copied ") ; }

/*

Save this file as FLCP.C [prompt] FLCP (source) (target) */

Notes : random() : This function returns the value which is between 0 and 1 less than the given num ber. This function's prototype has defined in the header file STDLIB.H Syntax : random(n); This function returns any number from 0 to n-1 . textattr() & textbackground() : This function changes the text color to the given color.

GRAPHICS Generally the screen is in textmode. It can be changed by the display adopters. There are a number of adopters like VGA, CGA,EGA, ... The normal text mode has 25 rows and 80 columns. It can be changed using the MO DE command in MS-DOS.Using C programs we can design some graphics by changing th e screen from text mode to graphics mode. initgraph() : This function changes the screen from text mode to graphics mode. But here we ha ve to mention the display driver and mode. To use this command the file EGAVGA. BGI must be in the current directory. This function's prototype has defined in the header file GRAPHICS.H

Syntax: initgraph( (address of driver), (address of mode), (path to file) ); Ex : int gdr = DETECT, gmd; initgraph(&gdr, &gmd, "C:\TC" ); detectgraph() : This function detects the using graphic driver and modes. It assign the values t o the variables. Syntax : detectgraph( (address of graphic driver), (address of graphic mode) ); Ex : int gdr, gmd; detectgraph(&gdr, &gmd); closegraph() : This function closes the graphics mode and changes to text mode. Syntax: closegraph(); setcolor() : This function changes the displaying color of screen. In VGAHI we can use 16 col ors. The color can be mentioned as integer. Syntax: setcolor(integer) ; Ex : setcolor(RED) ; setcolor(4) ; setbkcolor() : This function changes the background color of screen . Syntax : setbkcolor(integer) ; Ex: setbkcolor(YELLOW) ; setbkcolor(15); putpixel() : This function highlights the pixel(picture element) at the given co-ordinates to the given color . Syntax : putpixel(x-coordinate, y-coordinate, color) ; Ex : putpixel(320, 240, 5) ; line() : This function displays a line between the given coordinates. Syntax : line(x1, y1, x2, y2 ); Ex : line(200,300, 240, 370 ); lineto() : This function displays a line to the given coordinates from the current coordina tes. Syntax : lineto(x2, y2) ; Ex : lineto(400, 600) ; circle() : This function displays a circle with the given center coordinates and the radius

. Syntax : circle(x, y, radius) ; Ex : circle(320, 240, 100) ; ellipse() : Syntax : ellipse(x, y, st-angle, end-angle,x-radius, y-radius ); Ex : ellipse( 320, 240, 0, 360, 200, 100 ); arc() : Syntax : arc(x, y, st-angle, end-angle, radius ); Ex : arc(320, 240, 45, 135, 200 ) ; rectangle() : Syntax : rectangle( x1, y1, x2, y2 ); Ex : rectangle( 200, 150, 440, 320 ); settextstyle() : This function sets the displaying text style . Syntax : settextstyle( font, direction, size ); fonts : direction : HORIZ_DIR, VERT_DIR size : Ex : settextstyle( 3, 0, 5 ); outtextxy() : This functions displays the given text at the given coordinates with the setted style. Syntax : outtextxy(x, y, text) ; - 120 -

Anda mungkin juga menyukai