Thursday, 14 April 2016

C And C++ Program to store positive elements in an array Problem Solution.


C Solution:

#include <stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],i,j,sz,a;

    printf("Enter size of the array : ");
    scanf("%d", &sz);

    printf("Enter elements in array (Separated by space): ");
    for(i = 0,j = 0; i < sz; i++){
        scanf("%d", &a);
        if(a >= 0)ar[j++] = a;
    }

    printf("\nElements store in array are : ");
    for(i = 0; i < j; i++)printf("%d ",ar[i]);

    return 0;
}

C++ Solution:

#include <iostream>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    int ar[1000],j = 0,sz,a;

    cout << "Enter size of the array : ";
    cin >> sz;

    cout << "Enter elements in array (Separated by space): ";
    for(int i = 0; i < sz; i++){
        cin >> a;
        if(a >= 0)ar[j++] = a;
    }
    cout << endl << "Elements store in array are : ";
    for(int i = 0; i < j; i++)cout << ar[i] << " ";

    return 0;
}

No comments:

Post a Comment