INTERNAL ASSESSMENT TEST 1 SOLUTION PART 1 1 a Define algorithm. Write an algorithm to find sum and average of three numbers. 4 An Algorithm is a step by step procedure to solve a given problem in finite number of steps by Accepting a set of inputs and Producing the desired output for the given problem. step 1: [start] Step2: [input the value of three numbers] read a,b,c Step3: [ calculate the value of average] Avg.= (a+b+c)/3. Step4: [display the calculated average]write Avg. Step 5: [finished] Stop b Draw a flowchart for printing the largest number among three numbers. 4
2 a Explain the basic structure of a C program. 4 Documentation section (Comments) Link section Definition section (Pre-processor directives) (Define symbolic Constant) Global declaration section main( ) function local declarations part execution part(statements) Sub program section (user defined functions) function 1 function 2 It is a program that processes our source program before it is passed to the compiler. Preprocessor commands (often known as directives). The preprocessor statement starts with symbol #. The normal preprocessor used in all programs is include. The #include directive instructs the preprocessor to include the specified file contents in the beginning of the program. main(): It is called the main function An entry point is where control enters a program or piece of code. Every program must have main ( ) since this is where the computer begins to execute the program. The main ( ) function is divided into two parts: 1) Declaration Section: The variables that are used within the function main () should be declared in the declaration section only. The variables declared inside a function are called as local variables.
2) Executable Section : This contains the instruction given to the compiler to perform a specific task. The task may be to (a) display message (b) read data (c) perform calculations. Comments are those portions of the code which is ignored by the compiler. The comment allow the user to make simple notes in the source code. // this is used for sinlge line comment. /* this is used for multiple line comment. */ b Define Identifiers, keywords and constant. 4 Identifier: As the name indicates, identifier is used to identify various entities of program such as variables, constants, functions etc. In other words, an identifier is a word consisting of sequence of LettersDigits or _ underscore Keywords: Keywords are those words, which have predefined meaning to the compiler. Each keyword has fixed meaning and that cannot be changed by user. Hence, they are also called reserved- words Keywords cannot be used as a variable or function. All keywords should be written in lower letters. Constant: Data items that do not change their values while the program executes, are called constants. The constant cannot be modified in the program. For example: 1, 3,2.55, z. Types of Constant: 1) Integer Constant: An integer is a whole number without any fractional part. 2) Real Constant: Real Constant is a fractional number. 3) Character Constant: A symbol enclosed within a pair of single quotes ( ) is called a character constant.
Each character is associated with a unique value called an ASCII (American Standard Code for Information Interchange) code. 4) String Constant: A sequence of characters enclosed within a pair of double quotes ( ) is called a string constant. The string always ends with NULL (denoted by \0) character. For example: 9, a, hello 5) Escape Sequence Characters: An escape sequence character begins with a backslash and is followed by one character. A backslash (\) along with some characters give rise to special print effects by changing the meaning of some characters. PART 2 3 a What is a Variable? Explain the rules for constructing variables in C language. Give examples for valid and invalid variables. A variable is an identifier whose value can be changed during execution of the program. Σ The memory location name where the data can be stored. Rules for defining a variable: 1) The first character in the variable name must be an alphabet or underscore. 2) No commas or blanks are allowed within a variable name. 3) No special symbol other than an underscore can be used in a variable name. 4) A variable name is any combination of alphabets, digits or underscores. Valid Variables: a_b, g1, rem2 Invalid Variables: 1a, h$ b Two numbers are input through the keyboard into two locations a and b. WAP to interchange the contents of a and b using third variable. #include<stdio.h void main() int a,b,c; printf("\nenter the values of a and b"); scanf("%d%d",&a,&b); printf("\nbefore Swaping the values of a=%d and b=%d",a,b); 4 4
c=a; a=b; b=c; printf("\nafter Swaping the values of a=%d and b=%d",a,b); 4 a Explain the primary data types and their variants. Also mention the format specifiers for the same. Data Types: The data type defines the types of data stored in a memory location. Character Integer Float Void Character Data type: Keyword for character is char. It takes 1 byte in memory. Chars, signed and unsigned: Signed: When the variable is having signed like positive or negative. Unsigned: When the variable value is having no signed. A signed char is same as an ordinary char and has a range from -128 to 127; whereas, an unsigned char has a range from 0 to 255. Integer Data Type: Keyword for integer is int. Integer, long and short: Integer can also be classified as short int and long int. The intension of providing these variations is to provide integers with different ranges. Example: short int I; or short I; longint j; or long j; Integers, signed and unsigned: When the value stored in a given integer variable having positive or negative signed that comes under signed integer. Example: signed int I; When the variable is being used to only count things having no signed then it comes under unsigned integer. Example: unsigned int j; Floats and Doubles: A float occupies 4 btyes in memory. If this is insufficient, then C offers a double data type that occupies 8 bytes in memory. If this is insufficient, then C offers a longdouble data type that occupies 10 bytes in memory. 4
b Example: float I; double j; long double f; Type Code Example Char: %c Short int : %hd Int : %d Long int: %ld Long long int : %lld Float : %f double : %f Long double : %Lf Write a C program to read a year as an input and find whether it is a leap year or not. Also consider end of the centuries. #include<stdio.h> void main() int year: printf( enter the year ); scanf( %d,&year); if (year%100==0) if( year%400==0) printf( year is leap year ); printf( year is not a leap year ); if (year%4==0) printf( year is leap year ); printf( year is not a leap year ); 4 PART 3 5 a What do you mean by unary, binary and ternary operator explain briefly? 4 1) Unary Operators: Unary operations operate on a single operand, therefore the number 5 when operated by unary will have the value 5. a) Addition or Unary plus ( +). example: a = +5 b) Subtraction or Unary Minus (-). Example: a= -7
b c) Increment or Decrement operator (++, --). Example: If a=5 then ++a will be a=6, b=4 then b will be b=3. d) Logical Not (!). Example: A=1 then (! A) will be A=0 e) Bitwise Complement (~). Example: A=1001 then (~A) will be A=0110 f) Address of (&). Example: printf( %d,&a) will print the address of A g) Pointer dereference (*). Example: printf( %d,*a) will print the value at that address 2) Binary operators: A binary operator is an operator that operates on two operands and manipulates them to return a result. Operators are represented by special characters or by keywords and provide an easy way to compare numerical values or character strings. Binary operators are presented in the form: Operand1 Operator Operand2. (a) Arithmetic Operators ( +,-,*,/,%) (b) Relational Operators (<,<=,>,>=,==,!=) (c) Logical Operators (&&, ) (d) Assignment Operators and shorthand assignment operators ( =,+=,-=,etc.) (e) Bitwise Operators (&,,^,<<,>>) 3) Ternary Operators: a ternary operator is an operator that takes three arguments. The arguments and result can be of different types. Many programming languages that use C-like syntax feature a ternary operator,?:, which defines a conditional expression. Exp1?Exp2:Exp3 If exp1 is true ans will be exp 2 otherwise ans will be exp3. A cashier has currency notes of denominations 10,50 and 100. If the amount to be withdrawn is input through the keyboard, write a program to find the total number of currency notes of each denomination the cashier will have to give to the withdrawer. #include<stdio.h> void main() int amount, nohun, nofifty, noten; printf( Enter amount to be withdrawn ); scanf( %d,&amount); nohun=amount/100; amount=amount%100; nofifty=amount/50; 4
amount=amount%50; noten=amount/10; printf( No of hundred notes=%d,nohun); printf( No of fifty notes=%d,nofifty); printf( No of ten notes=%d,noten); 6 a Explain Precedence and Associativity with examples. 4 Precedence : An arithmetic expression without parenthesis will be evaluated from left to right using the rules of precedence of operators. There are two distinct priority levels of arithmetic operators in C. High priority * / % Low priority + - Rules for evaluation of expression Σ First parenthesized sub expression left to right are evaluated. Σ If parentheses are nested, the evaluation begins with the innermost sub expression. Σ The precedence rule is applied in determining the order of application of operators in evaluating sub expressions. Σ The associability rule is applied when two or more operators of the same precedence level appear in the sub expression. Σ Arithmetic expressions are evaluated from left to right using the rules of precedence. Σ When Parenthesis is used, the expressions within parenthesis assume highest priority. Associativity Each operator in c has a precedence associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. There are distinct levels of precedence and an operator may belong to one of these levels. The operators at the higher level of precedence are evaluated first. The operators of the same precedence are evaluated either from left to right or from right to left depending on the level. This is known as associativity property of an operator.
b Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number using bitwise operator. #include<stdio.h> void main() int a; printf( Enter the no ); scanf( %d,&a); if(a&1==0) printf( the number is even ); printf( the number is odd ); PART 4 7 a What do you mean type Conversion? Explain the two types of conversion with examples. 4 4 Type conversion is used to convert data of one type to data of another type. Type conversion is of 2 types as shown in below figure: Data Type Hierarchy: int->unsigned int -> long-> unsigned long->float-> double-> long double Implicit Conversion Σ If a compiler converts one type of data into another type of data automatically, it is known as implicit conversion.
There is no data loss in implicit conversion. The conversion always takes place from lower rank to higher rank. For ex: int a =22, b=11; Float c= a; //c becomes 21.000000. Explicit Conversion When the data of one type is converted explicitly to another type with the help of some pre defend functions, it is called as explicit conversion. There may be data loss in this process. Syntax : data_ type1 v1; data_type2 v2=(data_type2) v1; b What will be the output for the following code? 4 void main( ) int a =5, b=2, res1; float f1= 5.0, f2=2.0, res2; res1= 5/2.0 + a/2 + a/b; res2= f1/2 * f1 f2; printf( res 1= %d res2=%f, res1,res2); res 1=6,res2=10.500000 m=12 n=12 void main() int i=5,j=6,m,n; m=++i + j++; n= --i + j-- ; printf( m=%d n=%d,m,n); 8 a Write a program to implement a simple calculator using switch statement. The program should display the following menu and work according to the user choice. 1: Addition 2: Subtraction 3: Multiplication 4: Division 5: Modulus. Enter your choice #include<stdio.h> void main() int num1,num2,result; char operator; printf( enter the value of num1,operator, num2, \n"); scanf("%d%c%d",&num1,&operator,&num2); switch(operator) case + : result=num1+num2; break; case - : result=num1-num2; break; case * : result=num1*num2; 8
break; case / : result=num1/num2; break; case % : result=num1/num2; break; default: printf( Invalid operator"); printf( \nresult: %d %c%d= %d", num1,operator,num2,result); PART 5 9 a What are two-way selection statements? Explain if, if, and cascaded if with examples. Two Way Selection Statement: Decision of choosing one of the 2 alternatives. There are various types of two selection statements are as follows: 8 Decision Statements: A decision control instruction can be implemented in C using: (A) The if statement. (B) The if statement. (C) Nested if statement. (D) Cascaded or Ladder if statement. f statement: The keyword if tells the compiler that what follows is a decision control nstruction. If the condition whatever it is, is true, then the statement is executed. If the condition is not true, then the statement is not executed; instead the program skips past it. Syntax of if: if(this condition is true) execute this statement; Example: #include<stdio.h> void main( ) int num; printf( Enter a number less than 10 ); scanf( %d,&num); if(num<10) printf( XYZ );
The if statement: The keyword if tells the compiler that what follows is a decision control instruction. If the condition whatever it is, is true, then the statement is executed. If he condition is not true, then the statement is executed. In both the cases only one portion will be executed according to the if condition. Example: #include<stdio.h> void main() int a; clrscr(); printf( Enter the no ); scanf( %d,&a); if(a%2==0) printf( the number is even ); printf( the number is odd ); Nested if statement: A series of decisions to be made in an order General form if(test exp1) if(test exp2) stat-1; stat-2;
Stat-x; stat-3; Example: int a,b,c; if(a<b) if(a<c) printf( a is smallest\n ); printf( c is smallest\n ); if(b<c) printf( b is smallest\n ); printf( c is smallest\n ); Cascaded or Ladder if statement. When multi path decision has to be taken i.e a group of alternatives is available and one alternative has to be chosen Another way of putting if.. together
General form if(test exp1) stat-1; if(test exp2) stat-2; if(test exp3) stat-3; default-stat; Example: : #include<stdio.h> void main() int a,b,c; printf( enter the numbers); scanf( %d%d%d,&a,&b,&c); if(a>b&&a>c) printf( A is largest ); if (b>a&&b>c) printf( B is largest ); printf( C is largest );
10 a Design and develop a flowchart that takes three coefficients (a, b, and c) of a Quadratic equation (ax 2 +bx+c=0) as input and compute all possible roots. Implement a C program for the developed flowchart and execute the same to output the possible roots for a given set of coefficients with appropriate messages. #include<stdio.h> #include<math.h> void main() 8 float r1,r2,p1,p2,a,b,c,disc; printf("enter the co-efficients\n"); scanf("%f%f%f",&a,&b,&c); if(a==0) printf("the coefficients are zero\n"); disc=b*b-4*a*c; if(disc>0) r1=(-b+sqrt(disc))/(2*a); r2=(-b-sqrt(disc))/(2*a); printf("roots are distinct\n r1=%f\t r2=%f\n",r1,r2); if(disc==0) r1=r2=-b/(2*a); printf("roots are equal \n r1=%f \t r2=%f \n",r1,r2); p1=-b/(2*a); p2=(sqrt(-1*disc))/(2*a); printf("roots are complex\n r1=%f+i%f\t r2=%f-i%f\n",p1,p2,p1,p2);