Wednesday, May 8, 2019

Print 5 Sum of Positive Numbers

  • Problem Description

    Write a C program to find allow the user to enter n number and finds the number of positive numbers entered and the sum of all positive numbers entered using a while loop

    Input format:

    Input consists of n+1 integers. The first integer corresponds to n and the next n integers correspond to the numbers to be added.Consider 0 to be a positive number

    Output format:

    Refer sample input and output for formatting specifications.

  • Test Case 1

    Input (stdin)
    4

    5 -2 -1 6

    Expected Output
    Positive Numbers=2

    Sum=11
  • Test Case 2

    Input (stdin)
    5

    -1 -2 -3 -4 -5

    Expected Output
    Positive Numbers=0

    Sum=0
Solution

#include<stdio.h>
int main(){
  int n,sum=0,count=0,i=1,b;
  scanf("%d",&n);
  while(i<=n)
  {
    scanf("%d",&b);
    i++;
    if(b>=0)
    {
      count++;
      sum=sum+b;
    }
  }
  printf("Positive Numbers=%d\nSum=%d",count,sum);
  return 0;
}

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...