DEV Community

Cover image for Working with Numbers in Python: Extraction, Sum, and Product Concepts
Bhagyashree
Bhagyashree

Posted on

Working with Numbers in Python: Extraction, Sum, and Product Concepts

INTRODUCTION

πŸ”Ή 1) Concept of Digit Extraction using Modulo and Division

These programs are based on number manipulation techniques in Python using:

  • % (modulus operator) β†’ gives remainder
  • // (floor division) β†’ removes digits from a number

πŸ‘‰ Example:

  • 1234 % 100 = 34 β†’ last 2 digits
  • 1234 // 100 = 12 β†’ first 2 digits

This concept is used to split numbers into parts without converting them into strings.


πŸ”Ή 2) Concept of While Loop in Number Processing

All programs use a while loop, which is used when the number of iterations is not fixed.

Structure:

while condition:
    statements
Enter fullscreen mode Exit fullscreen mode

Purpose in these tasks:

  • Repeatedly extract digits
  • Reduce the number step-by-step
  • Stop when number becomes 0 or condition fails

πŸ”Ή 3) Concept of Pattern Formation in Numbers

These tasks are based on creating patterns like:

Examples:

  • 1234 β†’ 34 23 12
  • 1234 β†’ 34 12
  • 123456 β†’ 456 123

This is done by:

  • Changing how many digits are removed each time (// 10, // 100, // 1000)
  • Controlling overlapping or non-overlapping digit groups

πŸ”Ή 4) Concept of Sum of Extracted Values

In sum-based tasks:

Idea:

Instead of printing values, we accumulate them in a variable

sum = sum + extracted_value
Enter fullscreen mode Exit fullscreen mode

Working:

  • Extract number using %
  • Add it to sum
  • Reduce number using //

πŸ”Ή 5) Concept of Product of Extracted Values

Same logic as sum, but using multiplication:

Idea:

product = product * extracted_value
Enter fullscreen mode Exit fullscreen mode

Important:

  • Initial value must be 1
  • Used for cumulative multiplication of extracted digits

πŸ”Ή 6) Concept of Overlapping vs Non-Overlapping Extraction

πŸ”Έ Overlapping extraction:

Uses // 10

Example:

1234 β†’ 34 β†’ 23 β†’ 12
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ digits overlap


πŸ”Έ Non-overlapping extraction:

Uses // 100 or // 1000

Example:

1234 β†’ 34 β†’ 12
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ digits do NOT overlap


πŸ”Ή 7) Concept of Splitting Large Numbers

Example:

123456 β†’ 456 123
Enter fullscreen mode Exit fullscreen mode

Logic:

  • % 1000 β†’ last 3 digits
  • // 1000 β†’ first 3 digits

This is useful in:

  • ID processing
  • roll numbers
  • data segmentation

Task-1 : Print 34 23 12

Concept

