Computer Science, asked by apoorvamn1997, 9 months ago

Oddly even
Problem statement
Given a maximum of 100 digit number as input, find the difference between the sum of odd
and even position digits
Input-1 0
4567
Expected output
Explanation
Sum of odd position digits 4 and 6 is 10. Sum of even position digits 5 and 7 is 12. The
difference is 12 - 10 = 2
Input-2
9834698765123
Expected output
Input-3
5476
Expected output​

Answers

Answered by abhijeetkant103
8

Answer:

Explanation:

import java.until.Scanner;

public class Ex=2{

public static void main(string[]args){

Scanner s=new Scanner(System.in);

long a=s.nextLong();

long temp=a;

int flag=0,even=0,odd=0;

string aa=string.valueof(temp);

int len=aa.length();

while(a>0)

{

long rem=a%10;

a/=10;

if(len%2==0)

{

if(flag==0)

{

even+=rem;

flag=1;

}

else if(flag==1)

{

odd+=rem;

flag=0;

}

}

else

{

if(flag==0)

{

odd+=rem;

flag=1;

}

else if(flag==1)

{

even+=rem;

flag=0;

}

}

}

int sum=odd-even;

if(sum<0)

{

sum=-sum;

}

System.out.println(sum);

}

}

Answered by poojan
6

Python program to find absolute difference between the odd and even numbers in the inputted number.

Language used : Python Programming

Program :

n=list(input())

even=0

odd=0

for i in n:

   if int(i)%2==0:

       even=even+int(i)

   else:

       odd=odd+int(i)

print(abs(odd-even))

Input :

Input 1:

4567

Output 1:

2

Input 2:

9834698765123

Output 2:

3

Input 3:

35476

Output 3:

5

NOTE : Outputs are of absolute difference between odd and even sums, means difference in positive. If you want the normal difference, just remove abs() in last statement of the code.

It gives the difference between them, irrespective on sides.

Explanation :

  • Take a number as input and list the numbers individually.

  • Run a loop on the list and check if it is even or not with %2==0 condition. If it is even, add the number to even variable and store it in even variable.

  • If it is odd, do the same with odd variable.

  • Once the loop terminates, print the absolute difference between odd and even sums. That's it!

Learn more :

1) Printing all the palindromes formed by a palindrome word.

brainly.in/question/19151384

2) Indentation is must in python. Know more about it at :

brainly.in/question/17731168

3) Write a Python function sumsquare(l) that takes a nonempty list of integers and returns a list [odd,even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.

brainly.in/question/15473120

Attachments:
Similar questions