Problem statement:- Program to Separate positive and negative numbers in an array.
Data requirement:-
Input Data:- arr,size
Output Data:-arr
Output Data:-arr
Additional Data:- i, j
Program in C
Here is the source code of the C Program to Separate positive and negative numbers in an array.
Code:
#include<stdio.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],j,i;
printf("Enter the Element of the array:\n");
for(j=1;j<size;j++)
{
scanf("%d",&arr[j]);
}
printf("\nPositive numbers are:");
for(i=0;i<size;i++)
{
if(arr[i]>0)
{
printf("%d ",arr[i]);
}
}
printf("\nNegative numbers are:");
for(i=0;i<size;i++){
if(arr[i]<0)
{
printf("%d ",arr[i]);
}
}
}
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],j,i;
printf("Enter the Element of the array:\n");
for(j=1;j<size;j++)
{
scanf("%d",&arr[j]);
}
printf("\nPositive numbers are:");
for(i=0;i<size;i++)
{
if(arr[i]>0)
{
printf("%d ",arr[i]);
}
}
printf("\nNegative numbers are:");
for(i=0;i<size;i++){
if(arr[i]<0)
{
printf("%d ",arr[i]);
}
}
}
Input/Output:
Enter the size of the array:6
Enter the Element of the array:
-3
5
-6
8
9
-7
Positive numbers are:5 8 9
Negative numbers are:-3 -6 -7
Enter the Element of the array:
-3
5
-6
8
9
-7
Positive numbers are:5 8 9
Negative numbers are:-3 -6 -7
Program in C++
Here is the source code of the C++ Program to Separate positive and negative numbers in an array.
Code:
#include<iostream>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size],j=0,i;
cout<<"Enter the Element of the array:\n";
for(j=0;j<size;j++)
{
cin>>arr[j];
}
cout<<"\nPositive numbers are:";
for(i=0;i<size;i++)
{
if(arr[i]>0)
{
cout<<arr[i]<<" ";
}
}
cout<<"\nNegative numbers are:";
for(i=0;i<size;i++){
if(arr[i]<0)
{
cout<<arr[i]<<" ";
}
}
}
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size],j=0,i;
cout<<"Enter the Element of the array:\n";
for(j=0;j<size;j++)
{
cin>>arr[j];
}
cout<<"\nPositive numbers are:";
for(i=0;i<size;i++)
{
if(arr[i]>0)
{
cout<<arr[i]<<" ";
}
}
cout<<"\nNegative numbers are:";
for(i=0;i<size;i++){
if(arr[i]<0)
{
cout<<arr[i]<<" ";
}
}
}
Enter the size of the array:5
Enter the Element of the array:
-35
-78
56
74
35
Positive numbers are:56 74 35
Negative numbers are:-35 -78
Enter the Element of the array:
-35
-78
56
74
35
Positive numbers are:56 74 35
Negative numbers are:-35 -78
Program in Java
Here is the source code of the Java Program to Separate positive and negative numbers in an array.
Code:
import java.util.Scanner;
public class p19 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
int arr[ ]=new int[size];
int i,j=0;
System.out.println("Enter the Element of the array:");
while(j<size)
{
arr[j]=sc.nextInt();
j++;
}
System.out.println("Positive numbers are:");
for(i=0;i<size;i++)
{
if(arr[i]>0)
{
System.out.print(arr[i]+" ");
}
}
System.out.println("\nNegative numbers are:");
for(i=0;i<size;i++){
if(arr[i]<0)
{
System.out.print(arr[i]+" ");
}
}
sc.close();
}}
public class p19 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
int arr[ ]=new int[size];
int i,j=0;
System.out.println("Enter the Element of the array:");
while(j<size)
{
arr[j]=sc.nextInt();
j++;
}
System.out.println("Positive numbers are:");
for(i=0;i<size;i++)
{
if(arr[i]>0)
{
System.out.print(arr[i]+" ");
}
}
System.out.println("\nNegative numbers are:");
for(i=0;i<size;i++){
if(arr[i]<0)
{
System.out.print(arr[i]+" ");
}
}
sc.close();
}}
Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
-3
5
-78
44
Positive numbers are:
5 44
Negative numbers are:
-3 -78
4
Enter the Element of the array:
-3
5
-78
44
Positive numbers are:
5 44
Negative numbers are:
-3 -78
Program in Python
Here is the source code of the Python Program to Separate positive and negative numbers in an array.
Code:
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("\nPositive numbers are:")
for i in range(0,size):
if(arr[i]>0):
print(arr[i],end=" ")
print("\nNegative numbers are:")
for i in range(0,size):
if(arr[i]<0):
print(arr[i],end=" ")
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("\nPositive numbers are:")
for i in range(0,size):
if(arr[i]>0):
print(arr[i],end=" ")
print("\nNegative numbers are:")
for i in range(0,size):
if(arr[i]<0):
print(arr[i],end=" ")
Input/Output:
Enter the size of the array: 6
Enter the Element of the array:
-3
5
-77
69
-45
-78
Enter the Element of the array:
-3
5
-77
69
-45
-78
Positive numbers are:
5 69
Negative numbers are:
-3 -77 -45 -78
Most Recommend Questions:-
More Questions:-
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box