two simple questions
Author |
Message |
empror9
|
Posted: Sun Jul 04, 2010 11:32 pm Post subject: two simple questions |
|
|
hello everyone
Q1-see this equation
is this the correct code for this Q1?
c++: | #include<iostream>
using namespace std;
int main()
{
float s=0,p;
int i,j;
float x;
cout<<"x=";
cin>>x;
for(j=1;j<=6;j++)
{
p=1;
for(i=1;i<=j;i++)
p*=x;
if(j%2==0)
s+=(-p/j);
else
s+=(p/j);
}
cout<<"ln("<<x+1<<")="<<s<<endl;
system("pause");
return 0;
} |
****
Q2- i want to edit this code to accept one number and then show the numbaer whether is a prime or not
this is the code and i try to edit but i couldn't
this is the code
c++: | int num,isprime;
cout <<"Please enter a number: ";
cin >> num;
for(int i=2; i<=num;i++)
{
isprime = 1;
for(int j=2; j<=i ; j++)
{
if( i == j)
continue;
else if( i % j == 0){
isprime = 0;
}
}
if(isprime == i){
cout<<i<<"is a prime number ";
}else{
cout<<i<<"is not a prime number ";
}
} |
waiting for you
Description: |
|
Filesize: |
4.08 KB |
Viewed: |
2064 Time(s) |
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
rdrake
|
Posted: Sun Jul 04, 2010 11:56 pm Post subject: Re: two simple questions |
|
|
Welcome to the forums. There are a few things you need to know first.
- Use [ syntax = "lang" ] tags to provide syntax highlighting and to preserve the whitespace of your code. Anything without syntax tags is painful to look at.
- No need to double post. You are allowed to edit your post up until somebody replies to it.
Doing the above things will help encourage other users to provide you with assistance.
|
|
|
|
|
|
empror9
|
Posted: Mon Jul 05, 2010 12:14 am Post subject: Re: two simple questions |
|
|
ok thanks
but i'm still waiting for a help
|
|
|
|
|
|
Insectoid
|
Posted: Mon Jul 05, 2010 8:29 am Post subject: RE:two simple questions |
|
|
As for question 1- Does it work? Then it's one of the many, many 'correct' solutions. No? Then it's wrong.
As for question 2, you seem to be trying to find every prime up to 'num', while in your question you're saying you only want to know if 'num' is prime.
This line: doesn't look right. Remember what i is and what isprime can only equal. Hint: Make isprime a boolean rather than an int.
|
|
|
|
|
|
|
|