📜  在偶数和奇数位找到数字的总和

📅  最后修改于: 2021-04-22 08:15:12             🧑  作者: Mango

给定数字N ,任务是在偶数和奇数位找到数字的总和。
例子:

方法:

  • 首先,计算给定数字的倒数。
  • 对于逆数,我们应用模数运算符并提取其最后一个数字,该最后一个数字实际上是数字的第一个数字,因此它是奇数位数字。
  • 下一个数字将是偶数位,我们可以交替求和。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the reverse of a number
int reverse(int n)
{
    int rev = 0;
    while (n != 0) {
        rev = (rev * 10) + (n % 10);
        n /= 10;
    }
    return rev;
}
 
// Function to find the sum of the odd
// and even positioned digits in a number
void getSum(int n)
{
    n = reverse(n);
    int sumOdd = 0, sumEven = 0, c = 1;
 
    while (n != 0) {
 
        // If c is even number then it means
        // digit extracted is at even place
        if (c % 2 == 0)
            sumEven += n % 10;
        else
            sumOdd += n % 10;
        n /= 10;
        c++;
    }
 
    cout << "Sum odd = " << sumOdd << "\n";
    cout << "Sum even = " << sumEven;
}
 
// Driver code
int main()
{
    int n = 457892;
    getSum(n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG {
 
    // Function to return the reverse of a number
    static int reverse(int n)
    {
        int rev = 0;
        while (n != 0) {
            rev = (rev * 10) + (n % 10);
            n /= 10;
        }
        return rev;
    }
 
    // Function to find the sum of the odd
    // and even positioned digits in a number
    static void getSum(int n)
    {
        n = reverse(n);
        int sumOdd = 0, sumEven = 0, c = 1;
 
        while (n != 0) {
 
            // If c is even number then it means
            // digit extracted is at even place
            if (c % 2 == 0)
                sumEven += n % 10;
            else
                sumOdd += n % 10;
            n /= 10;
            c++;
        }
 
        System.out.println("Sum odd = " + sumOdd);
        System.out.println("Sum even = " + sumEven);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 457892;
        getSum(n);
    }
}
 
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the approach
 
# Function to return the
# reverse of a number
def reverse(n):
    rev = 0
    while (n != 0):
        rev = (rev * 10) + (n % 10)
        n //= 10
    return rev
 
# Function to find the sum of the odd
# and even positioned digits in a number
def getSum(n):
 
    n = reverse(n)
    sumOdd = 0
    sumEven = 0
    c = 1
 
    while (n != 0):
 
        # If c is even number then it means
        # digit extracted is at even place
        if (c % 2 == 0):
            sumEven += n % 10
        else:
            sumOdd += n % 10
        n //= 10
        c += 1
 
    print("Sum odd =", sumOdd)
    print("Sum even =", sumEven)
 
# Driver code
n = 457892
getSum(n)
 
# This code is contributed
# by mohit kumar


C#
// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return the reverse of a number
    static int reverse(int n)
    {
        int rev = 0;
        while (n != 0) {
            rev = (rev * 10) + (n % 10);
            n /= 10;
        }
        return rev;
    }
 
    // Function to find the sum of the odd
    // and even positioned digits in a number
    static void getSum(int n)
    {
        n = reverse(n);
        int sumOdd = 0, sumEven = 0, c = 1;
 
        while (n != 0) {
 
            // If c is even number then it means
            // digit extracted is at even place
            if (c % 2 == 0)
                sumEven += n % 10;
            else
                sumOdd += n % 10;
            n /= 10;
            c++;
        }
 
        Console.WriteLine("Sum odd = " + sumOdd);
        Console.WriteLine("Sum even = " + sumEven);
    }
 
    // Driver code
    public static void Main()
    {
        int n = 457892;
        getSum(n);
    }
}
 
// This code is contributed by
// Akanksha Rai


PHP


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the sum of the odd
// and even positioned digits in a number
void getSum(int n)
{
 
    // If n is odd then the last digit
    // will be odd positioned
    bool isOdd = (n % 2 == 1) ? true : false;
 
    // To store the respective sums
    int sumOdd = 0, sumEven = 0;
 
    // While there are digits left process
    while (n != 0) {
 
        // If current digit is odd positioned
        if (isOdd)
            sumOdd += n % 10;
 
        // Even positioned digit
        else
            sumEven += n % 10;
 
        // Invert state
        isOdd = !isOdd;
 
        // Remove last digit
        n /= 10;
    }
 
    cout << "Sum odd = " << sumOdd << "\n";
    cout << "Sum even = " << sumEven;
}
 
// Driver code
int main()
{
    int n = 457892;
    getSum(n);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
     
// Function to find the sum of the odd
// and even positioned digits in a number
static void getSum(int n)
{
     
    // If n is odd then the last digit
    // will be odd positioned
    boolean isOdd = (n % 2 == 1) ? true : false;
 
    // To store the respective sums
    int sumOdd = 0, sumEven = 0;
 
    // While there are digits left process
    while (n != 0)
    {
         
        // If current digit is odd positioned
        if (isOdd)
            sumOdd += n % 10;
 
        // Even positioned digit
        else
            sumEven += n % 10;
 
        // Invert state
        isOdd = !isOdd;
 
        // Remove last digit
        n /= 10;
    }
    System.out.println("Sum odd = " + sumOdd);
    System.out.println("Sum even = " + sumEven);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 457892;
    getSum(n);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 implementation of the approach
 
# Function to find the sum of the odd
# and even positioned digits in a number
def getSum(n):
 
    # If n is odd then the last digit
    # will be odd positioned
    if (n % 2 == 1) :
        isOdd = True
    else:
        isOdd = False
 
    # To store the respective sums
    sumOdd = 0
    sumEven = 0
 
    # While there are digits left process
    while (n != 0) :
 
        # If current digit is odd positioned
        if (isOdd):
            sumOdd += n % 10
 
        # Even positioned digit
        else:
            sumEven += n % 10
 
        # Invert state
        isOdd = not isOdd
 
        # Remove last digit
        n //= 10
     
    print( "Sum odd = " , sumOdd )
    print("Sum even = " ,sumEven)
 
# Driver code
if __name__ =="__main__":
    n = 457892
    getSum(n)
 
# This code is contributed by chitranayal


C#
// C# implementation of the above approach
using System;
 
class GFG{
     
// Function to find the sum of the odd
// and even positioned digits in a number
static void getSum(int n)
{
     
    // If n is odd then the last digit
    // will be odd positioned
    bool isOdd = (n % 2 == 1) ? true : false;
     
    // To store the respective sums
    int sumOdd = 0, sumEven = 0;
     
    // While there are digits left process
    while (n != 0)
    {
         
        // If current digit is odd positioned
        if (isOdd)
            sumOdd += n % 10;
  
        // Even positioned digit
        else
            sumEven += n % 10;
  
        // Invert state
        isOdd = !isOdd;
  
        // Remove last digit
        n /= 10;
    }
    Console.WriteLine("Sum odd = " + sumOdd);
    Console.Write("Sum even = " + sumEven);
}
 
// Driver code   
static public void Main ()
{
    int n = 457892;
     
    getSum(n);
}
}
 
// This code is contributed by offbeat


Javascript


Python3
# Python3 implementation of the approach
 
# Function to find the sum of the odd
# and even positioned digits in a number
def getSum(n):
 
    # To store the respective sums
    sumOdd = 0
    sumEven = 0
     
    # Converting integer to string
    num = str(n)
     
    # Traversing the string
    for i in range(len(num)):
        if(i % 2 == 0):
            sumOdd = sumOdd+int(num[i])
        else:
            sumEven = sumEven+int(num[i])
 
    print("Sum odd = ", sumOdd)
    print("Sum even = ", sumEven)
 
 
# Driver code
if __name__ == "__main__":
    n = 457892
    getSum(n)
 
# This code is contributed by vikkycirus


输出:
Sum odd = 20
Sum even = 15

另一种方法:无需调换数字即可解决问题。我们可以从末尾一一提取所有数字。如果原始数字是奇数,则最后一位必须是奇数位,否则它将是偶数位。处理完数字后,我们可以将状态从奇数转换为偶数,反之亦然。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the sum of the odd
// and even positioned digits in a number
void getSum(int n)
{
 
    // If n is odd then the last digit
    // will be odd positioned
    bool isOdd = (n % 2 == 1) ? true : false;
 
    // To store the respective sums
    int sumOdd = 0, sumEven = 0;
 
    // While there are digits left process
    while (n != 0) {
 
        // If current digit is odd positioned
        if (isOdd)
            sumOdd += n % 10;
 
        // Even positioned digit
        else
            sumEven += n % 10;
 
        // Invert state
        isOdd = !isOdd;
 
        // Remove last digit
        n /= 10;
    }
 
    cout << "Sum odd = " << sumOdd << "\n";
    cout << "Sum even = " << sumEven;
}
 
// Driver code
int main()
{
    int n = 457892;
    getSum(n);
 
    return 0;
}

Java

// Java implementation of the above approach
class GFG{
     
// Function to find the sum of the odd
// and even positioned digits in a number
static void getSum(int n)
{
     
    // If n is odd then the last digit
    // will be odd positioned
    boolean isOdd = (n % 2 == 1) ? true : false;
 
    // To store the respective sums
    int sumOdd = 0, sumEven = 0;
 
    // While there are digits left process
    while (n != 0)
    {
         
        // If current digit is odd positioned
        if (isOdd)
            sumOdd += n % 10;
 
        // Even positioned digit
        else
            sumEven += n % 10;
 
        // Invert state
        isOdd = !isOdd;
 
        // Remove last digit
        n /= 10;
    }
    System.out.println("Sum odd = " + sumOdd);
    System.out.println("Sum even = " + sumEven);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 457892;
    getSum(n);
}
}
 
// This code is contributed by jrishabh99

Python3

# Python3 implementation of the approach
 
# Function to find the sum of the odd
# and even positioned digits in a number
def getSum(n):
 
    # If n is odd then the last digit
    # will be odd positioned
    if (n % 2 == 1) :
        isOdd = True
    else:
        isOdd = False
 
    # To store the respective sums
    sumOdd = 0
    sumEven = 0
 
    # While there are digits left process
    while (n != 0) :
 
        # If current digit is odd positioned
        if (isOdd):
            sumOdd += n % 10
 
        # Even positioned digit
        else:
            sumEven += n % 10
 
        # Invert state
        isOdd = not isOdd
 
        # Remove last digit
        n //= 10
     
    print( "Sum odd = " , sumOdd )
    print("Sum even = " ,sumEven)
 
# Driver code
if __name__ =="__main__":
    n = 457892
    getSum(n)
 
# This code is contributed by chitranayal

C#

// C# implementation of the above approach
using System;
 
class GFG{
     
// Function to find the sum of the odd
// and even positioned digits in a number
static void getSum(int n)
{
     
    // If n is odd then the last digit
    // will be odd positioned
    bool isOdd = (n % 2 == 1) ? true : false;
     
    // To store the respective sums
    int sumOdd = 0, sumEven = 0;
     
    // While there are digits left process
    while (n != 0)
    {
         
        // If current digit is odd positioned
        if (isOdd)
            sumOdd += n % 10;
  
        // Even positioned digit
        else
            sumEven += n % 10;
  
        // Invert state
        isOdd = !isOdd;
  
        // Remove last digit
        n /= 10;
    }
    Console.WriteLine("Sum odd = " + sumOdd);
    Console.Write("Sum even = " + sumEven);
}
 
// Driver code   
static public void Main ()
{
    int n = 457892;
     
    getSum(n);
}
}
 
// This code is contributed by offbeat

Java脚本


输出:
Sum odd = 20
Sum even = 15

方法3:使用字符串()方法:

  1. 将整数转换为字符串。遍历字符串并将所有偶数索引和存储在一个变量中,并将所有奇数索引存储在另一个变量中。

下面是实现:

Python3

# Python3 implementation of the approach
 
# Function to find the sum of the odd
# and even positioned digits in a number
def getSum(n):
 
    # To store the respective sums
    sumOdd = 0
    sumEven = 0
     
    # Converting integer to string
    num = str(n)
     
    # Traversing the string
    for i in range(len(num)):
        if(i % 2 == 0):
            sumOdd = sumOdd+int(num[i])
        else:
            sumEven = sumEven+int(num[i])
 
    print("Sum odd = ", sumOdd)
    print("Sum even = ", sumEven)
 
 
# Driver code
if __name__ == "__main__":
    n = 457892
    getSum(n)
 
# This code is contributed by vikkycirus

输出:

Sum odd = 20
Sum even = 15