Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 26th, 2013, 11:42 AM
 R4g3
Registered User

Join Date: Sep 2013
Posts: 1
Time spent in forums: 1 m 25 sec
Reputation Power: 0
Fun with integers!

URL

http://imgur.com/6RZcKU6 check this for the programming problem

A friend of mine is taking C in school and he sends me over programming problems he gets so I can give it a shot in Java. I am not sure how to do this one?

Here is what i have come up with:

Code:
```package com.practiceApp;
import java.util.Scanner;
public class Main
{  	public static void main(String[] args)
{
System.out.println("Enter a number: ");
Scanner in = new Scanner(System.in);
int userNum = in.nextInt();
int r = 1;
while (10 * r < userNum)
{
int result = userNum % r;
r *= 10;
System.out.println(result);
}
in.close();
}
}```

A few issues:

So, for input value "101", the output should be:

101
01
1

as opposed to:

101
1

Anyone know how to do this with integers using math that would be awesome. I know you can do this using strings.

Thanks!

#2
September 26th, 2013, 01:23 PM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,967
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 31 m 57 sec
Reputation Power: 1283
One approach I could suggest is to start with your original number, and remove the digits from it one by one using the % and / operators. When you take the digits out of the number, place them into a new new. The number of digits you add to the new number will depend on which line you are printing. After you have your new number, you would have to add a String with a certain amount of spaces in it when you print it.

#3
September 28th, 2013, 10:47 AM
 akii
Registered User

Join Date: Sep 2013
Posts: 1
Time spent in forums: 39 m 57 sec
Reputation Power: 0
try this one
Code:
```import java.util.*;//for Scanner
import java.lang.*;//for Math.pow(double,double)
class myclass{

public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int num =sc.nextInt();

int numOfDigits=0;//holds number of digits
int temp=num;//holds copy of num  for temporarily

//calculate number of digits in entered number
while(temp>0)
{
temp=temp/10;
numOfDigits++;
}

numOfDigits--;//if number of digits is 3 then decrement it by 2

while(numOfDigits>0)
{
num=num%((int)Math.pow(10,numOfDigits));
System.out.println(num);
numOfDigits--;
}

}//main() ends

}//myclass ends```

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Fun with integers!