Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Even or Odd

Here are some projects of actual programs you can practice making.

Postby Gooberius on Sun Aug 03, 2008 1:49 am

Gooberius
 
Posts: 16
Joined: Mon Jul 28, 2008 7:25 pm

Postby Marss++ on Sun Aug 03, 2008 5:22 pm

Thanks :)
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby Doctor Salt on Sun Aug 03, 2008 7:10 pm

I looked up quite a lot about using square roots, but none were so specific on the syntax (Thank you! that finally allows me to finish my quadratic part of the ultra calculator!) so apparently to use it, you have to declare a new variable as the root of it?

So you couldn't just do

Code: Select all
#include <cmath>

int main ()
{

cout << sqrt (25) << "the square root of 25 is 5";
}
?




but instead
Code: Select all
#include <cmath>
#include <iostream>
using namespace std;

int main ()
{
float question = 25;
float answer = sqrt ( question)


cout << answer << "the square root of 25 is 5";
}
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby Marss++ on Sun Aug 03, 2008 7:23 pm

Code: Select all
#include <cmath>
double sqrt( double num );



That would be the actual syntax of the code.

The sqrt function returns the square root of a number. If the number returned is negative it gives you a domain error.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby Gooberius on Mon Aug 04, 2008 1:20 am

Doctor Salt wrote:So you couldn't just do

Code: Select all
#include <cmath>

int main ()
{

cout << sqrt (25) << "the square root of 25 is 5";
}
?


No, that's absolutely fine. Where you have a function that accepts a value you can use a function that returns a value as a parameter. Basically, each parameter to a function is expected to be a valid expression that evaluates to the same type as the parameter. Provided the function returns a value of the same type (or one that can be implicitly cast to the correct type - but that a whole other tutorial video for antiRTFM ;-) ) then its result can be used as the input to a function. For example,

Code: Select all
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>

using namespace std;

float DegreesToRadians( const float fAngleInDegrees )
{
    return (fAngleInDegrees * (float)M_PI) / 180.0f;
}

int main()
{
    for ( float fAngle = 0.0f; fAngle < 360; fAngle += 1.0f )
    {
        cout << "Sin(" << fAngle << ") = " << sinf(DegreesToRadians(fAngle)) << endl;
    }
    return 0;
}
Gooberius
 
Posts: 16
Joined: Mon Jul 28, 2008 7:25 pm

Postby Strazku on Tue Aug 26, 2008 8:57 pm

Rock on, hehe I learned all of this actually. In my technical training the military put me through, some craziness we have to work with ocassionally. (Work on F-16's, hehe.) Now knowing that this stuff ties into programming, when I get to the point to where I can actually make some magic happen, I'll gladly put this to use. Thanks alot!
Strazku
 
Posts: 16
Joined: Sat Aug 23, 2008 11:21 pm

I like bits...

Postby Alfaris on Sat Nov 15, 2008 2:50 am

AWESOME!

I totally got that (for the most part)

So yea I think I understand it but I still have a couple of questions:

A byte is 8 bits, which has 255 combinations (just under 256, which would start on a 9th bit I assume since that's what it does in binary). Well a kilobyte is suppose to be 1000 right? But it's not it's 1024 or something close to that...and same thing with megabytes and gigabytes. My question is if bytes are just sets of bits, then how come you can't just have 1000 sets of 8 bits, or 8000 bits?

I have recently been exposed to a '>' operator in C++, I didn't quite understand it since it was a small little internet tutorial but it was something about what they called 'nodes' and passing on pointers until you get to a 'stop' pointer or something...I can't really remember. Anyways, is the '>' operator related to the '>>' operator?

What subject would this type of stuff go into? Software engineering or hardware engineering?

I've had these questions for a while actually...
Alfaris
 
Posts: 4
Joined: Mon May 19, 2008 12:21 am

Even or Odd

Postby Zul'Head on Wed Jan 28, 2009 11:12 am

This should do the trick...

Code: Select all
#include <iostream>

using namespace std;


int main()
{
   int number;

   cout << "Insert a number: ";
   cin >> number;

   (number % 2 == 0) ? cout << endl << "The number is Even !\n\n" : cout << endl << "The number is Odd !\n\n";

   system("pause");
}
Zul'Head
 
Posts: 4
Joined: Thu Nov 27, 2008 5:32 pm

Re: Even or Odd

Postby fourtwizzy on Fri Jan 30, 2009 7:17 pm

My version. Can someone help me with the !cin.good() I get a infinite loop.
Code: Select all
#include <iostream>
using namespace std;

int main(void)
{
   //declare variable
   int input;

   //Asks user to input variable
   cout << "Please enter a whole number: ";
   cin>> input;
   
   while(!cin.good())
   {
      cout << "Please enter a valid number! ";
      cin >> input;
      return 0;
   }

   while(cin.good())
   {
      if ((input%2) == 0)
      {
         cout << "\nThis number is even!\n";
      }
      else
         cout << "\nThis number is odd!\n";
      return 0;
   }
   
   


}
fourtwizzy
 
Posts: 1
Joined: Fri Jan 30, 2009 4:44 pm

Re: Even or Odd

Postby programmattic on Wed Oct 21, 2009 9:20 pm

Here's my program for this. It's short but does the job:

Code: Select all
#include <iostream>

using namespace std;

int main(){
   
   int number;
   
   cout << "Enter your number: ";
   cin >> number;
   cout << endl;
   
   if(number%2 == 0){
            
        cout << "Even" << endl;
            
        }
            
   else if(number%2 == 1){
      
       cout << "Odd" << endl;
      
       }
   
   system("pause");
   return 0;
   }

programmattic
 
Posts: 8
Joined: Sun Oct 18, 2009 5:02 pm

Previous

Return to Projects

Who is online

Users browsing this forum: No registered users and 0 guests