Ok so I dont expect anybody to just give me the answer but I just dont get this at all. Im thinking this needs to be done with some kind of loop but I have no idea which kind. Can anyone give me some insight on this program???? Thnx
Write a program that prompts for and then accepts an integer (number) from the keyboard. The program then prompts for and accepts a small integer that represents a digit position (pos) in number. The program will then find and print the digit at position pos in the number. For example, if the number entered is 12345, the program should display 2 if the user says they want the fourth (4) digit.
(Hint: 5 =345 %10 and 34=345/10 )
int main()
{
int num = 0;
int number = 0;
int n = 0;
int result = 0;
cout << "Enter a number: ";
cin >> number;
cout << "Enter a digit position:";
cin >> n;
if ( 0 == number)
{
cout << "0";
}
while (n-- > 1)
{
num = number / (10^n-1);
result = num % 10;
}
cout << "\nThe value of this digit is: " << result << endl;
system("Pause");
}
#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int number = 0;
int n = 0;
int result = 0;
cout << "Enter a number: ";
cin >> number;
cout << "Enter a digit position:";
cin >> n;
if ( 0 == number)
{
cout << "0";
}
while (n-- > 1)
{
number = number / pow(10,(n-1.0));
result = number % 10;
}
cout << "\nThe value of this digit is: " << result << endl;
system("Pause");
}
I have no idea. It was based on repitition and I was told it had to be done using a loop, although I had no idea what kind of loop so thats just what I threw in there...