Cant figure simple calculation formulas

//Cost of one book is 50 dirhams. if you buy 2 books then the third book will be free.
//Write a C++ program that will input the number of the books and output the cost of the books.


I tried to understand the question and solve it but apparently its wrong.


// Alogrithim:
//1) Prompt user enter number of books
//2) Calculate cost of books using the following formula:
// if # of books is odd: cost = (n - 1 ) * 50;
// if # of books is even: cost = (n - 2 ) * 50;
//3) Print the total cost of books.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<iostream>
using namespace std;
int main()
{
   int n, cost;

   cout<<" Enter number of books:";
   cin>>n;

   if (n == % 2 )
   {
       cost = (n - 2 ) * 50;
       cout<<"books cost = "<<cost;
   }
   else
   {
       cost = (n - 1 ) * 50;
       cout<<"books cost = "<<cost;
   }
    return 0;
}
if (n == % 2 )
should probably be
if (n % 2 == 0 )

However, your algorithm
// if # of books is odd: cost = (n - 1 ) * 50;
// if # of books is even: cost = (n - 2 ) * 50;

is wrong.
Last edited on
thankyou
Topic archived. No new replies allowed.