According to a study, the approximate level of intelligence of a person can be calculated using the following formula:
i = 2 + ( y + 0.5x )
Write a C program that will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps 0.5.
Related Read:
Basic Arithmetic Operations In C
Compound Assignment Operators in C
Nested For Loop In C Programming Language
Logic To understanding the problem statement
1. Problem statement snippet: “..y varies from 1 to 6”. That means, we can accomplish it using a looping statement. So we can take a for loop and initialize variable y to 1 and iterate the for loop until y is less than or equal to 6.
2. Problem statement snippet: “..for each value of y, x varies from”. That means we should write a nested for loop here.
3. Problem statement snippet: “..for each value of y, x varies from 5.5 to 12.5 in steps 0.5”. That means, we need to write a nested for loop and initialize x value to 5.5 and iterate through the for loop until x value is less than or equal to 12.5 And for each iteration of the inner for loop we need to increment the value of x by 0.5
Video Tutorial: C Program To Calculate Approximate Level of Intelligence of a Person
[youtube https://www.youtube.com/watch?v=wRfgDzMMIvY]
Source Code: C Program To Calculate Approximate Level of Intelligence of a Person
#include<stdio.h> int main() { int y, count = 1; float i, x; for(y = 1; y <= 6; y++) { for(x = 5.5; x <= 12.5; x += 0.5) { i = 2 + (y + 0.5 * x); printf("%d. i = %0.2f,\t x = %0.2f,\t y = %d\n", count++,i, x, y); } } return 0; }
Output:
1. i = 5.75, x = 5.50, y = 1 2. i = 6.00, x = 6.00, y = 1 3. i = 6.25, x = 6.50, y = 1 4. i = 6.50, x = 7.00, y = 1 5. i = 6.75, x = 7.50, y = 1 6. i = 7.00, x = 8.00, y = 1 7. i = 7.25, x = 8.50, y = 1 8. i = 7.50, x = 9.00, y = 1 9. i = 7.75, x = 9.50, y = 1 10. i = 8.00, x = 10.00, y = 1 11. i = 8.25, x = 10.50, y = 1 12. i = 8.50, x = 11.00, y = 1 13. i = 8.75, x = 11.50, y = 1 14. i = 9.00, x = 12.00, y = 1 15. i = 9.25, x = 12.50, y = 1 16. i = 6.75, x = 5.50, y = 2 17. i = 7.00, x = 6.00, y = 2 18. i = 7.25, x = 6.50, y = 2 19. i = 7.50, x = 7.00, y = 2 20. i = 7.75, x = 7.50, y = 2 21. i = 8.00, x = 8.00, y = 2 22. i = 8.25, x = 8.50, y = 2 23. i = 8.50, x = 9.00, y = 2 24. i = 8.75, x = 9.50, y = 2 25. i = 9.00, x = 10.00, y = 2 26. i = 9.25, x = 10.50, y = 2 27. i = 9.50, x = 11.00, y = 2 28. i = 9.75, x = 11.50, y = 2 29. i = 10.00, x = 12.00, y = 2 30. i = 10.25, x = 12.50, y = 2 31. i = 7.75, x = 5.50, y = 3 32. i = 8.00, x = 6.00, y = 3 33. i = 8.25, x = 6.50, y = 3 34. i = 8.50, x = 7.00, y = 3 35. i = 8.75, x = 7.50, y = 3 36. i = 9.00, x = 8.00, y = 3 37. i = 9.25, x = 8.50, y = 3 38. i = 9.50, x = 9.00, y = 3 39. i = 9.75, x = 9.50, y = 3 40. i = 10.00, x = 10.00, y = 3 41. i = 10.25, x = 10.50, y = 3 42. i = 10.50, x = 11.00, y = 3 43. i = 10.75, x = 11.50, y = 3 44. i = 11.00, x = 12.00, y = 3 45. i = 11.25, x = 12.50, y = 3 46. i = 8.75, x = 5.50, y = 4 47. i = 9.00, x = 6.00, y = 4 48. i = 9.25, x = 6.50, y = 4 49. i = 9.50, x = 7.00, y = 4 50. i = 9.75, x = 7.50, y = 4 51. i = 10.00, x = 8.00, y = 4 52. i = 10.25, x = 8.50, y = 4 53. i = 10.50, x = 9.00, y = 4 54. i = 10.75, x = 9.50, y = 4 55. i = 11.00, x = 10.00, y = 4 56. i = 11.25, x = 10.50, y = 4 57. i = 11.50, x = 11.00, y = 4 58. i = 11.75, x = 11.50, y = 4 59. i = 12.00, x = 12.00, y = 4 60. i = 12.25, x = 12.50, y = 4 61. i = 9.75, x = 5.50, y = 5 62. i = 10.00, x = 6.00, y = 5 63. i = 10.25, x = 6.50, y = 5 64. i = 10.50, x = 7.00, y = 5 65. i = 10.75, x = 7.50, y = 5 66. i = 11.00, x = 8.00, y = 5 67. i = 11.25, x = 8.50, y = 5 68. i = 11.50, x = 9.00, y = 5 69. i = 11.75, x = 9.50, y = 5 70. i = 12.00, x = 10.00, y = 5 71. i = 12.25, x = 10.50, y = 5 72. i = 12.50, x = 11.00, y = 5 73. i = 12.75, x = 11.50, y = 5 74. i = 13.00, x = 12.00, y = 5 75. i = 13.25, x = 12.50, y = 5 76. i = 10.75, x = 5.50, y = 6 77. i = 11.00, x = 6.00, y = 6 78. i = 11.25, x = 6.50, y = 6 79. i = 11.50, x = 7.00, y = 6 80. i = 11.75, x = 7.50, y = 6 81. i = 12.00, x = 8.00, y = 6 82. i = 12.25, x = 8.50, y = 6 83. i = 12.50, x = 9.00, y = 6 84. i = 12.75, x = 9.50, y = 6 85. i = 13.00, x = 10.00, y = 6 86. i = 13.25, x = 10.50, y = 6 87. i = 13.50, x = 11.00, y = 6 88. i = 13.75, x = 11.50, y = 6 89. i = 14.00, x = 12.00, y = 6 90. i = 14.25, x = 12.50, y = 6
Note: Inner for loop executes from 5.5 to 12.5 with a 0.5 step increment for each loop. So its 15 iterations. Outer for loop executes 6 times. So total iterations equals 15 x 6 = 90 times. So 90 values will be printed to the console window.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert