Write a program to input a 6 digit number and divide it into three 2 digit numbers.
Answers
Answer:
Explanation:
The following cσdes have been written using Python.
Source cσde:
n = int(input("Enter a 6 - digit number: "))
if n > 99999:
n1 = n%100
nn1 = n//100
n2 = nn1%100
nn2 = nn1//100
n3 = nn2%100
print("The three 2 - digit numbers are: ")
print(n1)
print(n2)
print(n3)
else:
print("Please enter a 6 - digit number.")
The if-else clause is used to test for conditions.
% is used when you want to find the remainder alone.
// is used when you want to find the quotient alone.
Required Answer:-
Question:
- Write a program to input 6 digit number and divide into three 2 digit number.
Solution:
As no language is mentioned, I am using Python 3.
Here comes the program.
n=int(input("Enter a number: "))
if len(str(n))==6:
l=[]
while n!=0:
l.append(n%100)
n//=100
print(*l[::-1],sep=", ")
else:
print("Invalid Input.")
We will ask the user to enter a number. Then, we will check if the number is a six digit number or not. If it is a six digit number, we will divide the number into three two digit number using the following algorithm:
- Using modulo operator, get the remainder when the number is divided by 100.
- Store the result in a list.
- Divide the number by 100 (integer division)
- Continue this process till the result becomes 0.
- Display the reversed list separated by commas.
For example, if the number is 123456,
- Dividing 123456 by 100 leaves remainder 56
- 56 is added to the list.
- Divide 123456 by 100. Number becomes 1234
- Dividing 1234 by 100 leaves remainder 34
- 34 is added to the list.
- Divide 1234 by 100. Number becomes 12
- Dividing 12 by 100 leaves remainder 12.
- 12 is added to the list.
- Divide 12 by 100. Number becomes 0.
- As number becomes 0, iteration stops.
- List now = [56, 34, 12]
- List is displayed in reverse order. Output becomes: 12,34,56
Another shorter approach,
n=int(input("Enter a number: "))
if len(str(n))==6:
print(", ".join(str(n)[i:i+2:1] for i in range(0,6,2)))
else:
print("Invalid Input.")
Using TextWrap,
import textwrap
n=input("Enter a number: ")
if len(n)==6:
print(*textwrap.wrap(str(n),2),sep=", ")
else:
print("Invalid.")
Refer to the attachment.


