Thread: Number on a number

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    34

    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. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

    Some suggestions about your code:
    • 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.
    Last edited by dwks; 03-21-2008 at 12:51 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    thanks...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and better do not use global vars
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually, I said that in my edit. Buried deep within a list . . . .

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

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    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. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indentation is screwed up. I'm having trouble determining if the if save == 0 should be at function level or inside the if.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    this guy sometimes prints 2x Yes or No...lol

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    it was an error made by me...

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    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. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    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. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM