PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on If & Else

Program to check whether a number is positive or negative

Input - Output

Input
Enter a number : 6

Output
6 is a positive number

Input
Enter a number : -9

Output
-9 is a negative number

Algorithm

Step 1: START

Step 2: Take a number 'num' as an input.

Step 3: Check the condition:

   if(num>0)

      print positive number

   else

      print negative number

Step 4: END

CODE


/* check number is +ve or -ve */
#include  <stdio.h>
int main()
{
      int num;
      printf(" Enter a number : ");
      scanf("%d",&num);
      if(num>0)
          printf(" %d is a positive number",num);
      else
          printf(" %d is a negative number",num);
    return 0;
}


/* check number is +ve or -ve */
#include <iostream>
using namespace std;
int main()
{
      int num;
      cout<<" Enter a number : ";
      cin>>num;
      if(num>0)
          cout<<num<<" is a positive number";
      else
          cout<<num<<" is a negative number";
    return 0;
}

OutPut

Enter a number : 6

6 is a positive number