Click here to Skip to main content
15,917,506 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Write a program that Reads an integer, calculates,
and prints it is Factorial.
Also, print the equation.
Example:
Enter a number:
5
The result of 5! Is (1*2*3*4*5) = 120 ( output must be like this)

What I have tried:

C++
#include<stdio.h>
int main(){
  int i,f=1,number;
 
  printf("Enter a number: ");
  scanf("%d",&number);
 
  for(i=1;i<=number;i++)
      f=f*i;
 
  printf("Factorial of %d is: %d",number,f);
  return 0;
}
Posted
Updated 22-Mar-20 14:25pm
v2

1 solution

First of all your output string does not match what is asked. You should respect the instructions of your homework and output a proper message.

Then you should probably output the beginning of the message before the loop.
In the loop, you have two things to do: the first one is to append the current value to the already existing message, and the second is to compute the next term for the factorial.
After the loop you can complete the message with computed factorial result.

I won't write any code since I have the feeling that you just copied this code already. Give it a try, that is not difficult at all. Begin on a piece of paper: write down the successive steps to follow. Then translate that to actual code. Having that done by someone else will completely defeat the purpose of the exercise, which is to teach you how to analyze a requirement and follow a methodology to fulfill it. Copy/pasting will never build your skills.
 
Share this answer
 
Comments
abdalla_30 22-Mar-20 21:14pm    
i can't thank you enough
i thought it will be hard
but i tried many times and i did it !!!
thanks for your help
phil.o 23-Mar-20 4:31am    
You are welcome :) Well done!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900