How to make an integer from it's numbers? - c++

I have a number, which I have to write backward. I already separated it's digits in an array, but how can I make the new number from this array?

You don't need to store digits in an array to reverse digits of number.
#include <iostream>
using namespace std;
int main()
{
int input;
cin>>input;
int reverse = 0;
while(input)
{
reverse *= 10;
reverse += input % 10;
input /= 10;
}
cout << reverse << endl;
return 0;
}

Related

Detecting the first digit in the second digits?

Needle in the haystack. I'm a beginner in programming and we only learned a thing or two so far, barely reached arrays yet.
Input: 1 4325121
Output: 2
Input two values in one line. The first one shall accept any integer from 0-9 and the other one shall take a random positive integer.
Using a while loop, count how many of the first integer (0-9) is present in the digits of the second inputted integer and print the result.
No arrays to be used here, only while loops and else-if conditions with basic coding knowledge and without the use of advanced coding.
As you said, you need to keep it as simple as possible. Then this can be a solution:
#include <iostream>
int main()
{
int first { };
int second { };
std::cin >> first >> second;
int quo { second };
int rem { };
int count { };
while ( quo > 0 )
{
rem = quo % 10;
quo /= 10;
if ( first == rem )
{
++count;
}
}
std::cout << "Result: " << count << '\n';
}
Using while loop
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 4325121;
int count = 0;
while(b > 0)
{
int m = b % 10;
if(m == a)
{
count++;
}
b /= 10;
}
cout << count;
return 0;
}
Nice little problem. But actually, to keep it as simple as possible no calculations are needed at all. I simplified my example, and it just keeps working on the input text, which is 100% sufficient to solve the problem:
#include <iostream>
#include <string>
using namespace std;
int main() {
char digit;
std::string number;
cout << "Input: ";
cin >> digit >> number;
int count = 0;
for (char const character : number)
if (character == digit)
count++;
cout << "Result: " << count << endl;
return 0;
}
Given the question, this code solves the problem.

Subtract each elements of an array consecutively

I have an array and I want to subtract each of the elements consecutively, ex: {1,2,3,4,5}, and it will result to -13 which is by 1-2-3-4-5.
But I don't declare or make those numbers fixed as they're taken from the input (user). I only make it like, int array[100] to declare the size.
Then, to get the inputs, I use the for loop and insert them to the array. Let's say first input is 10, then array[0] must be 10 and so on.
The problem is, how do I subtract them? I have two options:
The first element of the array (array[0]) will subtract the next element (array[1]) right after the user input the second element, and the result (let's say it's int x) will subtract the next element (array[2]) after the user input it and so on.
I'll have the user input all the numbers first, then subtract them one by one automatically using a loop (or any idea?) *These elements thing refer to the numbers the user input.
Question: How do you solve this problem?
(This program will let the user input for as much as they want until they type count. Frankly speaking, yeah I know it's quite absurd to see one typing words in the middle of inputting numbers, but in this case, just how can you do it?)
Thanks.
Let's see my code below of how I insert the user input into the array.
string input[100];
int arrayInput[100];
int x = 0;
for (int i = 0; i >= 0; i++) //which this will run until the user input 'count'
{
cout << "Number " << i+1 << ": ";
cin >> input[i];
arrayInput[i] = atoi(input[i].c_str());
...
//code to subtract them, and final answer will be in int x
...
if (input[i] == "count")
{
cout << "Result: " << x << endl;
}
}
You can/should use a dynamic sized container like std::vector as shown below:
#include <iostream>
#include <vector>
int main()
{
int n = 0;
//ask user how many input he/she wants to give
std::cout << "How many elements do you want to enter: ";
std::cin >> n;
std::vector<int> vec(n); //create a vector of size n
int resultOfSubtraction = 0;
//take input from user
for(int i = 0 ; i < n ; ++i)
{
std::cin >> vec.at(i);
if(i != 0)
{
resultOfSubtraction-= vec.at(i);
}
else
{
resultOfSubtraction = vec.at(i);
}
}
std::cout<<"result is: "<<resultOfSubtraction<<std::endl;
return 0;
}
Execute the program here.
If you want a string to end the loop then you can use:
#include <iostream>
#include <vector>
#include <sstream>
int main()
{
std::vector<int> vec;
int resultOfSubtraction = 0, i = 0;
std::string endLoopString = "count";
std::string inputString;
int number = 0;
//take input from user
while((std::getline(std::cin, inputString)) && (inputString!=endLoopString))
{
std::istringstream ss(inputString);
if(ss >> number)
{
vec.push_back(number);
if(i == 0)
{
resultOfSubtraction = number;
}
else
{
resultOfSubtraction-= number;
}
++i;
}
}
std::cout<<"result is: "<<resultOfSubtraction<<std::endl;
return 0;
}

Can't get Right aligned number triangle pattern giving space using setw in C++

I want a right aligned number pattern using setw for giving spaces
either the spaces disappear when setw is less than no. of printed numbers or it makes any random pattern.
My Code is below:
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
int space, n,i,j,k,l,m;
cin>>n;
for (k = n; k <=2*n-1; k++)
{
cout<<setw(k);
}
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
cout<<j;
}
cout<<endl;
}
return 0;
}
The output I am getting is (n=5):
1
12
123
1234
12345
The output I want is (n=5):
1
12
123
1234
12345
The first for loop is not necessary.
setw sets the field length for the next field (the field following setw). So if the next field is an empty string, setw(x) will just reserve x characters space. Also, the value x should be equal to n - i (if no. of characters to be printed is 1, you need (5 - 1) i. e. 4 spaces)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n, i, j;
cin >> n;
for (i = 1; i <= n; i++) {
cout << setw(n - i) << "";
for (j = 1; j <= i; j++) {
cout << j;
}
cout << endl;
}
return 0;
}

