Question
Extract digits from number.
Enter the number
123456
Extracted digits from number:
6 5 4 3 2 1
Enter the number
2312
Extracted digits from number:
2 1 3 2
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.*;
public class DigitsExtraction
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int num=sc.nextInt();
System.out.println("Extracted digits from number:");
while(num!=0)//Loop to extract digits.
{
int r=num%10;
System.out.print(r+" ");
num=num/10;
}
}
}
Python
number=int(input("Enter a number:"))
print("Extracted digits:")
while(number>0):
rem=number%10
print(rem, end=" ")
number=number//10