Skip to content
Related Articles

Related Articles

Straight-line Number
  • Last Updated : 25 Jun, 2020
GeeksforGeeks - Summer Carnival Banner

Given a number N, the task is to check if N is an Straight-line Number or not. If N is an Straight-line Number then print “Yes” else print “No”.

A straight-line Number is a number greater than 99 in which the digits form an Arithmetic Progression.

Examples:

Input: N = 135
Output: Yes
Explanation:
The digits of the number N are 1, 3, and 5.
1 3 5 is an AP with d = 2

Input: N = 136
Output: No



Approach: The idea is to convert the given number N into a string and check, if differences between consecutive digits are same or not. If the difference between all the corresponding digit are same then print “Yes” else print “No”.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if N is a
// Straight Line number or not
bool isStraighLineNum(int N)
{
    // N must be > 99
    if (N <= 99)
        return false;
  
    string str = to_string(N);
  
    // Difference between consecutive
    // digits must be same
    int d = str[1] - str[0];
  
    for (int i = 2; i < str.length(); i++)
        if (str[i] - str[i - 1] != d)
            return false;
  
    return true;
}
  
// Driver Code
int main()
{
    // Given Number N
    int N = 135;
  
    // Function Call
    if (isStraighLineNum(n))
        cout << "Yes";
    else
        cout << "No";
}

Java




// Java program for the above approach
class GFG{ 
  
// Function to check if N is a
// Straight Line number or not
static boolean isStraighLineNum(int N)
{
      
    // N must be > 99
    if (N <= 99)
        return false;
  
    String s = Integer.toString(N);
  
    // Difference between consecutive
    // digits must be same
    int d = s.charAt(1) - s.charAt(0);
  
    for(int i = 2; i < s.length(); i++)
       if (s.charAt(i) - s.charAt(i - 1) != d)
           return false;
  
    return true;
}
  
// Driver code 
public static void main(String[] args) 
      
    // Given Number N
    int n = 135;
  
    // Function Call
    if (isStraighLineNum(n))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
  
// This code is contributed by Pratima Pandey 

Python3




# Python3 program for the above approach
  
# Function to check if N is a
# Straight Line number or not
def isStraighLineNum(N):
  
    # N must be > 99
    if (N <= 99):
        return False;
  
    str1 = str(N);
  
    # Difference between consecutive
    # digits must be same
    d = int(str1[1]) - int(str1[0]);
  
    for i in range(2, len(str1)):
        if (int(str1[i]) - 
            int(str1[i - 1]) != d):
            return False;
  
    return True;
  
# Driver Code
  
# Given Number N
N = 135;
  
# Function Call
if (isStraighLineNum(N)):
    print("Yes");
else:
    print("No");
  
# This code is contributed by Code_Mech

C#




// C# program for the above approach
using System;
class GFG{ 
  
// Function to check if N is a
// Straight Line number or not
static bool isStraighLineNum(int N)
{
      
    // N must be > 99
    if (N <= 99)
        return false;
  
    string s = N.ToString();
  
    // Difference between consecutive
    // digits must be same
    int d = s[1] - s[0];
  
    for(int i = 2; i < s.Length; i++)
       if (s[i] - s[i - 1] != d)
           return false;
  
    return true;
}
  
// Driver code 
public static void Main() 
      
    // Given Number N
    int n = 135;
  
    // Function Call
    if (isStraighLineNum(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
  
// This code is contributed by Code_Mech
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
Recommended Articles
Page :