Computer Science, asked by adritaghosh232k4, 3 months ago

wap
to input no from the user and find the sum of
positive and negative number
until
user enter Zero​

Answers

Answered by anindyaadhikari13
4

Answer:

Here comes the program to input number from the user and find the sum of positive and negative numbers until user enters zero.

1. In Java.

import java.util.*;

public class Java {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int n=0, p=0, a;

while(true) {

System.out.print("Enter a number: ");

a=sc.nextInt();

if(a==0)

break;

if(a>0)

p+=a;

else

n+=a;

}

System.out.println("Sum of Positive Numbers: "+p);

System.out.println("Sum of Negative Numbers: "+n);

sc.close();

}

}

2. In Python.

p,n=0,0

while True:

x=int(input("Enter a number: "))

if x==0:

break

if x>0:

p+=x

else:

n+=x

print("Sum of Positive Numbers: ",p)

print("Sum of Negative Numbers: ",n)

3. In C

#include <stdio.h>

void main() {

int n=0,p=0,x;

while(1==1) {

printf("Enter a number: ");

scanf("%d", &x);

if(x==0)

break;

if(x>0)

p+=x;

else

n+=x;

}

printf("Sum of Positive Numbers: %d\n", p);

printf("Sum of Negative Numbers: %d", n);

}

4. In C++

#include <iostream>

using namespace std;

void main() {

int p=0,n=0,x;

while (1) {

cout << "Enter a number: ";

cin >> x;

if (x==0)

break;

if(x>0)

p+=x;

else

n+=x;

}

cout << "Sum of Positive Numbers: "<<p<<endl;

cout << "Sum of Negative Number: "<<n<<endl;

}

Algorithm:

  1. START.
  2. Create an infinite loop. Ask the user to enter the number.
  3. Check if the number is zero. If true, break the loop.
  4. If the number is not zero, check if the number is positive or negative. If positive, add the number to p variable or else, add the number to n variable.
  5. Display the values of p and n.
  6. STOP.

See the attachment for output .

Attachments:
Answered by harinipuppy120
0

Answer:

printf ( "Enter positive numbers (0 or -ve number to stop):\n" );. sum = 0; ... Note the use of a scanf statement to read a number before the while loop. Another scanf ...

Similar questions