The digits 0, 1, and 8 look much the same if rotated 180 degrees on the page (turned

upside down). Also, the digit 6 looks much like a 9, and vice versa, when rotated 180
degrees on the page. A multi-digit number may also look like itself when rotated on the
page; for example 9966 and 10801 do, but 999 and 1234 do not.
You are to write a program to count how many numbers from a given interval look like
themselves when rotated 180 degrees on the page. For example, in the interval [1..100]
there are six : 1, 8, 11, 69, 88, and 96.
Your program should take as input two integers, m and n, which define the interval to be
checked, 1 ≤ m ≤ n ≤ 32000. The output from your program is the number of rotatable
numbers in the interval.
You may assume that all input is valid.
Input/output is not from/to files for this question. Keyboard input and screen output is
expected.
Sample Session User input is in italics.
Enter the lower bound of the interval:
1
Enter the upper bound of the interval:
100
The number of rotatable numbers is:
6

I am kind of stuck withe logic... help me please.
Here's what i have done so far...

import java.util.Scanner;

public class j2 {

/**Gaurav Sharma
*Date: Feb 26th 2011
*Rotatable numbers.
*/

public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=orNum.charAt(length--);
length--;
System.out.print (revVar);
}return revVar;

}
public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
//input
System.out.println("Enter the lower bound of the interval:");
int lowerbound = sc.nextInt();
System.out.println("Enter the upper bound of the interval:");
int upperbound = sc.nextInt ();
System.out.println ("The number of rotatable numbers is:");
int numofrotate = 0;
int rangevalues = 0;
String orNum;

for (int i=lowerbound; i<upperbound;i++){
orNum = i+"";

/*if (orNum.equals ("9")){
revVar = revVar+"6";
}
if (orNum.equals ("8")){
revVar= revVar+"8";
}
if (orNum.equals ("1")) {
revVar = revVar+ "1";
}
if (orNum.equals ("0")) {
revVar = revVar+"0";
}
if (orNum.equals ("6")){
revVar= revVar+"9";
}
else {
revVar = revVar+"$";
}
///System.out.println(revVar);
}
if (orNum.equals(revVar))
{
numofrotate++;
System.out.print(numofrotate);
}



*/}








}
}

Indeed, you need a little improvement on the logic.

The input/output part looks fine.

I suggest the following hints. If you need further help, post.

1. The input number should be better stored as a string, because you would like to manipulate the number digit by digit.

2. You would need a table of conversion, for example, 6->9, 1->1, 8->8, 0->0, etc. This can be implemented as an array of 10 strings, which will contain the strings
0,1,x,x,x,5,9,x,8,6 rekspectively
where x represents the non-rotatable numbers.
So a[0]="0", a[6]="9", a[8]="8", a[2]="x", etc.

3. Make a result string (s1) and assign "" as its value, i.e. it contains nothing.

3. Read the input string (s) from the left, converting each digit into a "char" type variable using s.charAt(i) for the ith character. This is also a number. Character '0' has a value of 48, '1' has a value of 49, etc.

4. Convert this character into a string using the previous array (a): using
a[s.charAt(i)-48], assuming input values are all digits.

5. add this digit to the left of the new string (s1):
s1=a[s.charAt(i)-48]+s1;

6. Process all the characters of the string the same way until all characters have been converted.

7. Print the resulting string s1.

If you have difficulties, post your latest code as a follow-up to this post, not a new post. Describe where your difficulties are.

thanks for the help. actually i made a new function to reverse the order of numbers. EXAMPLE. 1234 become 4321.

Here is the code:
public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=orNum.charAt(length--);
length--;
System.out.print (revVar);
}return revVar;

}

i want to see if this function works or not. i am printing out the string in the function but i wanna print out the string inside the main. any help would be appreciated. I just wanna see if i input 1234...i wanna print out the revesrsed num and that's why i am used a new function. please help!

Your function basically works, but two changes are required:

1. you have put in length-- in two places. That skips characters. Also, the first occurrence needs to be decremented before, so it is --length.

2. Characters are not automatically (and correctly) upgraded to String. The use of the function Character.toString(char) is recommended.

revVar+=Character.toString(orNum.charAt(--length));

Other than that, you can put in a standard main program to test your code.
public static void main(String[] args){
System.out.println(reversing("65432"));
return;
}

Post if you have other questions.

Also, the algorithm for reversing could be simply read characters from left to right and append each character to the left of the new string.

public static String reversing(String orNum){

String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=Character.toString(orNum.charAt(--length));
length--;
System.out.print (revVar);
}return revVar;

}
public static void main(String[] args) {
System.out.println(reversing("65432"));
return;

i did the correction but i get 224246

There were two little problems with this one. You have left the extra length-- on, so the string position got decremented twice, and ran out to give a StringOutOfBounds exception.

The extra print statement (inside the for-loop) helps to mess up the results. By removing it, you will see "23456".

Also, I noted that even without the Character.toString() function, Java does the character to string promotion correctly. However, personally I would not mind to see this promotion done manually, because it demonstration an expected promotion.

thank you it worked.

Second step is the rotate the numbers that are rotatable so i need to make another function...

public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=Character.toString(orNum.charAt(--length));


}return revVar;

}
public static String rotnum(String orNum){
String revVar="";
if (orNum.equals ("9")){
revVar = revVar+"6";
}
if (orNum.equals ("8")){
revVar= revVar+"8";
}
if (orNum.equals ("1")) {
revVar = revVar+ "1";
}
if (orNum.equals ("0")) {
revVar = revVar+"0";
}
if (orNum.equals ("6")){
revVar= revVar+"9";
}
else {
revVar = revVar+"$";
}
System.out.println(revVar);
}return rotnum;


