Java Program for Number of stopping station problem Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report There are 12 intermediate stations between two places A and B. Find the number of ways in which a train can be made to stop at 4 of these intermediate stations so that no two stopping stations are consecutive? Examples - Input : n = 12, s = 4 Output : 126 Input : n = 16, s = 5 Output : 792 Java // Java code to calculate number // of ways of selecting 'p' non // consecutive stations out of // 'n' stations import java.io.*; import java.util.*; class GFG { public static int stopping_station(int p, int n) { int num = 1, dem = 1, s = p; // selecting 's' positions out of 'n-s+1' while (p != 1) { dem *= p; p--; } int t = n - s + 1; while (t != (n - 2 * s + 1)) { num *= t; t--; } if ((n - s + 1) >= s) System.out.print(num / dem); else // if conditions does not satisfy of combinatorics System.out.print("not possible"); return 0; } public static void main(String[] args) { // n is total number of stations // s is no. of stopping stations int n, s; // arguments of function are // number of stopping station // and total number of stations stopping_station(4, 12); } } // ""This code is contributed by Mohit Gupta_OMG "" Output: 126 Please refer complete article on Number of stopping station problem for more details! Create Quiz Comment K kartik Follow 0 Improve K kartik Follow 0 Improve Article Tags : Java Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like