Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 two simple questions
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
empror9




PostPosted: 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 Smile



44.jpg
 Description:
for Q1
 Filesize:  4.08 KB
 Viewed:  2063 Time(s)

44.jpg


Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: 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.


  1. 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.
  2. 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




PostPosted: Mon Jul 05, 2010 12:14 am   Post subject: Re: two simple questions

ok thanks Smile

but i'm still waiting for a help
Insectoid




PostPosted: 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:
code:

if(isprime == i){
doesn't look right. Remember what i is and what isprime can only equal. Hint: Make isprime a boolean rather than an int.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: