w3resource

C Exercises: Counts the number of positive and negative numbers

C Basic Declarations and Expressions: Exercise-27 with Solution

Write a C program that reads 5 numbers and counts the number of positive numbers and negative numbers.

Pictorial Presetation:

C Programming: Counts the number of positive  and negative numbers

C Code:

#include <stdio.h>
int main() {
	float numbers[5];
	int j, pctr=0, nctr=0;
	printf("\nInput the first number: "); 
    scanf("%f", &numbers[0]);
    printf("\nInput the second number: "); 
    scanf("%f", &numbers[1]);
    printf("\nInput the third number: "); 
    scanf("%f", &numbers[2]);
	printf("\nInput the fourth number: "); 
    scanf("%f", &numbers[3]);
    printf("\nInput the fifth number: "); 
    scanf("%f", &numbers[4]);
	for(j = 0; j < 5; j++) {
		if(numbers[j] > 0)
		{
			
			pctr++;
		}
		else if(numbers[j] < 0)
		{
			nctr++;
		}
	}
	printf("\nNumber of positive numbers: %d", pctr);
	printf("\nNumber of negative numbers: %d", nctr);
	printf("\n");
	return 0;
}

Sample Output:

Input the first number: 5                                              
                                                                       
Input the second number: -4                                            
                                                                       
Input the third number: 10                                             
                                                                       
Input the fourth number: 15                                            
                                                                       
Input the fifth number: -1                                             
                                                                       
Number of positive numbers: 3                                          
Number of negative numbers: 2 

Flowchart:

C Programming Flowchart: Counts the number of positive  and negative numbers

C Programming Code Editor:


Contribute your code and comments through Disqus.

Previous: Write a C program that prints all even numbers between 1 and 50 (inclusive).
Next: Write a C program that read 5 numbers and counts the number of positive numbers and print the average of all positive values.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - "static const" vs "#define" vs "enum"

It depends on what you need the value for. You (and everyone else so far) omitted the third alternative:

  1. static const int var = 5;
  2. #define var 5
  3. enum { var = 5 };

Ignoring issues about the choice of name, then:

  • If you need to pass a pointer around, you must use (1).
  • Since (2) is apparently an option, you don't need to pass pointers around.
  • Both (1) and (3) have a symbol in the debugger's symbol table - that makes debugging easier. It is more likely that (2) will not have a symbol, leaving you wondering what it is.
  • (1) cannot be used as a dimension for arrays at global scope; both (2) and (3) can.
  • (1) cannot be used as a dimension for static arrays at function scope; both (2) and (3) can.
  • Under C99, all of these can be used for local arrays. Technically, using (1) would imply the use of a VLA (variable-length array), though the dimension referenced by 'var' would of course be fixed at size 5.
  • (1) cannot be used in places like switch statements; both (2) and (3) can.
  • (1) cannot be used to initialize static variables; both (2) and (3) can.
  • (2) can change code that you didn't want changed because it is used by the preprocessor; both (1) and (3) will not have unexpected side-effects like that.
  • You can detect whether (2) has been set in the preprocessor; neither (1) nor (3) allows that.

So, in most contexts, prefer the 'enum' over the alternatives. Otherwise, the first and last bullet points are likely to be the controlling factors - and you have to think harder if you need to satisfy both at once.

If you were asking about C++, then you'd use option (1) - the static const - every time.

Ref : https://bit.ly/3skplWU