Home › Forums › C Programming › ‘while’ statement ??
- This topic has 1 reply, 2 voices, and was last updated 16 years ago by GWILouisaxwzkla.
Viewing 1 reply thread
- AuthorPosts
- December 5, 2008 at 3:39 am #2164SilviaJamiesonParticipant
Hi, as i said i am new to all this computer stuff, I think that if i can see how codes are written a few times, then i can start to understand it better. Please help me to better understand this language.
Write a C program using a while-statement to compute n factorial (n!) of a nonnegative integer n where
p = 1 (for n = 0)
p = double = n*(n-1)*(n-2)*…*2*1 (for n1)
„ For example : 5! = 5*4*3*2*1 = 120
„ Prompt user to enter the value of n, computes
and prints its factorial (p)Best wishes ……..daisy
- December 7, 2008 at 7:57 pm #3490GWILouisaxwzklaParticipant
could do:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : December,7,2008<br />* Comments : new project<br />* Compiler/Assembler :<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 2:47:31 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br />//#include <br /><br />//using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />int number;<br />int factorial = 1;<br />int i = 1;<br /><br />printf ( "Enter a positive integer : " );<br />scanf ( "%i" , & number );<br /><br /><br /><br />while ( i <= number )<br />{<br />factorial = i * factorial;<br />i ++;<br />}<br /><br />printf ( "the factorial is %i " , factorial );<br /><br />return 0 ;<br />}<br /><br /><br /><br /><br />
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.