Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* file: divisionby11.c */
- /* author: Anastase Luca-George (email: l.anastase@student.rug.nl) */
- /* date: 9/17/2024 */
- /* version: 1.0 */
- /* Description: This programs tells how and why a code is divisible by 11 */
- #include <stdio.h>
- int main(){
- int n = 0, cnt = 0;
- char chr;
- scanf("%c", &chr);
- // 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
- while(chr != '\n')
- {
- if(cnt%2==0){
- printf("+%c", chr);
- n += chr - '0';
- }
- else{
- printf("-%c", chr);
- n -= chr - '0';
- }
- cnt++;
- scanf("%c", &chr);
- }
- printf("=%d\n", n);
- if(n < 0)
- n*=-1;
- while(n)
- {
- if(n < 11)
- {
- break;
- }
- int cnt = 0, z = n, s = 0, p = 1, c = n;
- // We do a loop to check that will give us a variable p that is gonna be 10^(number_of_digits(z) - 1)
- while(z){
- p = p * 10;
- z/=10;
- }
- p/=10;
- // Afterwards, we take it digit, do the sum and the substraction and we repeat this process until a number is smaller than 11
- while(p)
- {
- int cif = (c / p) % 10;
- if(cnt % 2 == 1){
- printf("-%d", cif % 10);
- s -= (cif % 10);
- }
- else{
- printf("+%d", cif % 10);
- s += (cif % 10);
- }
- cnt++;
- p/=10;
- }
- printf("=%d\n", s);
- if(s < 0)
- s*=-1;
- n = s;
- }
- if(n == 0){
- printf("YES\n");
- }
- else{
- printf("NO\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement