Sie sind auf Seite 1von 56

C PROGRAMMING Unit 1

Question 1. Answer What is Programming language? How many types of Programming language. Language is a medium of communication. Set of syntax and code is called instruction. Set of instruction is called program . Set of program is called software. Set of software is called package. Programming language consist of symbol and character and grammar rules. Two types of programming language. 1. Low level language 2. High level language Low level language: Low level language is show the binary codes 1s or 0s form. It is understood by the computer. It is machine dependent. Two types of low level language 1. Binary language 2. Assembly language Binary language: This language is directly understood by the computer. In this language all the program are written in binary form. This language has two parts operation code and address of operand. C=a+b . + is operation code. a,b is operand. C is address. Assembly language: Symbolic codes are used in assembly language. For example add, sub, sqrt, power. C=add(a,b). a,b are operand. Add is label. C is address. High Level language: This language is used symbols and words. The words are similar to English language. It is machine independent. It is easy to learn, write and modify. Two types of high level language. 1 Procedural language 2. Object Oriented language Procedural language: This language are designed for solving a general program. For example Pascal, Fortran, C language. Object Orientated language: This language is user friendly. The user solve the problem of data and function. It is called non procedural language. For example C++,Java, Visual Basic, C#.net, VB.net. Translator: It is called language processor. It convert the high level language or assembly language into machine language. Compiler: It compile and error checking program are used. Interpreter: It is run the program on Dos prompt.

Question 2. Answer

Difference Between Interpreter and Compiler. Interpreter Compiler It translate the program at a one time. It is slow and poor debugging It take more time It is secure.

1. 2. 3. 4. Question 3. Answer

It translate the program line by line. It is fast and good debugging It take less time It is not secure

What is C Programming? C language was developed by Dennis Ritchie in 1972 at Bells Laboratory in USA. It is a high level language. It was develop at AT & T Bells Laboratory. It is compiler based language. It is machine independent language. It is portable. How many features of C language? C Language has many features. It is a function language. It consist of function and variables. Every C program must have a function called void main( ) Function is composed of many statements. Every statement ends with a semicolon. Comments begin //* with *//. What is the characters set of C programming? Character set include the letters (A to Z) digits ( 0 to 9 ) special symbols (#,&,*,<,>,%) what is C tokens? A C program is a collection of tokens, comments. There are five types of tokens include a C language. 1. Reserved keyword 2. Identifier 3. Literals 4. Operators 5. Separator

Question 4. Answer 1. 2. 3. 4. 5. 6. Question 5. Answer

Question 6. Answer

Question 7. How many keywords in C Programming? Answer keywords are the words that have standard predefined meaning. These keywords cannot redefined by programmer. All keywords must be written in lower case. Keywords are also known as reserve words. There are 32 keywords.

1. 2. 3. 4. 5. 6. 7. 8. Question 8. Answer .

Auto Break Case Char Const Continue Default do

9. 10. 11. 12. 13. 14. 15. 16.

Double Else Enum Extern Float For Friend goto

17. 18. 19. 20. 21. 22. 23. 24.

Int If Long Private Protected Public Return short

25. 26. 27. 28. 29. 30. 31. 32.

Signed Switch Sizeof Typedef Union Unsigned Void While

What is Identifiers? Identifier refer to the name of variable, function array, class etc. It is created by the user. There are many rules: 1. An identifier may consist of letters, digit underscore. 2. The first character must be a letter 3. Special symbols and blanks are not allowed. 4. Keyword and reserved words are not allowed. 5. Both upper case and lower case allowed. 6. A identifier may be as long as you wish. Question 9. What are operators? Answer Operators are the symbols that represent specific operations .There are many operators. Question 10. What are separators? Answer There are few characters that are used to separators. Semicolon is the most commonly used separator. It is used to terminate the statement. Question 11. How many type of C statements? Answer 1. Label statement 2. Expression statement 3. Selection statement 4. Interaction statement 5. Jump statement 6. Array 7. Pointer 8. Structure 9. Functions 10. String 11. Macro 1. labeled statement: Any statement may begin with a label. Label must not be reserved keyword. 2. Expression statement: Assignment Pre increment Post increment Pre decrement Post decrement 3. Selection statement: 3

These statements select one of several control flows. There are three type of selection statement. If If else Switch statement 4. Interaction statement: These statement specify how and when looping will take place. There are three types of interaction statements. While Do while For 5. Jump statement: These statements pass control to the beginning or end of the current block or to a labeled statement within the block. This are four types. Break Continue Goto 6. Array : Array is a collection of similar data types of char, int, float. Array are the data structure which hold multiple variable of the data type. Array is a sequence of object all of which have same object. Syntax Datatype arrayname[size]; int a[10]; 7. Pointer: A Pointe is a variable that stores memory address. Like all other variables it also has a name. it has to be declared and occupies same space in memory. It is called pointer because it points to a particular location in memory by storing the address of that location. 8. String: String are used to manipulate text or group of text. Syntax Datatype stringname[size]; Char name[10]; 9. Structure: Data of different types can be grouped together under a single name using structure. The data element of a structure are called members. All these variable shares total memory space. i.e Struct student { Char name[10]; Int Rollno; }a,b; 10. Union: It is also a collection of variables. These variables are of different types. All these variable shares common memory space .i.e. union student 4

{ Char name[10]; Int Rollno; }a,b; 11. Macro: Macro is called preprocessor. It is a collection of special statements called directives that executed at the beginning of the compilation process. Question 12. What is constants? How many types of constants? Answer A constant s a quantity that remains unchanged during the execution of a program. C Program supports several types of constants. There are two types of constants. 1. Numeric constants 2. Character constant 3. Question 13. What is Numeric constants? How many types of numeric constants? Answer Numeric constants are show the numeric value. There are two type of numeric constants 1. Integer constant 2. Real/float constant Question 14. What is integer constant? Explain it rules? Answer Integer constants are show the whole numbers without any decimal point. Rules: 1. An integer constant must have at least on digit. 2. An integer constant contain neither a decimal point. 3. It may either +ve or ve sign. 4. Commas or blank space cannot be include. 5. The allowable range for integer constant for -32768 or +32767. Question 15. What is Real/Float constant? Answer Real constant are number having Decimal part. These may be written in one of the two forms called fraction form or the exponent form. Exponent form is 50.5e3. Rules: 1. A real constant must have at least one digit. 2. A real constant must contain a decimal point. 3. Commas and blank space cannot be include with in the number. 4. It should be either +ve or ve. 5. default sign is positive. Question 16. What is single character constant? Answer A single character constant is a single character , enclosed in apostrophes (single quotes ). The single constant may be letter, number and special symbol. For example a,$ 5etc. Question 17. What is string constant? 5

Answer

Multiple character constant are treated as string constant. A string constant is a sequence of character enclosed in double quotes. String constant automatically end with null character \0. Therefore the string RAM will automatically be represented as RAM\0 in the memory and it size is not 3 but 4 character. For example sham 2001.

Question 18. What is variable? How it declared in java progaming? Answer Variable are the identifier that are used to store a data value. A variable may take different values. A variable name is a arbitrary long sequence of letter and digits. The first letter must be a letter, underscore. Syntax Datatype variable1,vairable2, -----,variable n; int a,b,result; Variable are giving by two statement 1. By using assignment statement 2. By using read statement By using assignment statement: The assignment statement is used to assign the value of a variable. For example #include<stdio.h> #include<conio.h> void main() { int a,b,result; clrscr( ); a=10; b=20; result=a+b; printf(Answer=%d,result); getch( ); } By using read statement : we may also give value to the variable directly through the keyboard. #include<stdio.h> #include<conio.h> void main() { int a,b,result; clrscr( ); printf(Enter the value a=); scanf(%d,&a); printf(Enter the value b=); scanf(%d,&b); result=a+b; printf(Answer=%d,result); getch( ); } Question 19. structure of c program. 6

Answer #include<stdio.h> #include<conio.h> void main() { int a,b,result; clrscr( ); printf(Enter the value a=); scanf(%d,&a); printf(Enter the value b=); scanf(%d,&b); result=a+b; printf(Answer=%d,result); getch( ); } #include<stdio.h> It is a header file. It stand for standard input output. If this header file in not used then there is necessary to save the file in .c other wise if we use this header file the file is saved automatically .cpp. #include<conio.h> It is a header file. It stand for console input output. It is used the two function. First is clrscr( ) is used clear the screen. getch( ) is show the result on dos prompt. If we not the getch then we press the Alt+F5 to show the result on dos screen. void main( ) it is a special function to tell the compiler where the program start. If we not use the return keyword then we use the void keyword. { This opening braces. It indicate the beginning of the function main( ) } This is closing braces. It indicate the closing of the function main( ) int a,b,result; It show data type and variables. variable are always meaning full. result=a+b; clrscr( ) It is used to erase the contents of dos screen. it means that clear the screen. printf It is a statement that is used to print the message on the screen. for example printf(Enter the any number); scanf It is used to store the value in a variable. It show bitwise operator to store the value. For example scanf(%d,&number); result=a+b It show formula you have to applied. printf It show the result on the screen.

Question 20. Define Data Types? Explain its types? Answer Data Types are means to identify the type of data and associated operation handling. Data Types are three types. 1. Primitive Data Types 2. Non Primitive Data Types 3. User Defined Data Types 1. Primitive Data Types: Numeric and non numeric data types are primitive data types. It has four types. 1. Integer 2. float 3. long int 4. double 5. char Integer: Integer show the whole numbers without any decimal point. The value can be positive or negative. An integer value takes 2 bytes space in memory. Int value show in %d. Keywords Byte Short Int Long Size 1byte 2 byte 4 byte 8byte Range -28 to 27-1 216 to 215-1 232 to 231-1 264 to 263-1

Float/Real: Real and float are show the number having Decimal part. These may be written in one of the two forms called fraction form or the exponent form. Exponent form is20.5e3. The value can be positive or negative. An float value takes 4bytes space in memory. flaoat value show in %f. keywords Float Double Size 4byte 8 byte Range -232 to 231-1 -264 to 264-1

Double: Double are show the number having Decimal part. These may be written in one of the two forms called fraction form or the exponent form. Exponent form is20.5e3. The value can be positive or negative. An double value take 8 bytes space in memory. It show 12 digit after a decimal point. Long Integer: Long Integer show the whole numbers without any decimal point. The value can be positive or negative. An long integer value takes 4 bytes space in memory. Int value show in %Ld.

Character: character show the single one character , enclosed in apostrophes(single quotes ). The single data type may be letter, number and special symbol. For example a,$ 5etc 2. Non Primitive Data Type: Data types which are built with the help of primitive data types but are not actually primitive are know as non primitive data type. There are also called as derived data type. It has many types. 1. Array 2. Pointer 3. String Array : Array is a collection of similar data types of char, int, float. Array are the data structure which hold multiple variable of the data type. Array is a sequence of object all of which have same object. Syntax Datatype arrayname[size]; Int a[10]; Pointer: A Pointe is a variable that stores memory address. Like all other variables it also has a name. it has to be declared and occupies same space in memory. It is called pointer because it points to a particular location in memory by storing the address of that location. String: String are used to manipulate text or group of text. Syntax Datatype stringname[size]; Char name[10]; 4. User Defined Data types: User defined data types are used only by the user. It has two types. 1. Structure 2. Union 3. Enumerate Structure: Data of different types can be grouped together under a single name using structure. The data element of a structure are called members. All these variable shares total memory space. Struct student { Char name[10]; Int Rollno; }a,b; Union: It is also a collection of variables. These variables are of different types. All these variable shares common memory space .i.e. union student { Char name[10]; Int Rollno; }a,b; Enumerated: An enumerate type is a user defined data types. It can take 9

values only from a user defined list of named integer constant called enumerators. The syntax of defining on enumerators. The syntax of defining n enumeration data types is same as that of structure or union. Question 21. Define Operators? How many operators used in C programming? Answer Operator are the symbols that represent the specific operations. There are many types of operator. Arithmetic Data Operator Relational Data Operator Logical Data Operator Conditional Data Operator Assignment Data Operator Increment Data Operator Decrement Data Operator Bitwise Data Operator Sizeof operator Comma operator Unary Operator Binary Operator Ternary Operator Arithmetic Data Operator : This operator are used to mathematical or numerical calculation. For example Addition + Subtraction Multiplication * Division / Remainder % Relations Data Operator: This operator are used compare the two or three values. Greater than > less than < Greater than equal to >= Less than equal to <= Not equal to != Logical or Boolean Data Operator: An expression that combines two or more expression is called an logical expression. To combine these expressions logical operators are used. These operators return 0 for false and 1 for true. The operands may be constants , variables or expression. C has three logical operators. And && ( it show then all condition are true.) Or || ( it show then one condition is true) Not! 10

Conditional Data Operator: It is an ternary operator. it requires three expressions as operands. This in written as result= a>b ? a:b; at first the test expression is evaluated. If first expression is true the value of a variable print and expression is false then value of b variable print. Assignment data operator: A value can be stored in a variable with the user of assignment operator. the assignment operator= is used in assignment expression and assignment statement. The operand on the left hand side should be a variable, while the operand on right hand side can be only variable, constant or expression. i.e x=a, a= =1, s=s+y+2,temp=a; Increment Data Operator: This operator are used to increase the value in a variable. It has two type Pre Increment and Post Increment. Pre Increment operator (++a) show the first increase after print. Post Increment operator (a++) show the fist print after increase. Decrement Data Operator : This operator are used to decrease the value in a variable. It has two type Pre Decrement and Post Decrement. Pre Decrement operator (--a) show the first decrease after print. Post Decrement operator (a--) show the fist print after decrease. Bitwise Data Operator : This operator are used to manipulate the data at bit level. It are used for operations on individual bits. Bitwise operators operate on integer only. If a=8 b=15 ~ ones complement ~a is -19 & Bitwise And a&b is 8 | bitwise OR a|b is 15 ^ bitwise exclusive OR a^b is 7 >> shift right a>>3 is 1 << shift left a<<3 is 64 Comma operator: The comma operator is used to permit different expression to appear in used. The expression are separated by comma operator. the serrated expression are evaluated form left to right. The type and the value of right most express is the type and value of the compound expression. For example a=8,b=7,c=9,s=a+b+c;

11

Sizeof operator: It is an unary operator. this operator gives the size of its operands in the form of bytes. The operand can be variable, constant or any data types. Sizeof(int), Sizeof(float), Sizeof(doubel), Sizeof(char). Unary Operator : This operator are used to one operant. i.e a++, --a Binary Operator: This operator are used to two operant. i.e a+b Ternary Operator: This operator are used to three operant. i.e a+b+c Question 22. What is Expression Answer An Expression is composed of one or two operation. The objects of the operation are refereed to as operands. The operation are represented by operator. therefore operator , constant and variable are the constituents of expression. The expression in java can be of any type arithmetic expression, relation expression or compound expression. Question Show the program sum of two nos. #include<stdio.h> #include<conio.h> void main() { int a,b,result; clrscr( ); printf(Enter the value a=); scanf(%d,&a); printf(Enter the value b=); scanf(%d,&b); result=a+b; printf(Answer=%d,result); getch( ); } Question Show the program of size of data types. #include<stdio.h> #include<conio.h> void main() { clrscr(); printf("\n size of integer=%d",sizeof(int)); printf("\n size of longinteger=%d",sizeof(long int)); printf("\n size of float=%d",sizeof(float)); printf("\n size of double=%d",sizeof(double)); printf("\n size of char=%d",sizeof(char)); getch(); } 12

Question Calculate the simple interest. #include<stdio.h> #include<conio.h> void main() { float p,r,t,result; clrscr(); printf("enter principale,rate,time "); scanf("%f %f %f",&p,&r,&t); result=(p*r*t)/100; printf("Ans=%f",result); getch(); } Question Calculate the average of three subjects. #include<stdio.h> #include<conio.h> void main() { float hindi,eng,math,result; clrscr(); printf("enter marks of hindi,eng,math "); scanf("%f %f %f",&hindi,&eng,&math); result=(hindi+eng+math)/3; printf("Ans=%f",result); getch(); } Question Show the program reverse of two digits. #include<stdio.h> #include<conio.h> void main() { int num,div,rem,result; clrscr(); printf("enter number of two digit=); scanf("%d,&num); rem=num%10; div=num/10; result=rem*10+div; printf("Ans=%d,result); getch(); }

13

Question Show the swapped two values. #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter value a "); scanf("%d",&a); printf("Enter value b "); scanf("%d",&b); c=a; a=b; b=c; printf("\n a=%d",a); printf("\n b=%d",b); getch(); } Question Show the square root of any no. #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,result; clrscr(); printf("Enter value a "); scanf("%f",&a); result=sqrt(a); printf("\n b=%f",result); getch(); } Question Show the ASCII Code #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter value a "); scanf("%d",&a); printf("\n %d=%c",a); 14

getch(); } Question Show the lower case #include<stdio.h> #include<conio.h> void main() { char a,result; clrscr(); printf("Enter one character of upper case "); scanf("%c",&a); result=a+32; printf("\n lower case =%c",result); getch(); } Question Show the pre increment or post increment program #include<stdio.h> #include<conio.h> void main() { int a=10,b=20;; clrscr(); printf("\n post increment =%d",a++); printf("\n value of a=%d",a); printf("\n pre incrementt=%d",++b); printf("\n value of b=%d",b); getch(); }

UNIT-2
Question 1. Answer What are control flow statement? In C program, statement are executed in sequence but sometimes we want to use a condition for executing only a part of a program. Sometimes there is a situation in which we may want to execute many statements many times. These control statement are helpful for us to specify the order in which the different instructions in a program are to be executed. By this we can determined the flow of control. These control statement defines how the control is transferred to other part of the program. What is decision making? Explain its statements? Statement test a particular condition, if the condition is true then statement1 will be execute. If the condition is false the statement-2 will b e execute.

Question 2. Answer

15

Statement may be implemented in different form depending on complexity of condition to be tested. Simple if statement If else statement Else if ladder Nested if else statement Simple if statement : Syntax: If (condition) Statement; Write a program to show the simple if statement.

Question 3. Answer #include<stdio.h> #include<conio.h> void main( ) { int a; printf(Enter value a=); scanf(%d,&a); if (a= =0) printf("This is Zero no.); getch( ); }

If else statement: Syntax: if (condition) Statement-1; Else Statement-2; Question 4. Enter a number show the positive or negative. Answer #include<stdio.h> #include<conio.h> void main( ) { int number; clrscr(); printf(Enter the number=); scanf(%d,&number); if(number>0) printf("Possitive nos); else printf("Negative nos.); getch( ); }

16

Question 5. Write a program to show big of the numbers with if else statement. Answer #include<stdio.h> #include<conio.h> void main( ) { int a,b; clrscr( ); printf(Enter the value a=); scanf(%d,&a); printf(Enter the value b=); scanf(%d,&b); if(a>b) printf(a is big); else printf( b is big); getch(); } } Nested if statement: Syntax: If(condition - 1) statement-1; else If(condition - 2) statement-2; else If(condition - 3) statement-3; else If(condition - n) statement-n; Question 6. Write a program to show the Sunday/ Monday with nested if statement. #include<stdio.h> #include<conio.h> void main( ) { int a; printf(Enter the value a=); scanf(%d,&a); if(a= =1||a==8||a= =15||a= =22||a= =29) printf("Monday"); else if(a= =2||a= =9||a= =16||a= =23||a= =30) printf ("Tuesday"); else 17

if(a= =3||a= =10||a= =17||a= =24) printf ("Wednesday"); else if(a= =4||a= =11||a= =18||a= =25) printf ("Thursday"); else if(a= =5||a= =12||a= =19||a= =26) printf ("Friday"); else if(a= =6||a= =13||a= =20||a= =27) printf ("Saturday"); else if(a= =7||a= =14||a= =21||a= =28) printf ("Sunday"); getch( ); } Question 7. Enter a number show the even no or odd no. Answer #include<stdio.h> #include<conio.h> void main( ) { int number; clrscr(); printf(Enter the value a=); scanf(%d,&number); if(number%2= =0) printf("Even no); else printf("Odd no); getch( ); } Question 9. Write a program to calculate the big three nos. with AND operat Answer #include<stdio.h> #include<conio.h> void main( ) { int a,b,c; clrscr(); printf(Enter the value a=); scanf(%d,&a); printf(Enter the value b=); scanf(%d,&b); printf(Enter the value c=); 18

scanf(%d,&c); if(a>b && a>c) printf("a is big"); else if(b>a && b>c) printf(" b is big"); else print(" c is big"); getch( ); } Question 10. What is switch statement. Answer switch statement is a multi directional conditional control statement. Some time there is need in a program to make choice many numbers. This choice used for switch statement. It is mostly used for making menus. Syntax: Switch(choice) { Case 1: Statement-1; Break; Case 2: Statement-2; Break; Case n: Statement-n; } Question 11. Make a program with switch statement (choice int pass.) #include<stdio.h> #include<conio.h> void main( ) { int a,b,result,choice; clrscr(); printf(Enter the value a=); scanf(%d,&a); printf(Enter the value b=); scanf(%d,&b); printf(Enter the choice=); scanf(%d,&choce); switch(choice) { case 1: result=a+b; break; case 2: result=a/b; 19

break; case 3: result=a*b; break; case 4: result=a-b; } printf("ans=%d ",result); getch(); } Question 12. Make a program with switch statement (choice char pass.) #include<stdio.h> #include<conio.h> void main() { char choice; clrscr( ); printf("enter the choice="); scanf("%c",&choice); switch(choice) { case 'm': printf("Mumbai"); break; case 'b': printf("Bombay"); break; case 'd': printf("Delhi"); } getch(); } Question 1. Answer What is looping? A statement that allow a set of instructions to be performed repeatedly are iteration statement. They are also called loops. Java provide three loops. For loop While loop Do while loop For loop: For loop has several capabilities. In for loop you want to have a group of statement to be executed number of times. For loop has three expressions. Syntax: For(initialization; condition; increment or decrement) { Statement } Nested for statement: 20

A for loop may contain another loop in its body. This form of a loop is called nested loop. In nested loop has divide two category inner loop and outer loop. First is outer loop and second is inner loop. For (initialization;condition;increment/decrement) { For (initialization;condition;increment/decrement) { Statement ; } } Question Write a program with goto statement #include<stdio.h> #include<conio.h> void main() { clrscr(); int i; i=1; ABC: printf("\n%d",i); i++; if(i<=10) goto ABC; getch(); } Question show the hello 10 times. #include<stdio.h> #include<conio.h> void main( ) { int i; for(i=1;i<=10;i++) { printf( \n Hello); } getch( ); } Question show the counting 1 to 10 #include<stdio.h> #include<conio.h> void main( ) { int i; for(i=1;i<=10;i++) { 21

printf( \n %d,i); } getch( ); } Question show counting , square, cube. #include<stdio.h> #include<conio.h> void main( ) { int square,cube,i; for(i=1;i<=10;i++) { square=i*i; cube=i*i*i; printf(\n %d \t %d \t %d , i, square, cube); } getch( ); } Question Find out the factorial? #include<stdio.h> #include<conio.h> void main( ) { int i,fact=1,num; printf(Enter any number=); scanf(%d,&num); for(i=1;i<=num;i++) { fact=fact*i; } printf(\n Factroial=%d,fact); getch( ); } Question show the table 2 to 10. #include<stdio.h> #include<conio.h> void main( ) { int i,j,t; for(i=1;i<=10;i++) { printf(\n); for(j=2; j<=10; j++) 22

{ t=i*j; printf("%d * %d = %d,j,i,t); } } getch( ); } Question Show stars * ** *** #include<stdio.h> #include<conio.h> void main( ) { int i,j; for(i=1;i<=10;i++) { printf(\n ); for(j=1; j<=i; j++) { printf(*); } } getch( ); } Question Show stars * *** ***** #include<stdio.h> #include<conio.h> void main( ) { int i,j,s; for(i=1;i<=10;i++) { printf(\n); for(s=i;s<=40; s++) { printf( ); } for(j=1; j<=(2*i)-1;j++) { printf(*); } } getch( ); } 23

Question You have five nos. How many odd nos. or how many even nos.? #include<stdio.h> #include<conio.h> void main( ) { int even=0,odd=0,num,i; for( i=1;i<5;i++) { printf(Enter Five nos.=); scanf(%d,&num); if(num%2= =0) even++; else odd++; } printf("even number=%d",even); printf("odd number=%d",odd); getch( ); } Question You have five nos. How many +vs. nos. or how many -vs. nos.? #include<stdio.h> #include<conio.h> void main( ) { int pos=0,neg=0,num,i; for( i=1;i<5;i++) { printf(Enter Five nos.=); scanf(%d,&num); if((num>0) pos++; else neg++; } printf("pos number=%d",pos); printf("neg number=%d",neg); getch( ); } Question show the a number prime no. or not prime no. #include<stdio.h> #include<conio.h> void main( ) { 24

int i,num; printf(Enter the value a=); scanf(%d,&num); for(i=2;i<=num;i++) { if(num%i= =0) break; } if(num= =i) printf(Prime no); else printf(Not Prime no.); getch( ); } Question show 1 to 100 prime nos. #include<stdio.h> #include<conio.h> void main( ) { int i,num; for(num=2;num<=100;num++) { for(i=2;i<=num;i++) { if(num%i= =0) break; } if(num= =i) printf(\n %d ,num); } getch( ); } Question Calculate the compound Interest. #include<stdio.h> #include<conio.h> void main() { float p,r,t,temp=1,i; clrscr(); printf("enter Principle="); scanf("%f",&p); printf("enter Rate="); scanf("%f",&r); printf("enter Time="); scanf("%f",&t); for(i=1;i<=t;i++) 25

{ temp=temp*(1+r/100); } printf("compound Interest=%f",p*temp); getch(); } Question Show a chess Board. #include<stdio.h> #include<conio.h> void main() { char black,white; int i,j; clrscr(); black=32; white=219; for(i=1;i<=64;i++) { for(j=1;j<=64;j++) { if((i+j)%2==0) printf("%c",black); else printf("%c",white); } printf("\n"); } getch(); } Question Show ASCII Codes (65 to 110) #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for(i=65;i<=100;i++) { printf("\n %d = %c ",i,i); } getch(); } Question 11. What is while loop. Give its example? Answer The while loop is used when the number of times the loop has to executed is not known in advance. In while loop repeat where end point is not fix. Syntax: While (condition) { 26

Statement; } Question 1. Write a Program sum of n digits? #include<stdio.h> #include<conio.h> void main() { int num,rem,sum=0; clrscr(); printf(" enter any numbe="); scanf("%d",&num); while(num>0) { rem=num%10; sum=sum+rem; num=num/10; } printf("ans=%d",sum); getch(); } Question 2. Write a Program reverse of n digits? #include<stdio.h> #include<conio.h> void main() { int num,rem,sum=0; clrscr(); printf(" enter any numbe="); scanf("%d",&num); while(num>0) { rem=num%10; sum=sum*10+rem; num=num/10; } printf("ans=%d",sum); getch(); } Question 3. Write a program show palindrome no. or not palindrome no. #include<stdio.h> #include<conio.h> void main() { int num,rem,sum=0,i; 27

clrscr(); printf(" enter any numbe="); scanf("%d",&num); i=num; while(num>0) { rem=num%10; sum=sum*10+rem; num=num/10; } printf("ans=%d",sum); if(sum==i) printf("\n Palindrom no."); else printf("\n Not Palindrom no."); getch(); } Question 4. Write a program show Armstrong no. or not Arm strong no.

#include<stdio.h> #include<conio.h> void main() { int num,rem,sum=0,i; clrscr(); printf(" enter any numbe="); scanf("%d",&num); i=num; while(num>0) { rem=num%10; sum=sum+(rem*rem*rem); num=num/10; } printf("ans=%d",sum); if(sum==i) printf("\n Arm Strong no."); else printf("\n Not Arm Strong no."); getch(); } Question 5. Show the 11 to 500 Arm strong nos. #include<stdio.h> #include<conio.h> void main() { int num,rem,sum,i; 28

clrscr(); for(i=11;i<=500;i++) { num=i; sum=0; while(num>0) { rem=num%10; sum=sum+(rem*rem*rem); num=num/10; } if(sum= =i) printf("\n %d",i); } getch(); } Question 6. Show the 11 to 500 Palindrome nos. #include<stdio.h> #include<conio.h> void main() { int num,rem,sum,i; clrscr(); for(i=11;i<=500;i++) { num=i; sum=0; while(num>0) { rem=num%10; sum=sum*10+rem; num=num/10; } if(sum= =i) printf("\n %d",i); } getch(); } Question 7. Write a program to convert the Decimal no to Binary no. #include<stdio.h> #include<conio.h> void main() { int num,rem,sum=0,i,k=1; clrscr(); printf("Enter Decial no."); 29

scanf(" %d",&num); while(num>0) { rem=num%2; sum=sum+rem*k; k=k*10; num=num/2; } printf("\n Binary no=%d",sum); getch(); } Question 8. Write a program to convert the Binary no. to Decimal no.

#include<stdio.h> #include<conio.h> void main() { int num,rem,sum=0,i,k=1; clrscr(); printf("Enter Decial no."); scanf(" %d",&num); while(num>0) { rem=num%10; sum=sum+rem*k; k=k*2; num=num/10; } printf("\n Binary no=%d",sum); getch(); } Question 9. Show the 1 to 50 Decimal to Binary nos.

#include<stdio.h> #include<conio.h> void main() { int num,rem,sum,i,k; clrscr(); for(i=1;i<=50;i++) { num=i; sum=0; k=1; while(num>0) { rem=num%2; 30

sum=sum+rem*k; k=k*10; num=num/2; } printf("\n %d =%d",i,sum); } getch(); } Question 10. Write a program to convert the Decimal no to Octal no. #include<stdio.h> #include<conio.h> void main() { int num,rem,sum=0,i,k=1; clrscr(); printf("Enter Decial no."); scanf(" %d",&num); while(num>0) { rem=num%8; sum=sum+rem*k; k=k*10; num=num/8; } printf("\n Binary no=%d",sum); getch(); } Question Answer What is do while loop? Give its example? Do loop is condition evaluated at the end. The body of the loop is executed at least once in this statement. Syntax: Do { Statement; } While(Condition); Question 1. Write a program to convert the Binary no. to Decimal no. Question 2. Write a Program reverse of n digits? #include<stdio.h> #include<conio.h> void main() { int num,rem,sum=0; clrscr(); printf(" enter any numbe="); 31

scanf("%d",&num); do { rem=num%10; sum=sum*10+rem; num=num/10; } while(num>0); printf("ans=%d",sum); getch(); }

Question Answer

What is difference between while loop and do while loop? 1. In while loop condition is tested executed the body of the loop or In do loop condition is tested after executing the body of the loop. 2. while loop may not be executed at all. Do while lop is executed at least once. 3. In while loop if condition is not satisfy then the statement will not execute. In do while loop if condition is not satisfy the statement will execute.

Unit-3
Question 1. Answer What is Array. How many types of Array? Array is a collection of similar data types of char, int , float etc. array are a data structure which hold sequence of objects all of which have same type. Array has two types 1. One Dimensional Array 2. Two Dimensional Array One Dimensional Array: An array is a set of elements of the same type represented by a single variable name. One Dimensional Array is known as vector. Its declared in int number[]={10,30,60,50}; or int number[10]; syntax: data type arrayname [size]; Two Dimensional Array Two Dimensional Array are identified by two subscript.Two Dimensional Array is know as matrix. Its declared in this form int [row][column]; int a[2][2],b[2][2],c[2][2]; syntax datatype arrayname[rowsize][columnsize]; Question 2. Write a program sum of five nos. with one dimensional array.

32

#include<stdio.h> #include<conio.h> void main() { int i,sum=0,a[5]; clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d] elements=",i); scanf(" %d",&a[i]); } for(i=0;i<10;i++) { sum=sum+a[i]; } printf("\n sum=%d",sum); getch(); } Question Write a program big no. of five nos.

#include<stdio.h> #include<conio.h> void main() { int i,j,big,a[5]; clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d] elements=",i); scanf(" %d",&a[i]); } big=a[5]; for(i=0;i<5;i++) { if(big<a[i]) big=a[i]; } printf("\n big=%d",big); getch(); } Question Write a program small no. of five nos. #include<stdio.h> #include<conio.h> void main() { int i,j,small,a[5]; 33

clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d] elements=",i); scanf(" %d",&a[i]); } small=a[1]; for(i=0;i<5;i++) { if(small>a[i]) big=a[i]; } printf("\n small=%d",small); getch(); } Question Show a Program Array Sort in Ascending Order. #include<stdio.h> #include<conio.h> void main() { int i,j,t,a[5]; clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d] elements=",i); scanf(" %d",&a[i]); } for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("\n Array Sorted\n"); for(i=0;i<5;i++) { printf("\n %d",a[i]); } getch(); }

34

Question Show a Program Array Sort in Descending Order. #include<stdio.h> #include<conio.h> void main() { int i,j,t,a[5]; clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d] elements=",i); scanf(" %d",&a[i]); } for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("\n Array Sorted\n"); for(i=0;i<5;i++) { printf("\n %d",a[i]); } getch(); } Question Show the Array Merged. #include<stdio.h> #include<conio.h> void main() { int i,j,a[5],b[5],c[10]; clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d] elements=",i); scanf(" %d",&a[i]); } printf("\n"); for(j=0;j<5;j++) { 35

printf("Enter b[%d] elements=",i); scanf(" %d",&b[j]); } for(i=0;i<5;i++) { c[i]=a[i]; } for(j=0;j<5;j++) { c[i+j]=b[j]; } for(i=0;i<10;i++) { printf("\n %d",c[i]); } getch(); } Question Search the number to any location. #include<stdio.h> #include<conio.h> void main() { int i,num,a[5]; clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d] elements=",i); scanf(" %d",&a[i]); } printf(" Enter number to search="); scanf(" %d",&num); for(i=0;i<5;i++) { if(a[i]==num) { printf("Location =a[%d] ",i); break; } } getch(); } Question Delete the number to any location. #include<stdio.h> #include<conio.h> void main() { int i,num,a[5],loc; 36

clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d]elements=",i); scanf("%d",&a[i]); } printf(" Enter Location="); scanf("%d",&loc); num=a[loc]; printf("\n Delete number=%d",num); printf("\n"); for(i=loc;i<4;i++) { a[i]=a[i+1]; for(i=0;i<4;i++) printf("\n a[%d]=%d",i,a[i]); } getch(); } Question Insert a location to any number.

#include<stdio.h> #include<conio.h> void main() { int i,num,a[5],loc; clrscr(); for(i=0;i<5;i++) { printf("Enter a[%d]elements=",i); scanf("%d",&a[i]); } printf(" Enter Location and num"); scanf("%d%d",&loc,&num); for(i=4;i>loc;i--) a[i]=a[i-1]; a[loc]=num; printf("location at item is inersert\n"); for(i=0;i<5;i++) printf("\n%d=%d",i,a[i]); getch(); } Question Show the Transpose of two matrix. #include<stdio.h> #include<conio.h> void main() 37

{ int i,j,a[3][3],b[3][3]; clrscr(); printf("Enter a elements \n "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { b[i][j]=a[j][i]; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\t %d",b[i][j]); } printf("\n"); } getch(); } Question Show the Diagonal sum of two matrix #include<stdio.h> #include<conio.h> void main() { int i,j,a[3][3],sum=0; clrscr(); printf("Enter a elements \n "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { 38

if(i==j) sum=sum+a[i][j]; } printf("\n %d",sum); } getch(); } Question Show the matrix Addition. #include<stdio.h> #include<conio.h> void main() { int i,j,a[2][2],b[2][2],sum[2][2]; clrscr(); printf("Enter a elements \n "); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&a[i][j]); } } printf("Enter b elements \n "); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&b[i][j]); } } printf("\n Matrix Addition\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { sum[i][j]=a[i][j]+b[i][j]; printf("\t%d",sum[i][j]); } printf("\n "); } getch(); } Question Show the matrix multiplication. #include<stdio.h> #include<conio.h> void main() { 39

int i,j,a[2][2],b[2][2],s[2][2],k; clrscr(); printf("Enter a elements \n "); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&a[i][j]); } } printf("Enter b elements \n "); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { s[i][j]=0; for(k=0;k<2;k++) { s[i][j]=s[i][j]+a[i][k]*b[k][j]; } } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("\t %d",s[i][j]); } printf("\n"); } getch(); }

Unit-4
Question Answer What is function. How many types of function. A function is a self contained block of code that performs a particular task. There are many features of function. 1. function are divided into small parts is call sub program.

40

2. It is easy to understand 3. It is easy to error find out. 4. It is easy to modify. 5. Efficient use of memory. 6. They are team work. 7. Reverse of code. Two type functions 1. Library function 2. user defined function 1. Library function: library functions are in-build. These are not required to written by user. For example sqrt, pow. 2. User defined function: user defined function have to be developed by the user. Main( ) is a user defined function which we will apply many different formulas. The general form of all function FunctionType functionnmae (Agument list) int square(int a) { Local variable declaration Statement-1; } Return Statement: A function may or may not return value to the calling function. If it does, it is done through the return statement. The syntax of return statement is return expression. Formal parameter: These are variable which are defined in main( ) functions. These are used in body of the function. Square (int a ,int a) -------- Formal paratmeter Actual parameter: These are variable which are defined in main( ) function main( ) { int x,y; } Local variable: Any variable declared inside a function is called local variable. For example int sum (int a, int b) { int c; c=a+b; ------ Local variable return c; } Global variable : Any variable declared outside the main program is called global variable. int sum (int a, int b) --------- Global variable { int c; c=a+b; 41

return c; } Parameter passing in function by two approaches. 1. call by value 2. call by reference call by value : the call by value method copies the value of actual parameter into formal parameter, that is the function creates its own copy of arguments value. Call by reference: This method passes the addresses or references of the actual parameter to the called function. The actual and formal parameter share the same memory location. Question Difference between call by value and call by reference. Call by value 1. 2. 3. 4. 5. The formal and actual parameter are two different variable. The formal parameter are not declared as pointer Actual parameter is read only For example int x The value of formal parameter is changed the value of actual parameter in not changed Call by reference The formal and actual parameter are same though their names may be different The formal parameter are declared as pointer Actual parameter is read or write For example in *x The value of formal parameter is changed the value of actual parameter in changed

Recursion: Recursion refer to the process in which function calls itself. Recursion is a powerful technique using which is very close to their natural statement. Function has two category. 1. without argument without return 2. with argument with return 1. without argument without return: #include<stdio.h> #include<conio.h> star() { printf("\n *********\n"); } symbol() { printf("\n ##########\n"); } void main() { 42

clrscr(); star(); printf("My Name is Mrs. Seema"); symbol(); printf("My Qualification is MSC"); star(); printf("My City is Kaithal"); symbol(); getch(); } 2. with argument with return: #include<stdio.h> #include<conio.h> int sum(int x, int y) { int z; z=x+y; return z; } void main() { int a,b,result; clrscr(); printf("Enter two nos. "); scanf("%d %d",&a,&b); result=sum(a,b); printf("Ans=%d",result); getch(); } Question show star with char #include<stdio.h> #include<conio.h> void star(char ch) { for(int i=1;i<=20;i++) printf("%c",ch); printf("\n"); } void main() { clrscr( ); star('+'); star('-'); star('&'); getch( ); } Question show the star with n number. #include<stdio.h> 43

#include<conio.h> void star(char ch, int n) { for(int i=1;i<=n;i++) printf("%c",ch); printf("\n"); } void main( ) { int num; char s; clrscr( ); printf(Enter any symbol ); scanf(%c,&s); printf(Enter any number ); scanf(%d,&num); star(s,num); getch(); } Question Show the swapped two nos. #include<stdio.h> #include<conio.h> int swap(int x, int y) { int t; t=x; x=y; y=t; printf("x=%d",x); printf("\n y=%d",y); } void main() { int a,b; clrscr(); printf("Enter value a="); scanf("%d",&a); printf("Enter value b="); scanf("%d",&b); swap(a,b); getch(); }

Unit-5
Question What is pointer. Answer Pointer is used to store the address of variable in computer memory. Computer use their memory for storing the instruction of a program, as well 44

as the value of the variable that are associated with it. The computer memory is a sequential collection of storage cells, each cell has a unique address associated with it. Question Show a program with one value address. #include<stdio.h> #include<conio.h> void main() { int i=4; clrscr(); printf("\n value of i=%u",i); printf("\n address of i=%u",&i); printf("\n value of i=%u",i); getch(); } Question Show a program with two value address. #include<stdio.h> #include<conio.h> void main() { int i=4,*j; j=&i; clrscr(); printf("\n value of j=%u",*j); printf("\n address of i in j=%u",j); printf("\n address of j=%u",&j); getch(); } Question Show a program with three value address. #include<stdio.h> #include<conio.h> void main() { int i=4,**k,*j; k=&j; j=&i; clrscr(); printf("\n value of k=%u",**k); printf("\n address of j in k=%u",k); printf("\n address of k=%u",&k); getch(); } Question show the pointer with array. #include<stdio.h> #include<conio.h> 45

void main() { int a[]={10,20,30,40}; clrscr(); for(int i=0;i<=5;i++) { printf("\n element is %d address is %u",i,&a[i]); } getch(); } Question Show the swapped two nos. #include<stdio.h> #include<conio.h> int swap(int *x, int *y) { int t; t=*x; *x=*y; *y=t; printf(" x=%d",*x); printf("\n y=%d" , *y); } void main() { int a,b; clrscr(); printf("Enter value a="); scanf("%d",&a); printf("Enter value b="); scanf("%d",&b); swap(&a,&b); getch(); }

Unit-6
Question What is structure. Answer Structure is composed of data items that may be of different data types the data items of a structure are called fields. Each fields has a data types and field name. structure is a user defined data type. It is used for struct keyword. It can store the total memory space of data type. Syntax: Struct student { Datatype fieldname; Datatype fieldname;

46

Datatype fieldname; }object delaration; #include<stdio.h> #include<conio.h> struct student { char name[10]; int rollno; }a,b; Question Show a simple structure. void main() { clrscr(); printf("Enter name and rollno of student "); scanf("%s %d",&a.name,&a.rollno); printf("Enter name and rollno of student "); scanf("%s %d",&b.name,&b.rollno); printf("\n %s \t %d",a.name,a.rollno); printf("\n %s \t %d",b.name,b.rollno); getch(); } Question Show the two structure. #include<stdio.h> #include<conio.h> struct student { char name[10]; int rollno; }a; struct employee { char name[10]; char post[10]; }b; void main() { clrscr(); printf("Enter name and rollno of student "); scanf("%s %d",&a.name,&a.rollno); printf("Enter name and post of employee "); scanf("%s %s",&b.name,&b.post); printf("\n %s \t %d",a.name,a.rollno); printf("\n %s \t %s",b.name,b.post); getch(); 47

} Question Show the structure with Array. #include<stdio.h> #include<conio.h> struct student { char name[10]; int rollno; }a[4]; void main() { int i; clrscr(); for(i=0;i<4;i++) { printf("\n Enter name and rollno of student "); scanf("%s %d",&a[i].name,&a[i].rollno); } for(i=0;i<4;i++) { printf("\n %s \t %d",a[i].name,a[i].rollno); } getch(); } Question What is union. Answer union is composed of data item that may be different data types. The data item of a union are called field. Each field has a data type. It is used for union keyword. Union can store the highest value of data in memory Location. Syntax Union unionname { Datatype field name; Datatype fieldname; } Question Show the program with union. #include<stdio.h> #include<conio.h> struct student { char name[10]; int rollno; }s; struct employee { 48

char name[10]; char post[10]; }e; union customer { char name[10]; char address[10]; }c; union book { char name[10]; char author[10]; }b; void main() { int ch; clrscr(); printf("\n 1. Student record"); printf("\n 2. Employee record"); printf("\n 3. Customer record"); printf("\n 4. Book record"); while(1) { printf("\n Enter Choice "); scanf("%d",&ch); if(ch= =1) { printf("Enter name and rollno of student "); scanf("%s %d",&s.name,&s.rollno); printf(" \n %s \t %d\n",s.name,s.rollno); } if(ch= =2) { printf("Enter name and post of employee "); scanf("%s %s",&e.name,&e.post); printf(" \n %s \t %s \n",e.name,e.post); } if(ch= =3) { printf("Enter name and address of customer "); scanf("%s %s",&c.name,&c.address); printf(" \n %s \t %s\n ",c.name,c.address); } if(ch= =4) { printf("Enter name and author of book "); scanf("%s %s",&b.name,&b.author); printf("\n %s \t %s \n ",b.name,b.author); 49

} if(ch= =5) break; } getch(); } Question Answer What is String. String are use to manipulate text, string as word and sentence. String constant is a sequence of character enclosed by double quotes. String variable is an array of character.

Question show a string length. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20]; int n; clrscr( ); printf("Enter a string "); gets(str); n=strlen(str); printf("length=%d",n); getch(); } Question Show string reverse. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20]; clrscr(); printf("Enter a string "); gets(str); strrev(str); printf("reverrse=%s",str); getch(); } Question Show string concating. #include<stdio.h> #include<conio.h> #include<string.h> void main() 50

