奇数和偶数之和之间有什么区别()

本文概述

  • CPP
  • Java
  • python
  • C#
  • PHP
给定一个长整数, 我们需要确定奇数位和和偶数和之间的差是否为0。索引从零开始(0索引用于最左边的数字)。
例子:
Input : 1212112 Output : Yes Explanation:- the odd position element is 2+2+1=5 the even position element is 1+1+1+2=5 the difference is 5-5=0.so print yes.Input :12345 Output : No Explanation:- the odd position element is 1+3+5=9 the even position element is 2+4=6 the difference is 9-6=3 notequal to zero. So print no.

方法1:一位一位地遍历数字并找到两个和。如果两个和之间的差为0, 则打印是, 否则打印否。
【奇数和偶数之和之间有什么区别()】方法2:
可以使用以下方法轻松解决11的除数。仅当数字可被11整除时, 才能满足此条件。因此, 请检查数字是否可被11整除。
CPP
//C++ program to check if difference between sum of //odd digits and sum of even digits is 0 or not #include < bits/stdc++.h> using namespace std; bool isDiff0( long long int n) { return (n % 11 == 0); }int main() {long int n = 1243 if (isDiff0(n)) cout < < "Yes" ; else cout < < "No" ; return 0; }

Java
//Java program to check if difference between sum of //odd digits and sum of even digits is 0 or notimport java.io.*; import java.util.*; class GFG { public static boolean isDiff( int n) { return (n % 11 == 0 ); } public static void main (String[] args) { int n = 1243 ; if (isDiff(n)) System.out.print( "Yes" ); else System.out.print( "No" ); } }

python
# Python program to check if difference between sum of # odd digits and sum of even digits is 0 or notdef isDiff(n): return (n % 11 = = 0 )# Driver code n = 1243 ; if (isDiff(n)): print ( "Yes" ) else : print ( "No" )# Mohit Gupta_OMG < 0_o>

C#
//C# program to check if difference //between sum of odd digits and sum //of even digits is 0 or not using System; class GFG {public static bool isDiff( int n) { return (n % 11 == 0); }public static void Main () { int n = 1243; if (isDiff(n)) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } }//This code is contributed by vt_m.

PHP
< ?php //PHP program to check if //difference between sum of //odd digits and sum of //even digits is 0 or notfunction isDiff0( $n ) { return ( $n % 11 == 0); }//Driver Code $n = 1243; if (isDiff0( $n )) echo "Yes" ; else echo "No" ; //This code is contributed by nitin mittal ?>

输出如下:
Yes

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

    推荐阅读