public static void main(String[] args) {
System.out.println(reversing("123456"));
return;

Scanner sc = new Scanner(System.in);
//input
System.out.println("Enter the lower bound of the interval:");
int lowerbound = sc.nextInt();
System.out.println("Enter the upper bound of the interval:");
int upperbound = sc.nextInt ();
System.out.println ("The number of rotatable numbers is:");
int numofrotate = 0;
int rangevalues = 0;
String orNum;
String revVar="";
for (int i=lowerbound; i<upperbound;i++){
orNum = i+"";
if (orNum.equals(revVar))
{
numofrotate++;
System.out.print(numofrotate);
}



}








}
}


i did create another function but i am getting syntax error at return?

import java.util.Scanner;

public class j2 {

/**Gaurav Sharma
*Date: Feb 26th 2011
*Rotatable numbers.
*/

public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=Character.toString(orNum.charAt(--length));


}return revVar;

}
public static String rotnum(String orNum){
String revVar="";
if (orNum.equals ("9")){
revVar = revVar+"6";
}
if (orNum.equals ("8")){
revVar= revVar+"8";
}
if (orNum.equals ("1")) {
revVar = revVar+ "1";
}
if (orNum.equals ("0")) {
revVar = revVar+"0";
}
if (orNum.equals ("6")){
revVar= revVar+"9";
}
else {
revVar = revVar+"";
}return revVar;

}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//input
System.out.println("Enter the lower bound of the interval:");
int lowerbound = sc.nextInt();
System.out.println("Enter the upper bound of the interval:");
int upperbound = sc.nextInt ();
System.out.println ("The number of rotatable numbers is:");
int numofrotate = 0;
int rangevalues = 0;
String orNum;
String revVar="";

for (int i=lowerbound; i<upperbound;i++){
orNum = i+"";

if (orNum.equals(revVar))
{
numofrotate++;

System.out.print(numofrotate);

}
return;


}








}
}




this is my corrected code...my lowerbound was 1 and upper was 100... i don't get any rotatable number. I have created a function. what am i wrong?

You're getting closer.

There are unmatched braces in your code. I suppose they were introduced when you transferred it to the post.

One way to debug the program is to print each of the eligible cases. Don't just count. You can suppress the printing when everything works.

Also, try to write your code incrementally, not put all the code, and when it does not work, you don't know where to look. Always stay on firm ground, check one part, then write a few more lines, and check again. This way, if things don't work, you only have a few lines to check.

This line in main:
if (orNum.equals(revVar))
does NOT call your routines, so it does not work for you.

Also in your reversing() function, you have to reject numbers that contain 2,3,4,5,7. You can do this by something like:

char ch=orNum.charAt(--length);
if(ch=='2'||ch=='3'||ch=='4'||ch=='5'||ch=='7')return null;
revVar+=Character.toString(ch);

and check for a return String of null, which you can reject.

After that, you can work on the rotate function.

updated code... it still doesn't work. what am i doing wrong.

public static String reversing(String orNum){
String revVar = "";
int length = orNum.length();
for (int i = 0; i < orNum.length(); i++)
{
revVar+=Character.toString(orNum.charAt(--length));
char ch=orNum.charAt(--length);
if(ch=='2'||ch=='3'||ch=='4'||ch=='5'||ch=='7')return null;
revVar+=Character.toString(ch);
}return revVar;

}
public static String rotnum(String orNum){
String revVar="";
if (orNum.equals ("9")){
revVar = revVar+"6";
}
if (orNum.equals ("8")){
revVar= revVar+"8";
}
if (orNum.equals ("1")) {
revVar = revVar+ "1";
}
if (orNum.equals ("0")) {
revVar = revVar+"0";
}
if (orNum.equals ("6")){
revVar= revVar+"9";
}
else {
revVar = revVar+"";
}return revVar;

}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//input
System.out.println("Enter the lower bound of the interval:");
int lowerbound = sc.nextInt();
System.out.println("Enter the upper bound of the interval:");
int upperbound = sc.nextInt ();
System.out.println ("The number of rotatable numbers is:");
int numofrotate = 0;
String orNum="";
String revVar="";

for (int i=lowerbound; i<upperbound;i++){
orNum = i+"";

if (orNum.equals(revVar))
{

numofrotate++;

System.out.print(numofrotate);

}
return;


}
}
}

If you follow the main program line by line, you will find that the function that you have written have never been invoked.

You can add them in and give it a try.

Meanwhile I will see if there are other things.