In: Computer Science
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;
cout << "Enter your student ID number: ";
cin >> studentid;
cout << "Student ID Number = " << studentid << endl;
while (studentid != 0)
{
numberreverse[count] = studentid % 10;
if (count == 0)
{
minimum = numberreverse[count];
maximum = minimum;
}
else
{
if (minimum > numberreverse[count]) minimum = numberreverse[count];
if (maximum < numberreverse[count]) maximum = numberreverse[count];
}
count++;
studentid = studentid / 10;
}
cout << "\nReversed Number is : ";
for (int i = 0; i < count; i++)
cout << numberreverse[i];
cout << "\nLargest Number is :" << maximum;
cout << "\nSmallest Number is :" << minimum;
return 0;
}
can u pls Write up to 10 lines to explain the logic used behind this code?
In the above code, firstly a number is inputted from user in console
Then the number is reversed
After that, we have found the largest and the smallest digit out out of the number
Note: Number can be more than 1 digit as well
Output of the code is as below:
Enter your student ID number: 123451
Student ID number: 123451
Reversed Numbe Is: 154321
Largest Digit: 5
Smallest Digit : 1
______________________________
To explain the login shortly, please find below steps:
Step 1: Declare the array and variables with datatype integer , array with length 20
Step 2: Read input from user , and store in studentId variable. Also, display/print the value in studentId variable
Step 3: With the help of while loop , navigate/traverse through each digit of the number
and store the reverse of the digits in the numberreverse array, each digit at one index
Reverse is taken place by divide and modulus method by 10
Taking remainder or last digit of number and storing at starting index of array
Simililary , reducing the number to length , of 1 less than length , removing the last digit
Step 4: With the help of while loop, compare each digits , and with the help of if-else condition within the loop,
compare each digit and find the maximum and minimum of all the digits in number , And store the smallest digit value is variable minimum and largest digit value in variable maximum
Step 5: Print all the values of array numberreverse at each index in console with the help of FOR loop
Step 6: Print the maximum and minimum values in console
Note: The length of number varies depending on the input received from the user
__________________________
For expalining more clearly and widely with code:
Step 1: Firstly studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0; variables are declared with datatype Integer
numberreverse is and integer array with length 20, count, maximum and minimum variables are initialized with value 0.
Step 2: studentId is inputted from user using predefined object in C++ , cin
cin >> studentId
After taking input from user , studentId value is displayed/printed in console via predefined object in C++ , cout
cout<< studentId
Step 3: Now, while loop is used to iterate through all the digits present in the studentId
Example: studentId is 123 , so loop will iterate/run 3 times , to navigate through each digit 1, 2 ,3 in the number
Syntax: while(condition){}
Loop will continue executing till the condition in while is true , as the condition goes false, control will exit from the loop
while (studentid != 0)
Here, till the studentId value is greater than 0 , loop will continue executing/iterating.
As the studentId becomes 0, control will get out of loop.
Now , starling studentId=123 , that means studentId!=0 , condition becomes true, control will move inside the loop
numberreverse[count] = studentid % 10;
% = modulus operator
studentId will be divided by 10 and the result remainder will be stored numberreverse array at index count
Intially, we have initialized the count =0,
so , numberreverse[0] = 123 % 10 = 3 [here , by dividing , 12 becomes quotient and 3 remains remainder]
At 0th position of array ->numberreverse[0]=3 [store the value].
Moving to next step , if - else statement is used, if count is equal to 0 , then control goes to if statement , otherwsie , else statement
Here, count =0 , control will go inside if statement
if (count == 0) ,
{
minimum = numberreverse[count];
maximum = minimum;
}
value in array at count index , 0th index , will be stored in minimum variable
Note : minimum and maximum variables , here used are to find the smallest and largest out of each digit in the number
so, minimum will become 3
minimum =3
and, maximum also become 3,
maximum =3
Moving to next statment,
count variable will be incremented by 1
count++;
count =1
And, new value for studentId will be stored as, last digit is traversed for the number
studentId divided by 10 , quotient will become new value of variable studentId
studentid = studentid / 10;
studentId= 123/10 -> 12
studentId =12 [new value]
Step 4: Now , after changing/updating the values , control will again move towards while condition , to check whether condition is true or false
Now ,
while (studentid != 0)
Here, till the studentId value is greater than 0 , loop will continue executing/iterating.
As the studentId becomes 0, control will get out of loop.
Now , studentId=12 , that means studentId !=0 , condition becomes true, control will move inside the loop
numberreverse[count] = studentid % 10;
% = modulus operator
studentId will be divided by 10 and the result remainder will be stored numberreverse array at index count
Now, the count value is incremented and count=1
so , numberreverse[1] = 12 % 10 = 2 [here , by dividing , 1 becomes quotient and 2 remains remainder]
At 1st position of array ->numberreverse[1]=2 [store the value].
Moving to next step , if - else statement is used, if count is equal to 0 , then control goes to if statement , otherwsie , else statement
Here, count =1 , control will go inside else statement
else
{
if (minimum > numberreverse[count])
minimum = numberreverse[count];
Now , for finding minimum , we have compared the previously stored minimum with newly received digit , if the digit stored at 1st index of array is less than the old minimum value ,
minimum value will be updated with the digit stored at 1st index of array
if (minimum > numberreverse[count]) //checked the statement ,digit stored at 1st index of array is less than the old minimum value
If true ,value will be updated , if false, control will move towards next if statement , without changing/updating minimum value
Here, minimum is 3 and numberreverse[1] is 2 , so the value of array index is less than minimum value, control will move inside the if statement and update the value of minimum variable
Now, minimum will become 2
minimum =2
Moving towards next if statment, checked for the maximum value
if (maximum < numberreverse[count])
Now , for finding maximum , we have compared the previously stored maximum with newly received digit , if the digit stored at 1st index of array is greater than the old maximum value ,
maximum value will be updated with the digit stored at 1st index of array
if (maximum < numberreverse[count]) //checked the statement ,digit stored at 1st index of array is greater than the old maximum value
If true ,value will be updated , if false, control will move towards next statement outside the if condition , without changing/updating minimum value
Here, maximum is 3 and numberreverse[1] is 2 , so the value of array index is not greater than maximum value, control will not move inside the if statement , leaving the old maximum value without any updation
Now, maximum will remain 3
maximum =3
Moving to next statment,
count variable will be incremented by 1
count++;
count =2
And, new value for studentId will be stored as, last digit is traversed for the number
studentId divided by 10 , quotient will become new value of variable studentId
studentid = studentid / 10;
studentId= 12/10 -> 1
studentId =1 [new value]
Step 5: Now , after changing/updating the values , control will again move towards while condition , to check whether condition is true or false
Now ,
while (studentid != 0)
Here, till the studentId value is greater than 0 , loop will continue executing/iterating.
As the studentId becomes 0, control will get out of loop.
Now , studentId=1 , that means studentId !=0 , condition becomes true, control will move inside the loop
numberreverse[count] = studentid % 10;
% = modulus operator
studentId will be divided by 10 and the result remainder will be stored numberreverse array at index count
Now, the count value is incremented and count=2
so , numberreverse[1] = 1 % 10 = 1 [here , by dividing , 0 becomes quotient and 1 remains remainder]
At 2nd position of array ->numberreverse[2]=1 [store the value].
Moving to next step , if - else statement is used, if count is equal to 0 , then control goes to if statement , otherwsie , else statement
Here, count =2 , control will go inside else statement
else
{
if (minimum > numberreverse[count])
minimum = numberreverse[count];
Now , for finding minimum , we have compared the previously stored minimum with newly received digit , if the digit stored at 2nd index of array is less than the old minimum value ,
minimum value will be updated with the digit stored at 2nd index of array
if (minimum > numberreverse[count]) //checked the statement ,digit stored at 2nd index of array is less than the old minimum value
If true ,value will be updated , if false, control will move towards next if statement , without changing/updating minimum value
Here, minimum is 2 and numberreverse[2] is 1 , so the value of array index is less than minimum value, control will move inside the if statement and update the value of minimum variable
Now, minimum will become 1
minimum =1
Moving towards next if statment, checked for the maximum value
if (maximum < numberreverse[count])
Now , for finding maximum , we have compared the previously stored maximum with newly received digit , if the digit stored at 2nd index of array is greater than the old maximum value ,
maximum value will be updated with the digit stored at 2nd index of array
if (maximum < numberreverse[count]) //checked the statement ,digit stored at 2nd index of array is greater than the old maximum value
If true ,value will be updated , if false, control will move towards next statement outside the if condition , without changing/updating minimum value
Here, maximum is 3 and numberreverse[2] is 1 , so the value of array index is not greater than maximum value, control will not move inside the if statement , leaving the old maximum value without any updation
Now, maximum will remain 3
maximum =3
Moving to next statment,
count variable will be incremented by 1
count++;
count =3
And, new value for studentId will be stored as, last digit is traversed for the number
studentId divided by 10 , quotient will become new value of variable studentId
studentid = studentid / 10;
studentId= 1/10 -> 0
studentId =0 [new value]
Now the newly updated array will be;
numberreverse[0]=3
numberreverse[1]=2
numberreverse[2]=1
Step 6: Now , after changing/updating the values , control will again move towards while condition , to check whether condition is true or false
Now ,
while (studentid != 0)
This condition becomes false here , as studentId=0 and condition becomes false
Control will move out/exit of the loop
Step 7:
Print the statement via predefined object in C++ , cout
cout << "\nReversed Number is : ";
Step 8:
for (int i = 0; i < count; i++)
cout << numberreverse[i];
Here , for loop is used ,
Syntax: for(initial value;condition, incrementor)
Startingly , value of variable i is initilaized to 0
condition is checked , whether true or false , i<count , if it the true, control will move inside the loop
If the condition is false, control will exit the loop
here , count is 3 , incremented from previous While loop
0<3 , --->True
array at value i i.e; 0 is printed, numberreverse[0] which is 3 is printed
now , increment the i variable , i will become 1 ; i++
Check the condition ; 1<3 --->True
array at value i i.e; 1 is printed, numberreverse[1] which is 2 is printed
now , increment the i variable , i will become 2 ; i++
Check the condition ; 2<3 --->True
array at value i i.e; 2 is printed, numberreverse[2] which is 1 is printed
now , increment the i variable , i will become 3 ; i++
Check the condition ; 3<3 , condition become false, control moves out of loop to the immediate next statement
The final reversed string is printed as: 321
Step 9:
Print the statement via predefined object in C++ , cout , for largest number and value of maximum variable will be displayed
cout << "\nLargest Number is :" << maximum;
Print the statement via predefined object in C++ , cout , for smallest number and value of minimum variable will be displayed
cout << "\nSmallest Number is :" << minimum;
Step 10:
return 0 ; return value of main function and exit the code