C Program for Number of stopping station problem

Problem statement − A program to find the number of ways in which a train will stop in r stations out of n stations so that no two stopping stations are consecutive.

Problem Explanation

This program will calculate the number of ways i.e. permutations in which the train will stop. Here, the train will travel from point X to Y. Between these points, there are n stations. The train will stop on r stations of these n stations given the conditions that while stopping on r stations the train should not stop in two consecutive stations.

This permutation can be found using the direct npr formula.

Let's take a few examples,

Input : n = 16 , r = 6
Output : 462

Explanation − The number of ways the train can stop at 6 stops out of 16 stops fulfilling the condition is found using the permutation formula given by

npr or p(n, r) = n! ∕ (n-r)!


Input  : total numbers of stations n and number of stations train can stop r.
Step 1 : For values of n and r calculate the value of p(n,r) = n! / (n-r)!
Step 2 : print the value of p(n,r) using std print method.


 Live Demo

int main(){
   int n = 16, s = 6;
   printf("Total number of stations = %d
Number of stopping station = %d
", s, n);    int p = s;    int num = 1, dem = 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)       printf("Possible ways = %d", num / dem);    else       printf("no possible ways"); }


Total number of stations = 16
Number of stopping station = 6
Possible ways = 462

Updated on: 19-Sep-2019


Kickstart Your Career

Get certified by completing the course

Get Started