Anda di halaman 1dari 96

JIT Technologies Student Course Ware page

JIT TECHNOLOGIES
Student c course ware

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


C- LANGUAGE

Programming language: It acts as a interface between human beings and the machines. they are broadly classified into machine level, assembly level and high level languages. Machine level language: It is a language what system can understand directly. it is a representation of 1s& 0s.every operation in this language will be in the form of 1s& 0s..it is not a easy task to deal with this language. Assembly level language: In this language we will be using symbolic notation/mnemonics to perform any simple operation we should remember these notations. Ex: add addition Sub- subtraction Mul- multiplication Div- division etc programmes written using this language are understandable to us but not to the system to make this possible we are using interpreters the job of interpreter is just translation from assembly to machine code. ex: languages- microprocessor High level languages In this language we dont use symbolic notation as how the case of assembly i.e to perform addition we will use the regular mathematical symbol + and - for sub,programmes written using this language are understandable to us but not to machine.to make the possible we will be using compilers.the operation of compiler is translation of high level language into machine code i.e 1s& 0s. Interpretor: The operation of this translation is conversion of assembly level language to machine code .it is statement by statement conversion.if any of the error occurs at a particular line until cler this error,it doesntgoes for next statements.becauseof this it is a time consuming process even for simple programme. Compilors: When we use Compilors: Even here we are doing this translation process.if any of the error occurs in a statement apart from it translate the remaining statements comparative to interpreter ,it takes less time. There are 3 types of languages 1. High level

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

programming language, low level language, mid level language. C was developed by Dennis Ritchie in 1972 at AT&T Bell Laboratories in USA. C language developed from Basic Combined Programming Language (BCPL). Features of C language: It is a robust language. It supports in the development of simple business application to the complex business application Its a fast and efficient language It is a structured programming language. Ability to extend itself. Its a portable language. Ability to extend itself: In this language it supports a feature that we can include our require things which are not existent in library so that we can use them in our next programmes.this concept is called as reusability is nothing but we are utilizing the existing resources when ever we want ,because of this reusability we are reducing the time for the development of programmes and we are making our programmes as error free. Its a portable language . portability in the sense the programme which is executing in one system when we copy this to another its going to give the results in the same manner as how you are getting in earlier system.C programe can be wxecutable in both windows and unix-os.s . Structure of C-programme Documentation part Preposser statement Definition part Global declaration main() { variable declaration } note: every programme in the c languageis going to consists of some statement and every statement should terminated by semicolon(;). Documentation part Documentation part is ment only for user understanding purpose. the statements what we write in the Documentation part will not be considered by the compiler. /*-------------------*/ Preposser statements 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


#include<library file name> #include<stdio.h> Stdio.h Stdin Standard input stream Standard input device keyboard Stdout Standard output stream Standard output device controle/monitor

I/P O/P Stream: it is single direction passage way for information in continuous formate of bits(or) bytes stdin- standard input stream stdout- standard output stream when we use combine these tow streams which gives a file stdio standard input and output an h indicates it is a header and i.e from library file. Defintion part: it is an optional. Global declaration- it is an optional main() { variable declaration; main is afunction. C-program language is based as functions we are using functions to reduce complexitiex of program in terms of no.of statements in a programme. fundamentals of library: TOKEN it is a smallest individual part in a statement (or) in a programme. character set: a-z,A-Z,0-9 all special symbols like { },[],(),---etc. including white space/empty space. TOKENs they are divided into 6 types. C-TOKENS: keywords identifiers/variables operators 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

constants strings special symbols Keywords it is a basic building block of a programme each keyword has got a specifc meaning functionality .such that we cant do any modifications in its functionality but we can use them in our program. Identifiers/variables: it is for identifining the part of memory location. Variable: for every variable/identifier we are going to have five properties. name,address,value scope,life-time. Name: It shouldnt consists of any keyword. It shouldnt start with digit.. It shouldnt have any empty spaces. It shouldnt consists of any special symbols. Address: it is for identification of the memory location by the system .it is only one attitude what we cant specify to a variable. Value: it specifies the accessability reason if a variable in the program.here accessability is utilization of variables value in the program. Life-time:it is a duration of period that a variable is available in memory scope and life time they are they are almost similar but not the same. Operators: these are divided in to 7 types 1. Arthmetic Operators : +,-,*, / 2. Relational Operators : <, <=, >, >=, =,! = 3. Logical Operators : &,! , || 4. Assignment Operators: == 5. increment/Decrement Operators : ++,-6. Bit-wise Operators: &, |,~,<<,>>. 7. Ternory Operators. variable=(condition)?statements;statements; in the above syntax if the condition is true will be executing trueblock statement .if the condition is failed will be executingfalse block statement. [at a time any one of the statement and the statement result will be assigned to the variable.] Special Symbols: ( )--- Function [ ] --- Array { } --- unit/part of a program ; --- Termination symbol. 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Data Types: There are 3 types

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


1. Primary Data Types Data type Int Char Float Unsigned long int Double Unsigned Char Specification %d %s %f %lu % Bytes 2 1 4 32 8 16 Range -32,767 to 32,768 -128 to 127 3.4e-38 to 3.4e+38 0 to 4,294,967,295 -32,767 to 32,768

2. Derived Data Types Arrays Pointers Structures Union 3. User-defined Data Type Typedef 1).Write a program to display a texual message hello world a). #include<stdio.h> main( ) { printf(hello world); } 2).Write a program to display the value of variable? a). #include<stdio.h> main( ) { int x=10; printf(%d,x); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


}

3).Write a program to perform the sum of values ? a). #include<stdio.h> main( ) { int a=10, b=20,sum; sum=a+b; printf(%d,sum); } scanf(\:(control string,& variable); scanf is used to get run time data into the variables when the program is under execution .the basic difference between initialsation and scanf is we can get different values into the variables from every execution of program. the & symbol represents address then the syntactical meaning of scanf is we are accepting a value which will be placed at the address of what variable. #include<stdio.h> main( ) { int a, b,sum; printf(enter 2 values:); scanf(%d%d,&a,&b); sum=a+b; printf(%d,sum); } 4).Write a program to perform the product of 4 values ? a). #include<stdio.h> main( ) { int a, b,c,d, res; printf(enter 5 values:); scanf (%d%d%d%d,&a,&b,&c,&d); res=a*b*c*d; printf(%d,res); } <Conio.h> console input and output .h indicates header file. 1. clrscr() 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

2. getch() 3. getchar() clrscr meant for clearing the screen ,getch it is used for accepting a character. through keyboard when the program is under execution.,getchar even this function is also used to accept a character when the program is under excution through keyboard.

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

10

Difference b/w getch & getchar functions. when we use getch can accept character but we cant see the data what we are inserting .getchar here we can see the data which we are entering. 5).Write a program to accept, and display a character ? a). #include<stdio.h> main( ) { char ch; scanf(%c,&ch);/ instead of scanf we can write ch=getch();(or) ch= getchar(); printf(%c,ch); } in the above program when we use scanf statement is replaced by getch function with be accepting a character which cant be visualized to our naked eyes.the same scanf statement when it is replaced by getchar() function we can see the data even whole inserting. Variable: A named memory location were varying value can be stored. 6).Write a program to check whether a given pressed key is enter or not ? a). #include<stdio.h> main( ) { char ch; int res; printf(enter any key:); scanf(%c,&ch); res==(ch==\n)?1:0; printf((%d,res); getch(); } 7).Write a program to check whether a pressed key is spacebar or not ? a). #include<stdio.h> main( ) { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

11

char ch; int res; printf(enter any key:); scanf(%c,&ch); res=(ch = = )?1:0; printf((%d,res); getch(); } 8).Write a program to find the value of given equation? res=/b 2-4ac a). #include<stdio.h> #include<conio.h> #include<math.h> main( ) { double a,b,c res; clrscr(); printf(enter a,b,c values); scanf(%lf%lf%lf,&a,&b,&c); res= sqrt(pow(b*b)-4*a*c); printf((%lf,res); getch(); } 8).Write a program to find the value of given equation? res=/b 24ac*2a/4bc a). #include<stdio.h> #include<conio.h> #include<math.h> main( ) { double a,b,c res; clrscr(); printf(enter a,b,c values); scanf(%lf%lf%lf,&a,&b,&c); res= (float)sqrt((pow(b*b)-4*a*c)*2*a/(4*b*c); printf((%lf,res); getch(); } Scope & lifetime of a variable: 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

12

scope which specifies accebility of a range of a variable (or) from which part of program we can access the variable to till which part. Local variable(or) Auto variable: we can acces a local variable only within a function where we declare the variable.its accessibility scope is within the curly brases of the function. Life time: the time of avalibility of a variable in memory ex: main() { int x; } the scope of variable x in the above program is in from lifetime :from the statement execution till the function terminates. in the above example memory is allocated for the variable x when the statement is executed till the mainfunction terminate.if onces the main function is closed all the local variables will be deallocated. Global Variable(or) Extern Variable: ` variable which is declared before the mainfunction is called as global. Scope of Global Variable: through out the program we can use this variable. Life time of variable: from the statement execution till the program (main function) terminates. int x; main() { int y; } Static Variables:We can declare static local variables inside functions. These variables will retain their memory through out the life time of programme. Normally local variables memory will be get created and get destroyed for function call, because of this normal local variable can not be used to preserve previous function call state to achieve this we use static local variables. Recursion is a process where function calls itself until a condition is satisfied. Recursive programme is used in building complex algorithms and they act as alternatives to looping . ex: Static int x; main() { Static int y; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

13

} Scope: in the ex: the scope of y is within the function main.the scope of variable is through out the program. Life time: The Life time Static local variable memory is allocated at the time of compilation till the program terminates.till the function terminates. For a Static local variable memory will be allocated fromcompilation till the program terminates Garbage: its not a useful information. if a global variable is not initialize and till we want to access it .it gives the values as 0 Ex: int x; main() { int y; printf(%d,y); display garbage printf(%d,x); display 0 } Statemetns: 1. Sequential statements 2. Conditional statements 3. Unconditional statements Conditional Statements: Branching Statements: Switch If, If-else, Else-if, Nested-if Looping: For loop While loop Do-while. Unconditional Statements: Continue Break Goto Conditional Statements: It is used to check conditions. Syntax: Statement 0 if (condition) { Statement 1 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

14

} Statement 2 9) Write a program to check whether the given value is greater than 10? a). #include<stdio.h> #include<conio.h> main( ) { int x ; clrscr(); printf(enter x value); scanf(%d,&x); if(x>=10) { printf(x>10); } printf(end); getch(); } 10) Write a program to check whether the sum of 2 values is greater than 50 or not? a). #include<stdio.h> #include<conio.h> main( ) { int x ,y,sum; clrscr (); printf(enter x,y value); scanf(%d%d,&x,&y); sum=x+y; if(sum>=10) { printf(sum is>50); printf(%d,sum); } printf(end); getch(); } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

15

11) Write a program to perform division only when the dinaminator is equal 0? a). #include<stdio.h> #include<conio.h> main( ) { int nu,de; float res; clrscr (); printf(enter nu,de value); scanf(%d%d,&nu,&de); if(de!=0) { res=(float)nu/de; Printf (%f,res); } Printf (end); Getch (); } 12) Write a program to check whether the sum of 2 values is in b/w of 5,10? a). #include<stdio.h> #include<conio.h> main( ) { int x ,y,sum; clrscr (); printf(enter x,y value); scanf(%d%d,&x,&y); sum=x+y; if(sum>=5 && sum<10) { printf(sum is b/w 5 , 10 %d,sum); } printf(end); getch(); }

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


13) Write a program to check whether the pressed key is tab or enter? a). #include<stdio.h> #include<conio.h> Main ( ) { Char inp; clrscr (); Printf (press any key); Scanf (%c,&inp); If (inp= =\n || inp = =\t) { Printf (pressed key is an enter (or) tab); } printf (end); getch (); } 14) Write a program to check whether the given value is >5 or not using multiple if ? a). #include<stdio.h> #include<conio.h> main( ) { int x ; clrscr (); printf(enter x value); scanf(%d,&x); if(x>=5) { printf(x is>5); printf(%d,x); } if(x<10) { printf(x<10); printf(%d,x); }}

16

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

17

15) Write a program to check whether the sum of 2 values is greater than 50 or not? a). #include<stdio.h> #include<conio.h> main( ) { int x ,y,sum; clrscr (); printf(enter x,y value); scanf(%d%d,&x,&y); sum=x+y; if(sum>=10) { printf(sum is>50); printf(%d,sum); } printf(end); getch(); } 16) Write a program to check whether the given value is positive or negative or equal to 0 ? a). #include<stdio.h> #include<conio.h> main( ) { int x ; clrscr (); printf(enter x value); scanf(%d,&x); if(x>0 || x<0 || x= =0) { printf(x>0 or x<0 or x=0); } printf(end); getch(); } 17) Write a program to using multiple if ? a). 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


#include<stdio.h> #include<conio.h> main( ) { int x ; clrscr (); printf(enter x value); scanf(%d,&x); if(x>0) { printf(x>0); } if(x<0) { printf(x<0); } if(x=0) { printf(x=0); printf(end); } getch(); } 18) Write a program to check whether the age of person is in the group of 5-10,10-50,50-90 ? a). #include<stdio.h> #include<conio.h> main( ) { int age; clrscr (); printf(enter age); scanf(%d,&age); if(age>5 && age <=10) { printf(age is b/w 5 & 10); } if(age>10 && age <=50) { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

18

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


printf(age is b/w 10 & 50); } if(age>50 && age <=90) { printf(age is b/w 50 & 90); } printf(end); getch();

19

} 19) Write a program to display the division of a student results if the marks ? if total is in b/w of 45 to 65 2nd class,65 to 80 ,firstclass,80 to 95 3rd class a). #include<stdio.h> #include<conio.h> main( ) { int a,b,c,d,e; clrscr (); printf(enter a,b,c,d,e values); scanf(%d%d%d%d%d,&a,&b,&c,&d,&e); if(abcde>=45 && abcde <=65) { printf(abcde is in b/w of 45 & 65); } if(abcde>=65 && abcde <=80) { printf(abcde is in b/w of 65 & 80); } if(abcde>=80 && abcde <=90) { printf(abcde is in b/w of 80 & 90); } printf(end); } Nested If: Syntax: Statement 0 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

20

if (condition1) { Statement 1 } if (condition2) { Statement 2 } Statement 3 } Statemente 4 20) Write a program wher a student is passed in 3 subjects (or) not if the total is > 90 ? a). #include<stdio.h> #include<conio.h> main( ) { int a,b,c,total; clrscr (); printf(enter a,b,c, values); scanf(%d%d%d,&a,&b,&c); total=a+b+c; if(total>=90 && A>=35 && b>=35 && c>=35) { printf(he is passed in 3 subjects); printf(%d,total); } printf(end); } If-Else: Syntax: if(condition) { st 1; } else { st 2; } st 3; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

21

if else used to hold the failed conditions of if ,else doent have any condition .if indidates the opposite condition of else. at a time any one of these (if block or else block) will be executed. 21) a). #include<stdio.h> #include<conio.h> main( ) { int a; clrscr (); printf(enter a value); scanf(%d,&a); if(a>=10) { printf(a is >10); } else { printf(a<10); } printf(end); } **** if should consists of single if else 22) Write a program to generate a current bill if the units are in the range of this 5% surcharge ? 200-400 -5% total, 400 to 600 -16% total, 600 to 800-20% a) #include<stdio.h> main() { int oldunits,currentunits,amount,units; float res; printf(enter old,current units:); scanf(%d%d,&oldunits,&currentunits); units=oldunits-currentunits; if(units>=200) { if(units<=400) { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


res=amount+amount*0.05; printf(%f,res); } } if(units>=400) { if(units<=600) { res=amount+amount*0.16; printf(%f,res); } } if(units>=600) { if(units<=800) { res=amount+amount*0.20; printf(%f,res); } } printf(end); } Switch case Switch is a keyword Syntax: Switch(choice) { Case : statement; Break; Case : statement; Break; Default : statement; Break; } We can write n number of cases cases are similar to the conditions every case should starts by : to terminate we are using break. How to use switch case: 1. provide menu to the user. 2. accept the choiceas per the specified values in the menu. 3. pass the choice to switch. 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

22

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


4. write down the cases as per the menu specified values. 5. if none of the cases are matching write default block. 23) Write a program to perform arithmetic operations(+,-,*,/)? a) #include<stdio.h> main() { Int x,y,,res,ch; Printf(enter 2 values:); Scanf(%d%d,&x,&y); Printf(menu:); Printf(1.add,2.sub,3.mul,4.div); printf(enter ur choice:); scanf(%d,&ch); switch(ch) { Case 1: Res=x+y; Break; Case 2: Res=x-y; Break; Case 3: Res=x*y; Break; Case 4: Res =x/y; Break; Default: Printf(invalid choice:); Break; } } Unconditional statements: Goto: to do some execution of statements repeatedly till some condition satisfies. The statements to be executed repeatedly should be identified by label. 24) write a program ro display from 1 to 10 numbers using go to? a) #include<stdio.h> main() { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

23

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Int var=1; L1: Printf(%d,var); Var++; If(var<=10) { Goto L1; } } 25) write a program to display even numbers from 1 to 10 ? a) #include<stdio.h> main() { Int var=1; L1: If(var%2= =0) { Printf(%d,var); } Var++; If(var<=10) { Goto L1; } } 26) write a program to display odd numbers ? a) #include<stdio.h> main() { Int var=1; L1: If(var%2 ==1) { Printf(%d,var); } Var++; If(var<=10) { Goto L1; } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

24

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} 27) write a program to display from 1 to n numbers using go to? a) #include<stdio.h> main() { Int var=1,n; Printf(enter n value:); Scanf(%d,&n); L1: Printf(%d,var); Var++; If(var<=n) { Goto L1; } } 28) write a program to sum of 1 to 5 numbers using go to? a) #include<stdio.h> main() { Int var=1,sum=0; L1: Sum=sum+var; Var++; If(var<=5) { Goto L1; } Else { Printf(sum=%d,sum); } } } 29) write a program to display even sum from 1 to 5 numbers? a) #include<stdio.h> main() { Int var=1,sum=0; L1: 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

25

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


If(var%2==0) { Sum=sum+var; Var++; If(var<=10) { Goto L1; } Else { Printf(sum=%d,sum); } } 30) write a program to display even sum,odd sum from 1 to 5 numbers? a) #include<stdio.h> main() { Int var=1,sum=0; ` int evensum,oddsum; L1: If(var%2==0) { Evensum=evensum+var; } Else { Oddsum=oddsum+var; } Var++; If(var<=5) { Goto L1; } Else { Printf(%d%d,evensum,oddsum); } } 31) write a program to display 3rd multiplication table? 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

26

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


a) #include<stdio.h> main() { Int var=1,res; L1: Res=3*var; Printf(%d,res); Var++; If(var<=10) { Goto L1; } } 32) write a program to display 1 to 3 multiplication table? a) #inclu/de<stdio.h> main() { Int var=1,var2=1,res; L1: Res=var*var2; Printf(%d*%d=%d,var,var2,res); Var++; If(var<=10) { Goto L1; } Else { Var2=1; Var++; } If(var<=3) { Goto l1; } } }

27

Loops
To do execution repetedly use 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

28

While Do while For loops While: syntax: While(condition) { St 1; } Increment/decrement St 2; Do while: do while is the only loop which do the execution of statements atleast one irrespective of conditions. Every loop control variable in while loop should be initialize before the loop. In body of the loop do increment/decrement Syntax: Do { St 1; } While(condition) St 2; For loop: in for loop when it is executing first time it do the initialization and check the condition .when closing curly brase comes during executions it increments /decrements of control variable and check condition. Syntax: For(initialization;condition;increment/decrement) Switch Case: Switch is a keyword Syntax:Switch(choice) { Case: Statement; Break; Case: Statement; Break; Default: Break; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

29

} We can write N number of cases.cases are similar to the conditions every case should start by: to terminate we are using break; How to use the switch case: Step1 : Provide menu to the user. Step2: Accept the choice as per the specified value in the menu. Step 3: Pass the choice to the switch. Step 4: Write down the cases as per the menu specified values. Step 5: If none of the cases are matching write default block. 22) Write a program to perform arithmetic operations +,-,/,* a) #include<stdio.h> main() { int x,y,res,choice; printf(enter x,y values:); scanf(%d%d,&x,&y); Printf(1,ADD,2.MUL,3.SUB); printf(enter choice:); scanf(%d,&choice); Switch(choice) { Case 1: res=x+y; printf(%d,res); Break; Case 2: res=x*y; printf(%d,res); Break; Case 3: res=x-y; printf(%d,res); Break; Default: printf(wrong choice); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Break; }

30

} Uncoditional Statements: Goto: to do some execution statements repetedly till some condition satisfies the statement to be executed repetedly should be identified by label. 23) Write a program to display numbers from 1 to 10 using goto statement? a) #include<stdio.h> main() { int var=1; L1: printf(%d,var); var++; if(var<=10) { goto L1; } } 24) Write a program to display even numbers from 1 to 10 using goto statement? a) #include<stdio.h> main() { int var=1; L1: if(var%2= =0) { printf(%d,var); } var++; if(var<=10) { goto L1; } } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


24) Write a program to display odd numbers using goto statement? a) #include<stdio.h> main() { int var=1; L1: printf(%d, var); } var++; if(var<=10) { goto L1; }

31

} 25) Write a program to display numbers from 1 to n using goto statement? a) #include<stdio.h> main() { int var=1,n; printf(enter n value:); scanf(%d,&n); L1: printf(%d,var); var++; if(var<=n) { goto L1; } } 25) Write a program to display numbers from 1 to 5 using goto statement? a) #include<stdio.h> main() { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


int var=1,sum=0,n; printf(enter n value:); scanf(%d,&n); L1: printf(%d,var); var++; if(var<=n) { goto L1; } } 26) Write a program to display evensum from 1 to 5? a)#include<stdio.h> main() { Int var=1,sum=0; L1: If(var%2==0) { Sum=sum+var; Var++; If(var<=5) { Goto L1; } Else { Printf(sum=%d,sum); } } 26) Write a program to display evensum and oddsum from 1 to 5? a)#include<stdio.h> main() { Int var=1,evensum,oddsum; L1: If(var%2==0) { evenSum=evensum+var; } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

32

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Else { Oddsum=oddsum+var; } Var++; If(var<=5) { Goto L1; } Else { Printf(%d%d,evensum,oddsum); } } 27) Write a program to display 3rd multiplication table? #include<stdio.h> Main() { Int var=1,res; L1: Res=3*var; Printf(%d,res); (or) printf(3*%d=%d,var,res); Var++; If(var<=10) { Go to L1; } } 28) Write a program to display 1 to 3 multiplication tables? #include<stdio.h> Main() { Int var=1,var1=1,res; L1: Res=var*var1; Printf(%d*%d=%d,var,var1); Var1++; If(var1<=10) { Go to L1; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

33

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

34

} Else { Var1=1;var++; If(var<=3) { Go to L1; } } } Goto statement is use to do the execution repeatedly to till some conditions satisfy here control can be transferred to some other place without checking conditions .programs are not to be in a readable manner. LOOPS To do the execution repeatedly while ,do while,for loops are used While: Syn: Whie(condition) { St 1: } St 2; Do while: It is the only loop which do the execution of statements atleast once irrespective of conditions. Every loop control variable in while loop should be initialize before the loop. Syn: Do { St 1; Increment/decrement; } While(condition) { St 2; For loop: For(initialization;condition;increment/decrement) 29) Write a program to display numbers from 1 to 10? Using while loop: #include<stdio.h> 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Main() { Int var; Var=1; While(var<=10) { Printf(%d,var); Var++; } } Using Do while loop: #include<stdio.h> Main() { Int var; Var=1; do { Printf(%d,var); Var++; } While(var<=10) } Using For loop: #include<stdio.h> Main() { Int var; Var=1; For(var=1;var<=10;var++) { Printf(%d,var); } } 30) Write a program to display even numbers from 1 to 10? Using while loop: #include<stdio.h> Main() { Int var; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

