
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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)!
Algorithm
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.
Example
#include<stdio.h> 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"); }
Output
Total number of stations = 16 Number of stopping station = 6 Possible ways = 462
- Related Articles
- Python Program for Number of stopping station problem
- Find the Number of Stopping Stations using C++
- Minimum Number of Platforms Required for a Railway Station using C++.
- C Program for Activity Selection Problem
- C++ Program to Solve Travelling Salesman Problem for Unweighted Graph
- Python Program for Activity Selection Problem
- Python Program for Subset Sum Problem
- Python Program for 0-1 Knapsack Problem
- Corrupt stack problem in C, C++ program
- C++ Program to Solve a Matching Problem for a Given Specific Case
- Program for factorial of a number in C program
- C++ Program to Implement Network_Flow Problem
- Minimum Number of Platforms Problem
- Minimum Number of Jumps Problem
- Gas Station in C++
