Hi,
I have a little problem. I need to write a function that will be given a number (with unknown number of digits) and it will search the number for similar neighboring digits.

I must say that I'm not allowed to use arrays (unfortunately).
I thought of disassembling the number to it's digits in a loop, but i don't have a clue how it's possible...

Are you allowed to use strings? If you can, you could get input in the form of a string and cycle through that, comparing each character along the way, and then convert it to a number afterwards (if necessary at all).

The only other thing I can think of working is if you managed some very tricky work with the modulus operator.

Thanks for the reply. Unfortunately I'm not allowed to use strings as well. but i think i got it:

``````#include <stdio.h>
#include <conio.h>

int findSim(int num)
{
int i,u,t;
for(i=num;i>=0;i/10)
{
u=num%10;
t=(num/10)%10;
num=num/10;
if(u==t)
printf("There are two similar digits in the number");
else printf("There are no similar digits in the number");
}
return 0;
}

int main()
{
int num;
clrscr();
printf("Enter a number:");
scanf("%d",&num);

findSim(num);

getch();
return 0;
}``````

.....and no... tested. not working... any ideas? anyone?

You're on the right track. If you can't use an array, you can just assign variables for each digit.

Here's an example of how to handle 0-4 digits.

``````#include <stdio.h>

int main() {
unsigned long n;   //let's the number be rather large
int i, zero, one, two, three, four;
n=134434;
zero=one=two=three=four=0;
printf("\n\n");

while(n) {
i = n % 10;
if(i==4) four++;
else if(i==3) three++;
else if(i==2) two++;
else if(i==1) one++;
else if(i==0) zero++;
n /= 10;  //(n = n / 10;  //do this, last in the loop
}

if(zero>1) printf("\n I have %d zeroes", zero);
if(one>1) printf("\n I have %d ones", one);
if(two>1) printf("\n I have %d twos", two);
if(three>1) printf("\n I have %d threes", three);
if(four>1) printf("\n I have %d fours", four);

i = getchar();
return 0;
}``````

Hey ...
try this
#include<stdio.h>
#include<conio.h>
void main()
{
int n,x,y,c=0,temp;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
temp=n;
while(n!=0)
{
x=n%10;
c=c*10+x;
y=n/10;
n=y;
}
printf("Reverse of the number is: %d\n",c);
if(c==temp)
printf("Numbers are same");
else
printf("Numbers are not same");
getch();
}

Your original code was pretty good, but it does not change the value of "i" (i/10 doesn't do anything). It should be i = i/10 or i /= 10 (which is not as readable).

printf() needs a newline "\n" in it.

Also the function should return immediately when a match is found

``````if(u==t) {
printf("There are two similar digits in the number\n");
return 1;
}
}
printf("There are no similar digits in the number\n");
return 0;
}``````

For debugging, try printing the values of u and t inside the loop.
If it still doesn't work right, post the results that you get, and describe why you think it is wrong.

Thank you all. I think I really got it this time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.