![]() |
Given a Number, the task is to sum the Digit of Even and Odd places using PHP. Calculating the sum of digits at even and odd places in a number is a common programming task. Here, we will cover all approaches with detailed explanations and code examples. Table of Content Approach 1: Sum of Digits at Even and Odd Places using a LoopOne approach is to convert the number to a string, iterate over each digit, and calculate the sum based on whether the digit’s position is even or odd.
Output Even Places Digit Sum: 9 Odd Places Digit Sum: 12 In this approach, we convert the number to a string to easily access each digit. We then iterate over each digit, checking if its position (index) is even or odd and updating the respective sums. Approach 2: Sum of Digits at Even and Odd Places using Mathematical OperationsAnother approach is to extract digits from the number using mathematical operations and then calculate the sums.
Output Even Places Digit Sum: 12 Odd Places Digit Sum: 9 |
Reffered: https://www.geeksforgeeks.org
PHP |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |