Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PART B
9. Design, develop and execute a program in C to calculate the approximate value of exp (0.5) using the Taylor Series expansion for the exponential function. Use the terms in the expansion until the last term is less than the machine epsilon defines as FLT_EPSILON in the header file <float.h>. Print the value returned by the Mathematical function exp ( ) also. 10. Design, develop and execute a program in C to read two matrices A (M x N) and B (P x Q) and to compute the product of A and B if the matrices are compatible for multiplication. The program is to print the input matrices and the resultant matrix with suitable headings and format if the matrices are compatible for multiplication, otherwise the program must print a suitable message.(For the purpose of demonstration, the array sizes M, N, P, and Q can all be less than or equal to 3)
Page 1
Page 2
PART A
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Sample Output Enter two numbers: 34 66 The LCM = 1122 The GCD = 2
Page 10
Page 11
Page 12
Sample Output 1. Enter a number: 201 Not a four digit number 2. Enter a number: 5642 The Number is not Palindrome 3. Enter a number: 8118 The Number is Palindrome
Page 13
Page 14
Page 15
Sample Output Enter the degree of the polynomial : 4 Enter the 5 coefficient 12345 Enter the value of x : 1 The sum of polynomial = 15
Page 16
Page 17
Page 18
Sample output Enter the text welcome to hkbk College of Engineering welcome to hkbk College of Engineering
Page 19
Algorithm 1. Start. 2. Initialize Flag 0 3. Read n. 4. FOR i 0 to n-1 in steps of 1 Read numbers in ascending order in Array a[ ] END FOR. 5. Read key. 6. Initialize low0, highn -1. 7. Until low <= high mid(low + high)/ 2. 8. IF key = a[mid] Flag=1 Goto Step 10 END IF 9. IF key > a[mid] lowmid + 1 ELSE highmid 1 END IF END Until 10. IF Flag = 1 Print Successful Search. ELSE Print Unsuccessful Search. END IF 11. Stop.
Page 20
Page 21
Page 22
Page 23
Algorithm 1. Start. 2. Read n. 3. FOR i0 to n-1 in steps of 1 Read integer numbers in array a [ ] b[i]a[i] END FOR. 4. FOR i0 to n-1 in steps of 1 FOR j0 to n-i in steps of 1 IF a[j] > a[j + 1] temp= a[j] a[j] = a[j + 1] a[j+1] = temp END IF END FOR END FOR 5. Print original array b[i] FOR i0 to n-1 in steps of 1 Print b[i] END FOR. 6. Print sorted array a[i] FOR i0 to n-1 in steps of 1 Print a[i] END FOR. 7. Stop.
Page 24
Page 25
Page 26
Page 27
Flowchart
Page 28
Page 29
PART-B
Page 30
Algorithm 1. Start 2. Read X 3. Initialize sum0,term1,fact1 4. for i1; term >= FLT_EPSILON; i++ fact fact*i sum sum+term term pow(x,i)/fact 5. Print calculated value 6. Print Library function value. 7. Stop Flowchart
Page 31
Sample Output You have this series: 1+x/1!+x^2/2!+x^3/3!+x^4/4!+.x^n/n! Enter the value for X : 0.5 The Calculated value of e^ 0.500 = 1.648721 The Library Function Value of e^ 0.500 = 1.648721
Page 32
Page 34
Page 35
Page 36
} printf("\n MATRIX C \n"); for(i = 0 ; i < m ; i++) { for(j = 0 ; j < q ; j++) { printf(" %d \t", c[i][j]); } printf("\n"); } } else printf("Matrix A & B is not multiplicable"); } Sample Output Enter the order of the matrix A :2 2 Enter the order of the matrix B :2 2 Enter the elements of matrix A 1 2 3 4 Enter the elements of matrix B 2 3 4 5 MATRIX A 1 2 3 4 MATRIX B 2 3 4 5 MATRIX C 10 13 22 29 Enter the order of the matrix A :2 3 Enter the order of the matrix B :2 3 Matrix A & B is not multiplicable
Noor-E-Saba, Dept. of CSE@HKBKCE Page 38
Page 39
Page 40
Algorithm to rotate value by n bits 1. IF n==0 Return x ELSE Return ((x >> n) | (x << (32- n))) 2. Return
Flowchart
Page 41
Sample Output Enter x and n values 83 The value after rotating 3 bit is: 1
Page 42
Algorithm 1. Start 2. Read the number 3. Call function prime(n) P= prime(n) 4. IF p 1 Print Number is prime ELSE Print Number not a prime End IF 5. Stop. Algorithm to check the prime number 1. FOR i 2 to n/2 in steps of 1 IF (n mod i) is equal to zero Return 0 Return 1 End For 2. Return.
Page 43
Page 44
Sample output Enter the number 17 The given number is prime Enter the number 33 The given number is not prime
Page 45
Algorithm 1. Start 2. Initialize n 100 3. For i0 to n in steps of 1 num[i] i END For 4. For i2 to in steps of 1 IF num[i] != 0 For j i*i to n in steps of j j+i num[j] 0 END For END IF END For 5. For i0 to n in steps of 1 iF num[i] != 0 Print non zero numbers END IF END For 6. Stop
Page 46
Page 47
Page 48
Page 49
Algorithm 1. Start 2. Read the string 3. Print the string 4. Call function reverse(s) 5. Stop
Algorithm to reverse a string 1. Start 2. Calculate the length 3. For i 0, i<string length/2 in step of 1 temp str[i] str[i]str[len] str[len]temp decrement length END For 4. Print the reverse string 5. Stop Flowchart
Page 50
Page 51
Page 52
Page 53
Page 54
END
Page 55