Alternating Numbers - GeeksforGeeks

31 Mar, 2021
Given a number N, the task is to check if N is an Alternating Number or not. If N is an Alternating Number then print “Yes” else print “No”.
Alternating Number is a positive integer for which, in base-10, the parity of its digits are alternates i.e., the digits in number N is followed by odd, even, odd, ... or even, odd, even, ... order.  
Examples: 
Input: N = 129 Output: Yes Explanation: 129 has digits which is in alternate odd even odd form.
Input: N = 28 Output: No Explanation: 28 has digits which is not in alternate odd even odd form. 
Approach: The idea is to convert the number into a string and check if any digit is followed by the digit of the same parity then N is not an Alternating Numbers, else N is an Alternating Numbers.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to check if a string is// of the form even odd even odd...bool isEvenOddForm(string s){    int n = s.length();    for (int i = 0; i < n; i++) {        if (i % 2 == 0 && s[i] % 2 != 0)            return false;         if (i % 2 == 1 && s[i] % 2 != 1)            return false;    }    return true;} // Function to check if a string is// of the form odd even odd even  ...bool isOddEvenForm(string s){    int n = s.length();    for (int i = 0; i < n; i++) {        if (i % 2 == 0 && s[i] % 2 != 1)            return false;         if (i % 2 == 1 && s[i] % 2 != 0)            return false;    }    return true;} // Function to check if n is an// alternating numberbool isAlternating(int n){    string str = to_string(n);    return (isEvenOddForm(str)            || isOddEvenForm(str));} // Driver Codeint main(){    // Given Number N    int N = 129;     // Function Call    if (isAlternating(N))        cout << "Yes";    else        cout << "No";    return 0;}
// Java program for the above approachclass GFG{     // Function to check if a string is// of the form even odd even odd...static boolean isEvenOddForm(String s){    int n = s.length();    for(int i = 0; i < n; i++)    {       if (i % 2 == 0 && s.charAt(i) % 2 != 0)           return false;       if (i % 2 == 1 && s.charAt(i) % 2 != 1)           return false;    }    return true;} // Function to check if a string is// of the form odd even odd even ...static boolean isOddEvenForm(String s){    int n = s.length();    for(int i = 0; i < n; i++)    {       if (i % 2 == 0 && s.charAt(i) % 2 != 1)           return false;       if (i % 2 == 1 && s.charAt(i) % 2 != 0)           return false;    }    return true;} // Function to check if n is an// alternating numberstatic boolean isAlternating(int n){    String str = Integer.toString(n);    return (isEvenOddForm(str) ||            isOddEvenForm(str));} // Driver Codepublic static void main(String[] args){         // Given number N    int N = 129;     // Function call    if (isAlternating(N))        System.out.println("Yes");    else        System.out.println("No");}} // This Code is contributed by rock_cool
# Python3 program for the above approach # Function to check if a string is# of the form even odd even odd...def isEvenOddForm(s):    n = len(s)    for i in range(n):        if (i % 2 == 0 and            int(s[i]) % 2 != 0):            return False        if (i % 2 == 1 and            int(s[i]) % 2 != 1):            return False    return True # Function to check if a string is# of the form odd even odd even ...def isOddEvenForm(s):    n = len(s)    for i in range(n):        if (i % 2 == 0 and            int(s[i]) % 2 != 1):            return False        if (i % 2 == 1 and            int(s[i]) % 2 != 0):            return False    return True # Function to check if n is an# alternating numberdef isAlternating(n):    s = str(n)    return (isEvenOddForm(s) or            isOddEvenForm(s)) # Driver Code # Given Number NN = 129 # Function Callif (isAlternating(N)):    print("Yes")else:    print("No")     # This code is contributed by Vishal Maurya
// C# program for the above approachusing System;class GFG{     // Function to check if a string is// of the form even odd even odd...static bool isEvenOddForm(String s){    int n = s.Length;    for(int i = 0; i < n; i++)    {        if (i % 2 == 0 && s[i] % 2 != 0)            return false;        if (i % 2 == 1 && s[i] % 2 != 1)            return false;    }    return true;} // Function to check if a string is// of the form odd even odd even ...static bool isOddEvenForm(String s){    int n = s.Length;    for(int i = 0; i < n; i++)    {        if (i % 2 == 0 && s[i] % 2 != 1)            return false;        if (i % 2 == 1 && s[i] % 2 != 0)            return false;    }    return true;} // Function to check if n is an// alternating numberstatic bool isAlternating(int n){    String str = n.ToString();    return (isEvenOddForm(str) ||            isOddEvenForm(str));} // Driver Codepublic static void Main(String[] args){         // Given number N    int N = 129;     // Function call    if (isAlternating(N))        Console.WriteLine("Yes");    else        Console.WriteLine("No");}} // This code is contributed by Princi Singh
<script> // Javascript program for the above approach // Function to check if a string is// of the form even odd even odd...function isEvenOddForm(s){    let n = s.length;    for(let i = 0; i < n; i++)    {        if (i % 2 == 0 && s[i] % 2 != 0)            return false;        if (i % 2 == 1 && s[i] % 2 != 1)            return false;    }    return true;} // Function to check if a string is// of the form odd even odd even ...function isOddEvenForm(s){    let n = s.length;    for(let i = 0; i < n; i++)    {        if (i % 2 == 0 && s[i] % 2 != 1)            return false;        if (i % 2 == 1 && s[i] % 2 != 0)            return false;    }    return true;} // Function to check if n is an// alternating numberfunction isAlternating(n){    let str = n.toString();    return (isEvenOddForm(str) ||            isOddEvenForm(str));} // Driver code    // Given number Nlet N = 129; // Function callif (isAlternating(N))    document.write("Yes");else    document.write("No");         // This code is contributed by divyesh072019 </script>
Yes
 
Time Complexity: O(log10N) 
vishu2908
rock_cool
princi singh
divyesh072019
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for Decimal to Binary Conversion
Program for factorial of a number
Operators in C / C++
Euclidean algorithms (Basic and Extended)
The Knight's tour problem | Backtracking-1
Efficient program to print all prime factors of a given number