Jump to content

Negative or Positive Calculator.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Hello,Yesterday I was doing my math and it asked me the question

Quote

Is the product of 5 positive numbers and 6 negative numbers a positive or negative number?
so I answered the question and decided since I had been playing with C++ I would make a calculator that would tell me if the answer was a positive or negative number and this is what I came up with.
#include<iostream>

using namespace std;


int main()

{

 int firstnumb = 0;

 int secondnumb = 0;

 int answer = 0;

 char again = 'y';

	while(again == 'y')

	{

	

    cout << "\n\tPositive Negitive Calculator." << endl;


	cout << "\nHow many positive numbers? ";

	cin >> firstnumb;[COLOR="green"]//This is never used to calculate any thing.[/COLOR]


	cout << "\nHow many negative numbers? ";

	cin >> secondnumb;[COLOR="green"]// This is how many time we times -2 * -2.[/COLOR]


	if (secondnumb >= 0)[COLOR="green"]//Makes sure they entered a number above 0.[/COLOR]

	{

	 int i;

     answer = 2;

     for (i = 0; i < secondnumb; i++) {

     answer *= -2;[COLOR="green"]//This takes answer which equals 2 and times it times -2 intel i reaches secondnum.[/COLOR]

    }

	}


	 if (answer <= 0)[COLOR="green"]//If or answer is below 0 then tell the user its negative.[/COLOR]

	 {

	 cout << "\nYour answer is negative." << endl;

	 }

	 else if (answer >= 0)[COLOR="green"]//If or answer is above 0 then tell the user its positive.[/COLOR]

	 {

	 cout << "\nYour answer is positive." << endl;

	 }

	

	cout << "\nDo you want to make another calculation? (y/n) ";

	cin >> again;

	

	}

		

	cout << "OK maybe next time." << endl;


	cin.get();

	return 0;

}
Well that's it it's actually pretty easy so now you know how to make a negative or positive calculator if for some odd reason you wanted one, leave your comments about the program.:)
BTW Thanks Orjan for helping me with the variables.^^
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
as long as there are an even number of negative numbers, the product will always become positive, as long as there are an odd number of negative numbers, the broduct will always become negative.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
if (secondnumb % 2 != 0 )
{
  cout << "\nYour answer is negative." << endl;
}
else 
{
  cout << "\nYour answer is positive." << endl;
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Ya I figured that out half way though but it was fun to make so I finished it.:)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.