计算整数中的偶数和奇数
给定一个数字,任务是计算该数字的偶数位和奇数位,并且偶数位出现偶数次,同样,对于奇数。
Print Yes If:
If number contains even digits even number of time
Odd digits odd number of times
Else
Print No
例子 :
Input : 22233
Output : NO
count_even_digits = 3
count_odd_digits = 2
In this number even digits occur odd number of times and odd
digits occur even number of times so its print NO.
Input : 44555
Output : YES
count_even_digits = 2
count_odd_digits = 3
In this number even digits occur even number of times and odd
digits occur odd number of times so its print YES.
计算数字中偶数和奇数的有效解决方案。
C++
// C++ program to count
// even and odd digits
// in a given number
#include
using namespace std;
// Function to count digits
int countEvenOdd(int n)
{
int even_count = 0;
int odd_count = 0;
while (n > 0)
{
int rem = n % 10;
if (rem % 2 == 0)
even_count++;
else
odd_count++;
n = n / 10;
}
cout << "Even count : "
<< even_count;
cout << "\nOdd count : "
<< odd_count;
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver Code
int main()
{
int n;
n = 2335453;
int t = countEvenOdd(n);
if (t == 1)
cout << "\nYES" << endl;
else
cout << "\nNO" << endl;
return 0;
}
Java
// Java program to count
// even and odd digits
// in a given number
import java.io.*;
class GFG
{
// Function to count digits
static int countEvenOdd(int n)
{
int even_count = 0;
int odd_count = 0;
while (n > 0)
{
int rem = n % 10;
if (rem % 2 == 0)
even_count++;
else
odd_count++;
n = n / 10;
}
System.out.println ( "Even count : " +
even_count);
System.out.println ( "Odd count : " +
odd_count);
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver Code
public static void main (String[] args)
{
int n;
n = 2335453;
int t = countEvenOdd(n);
if (t == 1)
System.out.println ( "YES" );
else
System.out.println( "NO") ;
}
}
Python3
# python program to count even and
# odd digits in a given number
# Function to count digits
def countEvenOdd(n):
even_count = 0
odd_count = 0
while (n > 0):
rem = n % 10
if (rem % 2 == 0):
even_count += 1
else:
odd_count += 1
n = int(n / 10)
print( "Even count : " , even_count)
print("\nOdd count : " , odd_count)
if (even_count % 2 == 0 and
odd_count % 2 != 0):
return 1
else:
return 0
# Driver code
n = 2335453;
t = countEvenOdd(n);
if (t == 1):
print("YES")
else:
print("NO")
# This code is contributed by Sam007.
C#
// C# program to count even and
// odd digits in a given number
using System;
class GFG {
// Function to count digits
static int countEvenOdd(int n)
{
int even_count = 0;
int odd_count = 0;
while (n > 0) {
int rem = n % 10;
if (rem % 2 == 0)
even_count++;
else
odd_count++;
n = n / 10;
}
Console.WriteLine("Even count : " +
even_count);
Console.WriteLine("Odd count : " +
odd_count);
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver Code
public static void Main ()
{
int n;
n = 2335453;
int t = countEvenOdd(n);
if (t == 1)
Console.WriteLine ("YES");
else
Console.WriteLine("NO") ;
}
}
// This code is contributed by vt_m.
PHP
0)
{
$rem = $n % 10;
if ($rem % 2 == 0)
$even_count++;
else
$odd_count++;
$n = (int)($n / 10);
}
echo("Even count : " .
$even_count);
echo("\nOdd count : " .
$odd_count);
if ($even_count % 2 == 0 &&
$odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver code
$n = 2335453;
$t = countEvenOdd($n);
if ($t == 1)
echo("\nYES");
else
echo("\nNO");
// This code is contributed by Ajit.
?>
Javascript
C++
// C++ program to count
// even and odd digits
// in a given number
// using char array
#include
using namespace std;
// Function to count digits
int countEvenOdd(char num[],
int n)
{
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < n; i++)
{
int x = num[i] - 48;
if (x % 2 == 0)
even_count++;
else
odd_count++;
}
cout << "Even count : "
<< even_count;
cout << "\nOdd count : "
<< odd_count;
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver Code
int main()
{
char num[18] = { 1, 2, 3 };
int n = strlen(num);
int t = countEvenOdd(num, n);
if (t == 1)
cout << "\nYES" << endl;
else
cout << "\nNO" << endl;
return 0;
}
Java
// Java program to count
// even and odd digits
// in a given number
// using char array
import java.io.*;
class GFG
{
// Function to count digits
static int countEvenOdd(char num[],
int n)
{
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < n; i++)
{
int x = num[i] - 48;
if (x % 2 == 0)
even_count++;
else
odd_count++;
}
System.out.println ("Even count : " +
even_count);
System.out.println( "Odd count : " +
odd_count);
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver Code
public static void main (String[] args)
{
char num[] = { 1, 2, 3 };
int n = num.length;
int t = countEvenOdd(num, n);
if (t == 1)
System.out.println("YES") ;
else
System.out.println("NO") ;
}
}
// This code is contributed by vt_m
Python3
# Python3 program to count
# even and odd digits
# in a given number
# using char array
# Function to count digits
def countEvenOdd(num, n):
even_count = 0;
odd_count = 0;
num=list(str(num))
for i in num:
if i in ('0','2','4','6','8'):
even_count+=1
else:
odd_count+=1
print("Even count : ",
even_count);
print("Odd count : ",
odd_count);
if (even_count % 2 == 0 and
odd_count % 2 != 0):
return 1;
else:
return 0;
# Driver Code
num = (1, 2, 3);
n = len(num);
t = countEvenOdd(num, n);
if t == 1:
print("YES");
else:
print("NO");
# This code is contributed by mits.
C#
// C# program to count
// even and odd digits
// in a given number
// using char array
using System;
class GFG
{
// Function to count digits
static int countEvenOdd(char []num,
int n)
{
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < n; i++)
{
int x = num[i] - 48;
if (x % 2 == 0)
even_count++;
else
odd_count++;
}
Console.WriteLine("Even count : " +
even_count);
Console.WriteLine( "Odd count : " +
odd_count);
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver code
public static void Main ()
{
char [] num = { '1', '2', '3' };
int n = num.Length;
int t = countEvenOdd(num, n);
if (t == 1)
Console.WriteLine("YES") ;
else
Console.WriteLine("NO") ;
}
}
// This code is contributed by Sam007.
PHP
Javascript
C++
// C++ implementation of above approach
#include
using namespace std;
string getResult(int n)
{
// Converting integer to String
string st = to_string(n);
int even_count = 0;
int odd_count = 0;
// Looping till length of String
for(int i = 0; i < st.length(); i++)
{
if ((st[i] % 2) == 0)
// Digit is even so increment even count
even_count += 1;
else
odd_count += 1;
}
// Checking even count is even and
// odd count is odd
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return "Yes";
else
return "no";
}
// Driver Code
int main(){
int n = 77788;
// Passing this number to get result function
cout<
Java
// Java implementation of above approach
class GFG{
static String getResult(int n)
{
// Converting integer to String
String st = String.valueOf(n);
int even_count = 0;
int odd_count = 0;
// Looping till length of String
for(int i = 0; i < st.length(); i++)
{
if ((st.charAt(i) % 2) == 0)
// Digit is even so increment even count
even_count += 1;
else
odd_count += 1;
}
// Checking even count is even and
// odd count is odd
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return "Yes";
else
return "no";
}
// Driver Code
public static void main(String[] args)
{
int n = 77788;
// Passing this number to get result function
System.out.println(getResult(n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python implementation of above approach
def getResult(n):
# Converting integer to string
st = str(n)
even_count = 0
odd_count = 0
# Looping till length of string
for i in range(len(st)):
if((int(st[i]) % 2) == 0):
# digit is even so increment even count
even_count += 1
else:
odd_count += 1
# Checking even count is even and odd count is odd
if(even_count % 2 == 0 and odd_count % 2 != 0):
return 'Yes'
else:
return 'no'
# Driver Code
n = 77788
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus
C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
class GFG{
static String getResult(int n)
{
// Converting integer to String
String st = String.Join("",n);
int even_count = 0;
int odd_count = 0;
// Looping till length of String
for(int i = 0; i < st.Length; i++)
{
if ((st[i] % 2) == 0)
// Digit is even so increment even count
even_count += 1;
else
odd_count += 1;
}
// Checking even count is even and
// odd count is odd
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return "Yes";
else
return "no";
}
// Driver Code
public static void Main(String[] args)
{
int n = 77788;
// Passing this number to get result function
Console.WriteLine(getResult(n));
}
}
// This code is contributed by Princi Singh
Javascript
输出
Even count : 2
Odd count : 5
YES
解决此问题的另一种解决方案是字符数组或字符串。
C++
// C++ program to count
// even and odd digits
// in a given number
// using char array
#include
using namespace std;
// Function to count digits
int countEvenOdd(char num[],
int n)
{
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < n; i++)
{
int x = num[i] - 48;
if (x % 2 == 0)
even_count++;
else
odd_count++;
}
cout << "Even count : "
<< even_count;
cout << "\nOdd count : "
<< odd_count;
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver Code
int main()
{
char num[18] = { 1, 2, 3 };
int n = strlen(num);
int t = countEvenOdd(num, n);
if (t == 1)
cout << "\nYES" << endl;
else
cout << "\nNO" << endl;
return 0;
}
Java
// Java program to count
// even and odd digits
// in a given number
// using char array
import java.io.*;
class GFG
{
// Function to count digits
static int countEvenOdd(char num[],
int n)
{
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < n; i++)
{
int x = num[i] - 48;
if (x % 2 == 0)
even_count++;
else
odd_count++;
}
System.out.println ("Even count : " +
even_count);
System.out.println( "Odd count : " +
odd_count);
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver Code
public static void main (String[] args)
{
char num[] = { 1, 2, 3 };
int n = num.length;
int t = countEvenOdd(num, n);
if (t == 1)
System.out.println("YES") ;
else
System.out.println("NO") ;
}
}
// This code is contributed by vt_m
Python3
# Python3 program to count
# even and odd digits
# in a given number
# using char array
# Function to count digits
def countEvenOdd(num, n):
even_count = 0;
odd_count = 0;
num=list(str(num))
for i in num:
if i in ('0','2','4','6','8'):
even_count+=1
else:
odd_count+=1
print("Even count : ",
even_count);
print("Odd count : ",
odd_count);
if (even_count % 2 == 0 and
odd_count % 2 != 0):
return 1;
else:
return 0;
# Driver Code
num = (1, 2, 3);
n = len(num);
t = countEvenOdd(num, n);
if t == 1:
print("YES");
else:
print("NO");
# This code is contributed by mits.
C#
// C# program to count
// even and odd digits
// in a given number
// using char array
using System;
class GFG
{
// Function to count digits
static int countEvenOdd(char []num,
int n)
{
int even_count = 0;
int odd_count = 0;
for (int i = 0; i < n; i++)
{
int x = num[i] - 48;
if (x % 2 == 0)
even_count++;
else
odd_count++;
}
Console.WriteLine("Even count : " +
even_count);
Console.WriteLine( "Odd count : " +
odd_count);
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return 1;
else
return 0;
}
// Driver code
public static void Main ()
{
char [] num = { '1', '2', '3' };
int n = num.Length;
int t = countEvenOdd(num, n);
if (t == 1)
Console.WriteLine("YES") ;
else
Console.WriteLine("NO") ;
}
}
// This code is contributed by Sam007.
PHP
Javascript
输出
Even count : 1
Odd count : 2
NO
方法#3:使用类型转换(简化方法):
- 我们必须通过获取一个新变量将给定的数字转换为字符串。
- 遍历字符串,将每个元素转换为整数。
- 如果字符(数字)是偶数,则增加计数
- 否则增加奇数。
- 如果偶数是偶数而奇数是奇数,则打印 Yes。
- 否则打印编号。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
string getResult(int n)
{
// Converting integer to String
string st = to_string(n);
int even_count = 0;
int odd_count = 0;
// Looping till length of String
for(int i = 0; i < st.length(); i++)
{
if ((st[i] % 2) == 0)
// Digit is even so increment even count
even_count += 1;
else
odd_count += 1;
}
// Checking even count is even and
// odd count is odd
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return "Yes";
else
return "no";
}
// Driver Code
int main(){
int n = 77788;
// Passing this number to get result function
cout<
Java
// Java implementation of above approach
class GFG{
static String getResult(int n)
{
// Converting integer to String
String st = String.valueOf(n);
int even_count = 0;
int odd_count = 0;
// Looping till length of String
for(int i = 0; i < st.length(); i++)
{
if ((st.charAt(i) % 2) == 0)
// Digit is even so increment even count
even_count += 1;
else
odd_count += 1;
}
// Checking even count is even and
// odd count is odd
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return "Yes";
else
return "no";
}
// Driver Code
public static void main(String[] args)
{
int n = 77788;
// Passing this number to get result function
System.out.println(getResult(n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python implementation of above approach
def getResult(n):
# Converting integer to string
st = str(n)
even_count = 0
odd_count = 0
# Looping till length of string
for i in range(len(st)):
if((int(st[i]) % 2) == 0):
# digit is even so increment even count
even_count += 1
else:
odd_count += 1
# Checking even count is even and odd count is odd
if(even_count % 2 == 0 and odd_count % 2 != 0):
return 'Yes'
else:
return 'no'
# Driver Code
n = 77788
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus
C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
class GFG{
static String getResult(int n)
{
// Converting integer to String
String st = String.Join("",n);
int even_count = 0;
int odd_count = 0;
// Looping till length of String
for(int i = 0; i < st.Length; i++)
{
if ((st[i] % 2) == 0)
// Digit is even so increment even count
even_count += 1;
else
odd_count += 1;
}
// Checking even count is even and
// odd count is odd
if (even_count % 2 == 0 &&
odd_count % 2 != 0)
return "Yes";
else
return "no";
}
// Driver Code
public static void Main(String[] args)
{
int n = 77788;
// Passing this number to get result function
Console.WriteLine(getResult(n));
}
}
// This code is contributed by Princi Singh
Javascript
输出
Yes