Home > front end >  How to remove extra space in output of code?
How to remove extra space in output of code?

Time:01-31

Help! I am trying to print my code is giving the correct output BUT with an extra space that is not supposed to be there. I can not figure out where the extra space is coming from OR how to code it out of my program. Please help!

#include <iostream>
#include <cmath>
using namespace std;


long factorial(int n)
{
   long product = 1;
   for (int i = 1; i <= n; i  )
        product *= i;
   return product;
}

// Controls the operation of the program.
int main()
{
   int input = 0;
   cout << "Enter n: ";
   cin >> input;
   for (int i = i; i <= input; i  )
   {
        cout << factorial(input)/ (factorial(i) * factorial (input - i)) << " ";
   }
   return 0;
}

CodePudding user response:

Dont pad with space at the end on the last element

   for (int i = 1; i <= input; i  )
   {
        cout << factorial(input)/ (factorial(i) * factorial (input - i));
        if(i < input)
          cout << " ";
   }
  •  Tags:  
  • Related