35

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Var=1; While(var<=10) { If(var%2==0) { Printf(%d,var); } Var++; } Using Do while loop: #include<stdio.h> Main() { Int var; Var=1; Do { If(var%2==0) Printf(%d,var); } Var++; While(var<=10) } Using For loop: #include<stdio.h> Main() { Int var; For(var=1;var<=10;var++) { If(var%2==0) { Printf(%d,var); } } } 31) Write a program to display sum of numbers from 1 to 10? Using while loop: #include<stdio.h> Main() 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

36

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ Int var,sum=0; Var=1; While(var<=10) { Sum=sum+var; Var++; } Printf(%d,var); } Using Do while loop: #include<stdio.h> Main() { Int var,sum=0; Var=1; Do { Sum=sum+var; Printf(%d,sum); } Var++; While(var<=10) } Using For loop: #include<stdio.h> Main() { Int var,sum=0; For(var=1;var<=10;var++) { Printf(%d,var); } } 32) Write a program to display oddsum,evensum from 1 to 10? Using while loop: #include<stdio.h> Main() { Int oddsum,evensum=0; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

37

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Int num=1; While(num<=10) { If(num%2==0) { Evensum=evensum+num; } Else { Oddsum=oddsum+var; } Num++; Printf(%d%d,evensum,oddsum); } Using Do while loop: #include<stdio.h> Main() { Int evensum=0,oddsum=0,var; Var=1; Do { If(var%2==0) { Evensum=evensum+var; } Else { Oddsum=oddsum+var; } Var++; While(var<=10) } Printf(%d%d,evensum,oddsum); } Using For loop: #include<stdio.h> Main() { Int var,evensum=0,oddsum=0; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

38

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


For(var=1;var<=10;var++) { If(var%2==0) { Evensum=evensum+var; } Else { Oddsum=oddsum+var; } Printf(%d%d,evensum,oddsum); } 33) Write a program to find GCD of 2 numbers? #include<stdio.h> Main() { Int nu,de,rem; Printf(enter nu,de:); Scanf(%d%d,&nu.&de); While(nu%de!=0) { Rem=nu%de; Nu=de; De=rem; } Printf(%d,de); } 34) Write a program to find factorial of given number? #include<stdio.h> Main() { Int n,mul=1; Printf(enter number:); Scanf(%d,&n); While(n!=0) { Mul=mul*n; n--; } printF(%d,mul); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

39

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} 35) Write a program to display fibonoci series? #include<stdio.h> Main() { Int a=0,b=1,res,n,count=1; Printf(enter number:); Scanf(%d,&n); While(count<=n) { Res=a+b; A=b; B=res; Count++; } printF(%d,res); } Nested loops: While: Syn: St 0 Whie(condition 1) { St 1: While(condition 2) { St 2; } St 3; } st 4; Do while: Syn: Do { Do { } While(condition 2); } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

40

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


While(condition 1); For loop: For(initialization;condition;increment/decrement) { For(initialization;condition;increment/decrement) { } } 35) Write a program to display 1 to 3 multiplication table? #include<stdio.h> Main() { Int var1,var2,res; Var1=1; While(var1<=3) { Var2=1; While(va2<=10) { Res=var1*var2; Printf(%d*%d=%d,var1,var2,res); Var2++; } Var1++; }} 36) Write a program to display astric in the format of squre? #include<stdio.h> Main() { Int row=1.col=1; For(row=1;row<=2;row++) { Printf(\n); } For(col=1;col<=2;col++) { Printf(\t *); } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

41

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} } Using while loop: #include<stdio.h> Main() { Int row=1.col=1; row=1; while(row<=2) { Printf(\n); } While(col<=2) { Printf(\t *); col++; } Row++; } } 37) Write a program to display astrick in the rotation of this format? #include<stdio.h> Main() * * * { Int var, row=1.col=1; For(row=1;row<=3;row++) { Printf(\n); } For(col=1;col<=3;col++) { If(col<=row) { Printf(%d,var); Var++; } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

42

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} } } 38) Write a program to display astrick in the format of squre? #include<stdio.h> Main() * * * { * * * Int row=1.col=1; For(row=1;row<=2;row++) { Printf(\n); For(col=1;col<=2;col++) { If(row>=col) { Printf(\t *); } } } 39) Write a program to display in the format ? 11 12 13 14 15 16 #include<stdio.h> Main() { Int var=11,row=1.col=1; For(row=1;row<=3;row++) { Printf(\n); For(col=1;col<=3;col++) { If(col<=row) { Printf(%d,var); Var++; } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

43

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} } } 39) Write a program to display in the format ? 11 11 12 11 12 13 #include<stdio.h> Main() { Int var=11,row=1.col=1; For(row=1;row<=3;row++) { Printf(\n); For(col=1;col<=3;col++) { If(col<=row) { Printf(%d,var); Var++; } } Var=11; } } 40) Write a program to display in the format ? 10 11 12 13 14 15 #include<stdio.h> Main() { Int var=10,row=1,col=1; For(row=3;row<=3;row--) { Printf(\n); For(col=1;col<=3;col++) { If(col<=row) 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

44

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ Printf(%d,var); Var++; } } Var=10; } } 41) Write a program to display in the format ? * * * * * * #include<stdio.h> Main() { Int row=1.col=1; For(row=3;row>=1;row--) { Printf(\n); For(col=1;col<=3;col++) { If(col<=row) { Printf(\t *); } } } } 42) Write a program to display in the format ? 10 11 12 10 11 10 #include<stdio.h> Main() { Int var=10,row=1,col=1; For(row=3;row>=1;row--) { Printf(\n); For(col=1;col<=3;col++) 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

45

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ If(col<=row) { Printf(%d,var); Var++; } } Var=10; } }

46

ARRAYS
Array is a collection of similar data items which is common names. Datatype array-name[size] Int x[10]; Printf(%d,x[0]);-------10 scanf(%d,&x[0]); Printf(%d,x[1]);-------20 scanf(%d,&x[1]); Printf(%d,x[2]);-------30 scanf(%d,&x[2]); Printf(%d,x[3]);-------40 scanf(%d,&x[3]); Printf(%d,x[4]);-------50 scanf(%d,&x[4]); 2nd method Int x[5]={10,20,30,40,50}; 43) Write a program to find the sum of values of an array with maximum size as 3 ? A) #include<stdio.h> Main() { Int x[3]={10,20,30}; Int sum; Sum=x[1]+x[2]+x[3]; Printf(Sum is %d,sum); 44) Write a program to find even sum of the numbers? A) #include<stdio.h> Main() { Int x[3],I; Int sum=0; Printf(enter 3 values:); For(i=0;i<=2;i++) { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Scanf(%d,&x[i]); } For(i=0;i<=2;i++) { If(i%2==0) { Sum=sum+x[i]; } } } 45) Write a program to find odd sum of the numbers? A) #include<stdio.h> Main() { Int x[3],I; Int sum=0; Printf(enter 3 values:); For(i=0;i<=2;i++) { Scanf(%d,&x[i]); } For(i=0;i<=2;i++) { If(i%2!=0) { Sum=sum+x[i]; } } } 46) Write a program to find oddsum ,even sum of the numbers? A) #include<stdio.h> Main() { Int x[3],I,evensum=0,oddsum=0; Int sum=0; Printf(enter 3 values:); For(i=0;i<=2;i++) { Scanf(%d,&x[i]); } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

47

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


For(i=0;i<=2;i++) { If(i%2==0) { evensum=evensum+x[i]; } Else { Oddsum=oddsum+x[i]; } Printf(%d%d,evensum,oddsum);

48

} Single dimension array: Data type arrayname[size]; String is collection of characters. X[0]=a; printf(%c,x[0]; X[1]=b; printf(%c,x[1]; X[2]=b; printf(%c,x[2]; X[3]=c; printf(%c,x[3]; X[4]=d; printf(%c,x[4]; To stop the garbage value in the indices where we haven given data control string is %s used to accept a string at run time. Write a program to accept and disply a string? #include<stdio.h> Main() { Char x[5]; Printf(enter string:); Scanf(%s,&x[0]); Printf(%s,x); Operations on string: Finding the length ,reversing a string ,copying of a string to another concatenating to strings. To perform any operation on string till the null character 47)To find the length of the string? #include<stdio.h> Main() { Char x[5]; Int len=0,I; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Printf(enter a string:); Scanf(%s,x); For(i=0;x[i]!=\0;i++) { Len++; } Printf(length of a string%d,len); 48)To find the length of the string? #include<stdio.h> Main() { Char x[5]; Int len=0,I; Printf(enter a string:); Scanf(%s,x); For(i=0;x[i]!=\0;i++) { Len++; } For(i=i-1;i>=0;i--) { Printf(%c,x[i]); } } 49) Write a program To copy string to another string? #include<stdio.h> Main() { Char s1[5],s2[5]; Int I; Printf(enter a string:); Scanf(%s,s1); Printf(copying starts); For(i=0;s1[i]!=\0;i++) { S2[i]=s1[i]; } S2[i]=\0; Printf(copied string%s,s2); } 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

49

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Concatenation: #include<stdio.h> Main() { Char s1[5],s2[5]; Int j,i; Printf(enter a string:); Scanf(%s,s1); Printf(enter 2nd string:); Scanf(%s,s2); For(i=0;s1[i]!=\0;i++) {

50

} Printf(concatenating:); For(j=0;s2[j]!=\0;j++) { S1[i]=s2[j]; I++; } S1[i]=\0; Printf(concatenate string%s,s1); } Comparision Of Two Strings: 1. find the length of two strings. 2. compare the length if they are not equal strings are not equal if the lengths are equal compare for each and every character. #include<stdio.h> Main() { Char s1[5],s2[5]; Int len1=0,ken2=0,j,i; Int flag; Printf(enter a string:); Scanf(%s,s1); Printf(enter 2nd string:); Scanf(%s,s2); For(i=0;s1[i]!=\0;i++) { Len1++; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

51

} For(j=0;s2[j]!=\0;j++) { Len2++; } If(len1!=len2) { Printf(strings are not equal:); { Else { For(i=0;s1[i]!=\0;i++) { If(s1[i]!=s2[i]) { Flag=1; Break; } } If(flag==1) Printf(strings are not equal); Else Printf(string r equal:); }} } <string.h>header file Strlen(string) ---- strlen(string); Strrev(string ---- reverse the string Strcpy(destination,sourse) ---- copying Strcat(destination,source) ---- concatenating Strcmp(destination,source) ---- comparision 51) Write a program to find the length of a string using string handling functions. #include<stdio.h> #include<string.h> Main() { Char x[5]; Int len; Printf(enter string:); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Scanf(%s,x); Len=strlen(x); Printf(%d,len); } 52) Write a program to find the reverse string ?. #include<stdio.h> #include<string.h> Main() { Char x[5]; Int len; Printf(enter string:); Scanf(%s,x); strrev(x); Printf(%s,x); } 53) Write a program to copy a string?. #include<stdio.h> #include<string.h> Main() { Char s1[5],s2[5]; Printf(enter string:); Scanf(%s,s1); Strcpy(s2,s1); Printf(%s,s2); } 54) Write a program to concatenating two strings? . #include<stdio.h> #include<string.h> Main() { Char s1[5],s2[5]; Int len; Printf(enter 2 strings:); Scanf(%s%s,s1,s2); Strcat(s1,s2); Printf(%s,s1); } 55) Write a program to compare strings?. 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

52

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


#include<stdio.h> #include<string.h> Main() { Char s1[5],s2[5]; Int val; Printf(enter 2 strings:); Scanf(%s%s,s1,s2); val=strcmp(s1,s2); if(val==0) Printf(strings are equal); Else Printf(strings are not equal:); } 56) Write a program to accept and display the values of a 2/3? . #include<stdio.h> Main() { Int row[2][3]; Int row,col; For(row=0;row<=1;row++) { For(col=0;col<=2;col++) { Scanf(%d,&x[row][col]); } } } 57)Write a program to add two 2x2 matrix? #include<stdio.h> Main() { Int x[2][2],,y[2][2],z[2][2]; Int row,col; Printf(enter x elements:); For(row=0;row<=2;row++) { For(col=0;col<=2;col++) { Scanf(%d,&x[row][col]); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

53

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} } Printf(enter y matrix:); For(row=0;row<2;row++) { For(col=0;col<2;col++) { scanf(%d,&y[row][col]); } } Printf(sum of 2 matrices); For(row=0;row<=1;row++) { Printf(\n); For(col=0;col<=1;col++) { Z[row][col]=x[row][col]+y[row][col]; Printf(\t %d,z[row][col]); } } 57)Write a program to subtract two matrices? #include<stdio.h> Main() { Int x[2][2],,y[2][2],z[2][2]; Int row,col; Printf(enter x elements:); For(row=0;row<=1;row++) { For(col=0;col<=1;col++) { Scanf(%d,&x[row][col]); } } Printf(enter y matrix:); For(row=0;row<1;row++) { For(col=0;col<1;col++) { scanf(%d,&y[row][col]); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

54

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} } Printf(substraction of 2 matrices); For(row=0;row<=1;row++) { Printf(\n); For(col=0;col<=1;col++) { Z[row][col]=x[row][col]-y[row][col]; Printf(\t %d,z[row][col]); } } Character two dimensional arrays: Datatype arrayname[max row][max col]; Char x [4] [10]; Initialization : Char x[4][10]={xyz,aaa,cc,dd); 58)Write a program to accept &display 5 strings? #include<stdio.h> Main() { Int x[5][10]; Printf(enter 5 strings:); Scanf(%s,x[0]); Scanf(%s,x[1]); Scanf(%s,x[2]); Scanf(%s,x[3]); Scanf(%s,x[4]); Printf(strings are:); Printf((%s,x[0]); Printf((%s,x[1]); Printf((%s,x[2]); Printf((%s,x[3]); Printf((%s,x[4]); 59) Write a program to find the lengths of given 5 strings? #include<stdio.h> Main() { Char x[5][10]; Int I,len; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

55

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Printf(enter 5 string:); For(i=0;i<=5;i++) { Scanf(%s,x[i]); } Printf(find the length); For(i=0;i<=5;i++) { Len=strlen(x[i]); Printf(%d,len); } } 60) Write a program to reverse strings? #include<stdio.h> #include<string.h> Main() { Char x[5][10]; Int I,len; Printf(enter 5 string:); For(i=0;i<=5;i++) { Scanf(%s,x[i]); } Printf(find the reverse of a string); For(i=0;i<=5;i++) { strrev(x[i]); Printf(%d,x[i]); } } 61) Length of given 3 strings without usinh string.h ? #include<stdio.h> Main() { Char x[3][10]; Int i,j,len; Printf(enter 3 strings:); For(i=0;i<=3;i++) { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

56

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Scanf(%s,x[i]); } For(j=0;x[i][j]!=\0;j++) { Len++; Printf(%d,len); Len=0; } } 62) Write a program to copy string of one row to another? #include<stdio.h> #include<string.h> Main() { Char x[5][10]; Int I; Printf(enter 5 string:); Scanf(%s,x[0]); Strcpy(x[i]x[0]); Printf(%s,x[i]); } }

57

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

58

FUNCTIONS
Functions are of two types: 1.Library functions Scanf() Printf() Getch() 2. User defined functions. Main() User defined functions are used when a program has a set of repeated statements in different places. The statements which are repeating will be return in function such that we can call the functions whenever we want . a function can written once can be executed more than once.a program can consists of n number of functions. To execute a function we should call by means of its name how to write a user defined functions. Return type fun-name(arguments list) { Staments; } Return type indicates the type of value which we are returning from this function by default it is void. Indicates we are not returning any values. Function name can be any userdefined name. Arguments list indicates the type and number of values we are passing to the function. Advantages of user defined functions: 1. it increases the readability of the program. 2. it reduces the complexity of program. Prototype: return type fun-name(datatypes of arguments); Main() { Fun(); calling function } Fun() { Statements; called function }

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

59

Prototype indicates in this program we are using this statement this function .it is just an intimation to the compiler. 63) Write a program to display the value of variable? #include<stdio.h> Void display(); Main() { Printf(start); Display(); Printf(end); } Void display() { Int x; Printf(enter x value:); Scanf(%d,&x); Printf(%d,x); } In the above program x is a local variable to the function display the memory will be allocated to this variable x only when the function call happened as soon as the closing braces the control going bask to the calling function. 64) Write a program to display numbers from 1 to 10? #include<stdio.h> Void display(); Main() { Printf(start); Display(); Printf(end); } Void display() { Int x; For(x=1;x<=10;x++) { Printf(%d,x); } } Arguments passing in a function: 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


#include<stdio.h> Void fun(int) Main(0 { Int x=10; Fun(x);-----------actual value } Void fun(int y)---------formal parameter { Printf(%d,y); } Actual value can be a variable or constant but formal parameter should always be a variable. 65)Program to find sum of two numbers using argument passing method. #include<stdio.h> Void sum (int,int) { Int x=10,y=10; Sum(x,y); } Void sum(int a,int b) { Int res; Res=a+b; Printf(%d,res); } 66) Program to display numbers from 1 to n? #include<stdio.h> Void display (int) { Int n; Printf(enter value:); Scanf(%d,&n); Display(n); } Void sum(int a) { Int i; For(i=1;i<=n;i++) 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

60

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ Printf(%d,i); } } 67) Write a program to find sum of 3 numbers in a function and their average in another function? #include<stdio.h> Void sum(int,int,int) Void avg(int); Main() { Int m1,m2,m3; Printf(enter 3 marks:); Scanf(%d%d%d,&m1,&m2&m3); Sum(m1+m2+m3); } Void sum(int a,int b,int c) { Int total ; Total=a+b+c; Printf(total=%d,total); Avg(total); } Void avg(int total) { Float res; Res=(float)total/3; Printf(avg=%f,res); } 68) Write a program to find GCD of two numbers? #include<stdio.h> Void gcd(int,int) Main() { Int nu,de; Printf(enter nu,de:); Scanf(%d%d,&nu,&de); Gcd(nu,de); } Void gcd(int a,int b,) 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

61

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ Int rem ; While(a%b!=0) { Rem=a%b; A=b; B=rem; } Printf(gcdl=%d,b); Avg(total); } 69) Write a program to display the elements of an array? #include<stdio.h> Void display(int[]); Main() { Int x[5],i; Printf(enter 5 elements:); For(i=0i<=5;i++) { Scanf(%d,&x[i]); } Display(x); } Void display(int y[]) { Int i ; For(i=0;i<5;i++) { Printf(%d,y[i]); } } When we want to pass an array to a function pass the name of array of the formal parameterplace.no need to specify the maxsize of an array. 70) Write a program to find the sum of an array? #include<stdio.h> Void display(int[]); Main() { Int x[5],i; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

62

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Printf(enter 5 elements:); For(i=0i<=5;i++) { Scanf(%d,&x[i]); } total(x); } Void total(int x[]) { Int i ,sum=0; For(i=0;i<5;i++) { Sum=sum+x[i]; } Printf(%d,sum); } 71) Write a program to display the string using argument passing? #include<stdio.h> Void display(char[]); Main() { char x[5]; Printf(enter a string:); Scanf(%s,x); Display(x); Void Display charx[] { Printf(%s,x); } { Int i ; For(i=0;x[i]!=\0;i++) Printf(%d,x[i]); } 72) Write a program to find the length of a given string? #include<stdio.h> #include<string.h> Void display(char[]); Main() { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

63

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


char x[5]; Printf(enter a string:); Scanf(%s,x); Display(x); } Void display(char x[]) { Int i ,len=0; For(i=0;x[i]!=\0;i++) { Len++; } Len=strlen(x); Printf(%d,len); } 73) Write a program to find sum of even index of an array? #include<stdio.h> Void sum(int[]); Main() { int x[5]; Printf(enter 3 elements:); For(i=0;i<=5;i++) { Scanf(%d,&x[i]); Sum(x); }} Void sum(int x[]) { Int i ,sum=0; For(i=0;i<=5;i++) { If(i%2==0) { Sum=sum+x[i]; } Printf(%d,sum); } 74) Write a program to concatenate the given string? #include<stdio.h> 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

64

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

65

#include<string.h> Main() { Char s1 [5], s2 [5]; Printf (enter 2 strings :); Scanf (%s1%s2, &s1, &s2); Concatenate(x); } Void concatenate (char x [],char y[]) { Int i; Strcat(s1,s2); Printf(%s,x); } Note: whether it may be singler dimension & 2d array when we are passing that to a function we should specify the name of that array.in the formal parameter place it is must to specify the number of columns. 75) Write a program to display 2x3 matrix? #include<stdio.h> Void display (int [2][3]); Main() { Int x[2][3]; Int I,j; For(i=0;i<=2;i++) { For(j=0;j<=3;j++) { Scanf(%d,&x[i][j]); } } Display(x); { Int j,i; Printf(elements ); For(i=0i<=2;i++) { For(j=0;j<=3;j++) { Printf(%d,x[i][j]); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} } } 76) Write a program to display sum of 3x3 matrices? #include<stdio.h> Void display (x [3][3]); Main() { Int x[3][3]; Int j,i; For(i=0;i<=3;i++) { For(j=0;j<=3;j++) { Scanf(%d,&x[i][j]); } } Display(x); } Display(int x[][3]) { Int j,I,sum=0; Printf(elements ); For(i=0i<=3;i++) { For(j=0;j<=3;j++) { If(i==j) Sum=sum+x[i][j]; } } Printf(%d,sum); } 77) Write a program to display the given 3 strings? #include<stdio.h> Void display(char x[][10]); Main() { char name[3][10]; int I; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

66

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Printf(enter 3 strings:); Scanf(%s,x); For(i=0;i<=3;i++) { Scanf(%s,name[i]); } Display(name); } Void display(char x[][10]) { Int I; For(i=0;i<=3;i++) { Printf(%s,x[i]); } } 78) Write a program to display length s of the given 3 strings? #include<stdio.h> #include<string.h> Main() { char name[3][10]; int I; Printf(enter 3 strings:); For(i=0;i<=3;i++) { Scanf(%s,name[i]); } Display(name); } Void display(char x[][10]) { Int I,len; For(i=0;i<=3;i++) { Len=strlen(x[i]); Printf(%d,len); } } 79) Write a program to findreverse of given 5 strings? 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

67

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


#include<stdio.h> #include<string.h> Void display(char x[][10]); Main() { char name[3][10]; int I; Printf(enter 5 strings:); For(i=0;i<=5;i++) { Scanf(%s,name[i]); } Display(name); } Void display(char x[][10]) { Int I; For(i=0;i<=5;i++) { strrev(x[i]); Printf(%s,x[i]); } } 80)Write a program gcd of numbers? #include<stdio.h> Main() { Int nu,de,res; Printf(enter nu,de:); Scanf(%d%d,&nu,&de); Res=gcd(nu,de); Printf(gcd=%d,res); } Gcd(int nu,int de) { Int rem; While(nu%de!=0) { Rem=nu%de; Nu=de; De=rem; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

68

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} Return de; }

69

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

70

POINTERS
Pointer is available which holds the address of another variable of some datatype Declaration: Datatype *variable; Int *x; X=&y; Printf(%d,&x); Printf(%d,&x); & symbol represents =address and * symbol represents a pointer variable at the time of declaration .in remaining parts of the program * represents the value at the address, control string %u address to see. 1)Write a program to display the value of a variable using pointers. #include<stdio.h> Main() { Int y=10; Int *p=&y; Printf(%d,*p); } 2)write a program to find sum of two variables using pointers? a) #include<stdio.h> main() { Int x,y,sum; Printf(enter x,y values:); Scanf(%d%d,&x,&y); Sum=x+y; Printf(%d,sum); } Using pointers: Main() { Int x,y,sum; Int *p,*q; P=&x; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


q=&y; printf(enter x,y values:); scanf(%d%d,&x,&y); sum=*p+*q; printf(%d,sum); } 3)write a program to display values from 1 to 10 using pointers? a) #include<stdio.h> main() { Int n=1; While(n<=10) { Printf(%d,n); n++; } } Using pointers: Main () { Int num=1; Int *p=&num; While(*p<10) { printf(%d,*p); (*p)++; } } 4) Write a program to find even numbers using pointers? a) #include<stdio.h> Main () { Int n=1; While (n<=10) If (n%2 = =0) { Printf(%d,n); n++; }

