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;
}
Related
#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.
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;
}
#include <iostream>
using namespace std;
int main()
{
long int number;
int digits;
cout << "Enter Number: ";
cin >> number;
int counter[10] = { 0,0,0,0,0,0,0,0,0,0 };
while (number != 0) {
digits = number % 10;
counter[digits] = counter[digits] + 1;
number = number / 10;
}
for (int i = 0; i<10; i++) {
if (counter[i] != 0) {
cout << i << ": " << counter[i] << endl;
}
}
return 0;
system("pause");
}
I'm having an issue with my code that when I run it and enter a Number nothing really happens. It is supposed to run something like 1234556789 and the output should look like
1 : 9
2 : 8
3 : 7
4 : 6
5 : 5
I know sometimes if there isn't a system pause this happens where it runs part of the code and just ends, but I'm not sure whats wrong here.
#include <iostream>
using namespace std;
int main()
{
long int number;
int digits;
cout << "Enter Number: ";
cin >> number;
int counter[10]={0},a=0;
while (number != 0) {
digits = number % 10;
counter[a] = digits; //made changes to this line
number = number / 10;
++a;
}
for (int i = 0; i<10; i++) {
if (counter[i] != 0) {
cout << i << ": " << counter[i] << endl;
}
}
return 0;
}
All you are doing right now is printing how many digits there are of each number 0-9 in the number. If you want to pair elements together, then you can use std::vector and iterators. The number of digits in your input can be either even or odd and you would have to account for both cases.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long int number;
cout << "Enter Number: ";
cin >> number;
vector<int> digits;
if (number == 0)
{
digits.push_back(number);
}
while (number != 0)
{
digits.push_back(number % 10);
number /= 10;
}
auto it_begin = digits.begin();
auto it_end = digits.end() - 1;
if (digits.size() % 2 == 1)
{
for (; it_end != it_begin; ++it_begin, --it_end)
{
cout << *it_end << ": " << *it_begin << endl;
}
cout << *it_end << endl;
}
else
{
for (; it_begin < it_end; ++it_begin, --it_end)
{
cout << *it_end << ": " << *it_begin << endl;
}
}
}
With number = 1234556789, the output is:
1: 9
2: 8
3: 7
4: 6
5: 5
If you want first 10 no. Only use this code
include using namespace std;int main() {long int number;cout << "Enter Number: ";cin >> number;for (int i = 1; i<=10; i++) {cout << i << ": "<
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;
I would like to analyze the complexity of my code algorithm.Therefore,i must have 2 different programs giving the same functions to allow me to start off.
Currently this is my own code.
I'm not sure if it is allowed that i would like to have someone that could volunteer his own way code to compute summation of factorial for me as the 2nd program code.
Preferrably a nested loop.
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++)
{
c = c * i;
a = a + c;
}
cout << "The sum of the factorials is " << a << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int val;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
static const int results[] = {
0, 1, 3, 9, 33, 153, 873, 5913, 46233, 409113,
4037913, 43954713, 522956313
};
cout << "The sum of the factorials is " << results[val < 0 ? 0 : val] << endl;
system("pause");
return 0;
}
Note that I replicated the defect in the original program which causes it to return the incorrect value if the user enters 0.
This alternate version assumes 32-bit integers because it takes advantage of overflow behavior. Extending to 64-bit integers is left as an exercise.
I do not understand what you do with another nested way but i hope this can help...
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++){
c *= i;
a += c;
}
int c2=1;
for (i = val; i > 1; i--){
c2*=i;
c2++;
}
cout << "The sum of the factorials is " << a << endl;
cout << "The sum of the factorials is " << c2 << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int suma = 0;
int n = 0;
cout << "Sum of factorials\n";
cout << "-------------------------------\n";
cout << "Insert number of n: ";
cin >> n;
int i = 1;
while (i <= n)
{
int factorial = 1;
for(int j=1; j<=i; j++)
{
factorial = factorial * j;
}
suma += factorial;
i++;
}
cout << "Sum of factorials is: " << suma;
system("pause");
return 0;
}