hello guys

i have some questions and i want you to help me

1- how to find a factorial for a number

e.g. input 5 = 5x4x3x2x1 so the is output 120, now please see my code

int fac   = 0 ;
int count = 0;
int num;
cout <<"Please enter a number:";
cin >> num;
while(count < num){
cout << num << " * ";
fac = num * num;
num--;
count++;
}
cout <<",The factorial is " << fac;

:(

* * * * *

2- Draw a flowchart to calculate the value of sin(x):

http://www.daniweb.com/forums/attachment.php?attachmentid=15636&stc=1&d=1277989751

i don't know how to start with this question, is there any special function for it or what?

* * * * *

3- display all the prime numbers below a given number.

e.g. Given 23 then the result should be2 3 5 7 11 13 17 19

this is also i find it deficult for me :( , how can i do this programme?

* * * * *

that's all, waiting for you

#1:

int fact = num;
for(int i = 2; i < num; i++)
   fact *= i;

Dear Epmpror, please put your own code so we will say you what is going wrong. This is not a forum for solving your homework. We just help you for your improvment. By the way you could look at this http://www.daniweb.com/forums/thread43314.html

thanks Ancient Dragon for your help but could you please tell me the meaning of *= ?

also i don't inderstand why you declare i=2

***

hi VatooVatoo,

for Q1 i tried my best,but for Q2 and Q3 i really don't know how to start!!

if i know then sure i'll try many and many times :)

i have another question,

float a = 0,b = 0,c = 0,rst1 = 0,rst2 = 0; // rst1 = the first result , rst2 = the second result
bool done = false;
while(!done){
cout<<"Please enter number 'a': ";
cin >> a;
while(a == 0){
cout <<"Erorr ... you must enter a real number!"<<endl;
cout<<"Please enter number 'a' again: ";
cin >> a;
}
cout<<"Please enter number 'b': ";
cin >> b;
cout<<"Please enter number 'c': ";
cin >> c;
rst1 =((-b+sqrt((b*b)-(4*a*c)))/(2*a));
rst2 =((-b-sqrt((b*b)-(4*a*c)))/(2*a));
cout <<"Result1 = " << rst1;
cout <<" ,Result2 = " << rst2;
cout <<"Do you want to run the programme again? (y/x)";
cin >> done;
if(done == 'y' || done == 'Y'){
done = true;
}else{
done = false;
}
}

i wrote this programme to cuclate the real roots of a quadratic equation and it's working but my proble is i want to ask the user wether to run the programme agin or noy by using bool

but please give me a help for Q2 and Q3

waiting for you

The varible *= x means variable = variable * x. It is a short version. Also, 1 multiples with a number is equal to the number, so the loop could start from 2 without changing the result.

For question #2, drawing a flowchart means you do it by hand, not with program. It describes your thought about how to solve a problem. A flowchart will help you to see how a program flow is going to be. It used to be the first thing you should do before you start coding. It may still be important when you are dealing with a bigger program and/or project.

For question#3, do you know what prime number is? If you know, then what you need to do in this question is to go through a loop from 1 to the number given by a user. While going through the loop, test whether the number you are looking at is a prime number. If it is, display it; otherwise, keep going. It would be a good idea to write a flowchart for this question as well.

In C++, it is a strong type language. You are not allowed to switch variable type from one to the other as you did in your code. You need to either check the variable as its type or create another variable to take the input from user. I would suggest you to use it as is for now.

change this...

while(!done){
...
}

to this...

string done = "y";
while(done=='y' || done=='Y'){
...
}

and delete these lines

if(done == 'y' || done == 'Y') {
  done = true;
} else {
  done = false;
}

thanks Taywin for your addition

for Q2 yes it's a flowchart and also algoratim but i didn't copied here :P hehe

is there any special function to fo Q2?? please give me any help just for getting start

for Q3, yes i know the prime numbers, but i don't know how to cuclate it and i think there is no fourmla to find prime numbers

for last question,yes i know how to ues it but i'd like it by bool :)

is it like this the logic for Q3?

if (n < 2) return FALSE;

for (c = 2; c < n; c++)
{
if ((n % c) == 0) return FALSE; // n = the input of the user
}
return TRUE;
}

The algorithm is not that difficult. A sample algorithm may be as followed.

1)Obtain a value of num (for sin(num))
2)Initial sinVal to 0
3)Initial power to 1
4)Go into a loop value starting from 1 to num, and says 'x' is the value using in the loop (for each x in 1 to num)
5)Compute value of (power(x,pow)/fact(pow)) and says it is assigned to 'val'
6)Test if 'x' is an odd or even number. If it is odd, add value of 'val' to sinVal; otherwise, subtract 'val' from sinVal
7)Increment pow by 2
8)Once the loop is done, display/return sinVal and Done...