71

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} Using pointers: Main () { Int num=1; Int *p=&num; While(*p<=10) { If(*p)%2==0) { printf(%d,*p); (*p)++; }

72

} } 5) Write a program to find factorial of given number using pointers? a) #include<stdio.h> Main () { Int n,res=1; Int *p; *p=&n; Printf(enter n value:); Scanf(%d,&n); While((*p)!<=10) { Res=res*(*p); (*p)--; } Printf(%d,res); } 6) Write a program to 3rd multiplication table using pointers? a) #include<stdio.h> Main () { Int var=1,res; While (var<=10) { Res=var*3; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Printf(3*%d=%d,var,res); var++; } } Using pointers: Main () { Int var=1,res; Int *p; *p=&var; While((*p)<=10) { Res=(*p)*3; Printf(3*%d=%d,*p,res); (*p)++; } } 7) Write a program gcd of two numbers using pointers? a) #include<stdio.h> Main () { Int nu,de rem=1; Int *p,*q; *p=&nu; *q=&de; Printf(enter nu,de); Scanf(%d%d,,&nu,&de); While((*p)%(*q)!=10) { Rem=*p%*q; *p=*q; *q=rem; } printf(%d,*q); } 8) Write a program to find evensum,odsum of numbers using pointers? a) #include<stdio.h> Main () 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

73

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ Int evensum=0,oddsum=0; Int num=1; Int *p,*q; P=&evensum; Q=&oddsum; While(num<=10) { If(num%2==0) { *p=*p+num; } Else { *q=*q+num; } num++; } Printf(%d%d,*p,*q);} 9) Write a program to find sum of two numbers using pointers? a) #include<stdio.h> Main () { Int a,b,sum; Printf(enter a,b values:); Scanf(%d%d,&a,&b); Sum=(&a,&b); } Void sum(Int *a, int *b) { Int total; Total=*a+*b; Printf(%d,total); } 10) Write a program to find factorial of given number? a) #include<stdio.h> Main () { Int x, res =1; Printf (enter x value:); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

74

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Scanf (%d,&x); } Void fact (*a); { Int res=1; While (*n!=0) { Res=res*(*n); *n--; } Printf (%d, res); } } 11) Write a program to display the values of an array using pointers? a) #include<stdio.h> Main () { Int a[5]; Int I,*p; Printf(enter 5 elements:); { For(i=0;i<5;i++) { Scanf(%d,&a[i]); } } P=&a[0]; For(i=0;i<5;i++) { Scanf(%d,*p[i]); } } 12) Write a program to find sum of array? a) #include<stdio.h> Main () { Int a[5], i,sum=0; Int *p; P=&a[0]; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

75

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Printf(enter 5 elements:); { For(i=0;i<5;i++) { Scanf(%d,&a[i]); } } For(i=0;i<5;i++) { Sum=sum(*(p+i)); } Printf(sum=%d,sum); } 13)write aprogram to display a string character by character? a) #include<stdio.h> Main() { Char x[10]; Int I; Char *p; P=&x[0]; Printf(enter a string:); Scanf(%s,x); For(i=0;*(p+i)!=0;i++) { Printf(%c,*(p+1)); }} 14)write a program to display length of string ? a) #include<stdio.h> Main() { Char x[10]; Int I,len=0; Char *p; P=&x[0]; Printf(enter a string:); Scanf(%s,x); For(i=0;*p(p+i)!=\0;i++) 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

76

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ Len++; } Printf(%d,len); }

77

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


15) Write a program to copy one string to another string using pointers? a) #include<stdio.h> Main() { Char s1[10],s2[10]; Int I; Char *p,*q; P=&s1[0]; q=&s2[0]; Printf(enter a string:); Scanf(%s,s1); Printf(copying starts:); For(i=0;*(p+i)!=\0;i++) { *(q+i)=*(p+i); } *(q+i) =\0; Printf (copied =%s,(*q)); } Pointers on 2D arrays 16) write a program to find display 2x2 matrix? a) #include<stdio.h> Main() { Int x[2][2],i,j; Int *p=&x[0][0]; Printf(enter elements:); For(i=0;i<=2;i++) { For(j=0;j<=2;j++) { Scanf(%d,&x[i][j]); } } For(i=0;i<2;i++) { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

78

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


For(j=0;j<2;j++) { Printf(%d,*(p+2*i+j)); } } } 17)write a program to display sum of 3x3 matrix? a) #include<stdio.h> Main() { Int x[3][3],i,j,sum=0; Int *p=&x[0][0]; Printf(enter elements:); For(i=0;i<=3;i++) { For(j=0;j<=3;j++) { Scanf(%d,&x[i][j]); } } For(i=0;i<3;i++) { For(j=0;j<3;j++) { Printf(%d,*(p+3*i+j)); } } Printf(sum=%d,sum); } 18)Write a program to find sum of 2x2 matrix? #include<stdio.h> Main() { Int x[2][2],,y[2][2]i,j,sum=0; Int *p=&x[0][0]; Printf(enter elements:); For(i=0;i<=2;i++) { For(j=0;j<=2;j++) 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

79

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ Scanf(%d,&x[i][j]); } } For(i=0;i<2;i++) { For(j=0;j<2;j++) { Printf(%d,*(p+2*i+j)); } } Printf(sum=%d,sum); } 19) write a program to sum of two matrices? #include<stdio.h> Main() { Int x[2][2],,y[2][2],sum=0; Int *p,*q; p=&x[0][0]; q=&y[0][0]; Printf(enter 1st matrix elements:); For(i=0;i<=2;i++) { For(j=0;j<=2;j++) { Scanf(%d,&x[i][j]); } } Printf(enter 2nd matrix elements:); { For(i=0;i<2;i++) { For(j=0;j<2;j++) { Scanf(%d,&y[i][j]); } Printf(addition); For(i=0;i<2;i++) { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

80

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


For(j=0;j<2;j++) { Sum=*(p+2*i+j)+*(q+2*i+j); Printf(%d,sum); } } } 20) Write aprogram to display given 5 strings? #include<stdio.h> Main() { Int x[5][10],*p; Int I; p=&x[0][0]; Printf(enter 5 strings:); For(i=0;i<=5;i++) { Scanf(%s,x[i]); } For(i=0;i<5;i++) { For(j=0;j<2;j++) { Printf(%s,*[i]); } } 21) Write a program to display string? #include<stdio.h> Main() { Int x[5][10], *p; Int I,j; p=&x[0][0]; Printf(enter strings:); For(i=0;i<=5;i++) { Scanf(%s,x[i]); } } For(i=0;i<5;i++) 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

81

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


{ For(j=0;(*p[i]+j)!=\0;j++) { Printf(%c,*(p[i]+j)); } }

82

} 22) Write a program to find length of 5 strings. With out using string header file? #include<stdio.h> Main () { Int x[5][10],*p; Int i ,j,len=0; Int *p=&x[0][0]; Printf(enter 5 strings:); For(i=0;i<=5;i++) { Scanf(%s,x[i]); } For(i=0;i<5;i++) { For(j=0;*(p[i]+j)!=\0;j++) { Len++; } Printf(%d,len); } } Using header file #include<stdio.h> #include<string.h> Main() { Int x[5][10],*p; Int i ,j,len=0; p=&x[0][0]; Printf(enter 5 strings:); For(i=0;i<=5;i++) { 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Scanf(%s,x[i]); } For(i=0;i<5;i++) { Len=strlen(*p[i])); Printf(%d,len); } } 23) Write a program to display 3 strings in reverse order? #include<stdio.h> Main() { Int x[5]; Char *p; Int i ; p=&x[0]; Printf(enter a string:); Scanf(%s,x); For(i=0;*(p[i])!=\0;i++) { } For(i=i-1;i>=0;i--) { Printf(%c,*p[i]); } } 24) Write a program to find out subtraction of 3x3 matrix using pointers? #include<stdio.h> Main() { Int x[2][2],,y[2][2]i,j,sub; p=&x[0][0]; q=&y[0][0]; Printf(enter 1st matrix elements:); For(i=0;i<=3;i++) { For(j=0;j<=3;j++) { Scanf(%d%d,&x[i][j]); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

