Thread: how to get difference of digits

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    how to get difference of digits

    ok, say we have number 21. difference is 1.

    23, difference is -1.

    what is a mathematical way, only using the number 23, to get the difference of the digits. If there is no mathematical way, then how do you extract part of an int? i would really like a mathematical way though.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Code:
    #include <stdio.h>
    
    int Difference(int num)
    {
    	int i1 = num % 10;
    	int i2 = num / 10;
    	return(i2 - i1);
    }
    
    void main()
    {
    	printf("difference between 45 is %d\n", Difference(45));
    }
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    yes

    im so stupid sometimes, i got it

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    sometimes it's hard to see coz you're looking so hard dont worry mate, we've all been there.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sometimes it's hard to see coz you're looking so hard dont worry mate, we've all been there.

    U.
    Indeed. For example, I'm sure you really meant to use 'int main( void )' instead of 'void main( void )' in your code example:

    Code:
    void main()
    {
    	printf("difference between 45 is %d\n", Difference(45));
    }
    It's ok, lots of people do it. (Not me, but lots still do...)

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    hehe.. how picky!

    I agree.. i should have used int main(void)... that is what i always do...

    so why the hell did i have it that way?? well, i was in the middle of modifying someone else's code to help them with a problem...i copied and pasted from there to give an example. it's not how you would ever find it in my code

    needless to say, the point of the post was the Difference() function, not the niceties of how to properly declare and use the main function.

    have a great day
    U.
    Last edited by Uraldor; 12-20-2001 at 08:38 PM.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  4. reverse a number digits
    By tootoo in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2007, 11:24 AM
  5. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 04:05 PM