In this tutorial you will learn about the C Program to Put Positive and Negative Numbers in two Separate Arrays and its application with practical example.
C Program to Put Positive and Negative Numbers in two Separate Arrays
In this tutorial, we will learn to create a C program that will Put Positive and Negative Numbers in two Separate Arrays using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Arithmetic operations in C Programming.
What is a positive number?
A number is said to be a positive number when that number is having a value greater than “0”. It means 1,2,3,4.
What is a negative number?
A number is said to be a negative number when that number is having a value less than “0”. It means -1,-2,-3,-4.
Program to Put Positive and Negative Numbers in two Separate Arrays.
In this program, we will first take the input array size and the elements from the user. Then we will separate the positive and negative numbers from that array. After that, we will Put the positive and negative Numbers in Separate Arrays. At last, we will print the separated array containing both values.
With the help of this program, we can Print Positive & Negative Numbers in two Arrays.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include<stdio.h> void PrintArray(int a[], int Size); int main() { //declaring the required variables for the program. int Size, i, a[10], Positive[10], Negative[10]; int Positive_Count = 0, Negative_Count = 0; //i = it will hold the integer value for the loop. //Size = it will hold the integer value for the size of array //Taking the input size of the array printf("\n Please Enter the Size of an Array : "); scanf("%d", &Size); //taking the input elements of the array. printf("\nPlease Enter the Array Elements : "); for(i = 0; i < Size; i++) { scanf("%d", &a[i]); } //Seperating the positive negative numbers from the array for(i = 0; i < Size; i ++) { if(a[i] >= 0) { Positive[Positive_Count] = a[i]; Positive_Count++; } else { Negative[Negative_Count] = a[i]; Negative_Count++; } } //Printing the positive negative integers from the array. printf("\n Total Number of Positive Numbers in this Array = %d ", Positive_Count); //Printing Array Elements in Positive Array printf("\n Array Elements in Positive Array : "); PrintArray(Positive, Positive_Count); //Printing Array Elements in Negative Array printf("\n Total Number of Negative Numbers in this Array = %d ", Negative_Count); printf("\n Array Elements in Negative Array : "); PrintArray(Negative, Negative_Count); return 0; } void PrintArray(int a[], int Size) { int i; for(i = 0; i < Size; i++) { printf("%d \t ", a[i]); } printf("\n"); } |
Output:-
In the above program, we have first initialized the required variable.
- a[10] = it will hold the integer value.
- i = it will hold the integer value.
- Size = it will hold the integer value.
Input number of elements from the user.
Program Logic Code.
Printing output of the program.