Alternating Numbers

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 number
bool isAlternating(int n)
{
    string str = to_string(n);
    return (isEvenOddForm(str)
            || isOddEvenForm(str));
}
  
// Driver Code
int main()
{
    // Given Number N
    int N = 129;
  
    // Function Call
    if (isAlternating(N))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class 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 number
static boolean isAlternating(int n)
{
    String str = Integer.toString(n);
    return (isEvenOddForm(str) ||
            isOddEvenForm(str));
}
  
// Driver Code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 number 
def isAlternating(n):
    s = str(n)
    return (isEvenOddForm(s) or
            isOddEvenForm(s))
  
# Driver Code
  
# Given Number N 
N = 129
  
# Function Call 
if (isAlternating(N)): 
    print("Yes"
else:
    print("No")
      
# This code is contributed by Vishal Maurya

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using 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 number
static bool isAlternating(int n)
{
    String str = n.ToString();
    return (isEvenOddForm(str) ||
            isOddEvenForm(str));
}
  
// Driver Code
public 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

chevron_right


Output:

Yes

Time Complexity: O(log10N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : rock_cool, princi singh

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.