Ok so I played around with this for a while and it works..but I wana know the logic

#include <iostream>

using namespace std;

int main()
{
    int number;
    int max = 0;

    cout << "enter number: ";
    cin >> number;

    while (number !=0)
    {
    if ((number % 10)> max) //rem of 10
        {

            max = (number % 10);
        }
        number /= 10;
    }
        cout << "larggest" << max;
        return 0;
}

1)

int number;
    int max = 0;

why do you need int max..why doesnt max = 0 work?

2)

while (number !=0)

what does !=0 mean?

3)

if ((number % 10)> max) //rem of 10
        {

            max = (number % 10);
        }
        number /= 10;

how exactly does that work to print the largest number?

1) That's a rule we have to follow, no particular reason

2) This means run the following lines while "number" is not equal to zero

3) “%” calculates the remainder, in this case it takes out for example the 4 in 1234.
If one bit is larger than the last largest bit ever recorded, then this bit is
recorded as the largest bit then. if(..){max=(number%10);}

Then we move on, test the next bit.
With the same action, we have to take of the remainder and
devide the "number" with 10.
So that we can repeat it.

int max = 0;

u have to put the int because u have to declare to the compiler that the value of max is an integer and not a floating point number... its a whole number that can be negative or positive.. in other words, no letters, no decimals, etc..

max = 0 does not have a type.. and the compiler cannot know if max is a char or int or float or long, etc. you set it to 0 to initiate it with a value because if you don't then it is assumed a random number.. use a breakpoint and you will see.

! means not
= means equal example: x = 0. y = 0.
< less than
> greater than
!= not equal
== comparing left to right equals.. example: if(left == right) then do something..
/= means a number = itself divided by #.. Example: A /= 10.. means A = A/10

*= means a number = itself multiplied by #.. Example: A *= 10.. means A = A*10
+=
-= same thing..
++ means increase.. so A++ would be keep increasing A by 1..
-- opposite of above.

How does it give the max number? because while the number is not 0, it keeps testing and testing until the number = 0.. when it does, then it breaks out of the loop and prints it on screen for u.

Example:

1234567.. 7 will be the highest number.. 1234, 4 will be highest.. it prints the highest value of the numbers entered (splits them into one's).

Enter 10.. the two values would be 1 and 0.. 1 is highest.

Edited 5 Years Ago by triumphost: n/a

I am mainly confused in the logic of how the compiler processes:

if ((number % 10)> max) //rem of 10
        {
 
            max = (number % 10);
        }
        number /= 10;

k it goes:

if ((remainder of number/10) is greater than max then)
{
    max = that remainder;
}
number = number/10;

//Repeat everything above while number does not = 0;

Example:

int max = 0;
int number = 10;

//10/10 = 1 remainder 0;

if(0 > max)  //if remainder is greater than max.. then..
{
   max = 0; //because the remainder of 10/10 is 0.
}
//number = 10/10;  Therefore Number = 1;
number = 1;

//now the while loop says: while number != 0, do whatever is inbetween.. well.. Number = 1 right now.. so we do it again.

//1/10 = 0 remainder 1;
if(1 > 0)  // max = 0 as stated above when we did it the first time.
{
  max = 1;  // max = the remainder of 1/10.. which is 1.
}
number = 1/10;  // this is now <= 0 so it breaks out of the while loop.. and prints max. it doesn't do decimals because its an integer. so u round it to the nearest whole basically.

cout<<"Largest: "<<max<<"\n";  //should print 1 since max = 1;
cin.get();  //this will pause the screen so u can see the output..

Edited 5 Years Ago by triumphost: n/a

the main logic is from line 13 to 21
this code will generate the largest number in a given integer

lets see hw it works...
while loop keeps track of given integer..in every loop the number looses one digit

e.g. initially number is 1823

loop number max max is compared by
=============================================
0 1823 0 3
1 182 3 8
2 18 3 2
3 1 8 1

more exapples at java2k.blogspot dot com
happy coding .....:)

Edited 5 Years Ago by 115harman: n/a

the main logic is from line 13 to 21
this code will generate the largest number in a given integer

lets see hw it works...
while loop keeps track of given integer..in every loop the number looses one digit

e.g. initially number is 1823

loop          number            max            max is compared by
           ===================================================================
             0            1823                0                    3
             1             182                3                    8
             2              18                3                    2
             3               1                8                    1

more exapples at java2k.blogspot dot com
happy coding .....:)

divide an integer by 10 and you will get the last digit of the integer as the remainder and the rest of the number as quotient.

in C++ when you divide an integer by an integer you get an integer.
For Example, when you divide 234 by 10, the quotient is actually 23.4. But 23.4 is not an integer; its a floating point number. So it is converted into an integer i.e. it becomes 23.

This is the 'principle' on which the above algorithm works.

This article has been dead for over six months. Start a new discussion instead.