User entering three different numbers, but program summarizes only the sum of the positive numbers

+4 votes
asked Nov 13, 2019 by anonymous
I need a program which when a user types three different random numbers, the program only calculates and summarizes the sum of the positive ones and gives me an answer!

Thank you!

8 Answers

0 votes
answered Nov 13, 2019 by gameforcer (2,950 points)

nsum = 0
nums = tuple(map(lambda n: float(n), input('Input 3 numbers:\n').split(' ')))

for item in nums:
    if item > 0:
        nsum += item

print(nsum)

You can put more than 3 numbers. Also, type them in with a space in between, like this:

1 -20 3

0 votes
answered Nov 13, 2019 by Pranav U (140 points)
the following program is written in C:

#include <stdio.h>
int main()
{
int a, b, c, s=0;
printf("enter any three numbers:  ");
scanf("%d%d%d", &a, &b, &c);
if(a>0)
s=s+a;
if(b>0)
s=s+b;
if(c>0)
s=s+c;
printf("%d", s);
}
0 votes
answered Nov 14, 2019 by Lorenzo Romandini (140 points)
#include <iostream>
using namespace std;

double somma (double a, double b, double c)
{
  double sum = 0;
  if (a > 0) sum += a;
  if (b > 0) sum += b;
  if (c > 0) sum += c;
  return sum;
}

int main()  {
  double x, y, z;
  cout << "Inserire 3 numeri : ";
  cin >> x >> y >> z;
  cout << "La somma dei numeri positivi รจ : " << somma(x,y,z);
}
0 votes
answered Nov 16, 2019 by anonymous
#include <stdio.h>

int main(){

int num1, num2, num3, sum = 0;

printf("Enter three numbers:  ");
scanf("%d %d %d", &num1, &num2, &num3);

if(num1>0)
sum += num1;

if(num2>0)
sum += num2;

if(num3>0)
sum += num3;

printf("Sum of numbers entered = %d", sum);

return 0;

}
0 votes
answered Nov 16, 2019 by MouliMallina (140 points)
This is written in c using functins, hope it helps:

#include <stdio.h>
int add(int x,int y,int z);
int main()
{
  int x,y,z,sum;
  printf("Enter 3 no's: ");
  scanf("%d %d %d", &x,&y,&z);
  sum=add(x,y,z);
  printf("Addition of +ve no's is:%d",sum);
  return 0;
}
int add(int x,int y,int z)
{
    int sum,res;
    if(x>0)
    sum+=x;
    if(y>0)
    sum+=y;
    if(z>0)
    sum+=z;
    return sum;
}
0 votes
answered Nov 16, 2019 by xsevdam (350 points)
#include<iostream>

using namespace std;

int main()

{

int i,a[3],s=0;

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

{

cin>>a[i];

}

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

{

if(a[i]>0)

{

s+=a[i];

}

}

cout<<s;

}
0 votes
answered Nov 16, 2019 by ariefismail (190 points)
#include <iostream>

using namespace std;

int main()
{
    int ans;
    cout<<"insert 3 num:\n";
    for(int i=0;i<3;i++){
        int num;
        cin>>num;
        ans+= num>0 ? num:0;
    }
    cout<<"ans : "<<ans;

    return 0;
}
0 votes
answered Nov 18, 2019 by bhoomi2000 (780 points)

#include<stdio.h>

void main()

{

       int a,b,c,sum;

      scanf("%d%d%d",&a,&b,&c);

     if(a<0)

    {

              sum=b+c;

              printf("%d",sum);

     }

    if(b<0)

   {

         sum =a+c;

         printf("%d",sum);

   }

    if(c<0)

   {

         sum=a+b;

        printf("%d",sum);

   }

   if(a>0&&b>0&&c>0)

   {

        sum=a+b+c;

       printf("%d",sum);

   }

}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...