Sunday, August 2, 2009

C++ Program Help?

//hw8 5 functions


#include %26lt;iostream%26gt;


using namespace std;





int sumOfDigits(int n){


int sum=sum+n;


return sum;


}





int squared(int n) {


int answer=n*n;


return answer;


}





int factorial(int n) {


if (n%26lt;0) return 0;


int factorial=1


while (n%26gt;1) {


factorial *= n--;


return factorial;


}





int Sn(int n) {


int n= sum+n, n++;


return Sn;


}





void drawTriangle(int n) {


for (n=1; n%26lt;=x; n++) {





}


int main () {


int n;


cout %26lt;%26lt; "Enter a number,n: "%26lt;%26lt;endl;


cin %26gt;%26gt; n;


cout %26lt;%26lt; "The sum of digits of n are: "%26lt;%26lt;sumofDigits%26lt;%26lt; endl;


cout %26lt;%26lt; "The number n squared is: "%26lt;%26lt;squared%26lt;%26lt; endl;


cout %26lt;%26lt; "The number n factorial is : "%26lt;%26lt;factorial%26lt;%26lt; endl;


cout %26lt;%26lt; "The sum of all natural numbers from 1 to n is: "%26lt;%26lt;Sn%26lt;%26lt; endl;


cout %26lt;%26lt; "The triangle will look like this: "%26lt;%26lt;drawTriangle%26lt;%26lt; endl;


return 0;

C++ Program Help?
It looks mostly correct, but you should indent after the curly brackets.
Reply:int factorial(int n) {


if (n%26lt;0) return 0;


int factorial=1


while (n%26gt;1) {


factorial *= n--;


return factorial;


}





I'm no expert, but right there, I don't think you can initalize it twice. Also, your while loop has no ending bracket.
Reply:thats correct!


No comments:

Post a Comment