{ char str1[20]; char str2[20]; clrscr(); printf("Enter first string "); gets(str1); printf("Enter second string "); gets(str2); strcat(str1,str2); printf("string concating=%s",str1); getch(); } Question Show string comparison. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[20]; char str2[20]; int length; clrscr(); printf("Enter first string "); gets(str1); printf("Enter second string "); gets(str2); length=strcmp(str1,str2); if(length<0) printf("Less String"); else if(length>0) printf("More String"); else if(length==0) printf("Equal String"); getch(); } Question Show String Upper case. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20]; clrscr(); printf("Enter first string "); 51

gets(str); printf("Upper Case=%s",strupr(str)); getch(); } Question Show String Lower Case. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20]; clrscr(); printf("Enter first string "); gets(str); printf("Lower Case=%s",strlwr(str)); getch(); } Question Show String copy. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[20]; char str2[20]; clrscr(); printf("Enter first string "); gets(str1); printf("Enter second string "); gets(str2); strcpy(str1,str2); printf(\n%sstr1); printf(\n%sstr2); getch(); } Question Show String Palimdrom or not palimdrom. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[20]; char str2[20]; clrscr(); printf("Enter first string "); gets(str1); strcpy(str2,str1); 52

strrev(str2); if(strcmp(str1,str2)==0) printf("Palimdrom String"); else printf("\n Not Palimdrom String"); getch(); } Question Show student names (string ) with array. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[20][20]; int n,i; clrscr(); printf("Enter no of student "); scanf("%d",&n); printf("ente %d student name\n",n); for(i=0;i<n;i++) { scanf("%s",&name[i]); } printf("\n list of student"); for(i=0;i<n;i++) { printf("\n %s",name[i]); } getch(); } Question Answer What is macro. Macro is called preprocessor. It is a collection f special statement called directives that executed at the begging of the compilation process.

Question Show sum of two nos. with macro. #include<stdio.h> #include<conio.h> #define sum(a,b) (a+b) void main() { int x,y,z; clrscr(); printf("ente two nos"); scanf("%d %d",&x,&y); z=sum(x,y); printf("ans=%d",z); 53

getch(); } Question Show the area of circle. #include<stdio.h> #include<conio.h> #define pi 3.14 void main() { float area,r; clrscr(); printf("ente the radius "); scanf("%f",&r); area=pi*r*r; printf("ans=%f",area); getch(); } Question Show the square. #include<stdio.h> #include<conio.h> #define square(a) (a*a) void main() { int x,result; clrscr(); printf("ente the radius "); scanf("%d",&x); result=square(x); printf("ans=%d",result); getch(); } Question Show a if statement #include<stdio.h> #include<conio.h> #define range (a>0&&a<=100) void main() { int a; clrscr(); printf("ente the range 0 to 100 "); scanf("%d",&a); if(range) printf("with in the range"); else printf("Out of the range"); getch(); }

Unit-7
54

Question Answer

What is file. A file is a place on disk where a group of related data is stored. It is necessary to have a more flexible approach where data can be stored on disk and read whenever necessary, without destroying the data. C support a number of functions that have the ability to perform basic file operations which include. * naming a file * opening a file * reading data from a file * writing data to a file * closing a file There are two different ways to perform file operations in C. the first one is known as the low level I/O ad use UNIX system calls. The second method is referred to as the high level I/O operations ad uses functions in Cs standard I/o library Some high level I/o functions are fopen( ) fclose( ) getch( ) putch( ) fprintf( ) fscanf( ) getw( ) putw( ) fseek( ) ftell( ) Rewing( ) Opens an existing file for use Closes a file which has been opened for use Reads a character from file Write a character to a file Writes a set of data values to a file Reads a set of data values from a file Reads an integer from file Writes an integer to a file Sets the position to a desired point in the file Gives the current position in the file Sets the position to the beginning of the file

Question Answer

Explain the input/output operations on files. There are three input/output operations on files. 1. getc and putc functions 2. getw and putw functions 3. fprintf and fscanf functions 1. getc and putc functions: The simplest file I/O functions are getc and putc. These are similar to getchar and putchar functions. These can handle one character at a time. Assume that a file is opened with mode What and file pointer fp1. Then the statement will be: putc*c,fp1) It will write the character contained in the character variable c to the file associated with file pointer fp1. in the same way getc is used to read a character from a file that has been opened in read moce. For example the statement c=getc(fp2); would read a character from the file whose pointer is fp2.

55

2. getw and putw functions: The getw is integer oriented function. It is used to red the integer value. The syntax is getw(fp); Where fp is file pointer. The putw function is used to put nteger value or write integer value. This function is useful when you deal with only integer data . syntax putw(<integer>,<file pointer>); 3. fprintf and fscanf function: These function can handle a group of mixed data. The functions fprintf and fscanf perform I/O operations that are identical to the familiar printf and scanf functions. The first arguments of these functions is a file pointer which specifies the files to be used. The general form of fprintf is fprintf(fp,control string,list); where fp is a file pointer which is associated with a file that has been opened for writing for the item in first list. The list may include variable, constants and strings. For exaple fprintf(f1,%s %d %f,name,age,7.5); name is an array variable of char type and age is an int type variable the geneal format of fscanf is fscanf(fp,control string,list); this statement would cause the reading of items in the list from the file specified by fp, according to the specifications contained in the control string. For example fscanf(f2,%s %d %d, item ,&quantity); Question Answer Which functions are used in Random Access of files. A binary file may be accessed randomly by using function offset( ) and fopen( ), then the file. Whenever we write a record to a file, the pointer moves to next record automatically when we write again it starts writing from the location where file pointer is pointing. There are many functions are used in Random Access of files. 1. ftell( ) function: This function takes a file pointer and return a number of type long, that corresponds to the current position. This function is useful in saving the current position of a file. Syntax n=ftell(fp); n would give the relative offset of the current position. It means that n bytes have been read. 2. rewind( ) function: This function will help you in reading more than once, without having to close and open the file. Rewind( ) takes a file pointer and reset the position to the start of the file. Syntax rewind(fp); n=ftell(fp); 3. fseek function: By using this function we can change the file pointer such that it want to read. Syntax fseek(<fp>,<offset>,<mode>); 4. ferror( ) function: This function returns a non zero value if same error occurs during input function is file pointer. 5. perror( ) function: Function feror( ) does not give any information about what the error is function perror( ) gives us valueable information about what the exact error is. Function perror( ) takes as argument a character string.

56

Das könnte Ihnen auch gefallen