Not sure there is any special function, but you need factorial() function for it.

There are many ways to deal with prime number testing. I could give you a brute force one which is very simple to understand. The meaning of prime number is that it can be divided with only 1 or itself and the result is still an integer (no decimal). Because of that, you just keep walking through number from 2 up to square root of the number. The test is from doing modulo and check if the result is 0. If the result is 0, it is dividable, and thus is not a prime number. If it could not find a number that could divide it, it is a prime. This is the core part of this question (algorithm to test for prime). Do you know the "%"? It is what you need to use to find mod value.

If you want it to be bool, you need to create another variable to take a string from user input. Then use it to check, and assign a proper bool value to your 'done'. That's it.

string ans;  // add string variable outside the loop
.
.
while (!done) {
.
.
  cin >> ans;  // use the string variable to take the input instead
  if(ans == 'y' || ans == 'Y') {  // use it to check and assign proper value to bool
    done = true;
  }else {
    done = false;
  }
}

I am trying to not change your code that much, so you could understand how it works. There are better ways to deal with it, but you need to be more familiar to the language first.

back again,

this is the code for Q3

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)
     cout<<i<<" ";
  }
}

it's working but my problem in the output, if i enter 23 so i can see many repeated output ((23 = it shows about 7 times!)) i want it just one time

****

for Q1 i solved but when i run it again it shows a wrong output

this is the code

int count = 0;
int num;
int fact = num;
cout <<"Please enter a number:";
cin >> num;
for(int i = 2; i < num; i++)
   fact *= i;
cout <<",The factorial is " << fact;

the Q2 means just i have to find the sin for any number? am i right??

double param,result;
  cin >> param;
  result = sin(param);
 cout << result;

please see this code, what's the wrong

i want to tell you that i solved all the problems except Q2

so i'm still need a help

i don't what should i do thats why i cant start with this programme

okay, taywin already gave you an algorithm to approach the 2nd problem (about sine series). I guess you are having problems following it, so here's a bit more detailed description:

Firstly look at the series carefully. There are three important things to consider.
1) You have to calculate powers...(use library function)
2) You have to calculate factorials...(define a function separately to handle this)
3) You have to be careful about the sign of the terms (its alternate + and -)

There's also another thing to consider, the index of the main loop. Observe that the first term of the series has x^1/factorial(1), then x^3/factorial(3) and so on... so if you want 7 terms, the 7th term should have x^13/factorial(13). Right?? Do you see any relation?? Well, if you want 'n' terms you'll have the last term having x^(2n-1)/factorial(2n-1). So you may have your loop from 1 to (2*n-1) and increment it twice, something like:

for (int i = 1; i <= (2*n - 1); i = i+2;)

Note that this is just a way, not necessarily the best way. Generally its advisable (and sometimes fashionable) to start your loop from 0, but never mind.

Okay, after that you would have to think about the +ve, -ve thing. As taywin mentioned, checking for odd/even indexes can be a solution but in our present indexing system (from 1 to 2n-1 hopping 2 at a time, ie i=i+2) this won't be working (since, all i's are odd). Instead we have a better way. We will declare a variable called, say, 'count' and initialize it to 0. Then we will multiply each term with (-1)^count and increment count on every iteration.

Finally we will sum the terms like:

sum = sum + pow(-1, count)*(pow(x, 2*i - 1)/factorial(i));

The first pow takes care of the sign.(How??)
But don't forget to increment count by 1 after every iteration.

Based on this come up with your code. Quickly.
Hope it helps.

For Q3 check your curly braces -> your second for loop doesn't end where it should.
For Q1 check your condition for your for loop (< vs <=) recall 5! = 5*4*3*2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.