Trying to rewrite a code to include the modulus operator

The problem is as follows:
Input is the "t" number of data sets, followed by t 11-digit numbers. There's a cipher included in the code as an array. The code is supposed to multiply the subsequent digits from the input number by the corresponding digits from the cipher, thus creating a sum of 11 multiplications. After that the code checks whether the sum is divisible by 10. If so, it returns "correct", if not - "incorrect".
I've written a code which works as intended, but I'd like to simplify this code, specifically to include the modulus operator instead of fmod to extract the digits from the 11-digit input numbers. I've tried using modulus, but it can only be utilized for an int.
I found a code for a simple reversed order digit extractor (using the while loop and %10), but I'm having some trouble implementing it in my code... Any help would be appreciated.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long long int number;
int cipher [11] = {1,3,7,9,1,3,7,9,1,3,1};
int t, digit, sum;
cin >> t;
for (int i=0; i<t; i++)
{
sum = 0;
cin >> number;
for (int j=10; j>=0; j--)
{
digit = fmod(number/(pow(10,(10-j))),10);
sum = sum + digit*cipher[j];
}
if (sum%10==0)
cout << "Correct" << endl;
else
cout << "Incorrect" << endl;
}
return 0;
}
An example of a correct number is 44051401458. We're assuming all the input numbers are always 11 digits.
I think this is what you are referring to. You can just keep dividing number by 10, which will shift the decimal point. It will not turn into a float since you defined number as an int (long long int), so any decimal point just gets erased and you can use modulus freely.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long long int number;
int cipher [11] = {1,3,7,9,1,3,7,9,1,3,1};
int t, digit, sum;
cin >> t;
for (int i=0; i<t; i++)
{
sum = 0;
cin >> number;
for (int j=10; j>=0; j--)
{
digit = number%10;
number = number/10;
sum = sum + digit*cipher[j];
}
if (sum%10==0)
cout << "Correct" << endl;
else
cout << "Incorrect" << endl;
}
return 0;
}

How to get length of int variable as string have builtin function e.g string.length()

Is there any way to get the length of int variable e.g In string we get the length by simply writing int size = string.length();
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i = 0;
cout<<"Please Enter the value of i"<<endl;
cin>>i;
//if user enter 123
//then size be 3 .
// Is it possible to find that size
}
#include <cassert>
#include <cmath>
#include <iostream>
using namespace std;
int main () {
assert(int(log10(9)) + 1 == 1);
assert(int(log10(99)) + 1 == 2);
assert(int(log10(123)) + 1 == 3);
assert(int(log10(999)) + 1 == 3);
return 0;}
You have a few options here:
(This answer assumes you mean number of printable characters in the integer input)
Read the input as a string and get its length before converting to an int. Note that this code avoids error handling for brevity.
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char** argv) {
cout << "Please enter the value of i" << endl;
string stringIn = "";
cin >> stringIn;
cout << "stringIn = " << stringIn << endl;
size_t length = stringIn.length();
cout << "input length = " << length << endl;
int intIn;
istringstream(stringIn) >> intIn;
cout << "integer = " << intIn << endl;
}
Read in an integer and count the digits directly:
Many other answer do this using log. I'll give one that will properly count the minus sign as a character.
int length_of_int(int number) {
int length = 0;
if (number < 0) {
number = (-1) * number;
++length;
}
while (number) {
number /= 10;
length++;
}
return length;
}
Derived from granmirupa's answer.
Not sure whether it fits your requirement but you could use std::to_string to convert your numeric data to string and then return its length.
For length i assume you mean the number of digits in a number:
#include <math.h>
.....
int num_of_digits(int number)
{
int digits;
if(number < 0)
number = (-1)*number;
digits = ((int)log10 (number)) + 1;
return digits;
}
Or:
int num_of_digits(int number)
{
int digits = 0;
if (number < 0) number = (-1) * number;
while (number) {
number /= 10;
digits++;
}
return digits;
}
Onother option could be this (can works with float too, but the result is not guaranteed):
#include <iostream>
#include <sstream>
#include <iomanip>
...........
int num_of_digits3(float number){
stringstream ss;
ss << setprecision (20) << number;
return ss.str().length();
}