How do I extract the digits of a number in C++?
Basically I would like to make a small little program that when u enter a number (say 145) it read the 3 digits and prints the largest one.
int a, b, c, max;
cout << "Enter a, b and c: ";
cin >> a >> b >> c;
max = a;
if (b>max)
max = b;
if (c>max)
max = c;
cout << "Max is " << max << "\n";
I was think of using something like this, but I have no idea how to get the computer to read each indiv开发者_StackOverflowidual digit. thanks!
Change int
on the first line to char
.
#include <iostream>
int main() {
char a, b, c, max;
std::cout << "Enter a, b and c: ";
std::cin >> a >> b >> c;
max = a;
if (b>max)
max = b;
if (c>max)
max = c;
std::cout << "Max is " << max << "\n";
}
This works, but is really not the right way to approach this problem IMO for C++.
This is slightly better, but with no kind of input validation:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string s;
std::cout << "Enter a number: ";
std::cin >> s;
char maxChar = *max_element(s.begin(), s.end());
std::cout << "Max is " << maxChar << "\n";
}
No need to resort to anything C++-specific when plain C will do it in less time than the conversions in keith.layne's answer if you already have the number in hand:
unsigned big_digit(unsigned value)
{
unsigned biggest = 0;
while (value) {
unsigned digit = value % 10;
if ( digit > biggest )
biggest = digit;
value /= 10;
}
return biggest;
}
Hope that wasn't homework.
You can make use of %(modulus) for such operations.
I think this LINK will do you justice
Basically I would like to make a small little program that when u enter a number (say 145) it read the 3 digits and prints the largest one.
int a, b, c, max;
cout << "Enter a, b and c: ";
cin >> a >> b >> c;
max = a;
if (b>max)
max = b;
if (c>max)
max = c;
cout << "Max is " << max << "\n";
I was think of using something like this, but I have no idea how to get the computer to read each individual digit. thanks!
While the answer using by keith.layne with strings works if you would like an answer that doesn't use strings you can just use integer division and modulus to get the same result:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int userInput, max;
cout << "Input a number and I'll give the biggest digit: ";
cin >> userInput;
cout << "The max digit of " << userInput << " is ";
max = userInput % 10; // sets one's digit to max
userInput /= 10; // reduces number by a digit
while(userInput > 0) // while number has remaining digits
{
if(userInput % 10 > max)// checks for a new max
{
max = userInput % 10;
}
userInput /= 10; // reduces number by a digit
}
cout << max << endl;
return 0;
}
spec :must be a4 digit number, either - or +, Modify this code to get your desired output. cheers!
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
int a, b, c, d, rem;
int no;
int max = 0;
int min = 0;
cin >> no;
if (no < 0)
no = abs(no);
a = no/1000;
rem = no%1000;
b = rem/100;
rem = rem%100;
c = rem/10;
rem = rem%10;
//cout<<a;
if(a > 0 && a <= 9)
{
if(a > max)
max = a;
else min = a;
if(b > max)
max = b;
else min = b;
if(c > max)
max = c;
else min = c;
if(rem > max)
max = rem;
else min = rem;
if(max > min)
cout << max << endl;
else
cout << min << endl;
}
else
cout<<"Invalid no"<<endl;
return 0;
}
I came up with this while trying to solve a codewars problem.
int i = 0;
int num_ = num; //we will need a dummy, num is the original
while(num_ != 0){ //count the number of digits
num_ /= 10;
i++; //yayyy
}
int *ptr = new int[i]; //dynamic array to store individual numbers
int pos = 0;
while(1){ //copy digits to dynamic array
if(num > 10){
num_ = num%10;
ptr[pos] = num_;
num /= 10;
pos++;
}
else{
num_ = num%10;
ptr[pos] = num_;
break;
} //array now contains our digits
}
精彩评论