I need help on how to write a program in C++. for example, heres what i need to accomplish.
a=5
b=10
output=5*6*7*8*9*10
result should be 151200.
so i believe i need an IF statement?
or am i WRONG? o.O''
C++ Programming, Keep Multiplying two number?
The second answer shouldn't use a double for the product; it should be a long. (An int multiplied by an int will never yield a double value.)
None of the answers include a check to make sure the answer will not cause a data overflow.
Reply:#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
main()
{
int a,b,i;
long result=1;
clrscr();
cout%26lt;%26lt;"Enter a,b: ";
cin%26gt;%26gt;a%26gt;%26gt;b;
for(i=a;i%26lt;=b;i++)
{
result=result*a;
}
cout%26lt;%26lt;"The result is: "%26lt;%26lt;result;
getch();
}
Reply:No you need a loop
int result=1;
for (i=a;i%26lt;=b;i++)
{
result = result * i;
}
Reply:#include %26lt;iostream.h%26gt;
main()
{
int a=5, b=10, x;
double p=1;
for (x=a;x%26lt;=b;x++)
{
p = p * x;
}
cout %26lt;%26lt; "The product of the numbers between" %26lt;%26lt; a %26lt;%26lt; "and" %26lt;%26lt; b %26lt;%26lt; "is" %26lt;%26lt; p;
}
Reply:Ok, I believe this is the code you need.. If there are any doubts or you need something else.. Feel free to write to me..
void main(){
int a,b,i,temp;
printf("Enter the values of a and b : ");
scanf("%d%d",a,b);
temp = 1;
for(i=a;i%26lt;=b;i++){
temp = temp*i;
}
printf("The result is : %d",temp);
}
tropical flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment