unit 1 lesson 5 activity 2 edhesive
Write a program which takes a three digit number as an input, then prints the digits of this number one per line in reverse order.
Hint: Break this challenge down into repetitions of the steps used for activity 1. Using multiple variables will let you store digits and intermediate results you need.
Sample run:
Please enter a three digit number:
678
Here are the digits:
8
7
6
Answers
Answer:
int n;
cin>>n;
while(n!=0)
{r=n%10;
cout<<r<<endl;
n=n/10;
}
Answer:
import java.util.Scanner;
class U1_L5_Activity_One
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a three digit number: ");
int x = scan.nextInt();
int z = (x % 10) % 10;
int y = (x / 10) % 10;
x = x / 100;
System.out.println("Here are the digits:");
System.out.println(x);
System.out.println(y);
System.out.println(z);
Explanation:
I don't know if this makes sense or not but it works :) To be honest, I'm not sure how I did it so I don't know how to explain it. I hope this helps though! (oh, p.s. my assignment wanted it to be 6 7 8 not 8 7 6 so you might want to fix that!)