Advertisement
Guest User

Telesab

a guest
Sep 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. Question1:
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. //readln func that deals with the precistion
  6. int readln(char s[], int maxlen) {
  7. char ch;
  8. int i;
  9. int chars_remain;
  10. i = 0;
  11. chars_remain = 1;
  12.  
  13. int x=0;
  14. int z=0;
  15. while (chars_remain) {
  16. ch = getchar();
  17. if (isdigit(ch) || ch == '.' || ch == '\n') {
  18.  
  19. if ((ch == '\n') || (ch == EOF)) {
  20. chars_remain = 0;
  21. }
  22.  
  23. if (ch == '.') {
  24. if (x <= 3) {
  25. s[i] = ch;
  26. i++;
  27. continue;
  28. }
  29. else {
  30. printf("Output: error; numbers before [.] exceeds 3 digits.");
  31. z = 1;
  32. break;
  33. }
  34. }
  35.  
  36. else if (i < maxlen - 1) {
  37. s[i] = ch;
  38. i++;
  39.  
  40. if (ch != '.')
  41. x++;
  42.  
  43. }
  44.  
  45. }
  46. else {
  47. printf("Only digits and positive numbers are allowed");
  48. break;
  49. }
  50. }
  51. if (!z) {
  52. s[i] = '\0';
  53. return i;
  54. }
  55. else {
  56. i = -1;
  57. return i;
  58. }
  59. }
  60. int main(int argc, char** argv) {
  61. char [12] number;
  62. double In;
  63. int result;
  64. int integer;
  65. printf("Input/\n");
  66. readln(number,12);
  67. // since the value is in string i've to convert it into decimal using strod fun
  68. if (result >0) {
  69. In = strtod(number,NULL);
  70. In= In * (22 / 7);
  71. printf("Output/ %.8f \n", In);
  72. //checking whether it is even or odd
  73. integer = In;
  74. if (integer % 2 == 0) {
  75. printf("%d is even \n", integer);
  76. --integer;
  77. if (integer - 3 != 0) {
  78. int mod = 10 % (integer - 3);
  79. printf("result:%d \n", mod);
  80. }
  81. else
  82. printf("Error Can't Devide by Zero!");
  83. }
  84. else
  85. printf("%d odd \n", integer);
  86. }
  87.  
  88.  
  89. return 0;
  90. }
  91.  
  92. Question2:
  93. #include <stdio.h>
  94. #include <stdlib.h>
  95.  
  96. int main(int argc, char** argv) {
  97. char name[6];
  98. char letter;
  99. char age[10];
  100. int num;
  101. char sec[10];
  102. int Snum;
  103.  
  104. printf("Input/\n");
  105. printf("Name:\n");
  106. gets(name);
  107. printf("Favorite letter:\n");
  108. letter = getchar();
  109. printf("Age:\n");
  110. fgets(age, 10, stdin);
  111. num = atoi(age);
  112. printf("Secret number:\n");
  113. fgets(sec,10, stdin);
  114. Snum = atoi(sec);
  115. printf("Output/\n");
  116. if ((letter == 'A') && (num == 24 || num == 25 || num == 26 || num == 27) && (Snum == 123456))
  117.  
  118. printf("Access granted:");
  119. printf("Name %s, Favorite letter: %c, Age: %d, Secret Number %d", name, letter, num, Snum);
  120. return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement