key words - wordpress.com€¦  · web viewcontrol statements: ‘c’ is considered as a...

84
UNIT-II KEY WORDS There are 32 keywords in C Turbo c++ Operators [] () . -> ++ -- & * + - ~ ! sizeof / % << >> < > <= >= == != ^ | && || ?: = *= /= %= += -= <<= >>= &= ^= |= , # ## < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equality != Inequality & Bitwise AND ^ Bitwise NOR | Bitwise OR && Logical AND || Logical OR ?: Conditional expression = *= /= %= += -= &= ^= |= <<= >>= Assignment operators , Comma operator

Upload: others

Post on 27-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

UNIT-IIKEY WORDS

There are 32 keywords in C

Turbo c++ Operators

[] () . -> ++ -- &* + - ~ ! sizeof /% << >> < > <= >=== != ^ | && || ?:= *= /= %= += -= <<=

>>= &= ^= |= , # ##

BasicsGeneral characteristics of C/History

< Less than<= Less than or equal to> Greater than

>= Greater than or equal to

== Equality!= Inequality& Bitwise AND^ Bitwise NOR| Bitwise OR

&& Logical AND|| Logical OR

?: Conditional expression

= *= /= %= +=  -= &= ^=    |= <<=    >>=

Assignment operators

, Comma operator

Page 2: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

1. C programming language was originally developed for and implemented on the Unix operating system, by "Dennis Ritchie".

2. One of the best features of ‘C’ is that it is not tied to any particular hardware or system.

3. This makes easy for a user to write programs that will run without any changes on practically all machines.

4. It is a general-purpose structured programming language. 5. ‘C’ is of called a "middle-level" computer language as it combines the elements of

high-level language with the functionality’s of Assembly language. 6. ‘C’ manipulation of bits, bytes and addresses of the basic elements with the computer

function. 7. Another good point about ‘C’ is its portability, which makes it possible to adapt

software written for one type of computer to another. 8. As a programming language, C is rather like PASCAL or FORTRAN. 9. Values are stored in variables.

10. Programs are structured by datinis and calling functions. 11. Program flow is controlled using loop, if statements and function calls. 12. I/O can be directed to terminals or files. 13. Related data can be stored together in array or structure. 14. In terms of language level, ‘C’ is intermediate to high level language, yet it allows the programmer to control the hardware. Therefore ‘C’ is used to write programs from built in application to operating system i.e. It can be used for systems programming as well as Application Programming 15. Speed, portability and compactness of code. 16. It has a small set of instruction, which make easy to learn language. 17. It has supported by extensive library functions to enhance the basic instruction capability. 18. It is easy to code and debug.

Structure of a ‘C’ programAns. The ‘C’ is free form of language having the following sections Documentation section Link section Definition section Global declaration section main() function section {o Local variable declaration section; o executable section (statements); }o Subprogram section Function 1 { } Function 2 { }//user defined function section. Function n { }

1. Documentation Section: The documentation section consists of a set of comments lines giving the name of the program and the author name and other details.

E.g. /*This my fist C programme*/

Page 3: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

2. Link Section: It provides instructions to the compiler to link functions from the system library.

E.g. #include<stdio.h>3. Definition Section: The definition section defines all symbolic constants.E.g. #define max 504. Global Declaration Section: The variables that are used in more than one function are called

global variables and these are declared in the global declaration section i.e. out of all the functions.

E.g. int global;5. main() Function Section: Every ‘C’ program must have one main() function. This section

contains two parts namely local variable declaration part and statement part. The local variable declaration part declares all the variables used in statements. These two parts must appear between opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. All statements in the local variable declaration and statements end with a semicolon.

E.g. main(){int local;statement1;statement2;..statementn;}6. Sub-Program Section: It contains all the user defined functions that are called in this main

function. User-defined functions are placed after the main function..E.g. Int myfunction(){statement1;statement2;..stamentm;}

C tokens: Smallest individual units in a C program are known as C tokens, C has six tokens as given below.

C token example1. key words eg int, char2. Identifiers eg max,v,i3. Constants eg 2.52, ‘a’4. Strings eg ”maximum”5. Special symbol eg {} []6. Operators eg + - / * etc

Page 4: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

Key wordsKey words are the tokens used in C program which have predefined meaning an these

meanings cannot be changed by the programmer.

Standard key words are:Auto double int structbreak else long switchcase enum register typedefchar extern return unionconst float short unsignedcontinue for signed voiddefault goto sizeof volatiledo if static while

Identifiers Identifiers refer to the names of variables, constants, functions and arrays. Identifiers are user defined names and consist of sequence of letters or digits, with

letter or underscore as first character. Both lower case and upper case letters can be used for identifiers. Underscore can be used to link words of identifiers.

Example Some valid identifiers : max

List_of_wordsa123 etc

Invalid identifiers : 12 abcMaxi mum etc.

Data types in CEach data item in C program is associated with a type known as data type.ANSI C supports four classes of data types :1. Primary data types2. User defined types3. Derived data types4. Empty data set

Primary data types: Primary data type

Integral Floating point Character

Signed Unsigned Float double long double Signed Unsigned

int short int long int int short int Long int

Page 5: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

The size and range of primary data type in a 16- bit machine is given below.

Integers are whole numbers with the range supported by particular machine. Integer occupy one word of storage (16 bits or 32 bits depending on the machine)

Floating point numbers are stored in 32 bits on all 16-bit and 32 machines with 6 digits of precision.

Double type number uses 64-bits bite with 14 bits of precession. Character are stored in 8 bits(1 byte) of internal storage.

Declaration of primary type.Syntax of declaring variables of any primary data type is:

Data type var1,var2,------------var n.Example.

Int i,j;Float f;Double data;

Here int float and double are key word to represent integer type and real type data respected.

It indicates the type of data that variable can hold. The data may be numeric or non-numeric. The basic four data types in C are as follows. int float char double. a. Integer (int): It is indicated by the keyword int. It occupies 2 bytes of memory. The range of it is -32718 to 32767. It also contains long int and short int. When the value is

Type Byte RangeChar or signed char Unsigned char

Int or signed int

Unsigned int

Short or signed short int

Unsigned short int

Long int or signed int

unsigned long int

float

double

long double

1

1

2

2 1

1

4

4

4

8

10

-128 to 127

0 to 255

-12768 to 32767

0 to 65535

-128 to 127

0 to 255

-2,147,483,648 to2,147,483,6470 to 4,294,967,295

3,4E-38 to 3.4E+38

1,7E -308 to 1,7E +308

3,4E-4927 to 1.7E +4932

Page 6: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

greater than the int range then long int is used and less that’s the int range then & short int is used long int uses 4 bytes and short int uses 2 bytes. Example: int i; long int j; short int k.b. Character: It is indicated by the keyword char. It occupies one byte. The dates may be character constant or string constant. c. Float: To represent real number the float is used. It occupies 4 bytes of memory. The range is 3.4 E-38 to 34 E+38 (E is exponent. The numbers may be expressed with in decimator scientific notation. The scientific uses mantissa exponent notation. d. Double: This is similar to float. But it issued when the range of values greater than float. It takes 8 bytes. The range is 1.7E-308 to 1.7 E +308. Example: float m; Double n;

User-defined data types:These are data types defined by user, typedef and enumerated data types are two user defined types.

typedef.This allows the user to define an identifier with a name that represents some existing data types.It take the form.

Typedef type identifiers.Example:

typedef int demo-int;typedef float demo-float;

Here demo-int represents the data types int and demo float represents the data types float for declaring variables of types integer, demo-in can be used instead of keyword int.Example:

demo_int count,Here count is declared as of type int

AdvantageUsing typedef of we can create meaningful data types for increasing readability of

program

Enumerated data type.It is defined as follows

Enum identifier (value 1, value 2 ---------------value n);Where identifier is a user defined data typeVariables of enumerated data type are declared after defining enumerated data types a enum identifier a1, a2,-----------anEach enumerated variables can have not only one of the values value1,value2,-----------value nExample:enum colour (red, green, blue, white),Here colour is declared as enumerated data types.enum colour e1,e2,e3Here e1,e2,e3 are declared as of enumerated data type colour.Compiler assigns integer values to the enumeration constants. Internally red is assigned value 0, green is assigned value 1,blue is assigned 2 and so on.Automatic assignments can be overridden by assigning values explicitly.Example enum colour (red=2,green=1,blue=0,white=5)

Page 7: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

Derived data types:Arrays functions, structures and pointers are derived data types.ArrayArray is a group of related data items that chare a common name.Example:Int marks[50]Here marks is an array which can store 50 items and is of type int .Array can be one dimensional, two dimensional of multidimensional.Functions C functions are classified into two types. Library function and user defined functions-library function have predefined code existing for them. Where as user defined function are developed by users. Example of library function, printf, scanf, sqrt, etc. Example of user defined functions main();Structures:Structure is a collection of different data types under a single name structures help to organize data in a meaningful way. Pointers:Pointers are variables that hold memory addresses.Example:Data type *p_name.Here p_nameis a pointer to a particular data type.

Empty data set:It is also known as void data types. It indicates that no other data types has been used

with the given identifier.Example:Void abc(){}here void indicates that function doesn’t return value of any type.

Variable:In C a quantity that may vary during program execution is called variable. Variable names are given to locations in the memory of computer, where different constant are stored Rules:1) A variable name is any combination of 1to8 alphabet, digits or underscores.2) The first character in the variable must be an alphabet 3) No commas or blanks are allowed within the variable name 4) No special symbols other than an underscores can be used in a variable name.5) C keywords cant be used as variable name give some examples

INTIALIZING VARIABLES Intializing a variable is a process of declaring as well as defininf it. It is when a real

