waktu(clocklink.com)

27 November 2014

Tutorial C++ : How to determine positive, zero or negative number

In this session, i will discuss very simple program, it's about how to determine number. I use C++ to make this program and use if condition(if...else) to solve this problem.

Here is the source code :

#include<iostream>

using namespace std;

int main()
{
    int bil;
    cout<<"input number: ";
    cin>>bil;
   
    if (bil<0)
              {
              cout<<"negative\n";
              }
    else if (bil==0)
              {
              cout<<"zero\n";
              }
    else
              {
              cout<<"positive\n";
              }
       
system ("PAUSE");
return 0;

}

---***-----

i will analyze the meaning of that code.
#include<iostream>              //is library to determine cin(input) and cout(output)
int main()                              //this is the main program that will be execute
int bil;                                   //type is integer(number) and the name of variable is bil
cout <<"input number: ";     //display "input number: " to screen
cin>>bil;                              //if user enters number, the program scan that number as "bil"
 if (bil<0)
              {
              cout<<"negative\n";
              }
// if value of bil is less than 0, program will display negative

else if (bil==0)
              {
              cout<<"zero\n";
              }
// if value of bil is 0, program will display zero

else
              {
              cout<<"positive\n";
              }
// if value of bil is other, program will display positive to the screen.

system ("PAUSE");             //system will pause until user press a key
return 0;                        //it will end main function and the return value is 0(the program run correctly)

}

run this program and this is the result:



hope this tutorial is useful. . Keep trying!

No comments: