Extracting largest and smallest number

This is a discussion on Extracting largest and smallest number within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, I'm new to C++ and know how to extract numbers (for example, to extract 1 from 1289371 I ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    79

    Extracting largest and smallest number

    Hey everyone, I'm new to C++ and know how to extract numbers (for example, to extract 1 from 1289371 I need to do modulo 10 [% 10] and I can do it in a while loop with dividing to keep using modulo on the numbers after) but I don't know how to extract a number and then extract the one after and compare to see which one is larger and then do the same thing again and find out which one is smaller and print them both out.

    i.e. Input : 783214
    prints out: largest number is 8
    smallest number is 1


    Can anyone help me?
    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Show us the code for what you can do.

    Like a program showing all you learnt in this thread -> How to modulo to this digit
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    read the user input as a char array or string and run a loop through it. the first char in the array or string will be set as your min and max then as you iterate through the loop use conditional operators to check whether a number is higher or lower and if it is then reassign max or min depending on what you were testing for the new value.

    to convert the char to int you could use
    Code:
    int convertchar2int (const char c) { return (int)(c -'0'); }
    Last edited by codeprada; 02-18-2011 at 05:33 AM.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  3. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  4. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM
  5. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21