161k views
5 votes
Write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If the input is: 10 -7 4 39 -6 12 2 the output is: 2 4 10 12 39

2 Answers

0 votes

Answer:

integers=[]

while True:

number=int(input())

if number<0:

break

integers.append(number)

print("",min(integers))

print("",max(integers))

Step-by-step explanation:

User Manish Tiwari
by
5.0k points
2 votes

Answer:

Following are the code to this question:

#include <iostream>//defining header file

using namespace std;

int main()//defining main method

{

int a[]={10,-7,4,39,-6,12,2};//defining single deminition array and assign value

int i,x,j,t; //defining integer variable

cout<<"Before sorting value: ";

for(i=0;i<7;i++) //using loop to print value

{

cout<<a[i]<<" ";//print value

}

cout<<endl <<"After sorting value: ";

for(i=0;i<7;i++)//defining loop to sort value

{

for(j=i+1;j<7;j++)//count array value

{

if(a[i]>a[j]) //defining condition to inter change value

{

//performing swapping

t=a[i]; //integer variable t assign array value

a[i]=a[j];//swapp value

a[j]=t;//assign value in array

}

}

}

for(i=0;i<7;i++) //defining loop to print value

{

if(a[i]>=0) //defining condition to check positive value

{

cout<<a[i]<<" ";//print value

}

}

return 0;

}

Output:

Before sorting value: 10 -7 4 39 -6 12 2

After sorting value: 2 4 10 12 39

Step-by-step explanation:

Following are the description to the above code:

  • In the above program code, Inside the main method, an array a[] is declared that assign some value, and another integer variable "i, j, x, and t" is declared, in which variable "i and j" are used in the loop, and "x, t" is used to sort value.
  • In the next step, three main for loop is declared, in which the first loop is used to print array value.
  • In the second loop, inside another loop is used that sorts array values.
  • In the last loop, a condition is defined, that check the positive value in the array and print its values.
User Howlger
by
4.5k points