Thursday, July 30, 2009

C ++ help!!!!?

Hi guys I have this assignment but I'm having a hard time getting it done... can anyone help...


The assignment says:


Compute and display the following values for the n = 5,9 and 15





1) 1+2+3+...+n


2) (n*(n+1)) / 2


3) n^2





the Compiler used is Microsoft Visual C++ express

C ++ help!!!!?
Well, I won't do your homework for you, but I'll give you a few hints.





1.)





Use a loop, in which you initialize a counter to 1, terminate the loop when the counter exceeds n, and increment the counter by 1 at the end of each iteration. Use a "result" or "sum" variable, which you initialize to 0, and then add the value of the counter into the sum at each iteration.





2.)





The expression you have is a valid C++ expression. Simply assign the expression to some variable, and print the variable.





3.)





You can use n*n in order to compute the square of n. Alternatively, you can use the "pow" function defined in %26lt;cmath%26gt;.
Reply:1)#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


int main()


{


int n ,sum=0;


cout%26lt;%26lt;"\nEnter how many numbers"%26lt;%26lt;endl;


cin%26gt;%26gt;n;


for(int i=0;i%26lt;=n;i++)


sum+=i;


cout%26lt;%26lt;"sum="%26lt;%26lt;sum;


}





rest two too simple jus read value of n


2)sum=n*(n+1)


cout%26lt;%26lt;"sum="%26lt;%26lt;sum;





3)#include%26lt;math.h%26gt;


read n


exp=pow(n,2);


print exp
Reply:haha wow a nitghmare a foreign languages and maths ahahhahaha


No comments:

Post a Comment