Computer Science, asked by adityaneo, 2 days ago

Read a number and convert its even digit into odd and vice versa
Eg if number is 467 then output can be either 578 or 356
This is a question should br ans in python programme

Answers

Answered by shrutisinghrajput142
0

Explanation:

Given a number N, the task is to find the sum of digits of a number at even and odd places.

Examples: 

Input: N = 54873 

Output: 

Sum odd = 16 

Sum even = 11

Input: N = 457892 

Output: 

Sum odd = 20 

Sum even = 15  

Approach:

First, calculate the reverse of the given number.

To the reverse number we apply modulus operator and extract its last digit which is actually the first digit of a number so it is odd positioned digit.

The next digit will be even positioned digit, and we can take the sum in alternating turns.

Below is the implementation of the above approach: 

C++

// C++ implementation of the approach

#include <bits/stdc++.h>

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

Similar questions