0

Good afternoon. I need to enter an integer and determine whether there are two identical numbers which are located fairly close to each others( standing side by side). For instance, my input is "1224" and the output is "YES". Or my input is "1256" and the output is "NO". I tried to do it like that:

{int a,b,c,d,i;
scanf("%d",&i);
while (i!=0)
    {
      a=i/1000;
      b=(i/100)%10;
      c=(i%100)/10;
      d=i%10;
    }
if (a==b || b==c || c==d)
        {printf("YES");}
else
        {printf("NO");}
return 0;

}

However, it failed. My classmates said me that "i" does not changes and, as a result, my code does not work. Unfortunately, i did not understand what they meant. So, could you tell me what's wrong in this code?

Froman
  • 13
  • 1
  • 1
    They meant that using a `while` loop doesn't make sense because it will run infinitely, as the condition `i!=0` won't change. – interjay Mar 11 '21 at 10:10
  • Are those inputted integers always four digits numbers (e.g. 999 or 12345 would be invalid)? – Bob__ Mar 11 '21 at 10:13

4 Answers4

0

You don't need a while loop. For your implementation, you can extract digits like this:

a = (i / 1000) % 10;
b = (i / 100) % 10;
c = (i / 10) % 10;
d = i % 10;

Your implementation works only with a 4 digit number.

A general implementation should look like this:

int main() {
    int number;
    
    scanf("%d", &number);

    while (number >= 10) {
        if (number % 10 == (number / 10) % 10) {
            printf("YES");
            return 0;
        }

        number /= 10;
    }

    printf("NO");
}
David Rosu
  • 39
  • 4
0

I think you want the scanf() inside the while

{int a, b, c, d, i = -1;
while (i != 0)
    {
     scanf("%d", &i);
     b = (i/100) % 10;
     c = (i%100) / 10;
     d = i % 10;
     if (a==b || b==c || c==d)
             {printf("YES");}
     else
             {printf("NO");}
    }
return 0;
}

Note: I've tried to keep your strange indentation style, but not whitespace usage.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • This puts the condition in the wrong place. The loop won't be entered. – interjay Mar 11 '21 at 10:50
  • 1
    May I suggest `while ( scanf("%d", &i) == 1 ) { ... }`? – Bob__ Mar 11 '21 at 10:53
  • It's still problematic because it will run one more iteration than is needed. The condition should be checked right after the `scanf`. But OP didn't even say that this needs to run in a loop, so the best solution is just to remote it. – interjay Mar 11 '21 at 10:57
0

Your friends are right that i is not changing in the while loop so it is resulting in an infinite while loop. I will provide a solution for not just 4 digit numbers for any digit numbers.

#include <stdio.h>
#include<string.h>

int main() {
int i;
int m = 0;
int n = 0;
char strin[] = "NO  ";
scanf("%d", &i);
while (i != 0) {
    m = i % 10;
    i = i/10;
    n = i % 10;
    if(m == n){
        strcpy(strin, "YES");
        break;
    }
} 
printf("%s", strin);


    return 0;
}

The approach for this question is to separate the last two digits of a number and compare them, if they are the same break from the loop, if they are not, then check it for the remaining number.

EDIT- This could be done with lesser variables, but I suppose you are a beginner and it will help you understand better.

Vyom Yadav
  • 145
  • 13
-3

i Will do it in java cause my c is a little rusty. However, you just need to travel through the array of numbers checking if the A[i]=A[i-1] value. (current value is equal to previous one)

public String checkNumber(int[] array){
   for( int i=1; i<array.length-1; i++){
      if(A[i]==A[i-1]){
          return "yes";
      {
   {
   return "no";
}

Here you have a link which shows how to pass an integer to an array of ints: Convert an integer to an array of digits

tomazu07
  • 3
  • 4