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 = 2Input: 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 notbool 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 Codeint main(){ // Given Number N int N = 135; // Function Call if (isStraighLineNum(n)) cout << "Yes"; else cout << "No";} |
Java
// Java program for the above approachclass GFG{ // Function to check if N is a// Straight Line number or notstatic 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 notdef 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 NN = 135;# Function Callif (isStraighLineNum(N)): print("Yes");else: print("No");# This code is contributed by Code_Mech |
C#
// C# program for the above approachusing System;class GFG{ // Function to check if N is a// Straight Line number or notstatic 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 |
Javascript
<script>// JavaScript program for the above approach// Function to check if N is a// Straight Line number or notfunction isStraighLineNum(N){ // N must be > 99 if (N <= 99) return false; let s = N.toString(); // Difference between consecutive // digits must be same let d = s[1] - s[0]; for(let i = 2; i < s.length; i++) if (s[i] - s[i - 1] != d) return false; return true;}// Driver Code// Given Number Nlet n = 135;// Function Callif (isStraighLineNum(n)){ document.write("Yes");}else{ document.write("No");}// This code is contributed by susmitakundugoaldanga </script> |
Yes
Time Complexity: O(log10N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Information on that Topic: geeksforgeeks.org/straight-line-number/ […]
… [Trackback]
[…] Here you can find 84225 more Information to that Topic: geeksforgeeks.org/straight-line-number/ […]
… [Trackback]
[…] Find More Info here on that Topic: geeksforgeeks.org/straight-line-number/ […]
… [Trackback]
[…] Read More here to that Topic: geeksforgeeks.org/straight-line-number/ […]
… [Trackback]
[…] Read More to that Topic: geeksforgeeks.org/straight-line-number/ […]
… [Trackback]
[…] Read More on that Topic: geeksforgeeks.org/straight-line-number/ […]
… [Trackback]
[…] Here you can find 7936 additional Info on that Topic: geeksforgeeks.org/straight-line-number/ […]
… [Trackback]
[…] Info to that Topic: geeksforgeeks.org/straight-line-number/ […]