#include <iostream>
#include <math.h>
using namespace std;
int main() {
int n, temp, rem, digits=0, sum=0;
cout << "Enter a armstrong number: ";
cin >> n;
temp = n;
digits = (int)log10(n) + 1;
while (n != 0) {
rem = n % 10;
sum = sum + pow(rem, digits);
n = n / 10;
}
if (temp == sum) {
cout << "yes";
}
else {
cout << "not";
}
}
How does the " digits = (int)log10(n) + 1; " line actually calculates the digits?
can anyone explain?
Math.
Logarithms are basically "exponents in reverse." Log10(100) is 2.0, as 10 to the second power is 100.
Cast to 'int and add one to that are you get 3, which is the number of digits.
Related
This is the simple program to convert any base(2-9) to decimal.
It is completely compiled, but the output is not what I expected.
Please let me know what's the problem.
#include <iostream>
#include <cmath>
using namespace std;
int size_int(int num) //the number of num
{
int n = 0;
while(num > 0)
{
num = num/10;
n = n+1;
}
return n;
}
int convert(int num, int b) //conver num base b to base 10
{
int sum = 0;
int n = size_int(num);
while(n>0)
{
sum += (num/pow(10,n-1)) * (pow(b,n-1));
n -= 1;
}
return sum;
}
int main()
{
int num;
cout << "Enter a number to convert: ";
cin >> num;
int b;
cout << "Enter the base of number: ";
cin >> b;
int sum;
sum = convert(num, b);
cout << "Its decimal value is: "<< sum << endl;
return 0;
}
Enter a number to convert: 10110
Enter the base of number: 2
Its decimal value is: 12632
This is the result when I executed.
You calculated your current digit seems not right num/pow(10,n-1).
This is my suggestion:
#include <iostream>
#include <cmath>
using namespace std;
int size_int(int num) //the number of num
{
int n = 0;
while (num > 0)
{
num = num / 10;
n = n + 1;
}
return n;
}
int convert(int num, int b) //conver num base b to base 10
{
int sum = 0;
int n = size_int(num);
int counter = 0;
while (num > 0) {
int temp = num % 10;
num = num / 10;
sum += temp * (pow(b, counter));
counter++;
}
return sum;
}
int main()
{
int num;
cout << "Enter a number to convert: ";
cin >> num;
int b;
cout << "Enter the base of number: ";
cin >> b;
int sum;
sum = convert(num, b);
cout << "Its decimal value is: " << sum << endl;
return 0;
}
I tried to inverse the number. Ex: 243 > 342
its my quiz from school (Not to be graded)
#include <iostream>
using namespace std;
int main()
{
int n, reverse, rem;
cout << "Enter a number: ";
cin >> n;
while (n != 0)
{
rem = n % 10;
reverse = reverse / 10 + rem;
n /= 10;
}
cout << "Reversed Number: " << reverse << endl;
return 0;
}
You should do reverse = reverse* 10 + rem; instead of division:
#include <iostream>
using namespace std;
int main()
{
int n = 0, reverse = 0, rem = 0;
cout << "Enter a number: ";
cin >> n;
while (n != 0)
{
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
cout << "Reversed Number: " << reverse << endl;
return 0;
}
you can try this
#include <iostream>
using namespace std;
int main()
{
int n, reverse=0, rem;
cout<<"Enter a number: ";
cin>>n;
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
cout<<"Reversed Number: "<<reverse<<endl;
return 0;
}
Looking for some advice here on what I'm getting wrong. Everything in my main should be fine and left unchanged. My problem is in my reverse function. It's printing the reversed number right before the cout statement of "The number is" instead down below where it should be. I spent awhile trying to fix but can't come up with a solution.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
const int NUM_VALS = 10; //the maximum number of values to use
int reverse(int num);
bool isPrime(int num);
int main()
{
int number, //Holds the random number that is manipulated and tested
loopCnt; //Controls the loop
//set the seed value for the random number generator
//Note: a value of 1 will generate the same sequence of "random" numbers every
// time the program is executed
srand(1);
//Generate 10 random numbers to be manipulated and tested
for( loopCnt = 1; loopCnt <= NUM_VALS; loopCnt++ )
{
//Get a random number
number = rand();
//Display the sum of adding up the digits in the random number, the reversed
//random number, and whether or not the number is palindromic or a prime number
cout << "The number is " << number << endl
<< "----------------------------------------" << endl
// << "Adding the digits result" << setw(16) << sumDigits( number ) << endl
<< "Reversing the digits result" << setw(13) << reverse(number) << endl
// << "Is the number a palindrome?" << setw(13) << (isPalindrome(number)? "Yes" : "No") << endl
// << "Is the number prime?" << setw(20) << (isPrime(number)? "Yes" : "No") << endl
<< endl << endl;
}
return 0;
}
int reverse(int num)
{
int quo, rem;
quo = num;
while (quo != 0)
{
rem = quo % 10;
cout << rem;
quo /= 10;
}
}
bool isPrime(int num)
{
int i;
if (num % 2 == 0)
return false;
for (i = 3; i*i <= num; i+=2)
{
if (num % i == 0)
return false;
}
return true;
}
You need to have your reverse function return the number as reversed, because the return value is used in main.
You can build the reversed number by multiplying a "reversed" value by 10, then adding in the remainder:
int reverse(int num)
{
int reversed = 0;
int quo, rem;
quo = num;
while (quo != 0)
{
rem = quo % 10;
reversed = reversed * 10 + rem;
quo /= 10;
}
return reversed;
}
You can also use this method to reverse a number by taking string input and then reverse it and convert it to int.
#include <iostream>
#include<string>
using namespace std;
int reverse_num(string a)
{
string s;
for(int i=a.length()-1;i>=0;i--)
{
s+=a[i];
}
int n;
n=stoi(s);
return n;
}
int main()
{
string a;
cin>> a;
cout<<reverse_num(a);
return 0;
}
I'm making a program that prints all digits from an array (entered as an integer) and it works, but the digits are printed backwards and I don't know how to reverse them. Can someone help?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
cout << digit << '\n';
number /= 10;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
// numdigits = explode(n,digits);
cout << "[";
while (n > 0) {
int digit = n % 10;
n /= 10;
digits[digit] = digit;
cout << digits[digit];
}
cout << "]" << endl;
}
You just have to reverse the array using reverse() from <algorithm>.
Code:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int array_c = 0;
void explode(int number,int array[])
{
while (number > 0) {
int digit = number % 10;
number /= 10;
array[array_c++] = digit;
}
}
int main()
{
int digits[100];
int numdigits;
int n;
cout << "Enter number: ";
cin >> n;
explode(n,digits);
reverse(digits,digits+array_c);
cout << "[";
for(int i = 0; i < array_c; ++i)
cout<<digits[i];
cout << "]" << endl;
}
Your use of
digits[digit] = digit;
is not right. You probably meant to use
digits[numdigits] = digit;
You can fix your problem by dividing the work into two steps. In the first step, you store the digits. In the second step, you print the digits.
int numdigits = 0;
while (n > 0) {
cout << "n: " << n << endl;
int digit = n % 10;
n /= 10;
digits[numdigits++] = digit;
}
// Make sure to print them in reverse order.
cout << "[";
for ( ; numdigits > 0; )
{
cout << digits[--numdigits];
}
cout << "]" << endl;
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!
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
}