The program extracts and prints the last two digits of a number using the modulo operator (% 100). After printing, it removes only the last digit using integer division (// 10). This creates overlapping two-digit groups.

  • no % 100 prints the last two digits.
  • no // 10 removes only the last digit, allowing the next overlapping pair to be formed.

For 1234:

  • First iteration β†’ 34
  • Second iteration β†’ 23
  • Third iteration β†’ 12

Algorithm

  1. Start.
  2. Assign no = 1234.
  3. Check whether no >= 10.
  4. If true:
  • Print no % 100.
  • Update no = no // 10.
    1. Repeat Step 3 until no < 10.
    2. Stop.

Flowchart

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Start β”‚
 β””β”€β”€β”€β”¬β”€β”€β”€β”˜
     β”‚
     β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no=1234  β”‚
 β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no >= 10 │───────
 β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β”‚
    Yes            No
     β”‚             β”‚
     β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”
β”‚Print no%100β”‚  β”‚ Stop β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚no=no//10  β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
      β”‚
      └──────────► Back to Condition
Enter fullscreen mode Exit fullscreen mode

Program

no = 1234

while no >= 10:
    print(no % 100,end=" ")
    no = no // 10
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Execution Table

Iteration [3, 4, 5, 6, 7] Loop Condition (no >= 10) Execution Step Operation Result / Output
Start Initial Value Set starting number no = 1234 no is 1234
Loop 1 1234 >= 10 (True) 1. Print statement 1234 % 100 Prints 34
2. Floor division 1234 // 10 no becomes 123
Loop 2 123 >= 10 (True) 1. Print statement 123 % 100 Prints 23
2. Floor division 123 // 10 no becomes 12
Loop 3 12 >= 10 (True) 1. Print statement 12 % 100 Prints 12
2. Floor division 12 // 10 no becomes 1
End 1 >= 10 (False) Loop terminates Exit loop Program finishes successfully

Trace Table

Iteration no (Before) no % 100 Output no = no // 10
1 1234 34 34 123
2 123 23 23 12
3 12 12 12 1

Output


Task-2 : Print 34 12

Concept

This program extracts and prints the last two digits of a number repeatedly using the modulo operator (% 100). After printing, it removes the last two digits using integer division (// 100). The process continues until the number becomes 0.

For 1234:

  • First iteration β†’ 34
  • Second iteration β†’ 12

  • no%100) #prints 34 by 1234/100 = 34 remainder from 12.34

  • no//100 #prints 12 by 1234//100 = 12 quotient, 34 is rem, avoids it

Algorithm

  1. Start.
  2. Assign no = 1234.
  3. Check whether no > 0.
  4. If true:
  • Print no % 100.
  • Update no = no // 100.
    1. Repeat Step 3 until no = 0.
    2. Stop.

Flowchart

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Start β”‚
 β””β”€β”€β”€β”¬β”€β”€β”€β”˜
     β”‚
     β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no=1234  β”‚
 β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no > 0   │───────
 β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β”‚
    Yes            No
     β”‚             β”‚
     β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”
β”‚Print no%100β”‚  β”‚ Stop β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚no=no//100 β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
      β”‚
      └──────────► Back to Condition
Enter fullscreen mode Exit fullscreen mode

Program

no = 1234

while no > 0:
    print(no % 100,end=" ")
    no = no // 100
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Execution Table

Iteration [3, 4, 5, 6, 7] Loop Condition (no > 0) Execution Step Operation Result / Output
Start Initial Value Set starting number no = 1234 no is 1234
Loop 1 1234 > 0 (True) 1. Print statement 1234 % 100 Prints 34 (Last 2 digits)
2. Floor division 1234 // 100 no becomes 12 (removes last 2 digits)
Loop 2 12 > 0 (True) 1. Print statement 12 % 100 Prints 12 (Remaining digits)
2. Floor division 12 // 100 no becomes 0
End 0 > 0 (False) Loop terminates Exit loop Program finishes successfully

Trace Table

Iteration no (Before) no % 100 Output no = no // 100
1 1234 34 34 12
2 12 12 12 0

Output


Task-3 : Sum of task 1 (34 23 12)

Concept

This program extracts overlapping two-digit numbers from 1234 using the modulo operator (% 100) and adds them. After each extraction, it removes only the last digit using integer division (// 10).

For 1234:

  • First iteration β†’ 34
  • Second iteration β†’ 23
  • Third iteration β†’ 12

Sum = 34 + 23 + 12 = 69

  • no % 100 extracts the last two digits.
  • no // 10 removes only the last digit.
  • sum = sum + (no % 100) adds each extracted value to the total.

Algorithm

  1. Start.
  2. Assign no = 1234 and sum = 0.
  3. Check whether no >= 10.
  4. If true:
  • Add no % 100 to sum.
  • Update no = no // 10.
    1. Repeat Step 3 until no < 10.
    2. Print sum.
    3. Stop.

Flowchart

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Start β”‚
 β””β”€β”€β”€β”¬β”€β”€β”€β”˜
     β”‚
     β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no=1234,sum=0  β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no >= 10 │───────
 β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β”‚
    Yes            No
     β”‚             β”‚
     β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚sum=sum+no%100  β”‚ β”‚Print sum β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
        β”‚               β”‚
        β–Ό               β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”
 β”‚no=no//10  β”‚       β”‚ Stop β”‚
 β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       └──────────► Back to Condition
Enter fullscreen mode Exit fullscreen mode

Program

no = 1234
sum = 0

while no >= 10:
    sum = sum + (no % 100)
    no = no // 10

print(sum)
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Execution Table

Iteration Loop Condition (no >= 10) Operation Calculation Sum
Start Initial Value sum = 0 - 0
Loop 1 1234 >= 10 (True) sum = sum + (1234 % 100) 0 + 34 34
no = 1234 // 10 no = 123
Loop 2 123 >= 10 (True) sum = sum + (123 % 100) 34 + 23 57
no = 123 // 10 no = 12
Loop 3 12 >= 10 (True) sum = sum + (12 % 100) 57 + 12 69
no = 12 // 10 no = 1
End 1 >= 10 (False) Loop terminates Exit loop 69

Trace Table

Iteration no (Before) no % 100 Sum After Addition no = no // 10
1 1234 34 34 123
2 123 23 57 12
3 12 12 69 1

Output


Task-4 : Sum of task 2 (34 12)

Concept

This program extracts and adds the last two digits of a number repeatedly using the modulo operator (% 100). After each extraction, it removes the last two digits using integer division (// 100).

For 1234:

  • First iteration β†’ 34
  • Second iteration β†’ 12

Sum = 34 + 12 = 46

  • no % 100 extracts the last two digits.
  • no // 100 removes the last two digits.
  • sum = sum + (no % 100) adds each extracted value to the total.

Algorithm

  1. Start.
  2. Assign no = 1234 and sum = 0.
  3. Check whether no > 0.
  4. If true:
  • Add no % 100 to sum.
  • Update no = no // 100.
    1. Repeat Step 3 until no = 0.
    2. Print sum.
    3. Stop.

Flowchart

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Start β”‚
 β””β”€β”€β”€β”¬β”€β”€β”€β”˜
     β”‚
     β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no=1234,sum=0  β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no > 0   │───────
 β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β”‚
    Yes            No
     β”‚             β”‚
     β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚sum=sum+no%100  β”‚ β”‚Print sum β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
        β”‚               β”‚
        β–Ό               β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”
 β”‚no=no//100 β”‚       β”‚ Stop β”‚
 β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       └──────────► Back to Condition
Enter fullscreen mode Exit fullscreen mode

Program

no = 1234
sum = 0

while no > 0:
    sum = sum + (no % 100)
    no = no // 100

print(sum)
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Execution Table

Iteration Loop Condition (no > 0) Operation Calculation Sum
Start Initial Value sum = 0 - 0
Loop 1 1234 > 0 (True) sum = sum + (1234 % 100) 0 + 34 34
no = 1234 // 100 no = 12
Loop 2 12 > 0 (True) sum = sum + (12 % 100) 34 + 12 46
no = 12 // 100 no = 0
End 0 > 0 (False) Loop terminates Exit loop 46

Trace Table

Iteration no (Before) no % 100 Sum After Addition no = no // 100
1 1234 34 34 12
2 12 12 46 0

Output


Task-5 : Product of task 1 (34 23 12)

Concept

This program extracts overlapping two-digit numbers from 1234 using the modulo operator (% 100) and multiplies them. After each extraction, it removes only the last digit using integer division (// 10).

For 1234:

  • First iteration β†’ 34
  • Second iteration β†’ 23
  • Third iteration β†’ 12

Product = 34 Γ— 23 Γ— 12 = 9384

  • no % 100 extracts the last two digits.
  • no // 10 removes only the last digit.
  • product = product * (no % 100) multiplies each extracted value.

Algorithm

  1. Start.
  2. Assign no = 1234 and product = 1.
  3. Check whether no >= 10.
  4. If true:
  • Multiply product with no % 100.
  • Update no = no // 10.
    1. Repeat Step 3 until no < 10.
    2. Print product.
    3. Stop.

Flowchart

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Start β”‚
 β””β”€β”€β”€β”¬β”€β”€β”€β”˜
     β”‚
     β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no=1234, product=1 β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no >= 10 │───────
 β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β”‚
    Yes            No
     β”‚             β”‚
     β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚product=product*%100β”‚ β”‚Print productβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                   β”‚
        β–Ό                   β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”
 β”‚no=no//10  β”‚           β”‚ Stop β”‚
 β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       └──────────► Back to Condition
Enter fullscreen mode Exit fullscreen mode

Program

no = 1234
product = 1

while no >= 10:
    product = product * (no % 100)
    no = no // 10

print(product)
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Execution Table

Iteration Loop Condition (no >= 10) Operation Calculation Product
Start Initial Value product = 1 - 1
Loop 1 1234 >= 10 (True) product = product Γ— (1234 % 100) 1 Γ— 34 34
no = 1234 // 10 no = 123
Loop 2 123 >= 10 (True) product = 34 Γ— (123 % 100) 34 Γ— 23 782
no = 123 // 10 no = 12
Loop 3 12 >= 10 (True) product = 782 Γ— (12 % 100) 782 Γ— 12 9384
no = 12 // 10 no = 1
End 1 >= 10 (False) Loop terminates Exit loop 9384

Trace Table

Iteration no (Before) no % 100 Product After Multiplication no = no // 10
1 1234 34 34 123
2 123 23 782 12
3 12 12 9384 1

Output


Task-6 : Product of task 2 (34 12)

Concept

This program extracts the last two digits of a number repeatedly using the modulo operator (% 100) and multiplies them. After each extraction, it removes the last two digits using integer division (// 100).

For 1234:

  • First iteration β†’ 34
  • Second iteration β†’ 12

Product = 34 Γ— 12 = 408

  • no % 100 extracts the last two digits.
  • no // 100 removes the last two digits.
  • product = product * (no % 100) multiplies each extracted value.

Algorithm

  1. Start.
  2. Assign no = 1234 and product = 1.
  3. Check whether no > 0.
  4. If true:
  • Multiply product with no % 100.
  • Update no = no // 100.
    1. Repeat Step 3 until no = 0.
    2. Print product.
    3. Stop.

Flowchart

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Start β”‚
 β””β”€β”€β”€β”¬β”€β”€β”€β”˜
     β”‚
     β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no=1234,product=1  β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no > 0   │───────
 β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β”‚
    Yes            No
     β”‚             β”‚
     β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚product=product*%100β”‚ β”‚Print productβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                   β”‚
        β–Ό                   β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”€β”€β”
 β”‚no=no//100 β”‚           β”‚ Stop β”‚
 β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       └──────────► Back to Condition
Enter fullscreen mode Exit fullscreen mode

Program

no = 1234
product = 1

while no > 0:
    product = product * (no % 100)
    no = no // 100

print(product)
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Execution Table

Iteration Loop Condition (no > 0) Operation Calculation Product
Start Initial Value product = 1 - 1
Loop 1 1234 > 0 (True) product = 1 Γ— (1234 % 100) 1 Γ— 34 34
no = 1234 // 100 no = 12
Loop 2 12 > 0 (True) product = 34 Γ— (12 % 100) 34 Γ— 12 408
no = 12 // 100 no = 0
End 0 > 0 (False) Loop terminates Exit loop 408

Trace Table

Iteration no (Before) no % 100 Product After Multiplication no = no // 100
1 1234 34 34 12
2 12 12 408 0

Output


Task-7 : 123456 β†’ Print 456 123

Concept

This program splits a 6-digit number into two parts using:

  • % 1000 β†’ gives last 3 digits
  • // 1000 β†’ gives first 3 digits

It prints both values in a loop style similar to the previous tasks.

For 123456:

  • First iteration β†’ 456
  • Second iteration β†’ 123

Algorithm

  1. Start.
  2. Assign no = 123456.
  3. Check whether no > 0.
  4. If true:
  • Print no % 1000.
  • Update no = no // 1000.
    1. Repeat until no = 0.
    2. Stop.

Flowchart

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Start β”‚
 β””β”€β”€β”€β”¬β”€β”€β”€β”˜
     β”‚
     β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no = 123456  β”‚
 β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ no > 0   │───────
 β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β”‚
    Yes            No
     β”‚             β”‚
     β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”
β”‚Print no % 1000 β”‚ β”‚ Stop β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ no = no//1000β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      └──────────► Back to Condition
Enter fullscreen mode Exit fullscreen mode

Program

no = 123456

while no > 0:
    print(no % 1000, end=" ")
    no = no // 1000
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Execution Table

Iteration no (Before) no % 1000 Output no = no // 1000
1 123456 456 456 123
2 123 123 123 0

Trace Table

Iteration no (Before) no % 1000 Output
1 123456 456 456
2 123 123 123

Output


πŸ“Š Number Manipulation Tasks

Task No Input Number Operation Concept Used Method Output
1 1234 Print 34 23 12 Overlapping digit extraction % 100 + // 10 in while loop 34 23 12
2 1234 Print 34 12 Non-overlapping digit extraction % 100 + // 100 in while loop 34 12
3 1234 Sum of Task 1 values Accumulation (Sum) sum = sum + value 69
4 1234 Sum of Task 2 values Accumulation (Sum) sum = sum + value 46
5 1234 Product of Task 1 values Accumulation (Product) product = product * value 9384
6 1234 Product of Task 2 values Accumulation (Product) product = product * value 408
7 123456 Print 456 123 Number splitting % 1000 + // 1000 456 123

πŸ“Œ Summary

  • % β†’ extracts last digits
  • // β†’ removes last digits
  • while loop β†’ repeated digit processing
  • Overlapping extraction β†’ // 10
  • Non-overlapping extraction β†’ // 100 or // 1000
  • Sum logic β†’ sum = sum + value
  • Product logic β†’ product = product * value
  • Number splitting β†’ large number separation
  • %100 --> gives last two digits
  • //100 --> removes last two digits
  • Additive Identity 0
  • Multiplicative identity 1
  • Floor division operator (//) calculates the integer quotient by rounding down to the nearest whole number
  • Modulo operator (%) calculates the remainder left over from that division.

Overall Learning Outcome

From these programs, we learn:

βœ” How to extract digits without strings
βœ” How loops control number processing
βœ” How arithmetic operators manipulate data
βœ” How patterns are formed from numbers
βœ” Difference between sum and product accumulation
βœ” Real-world use of digit splitting logic

Top comments (0)