memmory is allocated to it. Basic intialization technique is using assignment operator, eg - int a = 0; Multiple intialization technique eg - int a = 0, b = 10, c, d = 20; Global variables and static local variables are intialized only once. Local variables are intialized each time they are created or thier code block is entered.

Page 8: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

Also unitialized static and local global variable are set to zero before thier prior use. Uninitalize local variable contains undefined value (Random) before thier prior use.

Using an unitialized variable specially the local variables and pointers can lead to intense bugs which are even capable of entire system crash. To prevent this some compilers will indicate error but almost all of them will flank a warning to make sure it is not by a mistake.

Constants:Constants refer to fixed values that do not change during execution of program.

Several types of constants are:

Constants

Numeric Character

Integer Float Single StringCharacter constant Constant

Constants are fixed values that do not change during the execution of program Constants are character constant numeric constants are further divided into four parts.1. Numeric constant 2. Rear constant3. Single character constant4. String character constantInteger Constant:These refer to integer consisting of sequence of digits... There are three types of integer constants decimal octal, hexadecimal.Rules for constructing integer constants:

1. An integer constant must have at least a single digit.2. It should not contain any commas, blank spaces or any special characters.3. It should not contain any decimal point or exponent.4. The integer constant cannot exceed the range allowed in a particular machine.

Decimal:Decimal integer constants consist of numbers from 0 to 9 preceded by optional sign.Example:

Valid decimal constants Invalid decimal constants+129 1,29-95 1 54 blank space135 etc 1.365 etc

Octal:Octal integer constants consist of any digits from 0 to 7 with leading 0.Examples:

Valid octal constants Invalid octal constants

Page 9: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

037 081 : eight should not be there 0 541 : no leading zero045 1,365 etc

Hexadecimal: Hexadecimal integer constants of combination of digits from 0 to 9 and characters from a to f or A to F. The characters from a to f( A to F) represent numbers from 10 to 15. Hexadecimal numbers are preceded OX.

Example:Valid hexadecimal Invalid hexadecimal

constants constants OxA5 075 : no leading OX

Ox OXAGE : G is not hexadecimal digitOXABCOXAB

Real Constant: These constants refer to the numbers containing fractional parts. These are also known as constants floating – point. Two ways of representing real constants are decimal form and exponential form.Real constants are also called as Floating-point constants. The real constants could be written in two forms. Fractional form and Exponential form.

Rules for constructing real constants in fractional form: -a.A real constant must have at least one digit. b. It must have a decimal point. c. It could be either positive or negative. d. Default sign is positive. e. No commas or blanks are allowed within a real constant. Example: +456.34 125.78 -35.93The exponential form representation is used if the value of the constant is either too small or too large. In the exponential form of representation, the real constant is represented as two parts. The part appearing before ‘e’ is called mantissa, whereas the part following ‘e’ is called exponent.Rules for constructing real constants in exponential form: -a. The mantissa part and exponential part should be separated by a letter e. b. The mantissa part may have a positive or negative sign. c. Default mantissa part is positive. d. The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive. e. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38. Example: +4.2e-5 4.8e5 -0.2e+3

Examples of decimal form floating point numbers: 0.0058

Page 10: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

- 0.154 45.36+30.15 30.0 - .895

Exponential form: If exponent is present, its effect is to shift point to the right if the exponent is positive or to the left if the exponent is negative. If a decimal point is not included within the number it a assumed to be positioned to the right of last digit.

Example 1:The number 4x104 can be represented as floating-point constants as

40000. 4E+4 400e +2 40.e+2 etc..4e5 4E4 40E2

Example 2:The number 3.04×10-5 is represent as floating point constant as

3.04 E -5 30.4E -6304e -4 0.00304 e -2 etc

Invalid floating point constants are:E + 10 : exponent alone is not allowed3.45 e 3.6 : exponent is not an integer

Rules for constructing real constants (floating point constants):(a) mantissa and exponential part should be separated by a letter e or E if it is in exponential

form.(b) mantissa must be positive or negative. By default sign of mantissa is taken as positive.(c) Commas, blank spaces and special characters not allowed.(d) Exponents must be integer(e) Exponent and mantissa must have at least one digit each.

Single character constants:These refer to the single character enclosed within single quote marks.Example:

‘a’,’y’,’5’ etc.Character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both inverted commas should point to left. For example, ’A’ is a valid character constant whereas ‘A’ is not. Character constant have integer values known as ASCII values.Example: ’A’, ’100’ String character constants consist of sequence of characters enclosed in double quote marks.Example: "red", "$100.05"Character constants can store either the character integers or real values where as integer constants can store only integer values. The character constants can represent the numeric values also.

character constants have integer values known as ASCII values. eg ‘a’ has ASCII value 97.

Page 11: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

String constants:These refer to sequence of characters enclosed within double quote marks. The characters may be letters, numbers, special characters, and blank spaces.Example:

“welcome”“max123”“a+b”“HELLO”

String constants do not have values like character constants.Special symbols:

Special symbols such as {;},[,],(,),\ etc also some under C tokens.

OPERATORS:The operators are used in expressions. Different types of operators are:

a) Arithmetic Operators : +,-,*,/,%b) Logical Operators :&&,||.!c) Relational Operators :<,<=,>===,!=d) Bitwise operators :&,1,^,<<>>e) Increment and decrement :++,--f) Conditional Operator :?:g) Assignment operator :+,=,*,=,/,=h) Special operators :sizeof, comma operators etc.

ARITHMETIC OPERATORWe can process data using arithmetic operators such as +, -, *, \ and the modulus operator %. % indicates the remainder after integer division; % cannot be used for float data type or double data type. If both operands i1 and i2 are integers, the expression i1/i2 provides integer division, even if the target is a floating point variable. The operators have normal precedence rules, as follows:

1. Unary operators such as −, + are evaluated.2. The multiplication (*) and division (/,%) operators are evaluated.

3. The addition (+) and subtraction (−) operators are evaluated.

4. The assignment operator is evaluated.

5. The expressions are evaluated from left to right for unary operators. The assignment is from right to left.

Program

#include<stdio.h>main( ){ int a,b,c,d; int sum,sub,mul,rem; float div; printf("ENTER VALUES OF b, c, d"); scanf("%d%d%d",&b&c,&d); sum = b+c; sub = b-c;

Page 12: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

mul = b*c; div = b/c; rem = b%d; a = b/c * d; printf("\n sum = %d, sub = %d, mul = %d, div = %f",sum,sub,mul,div); printf("\n remainder of division of b & d is %d",rem); printf("\n a = %d",a); }Inputb = 10, c = 5, d= 3.OutputENTER VALUES OF b, c, d1053sum = 15, sub = 5, mul = 50, div = 2.0remainder of division of b & d is 1a = 6

ExplanationSuppose we have the expression

a = b/c * dHere / and * both have the same priority. b/c first is evaluated because the expression is evaluated from left to right. After evaluating the expression b/c * d, the value is assigned to a because the assignment operator has an order of evaluation from right to left, that is, the right expression is evaluated first.

LOGICAL OPERATORWe can combine results of multiple relations or logical operations by using logical operation. The logical operators are negation (!), logical AND (&&), and logical OR (||), in the same order of preference.Program#include<stdio.h>main( ){ int c1,c2,c3;printf("ENTER VALUES OF c1, c2 AND c3");scanf("%d%d%d",&c1.&c2,&c3); if((c1 < c2)&&(c1<c3)) printf("\n c1 is less than c2 and c3");if (!(c1< c2)) printf("\n c1 is greater than c2");if ((c1 < c2)||(c1 < c3)) printf("\n c1 is less than c2 or c3 or both");}Inputc1= 2;c2= 3;c3= 4;OutputENTER VALUES OF c1, c2 AND c3234

Page 13: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

c1 is less than c2 and c3c1 is less than c2 or c3 or bothExplanation

1. Logical AND returns a true value if both relational expressions are true. Logical OR returns true if any of the expressions are true. Negations return complements of values of relational expressions, as shown in Table below.Table: Results of AND, OR, and Negation. R1 R2 R1 && R2 R1 || R2 ! R1T T T T FT F F T FF T F T TF F F F T

2. Logical operators AND, and OR have higher priority than assignment operators, but less than relational operators. Negation operators have the same priority as unary operators, that is, the highest priority.

3. While evaluating logical expressions, C uses the technique of short circuiting. So if the expression is:

4. C1 && C2 && C3 && C4 if C1 is truethen only C2 is evaluated. If C1 is false, the expression returns false even if C2, C3, and C4 are true. So if C1 is false C2, C3, and C4 are not evaluated. Remember this when we are doing something such as searching in an array. For example, if we want to search for K in an array, the last value of which is subscript N, we can write the search condition in two ways:I - (a [i] == K) && (i <= N)II - (i <= N) && (a[i] == K)

5. In case I we compare the array limit with K and check the bound. This is not correct because if the value of i is more than N we will get the array index out-of-bounds error.

6. In case II, we first check the bound and then compare the array element. This is correct because we will never compare the array element if value of i is more than N.The technique of short-circuiting is applicable to the OR operator also. Thus if the expression is:C1 || C2 || C3 || C4 if C1 is truethen the expression returns true and C2, C3 and C4 are not evaluated.

RELATIONAL OPERATORRelational operators are used in Boolean conditions or expressions, that is, the expressions that return either true or false. The relational operator returns zero values or nonzero values. The zero value is taken as false while the nonzero value is taken as true.ProgramTh relational operators are as follows:<, <=, >, >=, ==, !=The priority of the first four operators is higher than that of the later two operators. These operators are used in relational expressions such as:7 > 12 // false20.1 < 20.2 // true'b' < 'c' // true"abb" < "abc" // trueThe strings are compared according to dictionary comparison, so if the first characters are equal, the condition is checked for the second characters. If they are also equal then it is checked for the third character, etc. The relational operators return integer values of either zero or non zero.Note that the equality operator is == and not =. ‘=’ is an assignment operator.If we want to compare a and b for equality then we should write a == b, not a = b because a = b means we are assigning the value of b to a, as shown in Table below.

Page 14: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

Table: Comparing the equality operator (= =) with the ‘=’ assignment operator. Case a b a = b a == b1 5 3 a = 3 (true) false2 7 0 a = 0 (false) false3 0 0 a = 0 (false) trueIn case 1, the value of a = 5 and b = 3. The assignment expression assigns the value of b to a, so a will be 3. The expression returns a true value because 3 is not zero. For the same case a == b does not make any assignment and returns a false value because in the value of a does not equal that of b.In case 2, the value of a = 7 and b = 0. The assignment expression assigns the value of b to a, so a will be 0. The expression returns a false value of zero. For the same case, a == b does not make any assignment and returns a false value because the value of a does not equal that of b.In case 3, the values of a and b are both 0. The assignment expression assigns the value of b to a, so a will be 0. The expression returns a false value of zero. For the same case, a == b does not make any assignment and returns a true value because the value of a equals that of b.

INCREMENT OPERATORWe can increment or decrement the value of variable using the increment or decrement operator. These operators can be applied only to variables and they can be applied using prefix form or postfix form.Program#include<stdio.h>

main( ){int I,j,k;i = 3;j =4;k = i++ + --j;printf("i = %d, j = %d, k = %d",i,j,k);}Inputi =3, j = 4.Outputi = 4, j = 3, k = 6.ExplanationWhen the prefix form is used, the value of the variable is incremented/decremented first and then applied. In the postfix form, the value is applied and only after the assignment operator is done is the value incremented or decremented.Suppose we writei = 3;j =4;k = i++ + -j;we will get the value of k as 6, i as 4 and j as 3. The order of evaluation is as follows:i gets the value 3.j is decremented to 3.k gets the value 3 + 3.i is incremented.Suppose we writei = 5;i = i++ * i++

Page 15: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

Then we will get the value of i as 27. This is because first the value 5 is used as to make i = 25 and then i is incremented twice. The increment and decrement operators have higher priority than the arithmetic operators.

TERNARY OPERATOR/CONDITION OPERATORTernary operators return values based on the outcomes of relational expressions. For example, if we want to return the value of 1 if the expression is true and 2 if it is false, we can use the ternary operator.Program/ExampleIf we want to assign the maximum values of i and j to k then we can write the statementk = ( i>j ) ? i : j;If i > j then k will get the value equal to i, otherwise it will get the value equal to j.The general form of the ternary operator is:(expr 1) ? expr2 : expr3If expr1 returns true then the value of expr2 is returned as a result; otherwise the value of expr3 is returned.

COMMA OPERATOR Comma opertor as we can guess is represted by , Comma operator is a operator with lowest precedence.It is used to combine together several expression in one expression.In comma operator all the expression on left evaluates to null and the rightmost expression becomes the value of the entire comma seprated expression.The use of comma operator may be clear from following C source code -

Program:#include <stdio.h>int main (void){int x,y;x = (y=4,y+3); // Used of comma operator to string two expressions.printf ("%d %d", x,y);// Displays 7 and 4return 0;}OPERATOR PRECEDENCE:

Operators Order of evaluation Remarks

[] ( ) -> Left to right Array subscript, function call

− + sizeof( ) ! ++ −−    

& * ~ (cast) Right to left Unary

* / % Left to right Binary Multiplicative

+ - Left to right Binary Additive

>> << Left to right Shift operators

< <= > >= Left to right Relational operators

Page 16: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

Operators Order of evaluation Remarks

== != Left to right Equality operators

& Left to right Bitwise And operator

^ Left to right Bitwise Xor operator

| Left to right Bitwise Or operator

&& Left to right Logical And operator

|| Left to right Logical Or operator

?: Left to right Conditional operator

= += -= *= /= %=   

&= -= |= <<= >>= Right to left Assignment

, Right to left Comma

EXPRESSION EVALUATION In a expression order of evaluation is decided by user by using parenthesis.All subexpression that have to be solved on same level can be freely rearrange by compiler in any order to optimize code efficiency and some employ parsing them left to right but if you want a portable code you cannot make this assumption always true. This rearranging can also lead to some hard to find bugs which are caused by undesirable rearrangement and execution order. So as programmer it is your job to make sure that your code does not rely upon the execution subexpression. Situation can be described by the code below -

PROGRAM:#include <stdio.h>int input = 10;int f1 (){ input = input*5; return input;}int f1 (){ input = input+ 10; return input;}int main (void){sum = f1 ()+ f2 (); // Unreliablity introduce due to rearrangement of sub expressions by compiler. //Any one of f1 () or f2 () can be executed first.printf ("%d" , output); // Ambigious output (110,120)

Page 17: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

}

TYPE CONVERSION When variables and constatns of diffrent types are mixed in a single expression they all converted to same type.General rule follow by the compilers is that all variables and constants are promoted to the largest type occuring in the expression this is called type promotion. Also result is return in this largest variable type.Largest variable types are listed in decreasing order. If any operand is of not the largest type variable then next largest varible type is checked until the right one occuring in realation is found and then all the trems in that expresssion is - long double >> double >> float >> unsigned long int >> long int >>unsigned int >> int >> short int >> char Type conversion may some time lead to loss of data when a bigger size type casted into smaller type the table shown below indicat possible cases for char.Target Type Expression Type Possible Loss Of Datachar int(16) High Order 8 Bits char int (32) High Order 24 Bits char short int High Order 8 Bits char long int High Order 24 Bits char float High Order 24 Bits and preicision char double High Order 56 Bits and precision char long double High Order 120 Bits and precision

Similarly cases of all variables into other types can be calculated from their size

TYPE CASTING Type casting is an neccesary evil. C provides most of the automatic conversion between various compatible types but there may arise cases in which you dont want to use C's inbuilt type conversion or it is not capable of converting it. In this case you use a very powerfull tool know as type casting.In C type casting allows you to convert a varible, constant or data of any type from any type. Casting leads to conversion atoccurs at binary level. Its also a very dangerous things because when you do a unusal type casting you obtain a value in a variable which was never built to handle and when you used this variable there is on guessing what could happen. So be very very carefull while using type casting.The genral syntax for typing casting when you have to convert a type1 into type 2 is type2 variable = (type2) type1 variable.Code below illustrates a simple type conversion -

#include <stdio.h>int main (void){int a;for (a = 0; a < 10;a++) { printf ("%f", (double) a/2); // Prints a float number even though variable is an int } return 0;}

BITWISE OPERATORBitwise operators interpret operands as strings of bits. Bit operations are performed on this data to get the bit strings. These bit strings are then interpreted according to data type. There are six bit

Page 18: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

operators: bitwise AND(&), bitwise OR(|), bitwise XOR(^), bitwise complement(~), left shift(<<), and right shift(>>).Program# include<stdio.h>main(){ char c1,c2,c3; printf("ENTER VAULES OF c1 and c2"); scanf("%c,%c",&c1,&c2); c3 = c1 & c2; printf("\n Bitwise AND i.e. c1 & c2 = %c",c3); c3 = c1 | c2; printf("\n Bitwise OR i.e. c1 | c2 = %c",c3); c3 = c1 ^ c2; printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3); c3 = ~c1; printf("\n ones complement of c1 = %c",c3); c3 = c1<<2; printf("\n left shift by 2 bits c1 << 2 = %c",c3); c3 = c1>>2; printf("\n right shift by 2 bits c1 >> 2 = %c",c3);}Inputc1 = 4;c2 = 6;OutputENTER VALUES OF c1 and c246

Bitwise AND i.e. c1 & c2 = 4Bitwise OR i.e. c1 | c2 = 6Bitwise XOR i.e. c1 ^ c2 = 2ones compliment of c1 = -4left shift by 2 bits c1 << 2 = 16right shift by 2 bits c1 >> 2 = 1ExplanationSuppose we write

char c1, c2, c3;c1 = 4;c2 = 6;

The binary values arec1 = 0000 0100c2 = 0000 0110

Suppose we writec3 = c1 & c2;

The value of c3 is interpreted as follows: 0000 0100& 0000 0110 ----------. 0000 0100

Each bit of c1 is compared with the corresponding bit of c2. If both bits are 1 then the corresponding bit is set as 1, otherwise it is set as 0. Thus the value of c3 is 4.

Page 19: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

c3 = c1 | c2 The value of c3 is interpreted as follows:

0000 0100| 0000 0110 -----------. 0000 0110

Each bit of c1 is compared with the corresponding bit of c2. If any of the bits are 1 then the corresponding bit is set as 1; otherwise it is set as 0. Thus the value of c3 is 6.

c3 = c1 ^ c2 The value of c3 is interpreted as follows: 0000 0100^ 0000 0110 ----------. 0000 0010

Each bit of c1 is compared with the corresponding bit of c2. If only one bit is 1, the corresponding bit is set to 1; otherwise it is set to 0. Thus we will get the value of c3 as 2 because in the second position for c1, the bit is 0 and for c2, the bit is 1. So only one bit is set.

c3 = ~ c1 The value of c3 is interpreted as follows:

~ 0000 0100 ----------. 1111 1011

Each bit of c1 is complemented; for 1 the complement is 0. Thus we will get the value of c3 as −4, because the leftmost bit is set as 1.

c3 = c1 << 2; This is a left-shift operation. The bits are shifted left by two places. c1 indicates the operand that should be an expression returning a whole number. 2 indicates a shift that should not be negative, and its value must be less than the number of bits allocated to that data type.It is evaluated as follows:

c1 is 0000 0100It is shifted 2 bits to the left to produce

0001 00**While shifting, the high-order (left) bits are discarded. Since a vacuum is created on the right side, it is filled with 0s to get 0001 0000. Thus the value is 16.

c3 = c1 >> 2; This is a right shift operation. The bits are shifted right by two places. c1 indicates the operand that should be an expression returning a whole number. 2 indicates a shift that should not be negative, and its value must be less than the number of bits allocated to that data type.It is evaluated as follows:

c1 is 0000 0100It is shifted 2 bits to the right to produce

**00 0001While shifting, the low-order (right) bits are discarded. The asterisks are replaced using one of the following strategies:Logical shift: In this case, the high-order bits are filled with 0s, thus we get 0000 0001.Arithmetic shift: In this case the high-order bits are filled with the original sign bits, so if the sign bit is 1, then all bits are filled with 1s; otherwise, they are filled with 0s.For unsigned data types, logical shift is used, whereas for signed data types arithmetic shift is used.In these examples, the char data type which is signed is used. In number 4, the sign bit is 0, so we will get the bit pattern 0000 0001 (decimal 1).

Summarize the standard escape sequences in C. Describe them.The characters on the key board can be printed or displayed by pressing the key. But some characters such as line feed; form feed etc. cannot be printed or displayed directly. ‘C’

Page 20: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

provides the mechanism to get such characters that are invisible through escape sequence characters. These escape sequence are represented by a backslash ‘\’ followed by a character.The following are the list of escape sequence (or) backslash characters.CHARACTER MEANING RESULT

\o End of string Null\n End of line Moves the active position to the initial

position of the next line.\r Carriage return Moves the active position to the initial

position of the current line.\f Formfeed Moves the active position to the initial

position of the next logical page.\v Vertical tab Moves active position to the next vertical

tabulation position\t Horizontal tab Moves active position to the next

horizontal tabulation position\b Backspace Moves active position to the previous

position on the current line. after removing the current symbol.

\a Alert Provides an audible alert.\\ Backslash Produces with a backslash \.\” Double quote Produces with double quote\’ Single quote Produces with single quote.

What is the purpose of type declaration? What are the components of type declaration?typedef is a redefine the name of an existing variable type.Example: typedef unsigned long int twowords;The typedef provides a strong and meaningful way to call a data type. Usually uppercase letters are used to make it clear that we are dealing with a removed data type.The components of type are…typedef----- key word followed, basic data type and followed by a user-defined name.

CONTROL STATEMENTS:‘C’ is considered as a structured programming language. One of the reasons for this is having various program control statements ‘C’ possesses the decision making capabilities and supports the statements known as control statements. There are three control statements are supported by ‘C’. These are illustrating as shown below.

CONDITIONAL CONTROL STATEMENTS:The conditional control statements are mainly used for decision making purpose. The following are the conditional control statements

If statements, simple if, if else, nested –if-else, else if and switch statements

(a) If Statement : If statement is a powerful decision making statement used in ‘C’. It is used to control the flow of execution of statements.

The general form of If-statement is

Page 21: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

If (test condition);

If the value of expression is true or false transfer the control to a particular statement. If has two paths, one for true condition and the other for the false condition as shown in fig below

EXAMPLE:

If (marks>=35)printf (“pass in the subjects”);

The If–statement may be implemented in deferent forms depending on the complexity condition to be tested. The following are the different forms of if-statement.

(a) simple if –statement(b) if-else statement(c) nested if-else statement(d) else-if ladder(b) simple if-statement:

The general form of the simple if statement is

If (test expression) { Statement block;}

Statement-X;

If the test expression is true, the statement-block will be executed, otherwise statement block will be skipped and execution transfer to the statement-X. Here the statement block may be a single statement or group of statements. It is illustrated in the flow control diagram as in shown in fig below

EXAMPLE:

If(num>=0){count=count+1;}printf(“count is %d”, count);

SAMPLE PROGRAME:

Write a program to calculate the absolute value of an integer.

#include<stdio.h>main(){int num;printf(“Enter the integer number :\n”);

Page 22: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

scanf(“%d”,&num);if(num<0){num=-num;}printf(“The absolute value =5d”,num);}

OUTPUT: Enter the integer number :

-100 The absolute value=100

IF-ELSE STATEMENT: This statement is an extension of the simple if statement.

The general form is :

if (test expression){ statement –A;}else{ statement-B;}statement-X;

If the test expression is true, then statement –A is executed and statement-B is ignored from the execution .Otherwise text expression is false, statement-A is ignored and statement-B is executed. Either statement-A or statement-B will be executed, not both, the control transfer to the statement-X. It is illustrated in the flow control diagram as shown in Fig below.

EXAMPLE:- if(code==1){b=b+1;}else

{c=c+1;}

statement-X;

If the code is ‘1’, the statement b=b+1 is executed and control is transferred to statement-X, after skipping the else part.

If the code is not equal to ‘1’, the statement b=b+1 is executed and the statement else part c=c+1 is executed, before the control reaches the statement-X.

Page 23: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

SAMPLE PROGRAM:

Write a program to find whether the given number is positive or negative.

#include<stdio.h>main(){int n;printf(“Enter the number :\n”);scanf(“%d”,&n);\if(n>0){printf(“The number is positive”);}else{printf(“The number is negative”);}}

OUTPUT:

Enter the number :10 The number is positive.

NESTED IF-ELSE STATEMENT:

If a serried of decision are involved, we use more than one If-else statement known as nested if-else statement

The general form is:

If (test expression 1){if(test expression 2){statement-1;}else{statement-2;}}else{statement-3}statement –X;

if test expression-1 is false ,the statement -3 is executed otherwise it continue with test expression-2 if the test expression-2 is true, the statement-1 will be evaluated, otherwise

Page 24: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

statement-2 will be evaluated and the control is transferred to the statement-X .the flow control diagram is illustrated as shown as in fig below

EXAMPLE:

If (a>b){if(a>c){printf(“%d”,a);}else{printf(“%d”,c)}}else{printf(“%d”,b);}

SAMPLE PROGRAM:

Write a c program to find the largest number in the given three numbers

#include<stdio.h>void main (){int a,b,c;printf(: enter three numbers”);scanf(“%d%d%d”,&a,&b,&c);printf(“largest value is “);if(a>b){if(a>c){printf(“%d”,a);}else{printf(“%d”,c);}}else{if(c>b){printf(“%d”,c);}else{printf(“%d”,b);}}

Page 25: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

} out put:enter three values of a,b,c

10 20 30

largest value is 30

ELSE-IF LADDER:

this statement is also used for the series of decisions are involved the general form of else –if ladder is if(test expressison 1)statement-1;else if(test expression 2)statement-2;else(test condition n)statement –n;elsedefault-statement;statement-X;

This type of construction is known as else if ladder. Test expressions are evaluated from the top downwards. If the condition is true the statement is associated with it is executed and the control transfer to statement-x. When all the conditions are false, then the final else containing the default statement will be executed. The logic of the execution of the statement is as shown in the flow control diagram in fig below

EXAMPLE:If (condition=1)’Colour =pink;Else if (condition=2)Colour=black;Else if (condition=3)’Colour=green;ElseColour =white;

SAMPEL PROGRAM:

Write a ‘C’ program to check where the given number is positive or negative or zero

Include<stdio.h>Void main(){int n;printf(“enter the number”);scanf(“%d”,&n);if(n>0){printf(“the number is positive”);}else if (n<0){

Page 26: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

printf(“the number is negative”);}elseprintf(“the number is zero”);}}

Out put:

Enter the number: 10The number is positive.

Switch StatementThe switch statement is C’s multilevel branch statement. It provides an easy way to dispatch execution to different parts of your code based on the values of an expression. As such it provides a better alternative than a large series of if-else-if statements.The general form of a switch statement:Switch (expression){case value1://statement sequencebreak;case value2://statement sequencebreak;..case valueN://statement sequencebreak;default://default statement sequence}The expression must be of type short, int or char; each of the values specified in the case statements must be a type compatible with the expression. Each case value must be unique literal (that is, it must be a constant, not a variable). Duplicate case values are not allowed.The switch statement works like this: The value of the expression is compared with each of the literal values in the case statement. If a match is found, the code sequence following that case statement is executed. If none of the statements matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.The break statement is used inside the switch to terminate a statement sequence. When break statement is encountered, execution branches to the first line of code that follows the entire switch statement. This has the effect of "jumping out" of the switch.Here is a simple example that uses a switch statement: //A simple example of the switch.#include<stdio.h>#include<conio.h>void main(){int i;

Page 27: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

clrscr();for(i=0;i<6;i++)switch(i){case 0: printf("\n i is zero");break;case 1: printf("\n i is one");break;case 2: printf("\n i is two");break;case 3: printf("\n i is three");break;default: printf("\n i is greater than 3");}}

As you can see, each time through the loop, the statements associated with the case constant that matches i are executed. All others are bypassed. After i is greater than 3, no case statements match, so the default statement is executed.

The break statement is optional. If you omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them.

 For example, consider the following program:// In a switch, break statements are optional.#include<stdio.h>#include<conio.h>void main(){int i;clrscr();for(i=0;i<12;i++)switch(i){case 0: case 1: case 2: case 3: case 4:printf("\n i is less than 5");

break;case 5: case 6: case 7: case 8: case 9:printf("\n i is less than 10");break;default:

Page 28: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

printf("\n i is 10 or more");}}Nested switch statements:You can use a switch as part of the statement sequence of the outer switch. This is called nested switch. Since a switch statement defines its own block, no conflicts arise between the case constants in the inner switch and those in the outer switch. For example, the following fragment is perfectly valid:switch (count){Case 1:switch(target){ // nested switchcase 0:printf("target is Zero");break;case 1: // no conflicts with outer switchprintf("target is one");break;}}break;case 2://……Here the case 1: statement in the inner switch does not conflict with the case1: statement in the outer switch. The count variable is only compared with the list of cases.In summary, there are three important features of the switch statement to note:The switch differs from if in that switch can test only for equality, whereas if can evaluate any Boolean expression. That is switch looks only for a match between the value of the expression and one of its case constants.No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch can have case constants in common.A switch statement is usually more efficient than a set of nested ifs.

Distinguish between If and Switch. The Switch differs from if, in that switch can only test for equality, whereas if can evaluate any type of Boolean expression. That is, the switch looks only for a match between the value of the expression and one of its case constants.

No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch can have case constants in common.A switch statement is usually more efficient than a set of nested ifs.

6) Write a C program that performs all the arithmetic operators Using switch-case statement (Consider operators +, -, /, * and %)#include<stdio.h>#include<conio.h>void main(){int a,b,c,ch;clrscr();printf("enter the values of a,b :\n");

Page 29: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

scanf("%d%d",&a,&b);printf("1. addition\n");printf("2. subtraction\n");printf("3. multiplication\n");printf("4. division\n");printf("\nEnter the choice :");scanf("%d",&ch);switch(ch){case 1:c=a+b;

break;case 2:c=a-b;

break;case 3:c=a*b;

break;case 4:c=a/b;

break;default: printf(“\nInvalid option.”);}printf(“\nThe result of arithmetic operation is %d\’,c);getch();}13) A cloth show room has announced the following seasonal discounts on purchase of items.

Purchase amount

Discount Mill cloth Hand loom

cloth <=100

<=200

<=300

>300

Nil

5.0%

7.5%

10.0%

5.00%

7.5%

10.0%

15.0%Write a program using SWITCH and IF statements to complete the net amount to be paid by a customer.#include<stdio.h>#include<conio.h>void main(){int item,ch;float cost,tamt,dis,net;clrscr();printf("enter the no of items & cost :\n");scanf("%d%f",&item,&cost);tamt=item*cost;printf("1. millcloth\n");printf("2. handloom\n");printf("enter the choice.....");

Page 30: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

scanf("%d",&ch);switch(ch){case 1:

if(tamt<=100){net=tamt;printf("net amount %f",net);}else

if(tamt<=200){dis=tamt*5/100;net=tamt-dis;printf("net amount %f",net);}elseif(tamt<=300){dis=tamt*7.5/100;net=tamt-dis;printf("net amount %f",net);}elseif(tamt>300){dis=tamt*10/100;net=tamt-dis;printf("net amount %f",net); }break;case 2:if(tamt<=100){dis=tamt*0.05;net=tamt-dis;printf( "net amount %f",net);}elseif(tamt<=200){dis=tamt*0.075;net=tamt-dis;printf( "net amount %f",net);}else if(tamt<=300){

Page 31: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

dis=tamt*0.1;net=tamt-dis;printf( "net amount %f",net);}elseif(tamt>300){dis=tamt*0.15;net=tamt-dis;printf( "net amount %f",net);}break;}getch();}

LOOP CONTROL STATEMENTSThe control statements are used to construct the systematic block structure programming in ‘C’ .the loops are used to repeat a block of statements a number of times.The following are the same loop control statements used in ‘C’.

(a) while loop(b) do-while loop(c) for loop

WHILE LOOPwhile statement is a entry-controlled loop statement.the while loop start with a reserved word while,followed by the conditions inside the parenthesis.the sequences of statements are written within the brackets from a block of statemtns .these statements are executed if the condition is true.when the condition is false the control transfers to the next statement.The general form is While(condition){sequence of statements;}this statement test the condition.if it is true then the statement part of the loop is executed .if the test condition is false the statement part of the loop is ignored abd execution continuous with next statements.it is illustrated in the flow control diagram as shown in below fig

EXAMPLE:

Page 32: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

Digit=1;While(digit<=10){printf(“%d”<=10)digit=digit+1;}SAMPLE EXAMPLE:Write a sample program to print the first 10 positive integers starting from 1.

#include<stdio.h>main( ){int digit=1;printf(“\n first 10 positive integers are :\n”);while(digit<=10){printf(“%3d”,digit);digit=digit+1;}}

out put: First 10 positive integers are: 1 2 3 4 5 6 7 8 9 10

(2) Write a ‘C’ program to find the sum of the positive integers between 1 and 10#include<stdio.h>void main(){

int count, sum;count = 1;sum = 0;while(count<=10){

sum=sum+count;count=count+1;

}printf(“ sum is : %d\n”,sum);

}

output : sum is : 55

DO– WHILE LOOP

The do while statement is another repetitive loop used in C. The general form of do while loop is do { sequence of statements; }while (test condition);

The statements of the loops are executed only if the condition is true. If the condition is false, the loop is not executed and the control skips with out entering the loop.

When the control enters the loop, all the statements in the loop are executed with out testing any condition, at the end of the loop the condition is tested. If the condition is true the loop is re-entered and if the condition is false the control skips from loop. The control diagram is illustrated as shown in below figure.

Page 33: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

Example:

Digit = 1;Do {printf(“%3d\n”,digit);

digit = digit+1;}while(digit<=10);

Sample programs(1) Write a ‘C’ program to print the first ten positive integers starting from one

#include<stdio.h>void main(){int digit =1;printf(“\n first 10 integers are’);do{ printf(“%3d”,digit);

digit= digit+1;}while(digit<=10);

Output: First 10 integers are :1 2 3 4 5 6 7 8 9 10

(2) Write a ‘C’ program to find the sum of the positive integers from 1 to 10

#incluede<stdio.h>void main(){int count,sum;count = 1;sum =0;do{sum=sum+count;count=count+1;}while(count<=10);printf(“sum=%d”,sum);}

Output: sum=55

Page 34: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

FOR LOOP

For loop is the most commonly used loop stament in C. it consisting of three expressions. The first expression is used to initialize the index value. The second expression is used to check whether or not the loop is to be continued and the third expression is ued to change the index value for further iteration The general form of the for statement is for(expression1;expression2;expression3){sequence of statements;}

Here expression1 is the initial value, expression2 is the test condition and expression 3 in the incremented or decremented value.

EXAMPLE:

Sum=0For(i=0;i<=20;i++){sum=sum+I;}where ‘I’is the index value .during the first expression ‘I’ is is initialized with the value’0’in second expression the expression is evaluated and the loop is entred only if it is true , the loop statement are executed and then in the third expression,the expression is evaluated.

SAMPLE EXAMPLES:

(1) write a ‘C’ program to display the first 10 positive integers starting form one

#include<stdio.h>main(){int I;for(i=0;i<=10;i++){printf(“%3d”,i);}}

out put:1 2 3 4 5 6 7 8 9 10

(2) write a ‘C’ program to find the sum of the positive integers between 1 and 10

#include<stdio.h>main(){int i , sum=1;for(i=0;i<=10;i++){sum=sum+i ;}

Page 35: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

printf(“sum is:%d”,sum)}

out put:55

The BREAK StatementJust like the switch statement, break is used to break any type of loop. Breaking a loop means terminating it. A break terminates the loop in which the loop body is written.Program/ExampleFor example,i = 0;while (1){ i = i + 1; printf(" the value of i is %d\n"); if (i>5) break;}Explanation

1. The while (1) here means the while condition is always true.2. When i reaches 6, the if condition becomes true and break is executed, which terminates the

loop.

The CONTINUE StatementThe break statement breaks the entire loop, but a continue statement breaks the current iteration. After a continue statement, the control returns to top of the loop, that is, to the test conditions. Switch doesn't have a continue statement.Program/ExampleSuppose you want to print numbers 1 to 10 except 4 and 7. You can write:for(i = 0, i < 11, i++){ if ((i == 4) || (i == 7)) continue; printf(" the value of i is %d\n", i);}Explanation

1. If i is 1 then the if condition is not satisfied and continue is not executed. The value of i is printed as 1.

2. When i is 4 then the if condition is satisfied and continue is executed.3. After executing the continue statement, the next statement, (printf), is not executed; instead, the

updated part of the for statement (i++) is executed.

GOTO StatementThe goto statement is a jump statement which jumps from one point to another point within a function. The goto statement is marked by label statement.The goto statement is a jump statement which jumps from one point to another point within a function. The goto statement is marked by label statement. Label statement can be used anywhere in the function above or below the goto statement. You can see in the given example, we want to display the numbers from 0 to 9. For this, we have defined the label statement loop above the goto statement. The given program declares a variable n initialized to 0. The n++ increments the value of n till the loop reaches 10. Then on declaring the goto statement, it will jumps to the label statement and prints the value of n.Example:#include <stdio.h>#include <conio.h>

Page 36: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

int main() {  int n = 0;  loop: ;   printf("\n%d", n);  n++;  if (n<10) {  goto loop;  }  getch();  return 0;}

SIMPLE   PROGRAMS #include<stdio.h>void main( ){ printf("Hello !");}#include<stdio.h>   /* Addition of two numbers */void main( ){  int a, b, sum;  printf("Enter the value of a : ");  scanf("%d",&a);  printf("Enter the value of b : ");  scanf("%d",&b);    sum = a + b;  printf("Sum of a & b is %d",sum); getch( ); }

ADDITION #include<stdio.h>void main( ){  int n, sum;   printf("Enter the value of n : ");  scanf("%d",&n);    sum = n*(n+1)/2;  printf("Sum upto n numbers is %d",sum);   getch( );}

ADDITION #include<stdio.h>   /* Addition of two numbers */void main( ){

Page 37: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

  int a, b, sum;  printf("Enter the value of a : ");  scanf("%d",&a);  printf("Enter the value of b : ");  scanf("%d",&b);    sum = a + b;  printf("Sum of a & b is %d",sum); getch( );}

ADDITION #include<stdio.h>void main( ){  int n, i, sum = 0;   clrscr( );   printf("Enter the value of n : ");  scanf("%d",&n);    for(i = 1; i <= n; i++)     sum = sum + i;   printf("Sum upto n numbers is %d",sum);  getch( );}

ADDITION #include<stdio.h>void main( ){  int n, i = 1, sum = 0;   clrscr( );  printf("Enter the value of n : ");   scanf("%d",&n);   while(n >= i)    {    sum = sum + i;     i++;  }  printf("Sum upto n numbers is %d",sum);  getch( );}

ADDITION #include<stdio.h>void main( ){ int n, i = 1, sum = 0;  clrscr( );  printf("Enter the value of n : ");   scanf("%d",&n);

Page 38: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

  do   {     sum = sum + i;     i++;    } while(i <= n);   printf("Sum upto n numbers is %d",sum);   getch( );}

ADDITION #include<stdio.h>void main( ){ int a, b, i, sum = 0;  clrscr( );  printf("Enter the value of first number : ");   scanf("%d",&a);   printf("Enter the value of last number : ");    scanf("%d",&b);     for(i = a; i <= b; i++)    sum = sum + i;   printf("Sum of all numbers is %d",sum);   getch( );}

AVERAGE void main( ) {   int a, b, c, sum;  float ave;   printf("Enter the value of a,b,c : ");  scanf("%d%d%d",&a,&b,&c);    sum = a + b + c;   ave = sum / 3;   printf("Average of three numbers is %f",ave);}

MULTIPLICATION #include<stdio.h>void main( ){  int a,b,c;  clrscr( );  printf("enter values of a & b: ");  scanf("%d %d",&a,&b);    c = a*b;  printf("the value of c is %d",c);  getch( );}

Page 39: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

DIVISION #include<stdio.h>void main( ){  int a,b;  float c;  printf("enter two numbers : ");  scanf("%d %d",&a,&b);   c = a/b;  printf("Division is %f ",c);  getch( );}

REMAINDER #include<stdio.h>void main( ){  int a,b;  float c;  printf("enter two numbers : ");  scanf("%d %d",&a,&b);   c = a%b;  printf("Remainder is %f ",c);  getch( );}

POWER #include<stdio.h>void main( ){  int x, y, i; clrscr( );  printf(" Enter the two numbers : ");  scanf("%d%d",&x,&y);   i = pow(x,y);  printf("%d",i); getch( );}

POWER #include<stdio.h>void main( ){  int x, y, i, power = 1; clrscr( );  printf(" Enter the two numbers : ");  scanf("%d%d",&x,&y);   for(i = 1; i <= y; i++)

Page 40: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

    power = power * x;  printf("%d to the power %d is %d",x,y,power); getch( );}

POWER #include<stdio.h>void main( ){  int x, y, i = 1, power = 1; clrscr( );  printf(" Enter the two numbers : ");  scanf("%d%d",&x,&y);   while(i <= y)    {     power = power * x;     i++;    }  printf("%d to the power %d is %d",x,y,power); getch( );}

POWER #include<stdio.h>void main( ){  int x, y, i = 1, power = 1; clrscr( );  printf(" Enter the two numbers : ");  scanf("%d%d",&x,&y);   do    {     power = power * x;     i++;    } while(i <= y);  printf("%d to the power %d is %d",x,y,power); getch( );}

 AREA OF CIRCLE #include<stdio.h>void main(){  float area,r;  clrscr( );  printf("Enter the radius of the circle:");  scanf("%f",&r);   area=3.14 * r * r;  printf("The area of the circle is:%f",area);

Page 41: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

  getch( );}

 AREA & PERI OF RECT #include<stdio.h>void main( ){  float l,b,area,peri;  clrscr( );  printf("Enter the length & breadth:");  scanf("%f%f",&l,&b);    area=l * b;    peri=2*(l + b);  printf("The area of rectangle is:%f\n",area);  printf("The perimeter of rectangle is:%f",peri);  getch( );}

 VOL OF CYLINDER #include<stdio.h>void main( ){  float r, tsa, vol, h;  clrscr( );  printf("Enter radius & height of the cylinder:");  scanf("%f %f",&r,&h);   vol = 3.14 * r * r * h;   tsa = 2 * 3.14 * r * (r + h);  printf("The volume of the cylinder is:%f\n",vol);  printf("The total surface area of the cylinder is:%f\n",tsa);  getch( );}

 VOL OF CONE #include<stdio.h>void main( ){  float r,k=0,l,tsa,vol,h,x=0.5;  clrscr( );  printf("Enter radius & height of the cone:");  scanf("%f %f",&r,&h);  k=(h*h + r*r);  l=pow(k,x);   vol=0.333 * 3.14 * r * r * h;   tsa=3.14 * r * (l+r);  printf("The volume of the cone is:%f\n",vol);  printf("The total surface area of the cone is:%f\n",tsa);  getch( );}

Page 42: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

 VOL OF CUBOID #include<stdio.h>void main( ){  int l,b,h,vol,surfarea;  clrscr( );  printf("Enter length,breadth,height of the cubiod:");  scanf("%d %d %d",&l,&b,&h);   vol=l * b * h;   surfarea=2 * (l*b + b*h + h*l);  printf("The volume of the cuboid is:%d\n",vol);  printf("The total surface area of the cuboid is:%d",surfarea);  getch( );}

VOL OF CUBE #include<stdio.h>void main( ){  int i,vol,surfarea;  clrscr( );  printf("Enter length of the cube:");  scanf("%d",&i);   vol=i * i * i;   surfarea=6 * i * i;  printf("The volume of the cubeis:%d\n",vol);  printf("The surface area of the cube is:%d",surfarea);  getch( );}

PRINTING NUMBERS FROM 1-100 #include<stdio.h>void main( ){  int i;  for(i = 1; i <= 100; i++)   printf("%3d\n",i);}

PRINTING NUMBERS FROM 1-100 #include<stdio.h>void main( ){  int i = 1;  while(i <= 100)   {     printf("%d\n",i);    i++;

Page 43: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

   } getch( );}

PRINTING NUMBERS FROM 1-100 #include<stdio.h>void main( ){  int i = 1;  do   {    printf("%d ",i);    i++;   } while(i <= 100); getch( );}

POS OR NEG #include<stdio.h>void main( ){  int a;  printf("enter a number ");  scanf("%d",&a); if(a>0)   printf("number is positive"); else   printf("number is negative");  getch( );}

EVEN OR ODD #include<stdio.h>void main(){  int n;  clrscr();  printf("enter any number : ");  scanf("%d",&n); if(n == 0)  printf("number is neither even nor odd"); else   {    if(n%2 == 0)     printf("number is even");    else     printf("number is odd number");   }

Page 44: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

  getch();}

EVEN OR ODD #include<stdio.h>void main(){  int n;  clrscr();  printf("enter any number");  scanf("%d",&n);   if(n==0)     printf("number is neither even nor odd");   else    switch(n%2)      {      case 0: printf("number is even");      break;      case 1: printf("number is odd");      break;      }  getch();}

WHICH IS GREATER? #include<stdio.h>void main( ){    int a, b;      printf("Enter the values of a & b : ");      scanf("%d%d",&a,&b);       if(a > b)           printf("%d is greater",a);       else            printf("%d is greater",b);  getch( );}

WHICH IS GREATER? #include<stdio.h>void main( ){  int a, b, max;      printf("Enter the values of a & b : ");      scanf("%d%d",&a,&b);       max = (a > b ? a:b);      printf("%d is greater",max);  getch( );}

Page 45: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

WHICH IS GREATER? #include<stdio.h>void main( ){   int a, b, c, max;      printf("Enter the three numbers : ");      scanf("%d%d%d",&a,&b,&c);       max = (a > b ? (a > c ? a : c) : (b > c ? b : c));      printf("%d is Greater number",max);  getch( );}

ASCII #include<stdio.h>  void main( ){ int i; clrscr( );  for(i = 0; i < 256; i++)    printf("%d = %c ",i,i); getch( );}CONVERTING LOWER TO UPPER #include<stdio.h>void main( ){  char ch;    printf("Enter the character : ");    scanf("%c",&ch);      if(ch >= 'a' && ch <= 'z')        ch = ch - 32;     printf("%c",ch);  getch( );}

HORIZONTAL LINE #include<stdio.h>   void main( ){ int i, n;  printf("Enter the value of n : ");  scanf("%d",&n);   for(i = 1; i <= n; i++)    printf("%c",196); getch( );}

Page 46: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

VERTICAL LINE #include<stdio.h>   void main( ){ int i, n;  printf("Enter the value of n : ");  scanf("%d",&n);   for(i = 1; i <= n; i++)    {     printf("%c",179);     printf("\n");    } getch( );}

SWAPING   TWO   NUMBERS   WITH   USING   ANOTHER   VARIABLE. #include<stdio.h>void main( ){  int c, d, temp;  printf("Enter the values of c and d : ");  scanf("%d%d",&c,&d);   temp = c;   c = d;   d = temp;  printf("New values of c & d are %d and %d",c,d); getch( );}

SWAPING   TWO   NUMBERS   WITHOUT   USING   ANOTHER   VARIABLE. #include<stdio.h>void main( ){  int c, d;  printf("Enter the values of c and d : ");  scanf("%d%d",&c,&d);   c = c + d;   d = c - d;   c = c - d;  printf("New values of c & d are %d and %d",c,d); getch( );}

MULTIPLICATION TABLE #include<stdio.h>void main( ){ int i, n; clrscr( );

Page 47: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

  printf("Enter which table you want : ");  scanf("%d",&n);   for(i = 1; i <= 10; i++)    printf("%2d * %2d = %2d\n",n,i,n*i); getch( );}

MULTIPLICATION TABLE #include<stdio.h>void main( ){ int i, n; clrscr( );  printf("Enter which table you want : ");  scanf("%d",&n);   i = 1;  while(i <= 10)   {    printf("%2d * %2d = %2d\n",n,i,n*i);    i++;   } getch( );}

MULTIPLICATION TABLE #include<stdio.h>void main( ){ int i, n; clrscr( );  printf("Enter which table you want : ");  scanf("%d",&n);   i = 1;  do   {    printf("%2d * %2d = %2d\n",n,i,n*i);    i++;   }   while(i <= 10); getch( );}

SUM   OF   DIGITS. #include<stdio.h>void main( ){  long int sum, number;  printf("Enter the five digit number : ");  scanf("%ld",&number);

Page 48: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

   sum = 0;    while (number != 0)      {      sum = sum + number % 10;      number = number / 10;      }   printf("sum of digits of given number is %ld",sum);  getch( );}

REVERSING   A   NUMBER. #include<stdio.h>void main( ){  long int number, reverse; clrscr( );  printf("Enter the number which is to be reversed : ");  scanf("%ld",&number);   reverse = 0;    while ( number != 0 )     {      reverse = reverse * 10 + number % 10;      number = number/10;     }  printf("Reverse number is %ld\n",reverse);  getch( );}

PALINDROME. #include<stdio.h>void main( ){  int n, reverse, x; clrscr( );  printf("Enter a five digit number : " );  scanf("%d",&n);   x = n;   reverse = 0;  while(n != 0)     {      reverse = reverse*10 + n%10;      n = n/10;     }  printf("Reverse number is %d\n",reverse);   if(x == reverse)     printf("original and reverse no.'s are equal");   else     printf("original and reverse no.'s are not equal");

Page 49: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

 getch( );}

ARMSTRONG NUMBERS. #include<stdio.h>void main( ){  int i, x, y, z; clrscr( );  for(i = 1; i <= 500; i++)   {    x = i%10;    y = (i/10)%10;    z = i/100;   if(x*x*x+y*y*y+z*z*z == i)    printf("Armstrong number is %d\n",i);   } getch( );}

F TO C #include<stdio.h>void main( ){  float c,f;  clrscr( );  printf("Enter value of the temperature in farenheit:");  scanf("%f",&f);   c = 5*(f-32)/9;  printf("Temperature in the centigrade is: %f",c);  getch( );}

FACTORIAL #include<stdio.h>void main( ){  int n, fac, i; clrscr( );  printf(" Enter the number : ");  scanf("%d",&n);  fac = 1;   for(i = 1; i <= n; i++)    fac = fac * i;  printf(" Factorial is %d ",fac);  getch( );}

Page 50: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

COMBINATIONS OF 1,2,3 #include<stdio.h>void main( ){  int i, j, k, n = 0; clrscr( );  for(i = 1; i <= 3; i++)  {   for(j = 1; j <= 3; j++)   {    for(k = 1; k <= 3; k++)    {     printf("%d%d%d\n",i,j,k);     n++;    }   }  }  printf(" The total no. that can be formed are %d",n); getch( );}

LEAP   YEAR. #include<stdio.h>void main( ){  int year; clrscr( );  printf(" Enter any year ");  scanf("%d",&year);   if(( year%400 == 0) || (( year%4 == 0) && (year%100 != 0)))     printf("It is a leap year");   else     printf("It is not a leap year"); getch( );}

FIBONACCI   SERIES void main( ) { int m, n, temp, i; clrscr( );  m = 0;  n = 1;   printf("%d %d ",m,n);    for(i = 1; i < 10; i++)     {      temp = n;      n = m + n;      m = temp;

Page 51: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

      printf("%d ",n);     }getch( );}

PRIME NUMBERS. #include<stdio.h>void main( ){  int n, i ; clrscr( );   for(n = 2; n <= 300; n++)   {   i = 2;    while(i <= n-1)     {      if(n % i == 0)      {      break;      }    i++;    }  if(i == n)   printf(" %3d",n);   } getch( );}

PERFECT   NUMBERS. #include<stdio.h>void main( ){ int num, divisor, sum;  clrscr( );  num = 1;   while(num < 100)   {    sum = 0;    divisor = 1;    while(divisor <= num/2)     {      if(num%divisor == 0)        sum = sum + divisor;        divisor++;     }   if(sum == num)      printf("\n %4d is a perfect number \n",num);      num++;   }

Page 52: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

 getch( );}

FLOYDS   TRIANGLE #include<stdio.h>void main( ){ int max, row, count, num1 = 1, num2;  printf("Give the max number of rows : ");  scanf("%d",&max);   row = 1;  while(row <= max)   {    count = row;    num2 = num1;    while(count != 0)    {     printf("%d ",num2);     num2 = !num2;     count--;    }   printf("\n");    num1 = !num1;   row++;   } getch( );}

AUTOMORPHIC   NUMBERS #include<stdio.h>void main( ){ int num=1, divisor;  while(num < 1000)   {   if(num < 10)     divisor = 10;    else     if(num < 100)      divisor = 100;     else      divisor = 1000;  if(num == (num * num) % divisor)    printf("%d is a perfect number \n",num);    num++;   } getch( );}

Page 53: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

1.i) Evaluation of the algebraic expressions ax + b / ax – b#include<stdio.h>#include<conio.h>#include<math.h>void main(){int a,b,x;float c,d,r;clrscr();printf(" ENTER THE VALUES OF A,B,X ");scanf("%d%d%d",&a,&b,&x);c=a*x+b;d=a*x-b;r=c/d;printf(" THE VALUE OF ax+b/ax-b IS %f ",r);getch();}

1. ii) Evaluation of the algebraic expressions2.5 log x + cos 320 + | x2 – y2 | + Ö 2xy

#include<stdio.h>#include<conio.h>#include<math.h>void main(){int x,y,c,d,e,z;float r;clrscr();prnif(" ENTER THE VALUE OF X AND Y ");scanf("%d%d",&x,&y);c=2.5*log(x);d=cos(32);e=abs(pow(x,z)-pow(y,z);z=sqrt(2*x*y);r=c+d+e+z;printf(" THE VALUE IS %f",r);getch();}

1.iii) Evaluation of the algebraic expressions1 / aÖ2p – ( x – m / Ö2s )2

#include<stdio.h>#include<conio.h>#include<math.h>void main(){float a,s,e,x,m,p,q,r,t,u;clrscr();prnif(" ENTER THE VALUE OF A,X,S AND M ");

Page 54: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

scanf("%f%f%f%f",&a,&x,&s,&m);p=a*(sqrt(2*s*e));q=(x-m/sqrt(2*s));r=q*q;t=p-r;u=1/t;printf(" THE VALUE IS %f ",u);getch();}

a) (4x + 3)(3y + 2z - 4) #include<stdio.h>#include<math.h>void main(){ float x, y, z, result; clrscr(); printf("Enter the values of x, y, z : "); scanf("%f%f%f",&x,&y,&z); result = (4 * x + 3) * (3 * y + 2 * z - 4); printf("Result is %f",result); getch();}

e) ae-kt

#include<stdio.h>#include<math.h>void main(){ float a, k, t, result; clrscr(); printf("Enter the values of a, k, t : "); scanf("%f%f%f",&a,&k,&t); result = a * exp(-k * t); printf("Result is %f",result); getch();}

2 Write a C Program to find out biggest among three numbers.#include<stdio.h>#include<conio.h>void main(){int a,b,c;clrscr();printf("enter the three nos \n");scanf("%d%d%d",&a,&b,&c);if(a>b)

if(a>c)

Page 55: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

printf("greatest no %d:",a);else

printf("greatest no %d:",c);else if(b>c)

printf("greatest no %d:",b);else

printf("greatest no %d:",c);getch();}

3. Write a C Program to calculate total, average & grade of the student, after reading five subject marks.#include<stdio.h>#include<conio.h>void main(){int m1,m2,m3,m4,m5,tot;float ave;clrscr();printf("enter the marks \n");scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);tot=m1+m2+m3+m4+m5;ave=tot/3;printf("total marks :%d\npercentage:%f\n",tot,ave);if(m1>=40&&m2>=40&&m3>=40&&m4>=40&&m5>=40){if(ave>=60)printf("first division");else if(ave>=50)printf("second division");elseprintf("pass"); }elseprintf("fail");getch();}

4. Write a C Program to find the factorial of the given number.Exampl1:#include<stdio.h>#include<conio.h>void main(){int i,n;long int fact;clrscr();printf("enter the number");scanf("%d",&n);

Page 56: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

i=1;fact=1;while(i<=n){fact=fact*i;i++;}printf("\n factorial %ld ",fact);getch();}

Example2:#include<stdio.h>#include<conio.h>void main(){int i,fact,n;clrscr();printf("enter the no :");scanf("%d",&n);fact=1;for(i=1;i<=n;i++)fact=fact*i;printf("factorial of %d is %d ",n,fact); getch();}

5a) Printing three given integers in ascending order#include<stdio.h>#include<conio.h>void main(){int a,b,c;clrscr();printf(" ENTER THE VALUES OF A, B AND C ");scanf("%d%d%d",&a,&b,&c);if(a>b &&a>c){if(b>c)printf(" THE ORDER IS %d %d %d ",c,b,a);elseprintf(" THE ORDER IS %d %d %d ",b,c,a);}if(b>a &&b>c){if(a>c)printf(" THE ORDER IS %d %d %d ",c,a,b);elseprintf(" THE ORDER IS %d %d %d ",a,c,b);

Page 57: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

}else{if(a>b)printf(" THE ORDER IS %d %d %d ",b,c,a);elseprintf(" THE ORDER IS %d %d %d ",a,b,c);}getch();}

5b) Sum of 1 + 2 + 3 + ----------------n#include<stdio.h>#include<conio.h>void main(){int i,n;lint sum;clrscr();printf("enter the number");scanf("%d",&n);i=1;sum=0;while(i<=n){sum=sum*i;i++;}printf("\n sum = %d ",sum);getch();}

5c) Write a C program for 1 + x2/2! + x4 / 4! + ---------------- up to ten terms/*program to calculate sum of the sin series*/#include<stdio.h>#include<conio.h>void main(){int x,n,num=1,den=1,i=0;float sum=1,term;printf(“\nEnter x value : “);scanf(“%d”,&x);printf(“\nEnter no of terms : “);scanf(“%d”,&n);while(i<2n){num=num*x*x;den=den*(i+1)*(i+2);term=num/den;

Page 58: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

sum=sum+term;i=i+2;}printf(“\nSum of the series is : %d”,sum);getch();}

5d) Write a C program for x + x3 / 3! + x5 / 5! + ------------- up to 7th digit accuracy#include<stdio.h>#include<conio.h>void main(){int x,n,num=1,den=1,i=1;float sum,term;printf(“\nEnter x value : “);scanf(“%d”,&x);printf(“\nEnter no of terms : “);scanf(“%d”,&n);sum=x;while(i<2n){num=num*x*x;den=den*(i+1)*(i+2);term=num/den;sum=sum+term;i=i+2;}printf(“\nSum of the series is : %d”,sum);getch();}

5e) Read x and compute Y = 1 for x > 0 Y = 0 for x = 0 Y = -1for x < 0#include<stdio.h>#include<conio.h>main(0{int x,y;printf(“\nEnter value for x : “);scanf(“%d”,&x);if(x>0)

y=1;else if (x==0)

y=0;else

y=-1;printf(“\nThe value of Y is = %d”,y);getch();

Page 59: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

}

7) Write C program using While loop to reverse the digits of the Number. Example 1234 to be written as 4321.#include<stdio.h>#include<conio.h>void main(){int i,n,r,s=0;clrscr();printf(" ENTER THE VALUE OF N ");scanf("%d",&n);while(n>0){r=n%10;s=s*10+r;n=n/10;}printf(" REVERSE OF THE GIVEN NUMBER IS %d", s);getch();}

8) Write C program to calculate Sum of digits of the given Number.#include<stdio.h>#include<conio.h>void main(){int i,n,r,s=0;clrscr();printf(" ENTER THE VALUE OF N ");scanf("%d",&n);while(n>0){r=n%10;s=s+r;n=n/10;}printf(" THE SUM OF THE DIGITS OF GIVEN NUMBER IS %d", s);getch();}

9.i) Write a C program to find whether the given number is Palindrome or not#include<stdio.h>#include<conio.h>void main(){int i,n,r,s=0,n1;clrscr();printf(" ENTER THE VALUE OF N ");

Page 60: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

scanf("%d",&n);n1=n;while(n>0){r=n%10;s=s*10+r;n=n/10;}if(s==n1)printf("%d IS A PALLINDROME",n1);elseprintf("%d IS NOT A PALLINDROME",n1");getch();}

9.ii) Write a C program to find whether the given number is Armstrong or not.(sum of cubes)#include<stdio.h>#include<conio.h>void main(){int i,n,r,s=0,n1;clrscr();printf(" ENTER THE VALUE OF N ");scanf("%d",&n);n1=n;while(n>0){r=n%10;s=s+r*r*r;n=n/10;}if(s==n1)printf("%d IS AN AMSTRONG NUMBER",n1);elseprintf("%d IS NOT AN AMSTRONG NUMBER",n1);getch();}

9.iii) Write a C program to find whether the given number is Perfect or not.(sum of factors) #include<stdio.h> #include<conio.h> void main() { int i,n,sum=0; clrscr(); printf("enter a number"); scanf("%d",&n); for(i=1;i<n;i++)

Page 61: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

if(n%i==0) sum=sum+i;

if(sum==n) printf("%d is prfect number",n);

else printf ("%d is not perfect number",n);

}

9.iv) Write a C program to find whether the given number is Strong or not.(sum of factorials)#include<stdio.h>#include<conio.h>void main(){int n,s,r,f,t;clrscr();printf("enter any number");scanf("%d",&n);t=n;s=0;while(n>0){r=n%10;f=1;while(r>0){f=f*r;r--;}s=s+f;n=n/10;}if(s==t)printf("number is strong");elseprintf(" number is not strong");getch();}

9.iv) Write a C program to find whether the given number is Strong or not.(sum of factorials)#include<stdio.h>#include<conio.h>void main(){int n,s,r,f,t;clrscr();printf("enter any number");scanf("%d",&n);

Page 62: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

t=n;s=0;while(n>0){r=n%10;f=1;while(r>0){f=f*r;r--;}s=s+f;n=n/10;}if(s==t)printf("number is strong");elseprintf(" number is not strong");getch();}

10.ii) Write C program using FOR statement to find the following from given set of n integers Print all even and odd integers.include<stdio.h>#include<conio.h>void main(){int n,i;clrscr();printf("\nenter the number n : ");scanf("%d",&n);printf(“\nodd numbers even numbers”);for(i=1;i<=n;i++)if(i%2==0)

printf(“\n %d”,i);else

printf(“\n %d”,i);getch();}

10.iii) Write C program using FOR statement to find the following from given set of n integers Sum of all even and odd integers.#include<stdio.h>#include<conio.h>void main(){int n,i,esum=0,osum=0;clrscr();printf("\nenter the number n : ");

Page 63: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

scanf("%d",&n);for(i=0;i<=n;i++)if(i%2==0)

esum=esum+i;else

osum=osum+i;printf("\nodd sum = %d, even sum = %d”,osum,esum);getch();}

18) Write a C program to find the factorial of the given number using recursive function.#include <stdio.h>#include <conio.h>void main(){int f,n;clrscr();printf("\nEnter number n : ");scanf("%d",&n);f=fact(n);printf("\nfactorial of n = %d",f);getch();}int fact(int n){int f;if (n==1) return 1;else{f=n*fact(n-1);return f;}}

11) The total distance traveled by vehicle in ‘t’ seconds is given by distance = ut + 1/2at2

where ‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find the distance traveled a regular intervals of time given the values of ‘u’ and ‘a’. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of ‘u’ and ‘a’.#include<stdio.h>#include<conio.h>void main(){int count=1,op;float u,t,a,distance;clrscr();while(1){

Page 64: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

printf("\nenter the values of u,a,t.. \n");scanf("%f%f%f",&u,&a,&t);distance=(u*t)+(0.5*a*t*t);printf("distance...%f",distance);printf(“\nAre u want to continue. More calculations. : “);scanf(“%d”,&op);if(op==0)break;}getch();}

12(i). Write C program using FOR statement to print the following outputs11 21 2 31 2 3 41 2 3 4 5

#include<stdio.h>void main(){

int n,i,j;printf("Enter the no. of lines");scanf("%d",&n);for(i=1;i<=n;i++){

for(j=1;j<=i;j++)printf("%5d ",j);

printf("\n");}

getch();}

12(ii). Write C program using FOR statement to print the following outputs 1

2 23 3 3

4 4 4 45 5 5 5 5

#include<stdio.h>void main(){

int n,i,j;clrscr();printf("Enter the no. of lines");scanf("%d",&n);

Page 65: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

for(i=1;i<=n;i++){

for(j=4-i;j>=0;j--)printf(“ “);for(j=1;j<=i;j++)

printf("%5d ",i);printf("\n");

}getch();}

14) Write a C program to find the largest and smallest element in an array#include <stdio.h>void main(){int a[10],i,max,min;clrscr();printf("\n Enter the elements in array ");for (i=0;i<10;i++)scanf("%d",&a[i]);max = a[0];min=a[0];for(i=1;i<10;i++){if (max < a[i])max = a[i];if (min>a[i])min=a[i];}printf("\n The larges element is %d",max);printf("\n The least element is %d",min);getch();}

17.i) The Fibonacci sequence of numbers is 1,1,2,3,5,8… based on the recurrence relation f(n) = f(n-1) + f(n-2) for n>2. #include<stdio.h>#include<conio.h>void main(){

int n,i;int fib(int);clrscr();printf("\nEnter the number of terms");scanf("%d",&n);for(i=0;i<n;i++)

printf("%d\t",fib(i));getch();

}

Page 66: KEY WORDS - WordPress.com€¦  · Web viewCONTROL STATEMENTS: ‘C’ is considered as a structured programming language. One of the reasons for this is having various program control

int fib(int n){

if(n==0||n==1)return 1;

elsereturn fib(n-1)+fib(n-2);

}

17.ii) The Fibonacci sequence of numbers is 1,1,2,3,5,8… based on Non-recursive#include<stdio.h>

#include<conio.h>void main(){ int t1=0,t2=1,t3,n,i;clrscr(); printf("enter no of terms:\n"); scanf("%d",&n); printf(" the series is\n "); printf("%d\n%d",t1,t2); for(i=1;i<=n;i++) { t3=t1+t2;

printf("\n%d",t3);t1=t2;t2=t3;

} getch(); }