# Number on a number

This is a discussion on Number on a number within the C Programming forums, part of the General Programming Boards category; I want to do a program that verifys if a number is in other number...We have a "x" between(1 and ...

1. ## Number on a number

I want to do a program that verifys if a number is in other number...We have a "x" between(1 and 99) and a "n" integer...
We say the number x and the n and it will verify if x is in n or not...i've tried something like this:

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

int n;
int x;
int y=0;

int main(void){

printf("x: ");
scanf("%d",&x);

printf("n: ");
scanf("%d",&n);

verificar(x);

}

int verificar(int x){

if (x<10)
while (n>0){
n=n%10;
printf("%d",n);
return n;
}
}

I have a problem in the cicle cause it will print only the first number of n&10....
anyone to help?```

2. To get each digit of a number one by one, consider an algorithm like this.
Code:
```do {
digit = number &#37; 10;
number /= 10;
} while(number);```
Note that that gives you the ones column first etc. Getting the most significant digit first is a little harder, but you don't care about the order of the digits in your case as far as I can see, so that's not an issue.

• Don't use global variables. You're passing x into verificar() anyway.
• Do use function prototypes, or put verificar() before main(). Prototypes would be a better approach.
• Do make sure your functions will return something no matter what. For example, if x is >= 10, verificar() doesn't return anything in a defined way.
• Do try to work out how you would do something by hand before trying to code it. You'll get better algorithms that way.

3. thanks...

4. and better do not use global vars

5. Actually, I said that in my edit. Buried deep within a list . . . .

Also consider returning 0 from the end of main().

6. What you think of that?

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

int main(){

int n;
int x;
int y=0;
int save;

printf("x: ");
scanf("%d",&x);

printf("n: ");
scanf("%d",&n);

if (x<10)
do{
y=n%10;
n=n/10;
if (y==x)
save=y;

}

while (n);

if (save==0)
printf("No");
else
printf("Yes");

}```

7. Well the indentation could do with some work. You may also want to add some braces to go with this if statment: if (x<10). And, as said before, return 0 at the end might be a good idea to.

8. Indentation is screwed up. I'm having trouble determining if the if save == 0 should be at function level or inside the if.

9. You need to indent the code better:
Code:
```#include <stdio.h>

int main() {
int n;
int x;
int y = 0;
int save;

printf("x: ");
scanf("&#37;d", &x);

printf("n: ");
scanf("%d", &n);

if (x < 10)
do {
y = n % 10;
n = n / 10;
if (y == x)
save = y;
} while (n);

if (save == 0)
printf("No");
else
printf("Yes");

return 0;
}```
Quite clearly there is a control path where save is not properly initialised yet it is compared to 0.

10. While this may work:
Code:
```    if (x < 10)
do {
y = n &#37; 10;
n = n / 10;
if (y == x)
save = y;
} while (n);```
It might be better to do:
Code:
```if (x < 10)
{
do {
y = n % 10;
n = n / 10;
if (y == x)
save = y;
} while (n);
}```
To avoid confusion.

11. this guy sometimes prints 2x Yes or No...lol

12. it was an error made by me...

13. Now, is that he prints YesYes, or NoNo or YesNo...

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

int main(){

int n;
int x;
int y=0;
int save;

printf("x (number between 1-99): ");
scanf("%d",&x);
while (x<1 || x>99){
printf("Number not valid\n");
printf("x (number between 1-99): ");
scanf("%d",&x);
}

printf("n: ");
scanf("%d",&n);

if (x<10){
do{
y=n%10;
n=n/10;
if (y==x)
save=y;
}

while (n);
}

if (save==0)
printf("No");
else
printf("Yes");

/* For x>=10 */

if (x>=10){
do{
y=n%100;
n=n/100;
if (y==x)
save=y;
}

while (n);
}

if (save==0)
printf("No");
else
printf("Yes");

return 0;
}```

14. It was the " } "... now i think thats right...anyone has found any bug?

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

int main(){

int n;
int x;
int y;
int save;

printf("x (number between 1-99): ");
scanf("%d",&x);
while (x<1 || x>99){
printf("Number not valid\n");
printf("x (number between 1-99): ");
scanf("%d",&x);
}

printf("n: ");
scanf("%d",&n);

if (x<10){
do{
y=n%10;
n=n/10;
if (y==x)
save=y;
}

while (n);

if (save==0)
printf("No");
else
printf("Yes");
}

if (x>=10){
do{
y=n%100;
n=n/100;
if (y==x)
save=y;
}

while (n);

if (save==0)
printf("No");
else
printf("Yes");
}

return 0;
}```

15. Start indenting properly and you won't have bracket problems.
Adhere to the indentation example here http://cpwiki.sf.net/Indentation or give yourself a go at a little more in-depth tutorial here: http://cpwiki.sf.net/User:Elysia/Indentation

Page 1 of 3 123 Last