Monday, November 23, 2020

Write a C program that read 5 numbers and counts the number of positive numbers and print the average of all positive values.

 /*Write a C program that read 

5 numbers and counts the 

number of positive numbers 

and print the average of all 

positive values. 

Test Data :

Input the first number: 5

Input the second number: 8

Input the third number: 10

Input the fourth number: -5

Input the fifth number: 25

Expected Output:

Number of positive numbers: 4

Average value of the said 

positive numbers: 12.00

*/

#include <stdio.h>

int main()

{

  int num[5],i,sum=0,

   positive=0;

  printf("Input the first\

 number: ");

  scanf("%d",&num[0]);

  printf("Input the second\

 number: ");

  scanf("%d",&num[1]);

  printf("Input the third\

 number: ");

  scanf("%d",&num[2]);

  printf("Input the fourth\

 number: ");

  scanf("%d",&num[3]);

  printf("Input the fifth\

 number: ");

  scanf("%d",&num[4]);

  for(i=0;i<5;i++)

  {

    if(num[i]>=0)

    {

      positive++;

      sum=sum+num[i];

    }

    

  }

  printf("Number of positive\

 numbers: %d\n", positive);

 

  printf("Average value of\

 \nthe said positive numbers:\

  %.1f",(float)sum/4);

  getchar();

}

No comments:

Post a Comment