Posted By

AlexanderRavikovich on 11/18/11


Tagged


Versions (?)

The programm searches for the same digit in the same place in two different numbers


 / Published in: C
 

The program accepts two different real numbers between 100.00 to 999.00. The program checks to see if there is one number that appears in both numbers in the same place at complete part of number (before dot). If such a digit exists or not the program will print an appropriate message.

  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5. /* Initializing variables
  6. again - program stops when it 0
  7. num_f, num_s (first & second) variables used to convert float numbers to integer
  8. i - need it in FOR loop
  9. index - an index that show us that we run programm first time
  10. */
  11. int again=1, num_f=0, num_s=0, i=0, index=0;
  12. /* Variables for float numbers entered by user */
  13. float num1=0, num2=0;
  14.  
  15. while(again)
  16. {
  17. /* If index is 0 - print this stroke */
  18. if (!index)
  19. {
  20. printf("Enter 2 diferent decimal numbers between 100.00 to 999.00:\n");
  21. /* set index to 1 to avoid showing
  22. previous message if we get an error */
  23. index = 1;
  24. }
  25.  
  26. /* Put entered numbers in the float variables float */
  27. scanf("%f %f", &num1, &num2);
  28.  
  29. /* If entered numbers in our condition so we do next code */
  30. if (999>num1 && 100<num1 && num1!=num2)
  31. {
  32. /* Convert float to integer
  33. (we need check digits only before dot) */
  34. num_f = num1;
  35. num_s = num2;
  36.  
  37. /* We have exactly condition, so we know that we need
  38. only 3 times to loop for checking ones/tens/hundreds */
  39. for (i=0; i <3; i++)
  40. {
  41. /* Compare remainder from deviding by 10 each number */
  42. if(num_f % 10 == num_s % 10)
  43. {
  44. switch (i)
  45. {
  46. case 0:
  47. printf("The ones are equals\n");
  48. break;
  49. case 1:
  50. printf("The tens are equals\n");
  51. break;
  52. case 2:
  53. printf("The hundreds are equals\n");
  54. break;
  55. }
  56. }
  57. else
  58. {
  59. switch (i)
  60. {
  61. case 0:
  62. printf("The ones are NO equals\n");
  63. break;
  64. case 1:
  65. printf("The tens are NO equals\n");
  66. break;
  67. case 2:
  68. printf("The hundreds are NO equals\n");
  69. break;
  70. }
  71. }
  72. /* Devide each number by 10 for the next using */
  73. num_f = num_f/10;
  74. num_s = num_s/10;
  75. }
  76. /* Ask to run programm again */
  77. printf("Item 4 again ? (yes = 1 / no = 0)\n");
  78. scanf("%d", &again);
  79. /* Emulate first run of programm */
  80. index=0;
  81. }
  82. else
  83. {
  84. /* Error handler, if entered numbers is wrong */
  85. printf("Wrong number, try again!!\n");
  86. }
  87. }
  88. }

Report this snippet  

You need to login to post a comment.