Dr. Ehoneah Obed
Dr. Ehoneah Obed

Dr. Ehoneah Obed

Write a C program that prints whether a given number is positive or negative

Write a C program that prints whether a given number is positive or negative

Dr. Ehoneah Obed's photo
Dr. Ehoneah Obed
·Jun 4, 2022·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • How the program generates a random number any time it is run
  • How to check whether a given number is negative or positive

Practical Example

Assume that you have a program that generates a random number each time 
the program is run.

Now, write a C program to tell whether the random number generated (which
will be stored in a variable x) is a positive or negative number.

How the program generates a random number any time it is run

If you are new to programming, don't worry yourself about the part where the program generates random numbers each time it runs. You will get to understand that part later.

Below is a C program that generates a random number any time it is run.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
      int x;

      srand(time(0));
      x = rand()  - RAND_MAX / 2;

      printf("%d \n", x);

      return 0;
}

Output of the program on first run was

-810798084

Output 2

-988266352

Output 3

166766229

You realize that anytime the program is run a random number is generated.

Now the real task for you to think about is how to write a program that will tell whether x is a positive number or negative number.

How to check whether a given number is negative or positive

Once you are given such a question, you have to reason through it and come up with possible ways that such a problem can be solved.

Note there for each given problem there may be several possible ways of solving the problem. You should be able to come up with as many possible solutions as possible.

Upon going through the possible solutions, we may find that some of them are more feasible than the others. You will then pick and choose one and go ahead to write the code for the chosen solution.

The various methods that you come up with to solve the problem at hand are known technically as algorithms.

So, in essence, whenever, you are given a problem, you have to come up with an algorithm to solve that problem before finally setting off to code your solution after selecting the most feasible algorithm.

For the problem at hand, let me ask you (make sure you think about it before you continue to read). If I give you any number at all right now, how will you know whether the number is positive or negative?

There are two common ways that I can think of right now;

First method

Check the sign (+/-) attached to the number given. If the sign is `+`, it 
means that the number is a positive number but if the sign is `-` then it is 
a negative number.

Second method

Compare the number to zero, if the number is greater than zero then it is 
positive but if it is less than zero then it is negative.

Alright, so the above methods are the possible algorithms that can be used and you can probably come with your own algorithm that can also be used to solve the problem.

You should by now realize that both methods make use of a conditional statement involving if (you can read more on if conditions and how to use them in C programming).

We therefore have to make a decision on which of them is feasible for us to implement. As a beginner at programming in C, I believe that the second one will be simpler to implement.

But how do we implement it in code. To help ease the process of coming up with the right code, let's develop a pseudocode for our solution.

Pseudocode for chosen solution

- Get the variable given (x)
- Check if x > 0
        Output "The number is positive"
- Else if x < 0
        Output "The number is negative"
- Else
        Output " The number is zero"

Now that we have pseudocode, we can easily implement the code in the C language.

Since, the C program that we are going to produce depends on the already created C program that generates random numbers, we just have to copy that and add our own code to it.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
      int x;

      srand(time(0));
      x = rand()  - RAND_MAX / 2;

      printf("%d \n", x);

      /* Our own code will go here */
      return 0;
}

Now let's go ahead and add our own bit of code under the comment.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
      int x;

      srand(time(0));
      x = rand()  - RAND_MAX / 2;

      printf("%d \n", x);

      /* Our own code will go here */
      if (x > 0)
      {
           printf("The number is positive \n");
      }
      else if (x < 0)
      {
           printf("The number is negative \n");
      }
      else
      {
           printf("The number is zero \n");
       }
      return 0;
}

A sample outcome that I got when I run the above program is shown in the image below:

image.png

Voila! Our program is working perfectly telling us whether the random number generated is negative or positive.

Conclusion

I hope you enjoyed reading this and made you understand how to solve a problem like this. I would like to hear back from you, is there anything I missed, or explained wrongly or something you want me to elaborate more on?

Share with me what you also think, mind you I am also learning and your feedback will be valuable to me.

Thanks for reading and I will love to connect personally with you. If you are on Twitter then you can send me a DM and let's have a good chat.

 
Share this