-2

This program (factorial.c) is running but it does not return the prompt at the end, unless you hit enter:

#include <stdio.h>

int main()
{
  int num, i, fact = 1;

  printf("Enter a number to know its factorial: ");
  scanf("%d",&num);

  for(i=num;i>0;i--){
    fact = fact * i;
  }

  printf("Factorial of %d! = %d",num,fact);

  return 0;
}

Compiling and running the program:

bash-4.2$ gcc factorial.c
bash-4.2$ ./a.out
Enter a number to know its factorial: 5
Factorial of 5! = 120bash-4.2$

After I hit enter:

bash-4.2$

closed as off-topic by cuonglm, G-Man, Archemar, Anthon, dhag Sep 16 '15 at 5:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – G-Man, Anthon, dhag
If this question can be reworded to fit the rules in the help center, please edit the question.

  • This question is more likely to be answered on Stack-overflow than here. – perror Sep 15 '15 at 20:12
  • 1
    Fix your formatting, please. – Tom Hunt Sep 15 '15 at 20:22
  • 3
    Notice: 120bash-4.2$. It printed the prompt at the end of the last line of output. You didn't include a newline in your last call to printf. – Andy Dalton Sep 15 '15 at 20:43
  • 3
    It seems to me that the prompt is returned at the end, you just need to add a newline to your output: printf("Factorial of %d! = %d\n"...) – Joseph R. Sep 15 '15 at 20:44
  • 2
    The prompt is visible in the output you show; there just isn't a newline before. What happens if you add a \n character at the end of your printf format string? – dhag Sep 15 '15 at 20:44
2

This behaves identically to the printf that's included via the command line. It uses the same C libraries to implement as well. Note the behavior:

$ printf "Factorial of %d! = %d" 4 24
Factorial of 4! = 24$

You must include a literal newline (\n) to printf if you want it to display it like so:

$ printf "Factorial of %d! = %d\n" 4 24
Factorial of 4! = 24
$
  • can you please show me where to put the \n in the following line – tester787 Sep 16 '15 at 18:48
  • printf("Factorial of %d! = %d",num,fact); – tester787 Sep 16 '15 at 18:48
  • printf("Factorial of %d! = %d\n",num,fact); – tester787 Sep 16 '15 at 18:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.