Advertisement
LA77

Untitled

Sep 20th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /* file: divisionby11.c */
  2. /* author: Anastase Luca-George (email: l.anastase@student.rug.nl) */
  3. /* date: 9/17/2024 */
  4. /* version: 1.0 */
  5. /* Description: This programs tells how and why a code is divisible by 11 */
  6.  
  7. #include <stdio.h>
  8.  
  9.  
  10.  
  11. int main(){
  12.  
  13. int n = 0, cnt = 0;
  14. char chr;
  15. scanf("%c", &chr);
  16.  
  17. // Because the number is between 1 and 10^1000 means that we cant read it as an it so we use a char to do the work for us
  18. while(chr != '\n')
  19. {
  20. if(cnt%2==0){
  21. printf("+%c", chr);
  22. n += chr - '0';
  23. }
  24. else{
  25. printf("-%c", chr);
  26. n -= chr - '0';
  27. }
  28. cnt++;
  29. scanf("%c", &chr);
  30. }
  31.  
  32. printf("=%d\n", n);
  33. if(n < 0)
  34. n*=-1;
  35.  
  36. while(n)
  37. {
  38. if(n < 11)
  39. {
  40. break;
  41. }
  42. int cnt = 0, z = n, s = 0, p = 1, c = n;
  43.  
  44. // We do a loop to check that will give us a variable p that is gonna be 10^(number_of_digits(z) - 1)
  45. while(z){
  46. p = p * 10;
  47. z/=10;
  48. }
  49. p/=10;
  50.  
  51. // Afterwards, we take it digit, do the sum and the substraction and we repeat this process until a number is smaller than 11
  52. while(p)
  53. {
  54. int cif = (c / p) % 10;
  55. if(cnt % 2 == 1){
  56. printf("-%d", cif % 10);
  57. s -= (cif % 10);
  58. }
  59. else{
  60. printf("+%d", cif % 10);
  61. s += (cif % 10);
  62. }
  63. cnt++;
  64. p/=10;
  65. }
  66. printf("=%d\n", s);
  67. if(s < 0)
  68. s*=-1;
  69. n = s;
  70. }
  71.  
  72. if(n == 0){
  73. printf("YES\n");
  74. }
  75. else{
  76. printf("NO\n");
  77. }
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement