SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, 2016-17) SOLUTION Program: B. Tech. Branch: All Term:I Subject: Logic Building and Problem Solving Using C Paper Code: CSE110 Date: Shift: Time: M.M: 20 Note: (i) No student will leave the room till end of the examination. (ii) Attempt all questions. SECTION A Q.1 Attempt any FOUR questions in this section: (i) Why we require a translator in any programming language? Discuss various types of translators available for computer languages. Programming means giving some instructions to the computer by a programmer to achieve a particular task. In this process two objects are communicating; (1) Computer that knows only machine language i.e. everything in the form of computer, (2) Programmer that knows a programming language in which most of the words have been taken from English i.e. English like language. To make conversation possible we need a third object (translator) having knowledge of both the language. Translators are as follows. i. Assembler ii. Compiler iii. Interpreter Assembler: An assembler translates the symbolic codes of programs of an assembly language into machine language instructions Compiler: A compiler searches all the errors of a program and lists them. If the program is error free then it converts the code of program into machine code and then the program can be executed by separate commands. Interpreter: An interpreter checks the errors of a program statement by statement. After checking one statement, it converts that statement into machine code and then executes that statement. The process continues until the last statement of program occurs. (ii) What would be the output of following C programs a)void main(){ int i=2,j=3,k,l; float a,b; k = i / j * j ; l = j / i * i ; a = i / j * j ; b = j / i * i ; printf("%d %d %f %f",k,l,a,b); OUTPUT b) void main(){ int a=4,b=3,c=2,d; float w=1.5,x=2.5,y=3.5,z; d = a + b /(w+c) * x - y; z = c/a % b + x *a / y w; printf( d=%d ; z=%f,d,z); OUTPUT
(iii) Write a program in C to add two distances given in kilometers and meters. #include<stdio.h> #include<conio.h> void main() { //Variable declaration part int km1,m1,km2,m2,km3,m3,convertedkm; //receive input from user printf( Enter first distance (Kilometer and Meter):\n ); scanf( %d%d,&km1,&m1); printf( Enter second distance (Kilometer and Meter):\n ); scanf( %d%d,&km2,&m2); //calculate sum m3 = m1 + m2; convertedkm = m3/1000; m3 = m3 % 1000; km3 = km1 + km2 + convertedkm; //print output printf( Distance1:\t%d\t%d\n,km1,m1); printf( Distance2:\t%d\t%d\n,km2,m2); printf( ------------------------------------- ); printf( Distance3:\t%d\t%d,km3,m3); (iv) Write an algorithm to convert number of seconds into equivalent Hours minutes and seconds. INPUT: Number of Seconds OUTPUT: equivalent time in Hours:Minutes: Seconds ALGORITHM: 1. Start 2. Take number of seconds from user, N_S 3. Calculate Hours = N_S / 3600 4. Calculate R_S = N_S % 3600 5. Calculate Minutes = R_S / 60 6. Calculate Seconds = R_S % 60 7. Print Hours:Minutes:Seconds 8. End
(v) What is a data type in C? Discuss all basic data types available in C along with their modifiers. A data type defines a set of values and the operations that can be performed on them with size required in memory. C provides the programmer with FIVE basic data types These are: integer character float double void Modifying the Basic Types Except for type void, the basic data types may have various modifiers preceding them. You use a modifier to alter the meaning of the base type to fit various situations more precisely. The list of modifiers is shown here: signed unsigned long short You can apply the modifiers signed, short, long, and unsigned to integer base types. You can apply unsigned and signed to characters. You may also apply long to double. SECTION B Q.2 Attempt any TWO questions in this section: (i) Write an algorithm to find if the reverse of a number is equals to it or not. (Assume that number is having 5 digits) INPUT: A Five Digit Number OUTPUT: Yes, if given number and its reverse is equal, No otherwise ALGORITHM: 1. Start 2. Take a Five Digit Number, N //separate each digit from the number 3. Calculate D1 = N % 10 //first digit 4. Calculate N = (N D1) / 10 5. Calculate D2 = N % 10 //second digit 6. Calculate N = (N D2) / 10 7. Calculate D3 = N % 10 //third digit 8. Calculate N = (N D3) / 10 9. Calculate D4 = N % 10 //fourth digit 10. Calculate N = (N D4) / 10 11. Calculate D5 = N % 10 //fifth digit //calculate reverse of the number 12. Calculate Rev_N = D1*10000 + D2*1000 + D3*100 + D4*10 + D5 13. If N == Rev_N then GOTO Step 14 Else GOTO step 15 14. Print YES GOTO 16 15. Print NO 16. End
(ii) Draw a flow chart that receives two integers A and B and performs following operations: a) If A is greater than B and divisible by B then print quotient of A/B. b) If A is greater than B and not divisible by B then print remainder of A/B. c) If B is greater than A then calculate and print A*B d) If A and B are equal print a message There is a tie! (iii) Solve the following number conversion: a) (ABC.D) 15 = (? ) 13 c) (125.45) 11 = (? ) 9 (ABC.D) 15 = ( 2427.8666 ) 10 = ( 1149.B35B ) 13 (125.45) 11 = (148.4049 ) 10 = ( 174.3571 ) 9 b) (246) 8 = (? ) 16 d) (FFF.AA) 16 = (? ) 8 (246) 8 = ( 166 ) 10 = ( A6 ) 16 (FFF.AA) 16 = ( 4095.664 ) 10 = (7777.5244 ) 8
SECTION C Q. 3 Draw a flow chart and write a program in C that will add 5 in each digit of a 4 digit number and map it between 0 and 4 according to following mapping: Calculated Digit 5 6 7 8 9 10 11 12 13 14 Mapped Digit 0 1 2 3 4 0 1 2 3 4
#include<stdio.h> #include<conio.h> void main() { //declare all required variables int Num, d1, d2, d3, d4, New_Num; //Receive input printf( Enter a Four Digit Number ); scanf( %4d,&Num); //Mapping of Digits d1 = (Num % 10 + 5) % 5; Num = (Num d1) / 10; d2 = (Num % 10 + 5) % 5; Num = (Num d2) / 10; d3 = (Num % 10 + 5) % 5; Num = (Num d3) / 10; d4 = (Num % 10 + 5) % 5; New_Num = d4*1000 + d3*100 + d2*10 +d1; //output printf( New Number (After Processing Each Digit) is: %d,new_num);