83

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


} } Printf(enter 2nd matrix elements:); { For(i=0;i<2;i++) { For(j=0;j<2;j++) { Scanf(%d%d,&y[i][j]); } Printf(subtraction); For(i=0;i<3;i++) { For(j=0;j<3;j++) { Sub=*(p+3*i+j)-*(q+3*i+j); Printf(%d,sub); } } }

84

STRUCTURES
Syntax: Struct stru-name { Variable declaration; }; With structure definition we want to allocate space to the structre members.it is one of data representation . to allocate memory for the structure members we should create a structure variable . Ex: Strutag variable Struct student s1; Datatype When structure variable is created will allocate space to all of the data members we can create n members of structe variables every structure variable has got the separate copy of data members structure variables are independent write a structure definiti0n before main() create a structure variable within a main function. 1st method : creation of structure 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Struct student { Int id; Char name[10]; }; Main() Struct student s1,s2,s3; 2nd method: Struct student { Int id; Char name[10]; }sy; ------ global variable Main() Struct student s1,s2,s3;-----------local variable Int x; X=10; Initialization of structure variable: Struct student { Int id; Char name[10]; }sy={20,abc}; Main() Struct student s1={10,x y z}; Printf(%d,s1.id); -----10 Printf(%d,sy.id);-------20 Data accessing: Structure variable .structure member Runtime accepting of data that is scanf. #include<stdio.h> Struct student { Int id,marks; }; Main() { Struct student s1; Printf(enter id and number); Scanf(%d%d,&s1.is,&s1.marks); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

85

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Printf(%d%d,s1.id,s1.marks); } 1) Write a program to find sum of 3 subjects of a1 student ? The structure members are m1,m2,m3,total. #include<stdio.h> Struct student { Int m1,m2,m3,total; }; Main() { Struct student s1; Printf(enter 3 subject marks); Scanf(%d%d%d,&s1.m1,&s1.m2,&s.m3); Total =s1.m1+s1.m2+s1.m3; Printf(total is =%d,s1.total); }

86

To find for number of students use arrays. Ex: #include<stdio.h> Struct student { Int m1,m2,m3,total; }; Main() { Struct student s[4]; Int i; Printf(enter 3 subject marks for 4 students); For(i=0;i<4;i++) { Scanf(%d%d%d,&s[i].m1,&s[i].m2,&s[i].m3); S[i].Total =s[i].m1+s[i].m2+s[i].m3; Printf(total is =%d,s[i].total); } } 2) Write a program to calculate gross salary for five emplotees in the structure members are employe id,employe name employe basic salary(da=15% of basic salary)? a) #include<stdio.h> 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Main() { Int basic salary; Char name[10]; }; Main() { Struct employee s[5]; Int grosssalary,I,da; Printf(enter id,name,basic salary:); For(i=0;i<=5;i++) { Scanf(%d%s%d,&s[i].id,&s[i].name,&s[i].basicsalary); Da==s[i].basicsalary*0.15; Grosssalary=s[i].basicslary+da; Printf(%d%d,s[i].id,grosssalary); Printf(%s,s[i].name); } }

87

UNIONS
Union is collection of different data items(types).it is used in the creation of user defined data types. Syntax: Union un-name { Variable; } Union student { Int x; Float y; union members. Char z; } main() when we create a union variable it allocate space only to the structure members which has got highest occupies in the above example for the union variable v1 memory allocation was 2 bytes because in unions when we are allocating space always the system for highest occupy the memory which

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

88

will be shared by all the members the main advantage of unions are because of common location we can store different types of values always. Ex: #include<stdio.h> Union student { Int x,y; }; Main() { Union student u1; Scanf(%d,&u1.x); Printf(%d,u1.x);--------20 Printf(%d,u1.y);--------20 scanf(%d,&u1.x); Printf(%d,u1.y);-------30 Printf(%d,u1.x);-------30

FILES
File is collection of related data on which we are performing operation by using some library function .record is a collection of all columns data in a row. Creating a file Open a file Perform the operation Read (or) write Close the file. 1. fopen(file,mode) Fopen is used to open a file .so file we should specify the file . Mode specifies the type of operation that we are performing on this file. Modes. r------ read only w-----write only a-----appending(joining) 1.If the mode is r and the file is not existent earlier f open function gives NULL.

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

89

2. If the mode is r and the file is existent it opens the file such that we can perform reading operations only. 3. If the mode is w and the file is not existent it creates a new file . 4. If the mode is r and the file is existing it open the file and clears all the previous datra now we can perform writing operations newly. 5.if the mode is a and the file is not existing it creates a file and opens in writing mode. 6. If the mode is a and the file is existent it opens the file such that the previous content (or) data will be remines the same and newly enterd data will be at last of the file. We use f open function if the file is existent it gives the address of file. To hold the address of a file pointer should be same as the file datatype FILE *fp

Read writing Get c () put c () Get w () put w () Gets puts () Get c () is used to read a character from file. Put c () is used to write a character to the file. Get w () is used to read an integer data. Put w () is used to read a integer to a file. Gets () is used to read a string from file. Puts () is used to write a string to the file. Syntax: Char=getc(filepointer) putc(char variable,file pointer) Integer variable=getw(file pointer) putw(integer variable,file pointer) Char array= gets(file pointer) puts(char arrayname,file pointer) Read Fscanf (stream, control string, &variable) Write: Fprintf (stream, control string, variable) Write a program to write a integer data to a file? #include<stdio.h> Main() { FILE *fp; 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Int ch; Fp=fopen (file.txt,w); Printf(enter an integer data); Fscanf(stdin,%d,&ch) Fprintf(fp,%d,ch); Fclose(fp); Fp=fopen(file.txt,r); Fscanf(fp,%d,&ch); Fprintf(stdout,%d,ch); Fclose(fp);

90

} Write a program to write a string to a file? #include<stdio.h> Main() { FILE *fp; Char x[10]; Fp=fopen(file.txt,w); Printf(enter a string:); Scanf(%s,x); Puts(x,fp); Fclose(fp); Fp=fopen(file.txt,r); X=gets(fp); Printf(%s,x); Fclose(fp); } Write a program to write some data into a file till we got any special symbols? #include<stdio.h> Main() { FILE *fp; Char ch; Fp=fopen(file.text,w); While((ch=getch())!=#) { Puts(ch,fp); } Putc(#,fp); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


Fclose(fp); Fp=fopen(file.txt,r); While((ch=getc(fp))!=#) { Printf(%c,ch); } Fclose(fp); } Write a program to copy the data of one file to another? #include<stdio.h> Main() { FILE *fp1,*fp2; Char ch; Fp1=fopen(first.txt,a); While((ch=getch())!=#) { Puts(ch,fp1); } Putc(#,fp1); Fclose(fp1); Fp1=fopen(first.txt,r); Fp2=fopen(second.txt,a); While((ch=getc(fp))!=#) { Putc(ch,fp2); } Fclose(fp2); Fclosse(fp1); } Feof function is used for finding whether the filr has reached at the end or the cursor is thet last character of the file it gives us 0 when the file has data and not 0 when we dont have any data. Ex: #include<stdio.h> Main() { FILE *fp1; Char ch; Fp=fopen(read.txt,w); 18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

91

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page


While((ch=getch())!=#) { Puts(ch,fp1); } Fclose(fp1); Fp1=fopen(read.txt,0); While(feof(fp)==0) { Ch=get(fp1); Printf(%c,ch); } Fclose(fp1); } Write a student details into file.(id,name,total). #include<stdio.h> Struct student { Int id; Char name[20]; Int Total; }; Main() { Struct std s1; FILE *fp; Printf(enter id,name,total:); Scanf(%d%s%d,&s1.id,&s1.name,&s1.total); Fp=fopen(file.text,w); Fprintf(fp,%d%s%d,s1.id,s1.name.s1.total); Fclose(fp); Fp=fopen(file.txt,r); Fscanf(fp,%d%s%d,&s1.id,&s1.name,&s1.total); Fclose(fp); }

92

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

93

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

94

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

95

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++

JIT Technologies Student Course Ware page

96

18-2-139, Ashok Nagar (Near Devendra Theatre), Tirupathi -517501, 0877-2255112

Offers Java 5.0 & 6.0, Java EE1.5, DOT Net 2005, Testing Tools, C and C++