check number is octal or not??

Write a C++ program to read an integer and test that the given integer is an octal number or not?????

i m in problem when u put 0 in first position of integer example: 0123, it creates a problems...

please tell me right program , how i find it out
i m in problem when u put 0 in first position of integer example: 0123, it creates a problems...
What kind of problems?
thanks for reply,

it contain 0 , so that when we get last digit to check whether it is 8 OR 9 by using % (mode) it give other value (as we do it divide method) , i also try it to convert integer to string and than i m also not able to get last digit to check its octal value .... so please send me full programm in which octal value is check........
I don't know what you're trying to do.

Octal is just another kind of representation of an integer as a string. I guess you're talking about that string. To check whether the string contains only octal number the check + conversion would look like this
1
2
3
4
5
6
7
8
9
10
11
12
13
int val = 0;
int factor = 1;
for(int i = 0; i < str_size; ++i)
{
  const int cur_digit = str_size - i - 1;
  if((str[cur_digit] >= '0') && (str[cur_digit] <= '8'))
  {
    val += (str[cur_digit] - '0') * factor;
    factor *= 8;
  }
  else
    break; // not octal
}
[Not tested]

A leading 0 won't harm
Topic archived. No new replies allowed.