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;
Related
How do I make my code specifically on else if to print out this if my input is 45?
01.02.03.04.05.06.07.08.09.10
11.12.13.14.15.16.17.18.19.20
21.22.23.24.25.26.27.28.29.30
31.32.33.34.35.36.37.38.39.40
41.42.43.44.45
#include <iostream>
#include <string>
using namespace std;
int main() {
string dot = "";
int x;
cin >> x;
if (x<=10){
for (int n=1; n<=x; n++){
cout << dot << n ;
dot =".";
}
}
else if(x>10&&x<=100) {
for (int i = 1; i <=x; ++i){
for (int j = 1; j <=10; ++j){
cout << dot << x;
dot=".";
}
cout << endl;
}
}
else{
cout << "OUT OF RANGE";
}
return 0;
}
Your entire program can be simplified using setw and setfill to do the hard work for you of inserting leading zero chars where needed. #include <iomanip> to have access to these stream modification functions.
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
for (int i = 1; i <= x; i++)
{
cout << setw(2) << setfill('0') << i;
char delimiter = ((i % 10) && (i != x)) ? '.' : '\n';
cout << delimiter;
}
cout << endl;
}
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int x = 0;
cin >> x;
if (x > 100)
{
cout << "Out of range." << endl;
return 0;
}
for (int i = 1; i <= x; i++)
{
cout << setfill('0') << setw(2) << i << ((i % 10) ? "." : "\n");
}
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;
}
#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 am trying to get the factors of positive integer. What I want is 8 = 2*2*2. However, what I get is *2*2*2. How can I get ride of the first *? Is there a standard way to better describe this situation?
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char separator = '*';
cout << "Input a positive integer: ";
cin >> num;
while(num !=1){
while((num % i) != 0){
i++;
}
cout << setw(2) << setfill(separator) << i;
num = num/i;
}
}
Input a positive integer: 8
*2*2*2
Use a separator that is updated. Start with "" and set to "*" thereafter.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char *separator = "";
cout << "Input a positive integer: ";
cin >> num;
do {
while((num % i) != 0){
i++;
}
cout << separator << i;
separator = "*";
num = num/i;
} while (num > 1);
}
Also changed to do loop to cope with num == 1 and num == 0 which print nothing in OP's original code. Code could use unsigned as a further extension/ protection.
One way to accomplish that is by outputting setfill(separator) only when num is not equals to 1.
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int num, i = 2;
const char separator = '*';
cout << "Input a positive integer: ";
cin >> num;
while(num !=1){
while((num % i) != 0){
i++;
}
cout << setw(2) << i;
num = num/i;
if (num != 1)
cout << setfill(separator);
}
}
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;
}