while working on a module i came across a problem and could not figure out the solution to the problem. here is the question . program will get the input number and position from the user output the digit which is is at that position. for example if number = 256314 and position = 0 then output should be "2" if number = 1256985 and position = 3 then output should be "6" here is my code
#include <iostream>
#include <cmath>
using namespace std;
int returnDigitAtPosition(int &position,int &totaldigits,int &number)
{
{
int positiondigit = number/pow(10,(totaldigits - position));
positiondigit = positiondigit % 10;
return positiondigit;
}
}
int main()
{
int number = 0,position = 0,digit = 0;
cout << " Enter the number : ";
cin>>number;
cout<<" Enter the position : ";
cin>>position;
/*cout<<" Enter the digit : ";
cin>>digit;*/
int totaldigits = log(number);
int DigitAtPosition = returnDigitAtPosition(position,totaldigits,number);
cout<<DigitAtPosition;
#include <cmath>
usingnamespace std;
int returnDigitAtPosition(int &position,int &totaldigits,int &number)
{
{
int positiondigit = number/pow(10,(totaldigits - position));
positiondigit = positiondigit % 10;
return positiondigit;
}
}
int main()
{
int number = 0,position = 0,digit = 0;
cout << " Enter the number : ";
cin>>number;
cout<<" Enter the position : ";
cin>>position;
/*cout<<" Enter the digit : ";
cin>>digit;*/
int totaldigits = log(number);
int DigitAtPosition = returnDigitAtPosition(position,totaldigits,number);
cout<<DigitAtPosition;
}
int returnDigitAtPosition(int &position,int &totaldigits,int &number)
{
{ // this right here is wrong it should be like this I don't know if it solve your problem but this is wrong. No time to look over the whole thing because im sleepy. close your bracket around your function.
#include <iostream>
#include <cmath>
int DigitAtPosition(int &number, int &position)
{
int positiondigit = int(number/pow(10,position)) % 10;
return positiondigit;
}
int main()
{
int number = 0, position = 0;
std::cout << " Enter the number : ";
std::cin >> number;
std::cout << " Enter the position : ";
std::cin >> position;
std::cout << " Number is: " << DigitAtPosition( number, position);
return 0;
}