Computer Science, asked by cypher00, 6 months ago

Write a program to input a 6 digit number and divide it into three 2 digit numbers.​

Answers

Answered by Abrar0321
3

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.

Answered by anindyaadhikari13
4

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:

  1. Using modulo operator, get the remainder when the number is divided by 100.
  2. Store the result in a list.
  3. Divide the number by 100 (integer division)
  4. Continue this process till the result becomes 0.
  5. Display the reversed list separated by commas.

For example, if the number is 123456,

  1. Dividing 123456 by 100 leaves remainder 56
  2. 56 is added to the list.
  3. Divide 123456 by 100. Number becomes 1234
  4. Dividing 1234 by 100 leaves remainder 34
  5. 34 is added to the list.
  6. Divide 1234 by 100. Number becomes 12
  7. Dividing 12 by 100 leaves remainder 12.
  8. 12 is added to the list.
  9. Divide 12 by 100. Number becomes 0.
  10. As number becomes 0, iteration stops.
  11. List now = [56, 34, 12]
  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.

Attachments:

anindyaadhikari13: I have added two approaches. You can follow the first approach.
anindyaadhikari13: *three
Similar questions