P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS DEPARTMENT OF SCIENCE AND HUMANITIES ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS) EVEN SEMESTER FEB 07 FACULTY: Dr.J Surya Prasad/Ms. Saritha/Mr. Sreenath M.V/Ms.Swathi G SUBJECT / CODE: Programming in C and Data Structures- 5PCD3 PART a Write an algorithm to compute Simple Interest that takes three inputs as p,t,r. Ans Algorithm:To compute simple interest Step:start Step:Read p,t,r Step3:compute SI<-p*t*r Step:Display SI Step5:stop b What is variable? Explain the rules for constructing variables in C-Language. Give examples for valid and invalid variables. Ans A variable is an identifier whose value can be changed during execution of the program. In other words, a variable is a name given to a memory-location where the data can be stored. Using the variable-name, the data can be stored in a memory-location and accessed or manipulated Rules for defining a variable ) The first character in the variable should be a letter or an underscore ) The first character can be followed by letters or digits or underscore 3) No extra symbols are allowed (other than letters, digits and underscore) ) Length of a variable can be up to a maximum of 3 characters 5) Keywords should not be used as variable-names Valid variables: a, principle_amount, sum_of_digits Invalid variables: 3fact //violates rule sum= sum-of-digits 6$ //violates rule 3 for int if //violates rule 5 A What are data types? Mention the different data types supported by C Language giving an example to each.
Ans The data type defines the type of data stored in a memory-location. C supports 3 classes of data types: ) Primary data type ex: int, float,char,double,void,double ) Derived data types For ex: array C supports 5 primary data types: ) int An int is a keyword which is used to define integers. Using int keyword, the programmer can inform the compiler that the data associated with this keyword should be treated as integer. C supports 3 different sizes of integer: short int int long int ) float A float is a keyword which is used to define floating point numbers. 3) double A double is a keyword used to define long floating point numbers. ) char A char is a keyword which is used to define single character. 5) void void is an empty data type. Since no value is associated with this data type, it does not occupy any space in the memory. This is normally used in functions to indicate that the function does not return any value. Range of data types Data type Bytes Range of data type Char bytes -8 to 7 Int bytes -3, 768 to 3,767 Float bytes 3.E-38 to 3.E38 Double 8 bytes.7e-308 to.7e308 B Draw a flowchart to find the largest of three numbers. Ans PART
3 A Ans What is the output of the following Code fragment? int a,b; a=b=50; printf( %0d%d,a,b); a=a*b; b=b/; printf( %0d%d,a,b); Ans 0050 50500 5 b What do you mean by type conversion? Explain the two types of conversion with example. Ans Type conversion is used to convert data of one type to data of another type. Type conversion is of types as shown in below above: Implicit Conversion If a compiler converts one type of data into another type of data automatically, it is known as implicit conversions. There is no data loss in implicit conversion. The conversion always takes place from lower rank to higher rank. For ex, int to float as shown in the above datatype hierarchy. For ex: int a =, b=; float c = a; //c becomes.000000 float d=b/c=/.000000=.000000/.000000=0.500000 If one operand type is same as other operand type, no conversion takes place and type of result remains same as the operands i.e. int+int=int float+float=float Conversion rules are as follows: If either operand is long double, convert the other to long double. Otherwise, if either operand is double, convert the other to double. Otherwise, if either operand is float, convert the other to float. Otherwise, convert char and short to int. Then, if either operand is long, convert the other to long.
EXPLICIT CONVERSION When the data of one type is converted explicitly to another type with the help of some predefined functions, it is called as explicit conversion. There may be data loss in this process because the conversion is forceful. The syntax is shown below: data_type v; data_type v= (data_type) v; where v can be expression or variable For ex: float b=.000000; int c = ; float d=b/(float)c=.000000/.000000=0.500000 a Find the values of a, b, c, d, e in the following code segment. int a=0,b=3,c;(take the original values of a and b for every expression) ) c=(a==3)&& b++; ) a+=b--; 3) d=a++ + b++; ) e=++a - --b; Ans C=0 * A=3 D=3 E=- b Write a program to check whether a given number is even or odd without using modulus operator. Ans #include<stdio.h> Main() Int a; Printf( enter anumber\n ); Scanf( %d,&a); If((a&)==0) Printf( Even ); Else Printf( Odd ); PART 3 5 a What is the purpose of a printf() statement? Explain the formatted printf () along with the respective examples. Ans The printf function does following tasks: Accept a series of arguments Apply to each argument a format-specifier contained in the format-string Output the formatted data to the screen The syntax is shown below: n=printf("format-string", variable-list); where format-string contains one or more formatspecifiers variable-list contains names of variables
For ex: n=printf("%d %f %c", x, y, z ); b Write a C program that computes the size of int, float, double and char variables. Ans #include<stdio.h> * Main() Printf( size of int is %d bytes,sizeof(int)); Printf( size of float is %d bytes,sizeof(float)); Printf( size of char is %d bytes,sizeof(char)); Printf( size of double is %d bytes,sizeof(double)); 6 a What is an Expression? Explain bitwise operators with example Ans An expression is combination of operands and operator that reduces to a single value. For ex: Consider the following expression a+b here a and b are operands while + is an operator These operators are used to perform logical operation (and, or, not) on individual bits of a binary number. There are 6 bitwise operators: Operators Meaning of operators & Bitwise AND Bitwise OR ^ Bitwise exclusive OR ~ Bitwise complement << Shift left >> Shift right Truth Table A B A&B A B A^B ~A 0 0 0 0 0 0 0 0 0 0 0 Ex for ~ (bitwise complement) Ex for & (bitwise AND) a = 3 0000 0 a = 3 0000 0 ~a= 000 b = 6 0000 00 a & b 0000 000 Ex for (bitwise OR) Ex for ^ (bitwise xor) a = 3 0000 0 a = 3 0000 0 b = 6 0000 00 b = 6 0000 00 a b 0000 a ^ b 0000 0 The operator that is used to shift the data by a specified number of bit positions towards left or right is called shift operator. The syntax is shown below for << The syntax is shown below for >> b=a << num; b=a >> num; where a is value to be shifted num is number of bits to be shifted Ex for <<(left shift): Ex for >>(right shift): a = 3 b=a<< 0000 0 000 00 a = 3 b=a<< 0000 0 0000 00
b Write a program to swap two integers without using third variable Ans #include<stdio.h> #include<conio.h> Main() Int a,b; Printf( enter numbers\n ); Scanf( %d%d,&a,&b); Printf( before swapping a=%d\nb=%d\n,a,b); A=a+b; B=a-b; A=a-b; Printf( After swapping a=%d\nb=%d\n,a,b); PART 7 a What is the output of this C code? i) #include<stdio.h> int main() int x = 0; if (x++) printf("true\n"); if (x == ) printf("false\n"); ii) #include<stdio.h> int main() int x=0; if(x==) if(x==0) printf( inside if\n ); printf( inside if\n ); printf( inside \n ); Ans False Inside * b Write a program to check whether the given input is an alphabet or not. int main() char c; printf("enter a character: "); scanf("%c",&c); if( (c>='a' && c<='z') (c>='a' && c<='z')) printf("%c is an alphabet.",c); printf("%c is not an alphabet.",c); return 0; 8 a Write a C program to read a year as input and find whether it is a leap year or not. also consider end of the centuries using Nested if.
int main() int year; printf("enter a year: "); scanf("%d",&year); if(year% == 0) if( year%00 == 0) // year is divisible by 00, hence the year is a leap year if ( year%00 == 0) printf("%d is a leap year.", year); printf("%d is not a leap year.", year); printf("%d is a leap year.", year ); printf("%d is not a leap year.", year); 3 return 0; b Explain Precedence and associativity with example. Ans Precedence Table THE PRECEDENCE OF OPERATORS
9 A Ans The order in which different operators are used to evaluate an expression is called precedence of operators. Associativity:the order in which operator is evaluated Left-right Right-Left PART 5 Write a C program to simulate a simple calculator that performs arithmetic operations like addition, subtraction, multiplication, and division only on integers. Error message should be reported, if any attempt is made to divide by zero. (Using switch statement) int main() char operator; double firstnumber,secondnumber; printf("enter an Expression: "); scanf("%lf %c %lf",&firstnumber, &operator,&secondnumber); switch(operator) case '+': printf("%.lf + %.lf = %.lf",firstnumber, secondnumber, firstnumber + secondnumber); break; case '-': printf("%.lf - %.lf = %.lf",firstnumber, secondnumber, firstnumber - secondnumber); break; case '*': printf("%.lf * %.lf = %.lf",firstnumber, secondnumber, firstnumber * secondnumber); break; case '/': printf("%.lf / %.lf = %.lf",firstnumber, secondnumber, firstnumber / firstnumber); break; 8 6 // operator doesn't match any case constant (+, -, *, /) default: printf("error! operator is not correct"); return 0; 0 A Design and develop a flowchart that takes three coefficients (a, b, and c) of a Quadratic equation 8
Ans (ax +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> int main() double a, b, c, determinant, root,root, realpart, imaginarypart; printf("enter coefficients a, b and c: "); scanf("%lf %lf %lf",&a, &b, &c); determinant = b*b-*a*c; // condition for real and different roots if (determinant > 0) // sqrt() function returns square root root = (-b+sqrt(determinant))/(*a); root = (-b-sqrt(determinant))/(*a); printf("root = %.lf and root = %.lf",root, root); //condition for real and equal roots if (determinant == 0) root = root = -b/(*a); printf("root = root = %.lf;", root); // if roots are not real realpart = -b/(*a); imaginarypart = sqrt(-determinant)/(*a); printf("root = %.lf+%.lfi and root = %.f-%.fi", realpart, imaginarypart, realpart, imaginarypart); ` return 0;