Thursday, 14 April 2016

C And C++ Program to print all negative elements in an array Problem Solution.

C Solution:

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

    int ar[1000],i,sz;

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

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

    printf("\nAll negative elements in array are : ");
    for(i = 0; i < sz; i++)if(ar[i] < 0)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],sz;

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

    cout << "Enter elements in array (Separated by space): ";
    for(int i = 0; i < sz; i++)cin >> ar[i];

    cout << endl << "All negative elements in array are : ";
    for(int i = 0; i < sz; i++)if(ar[i] < 0)cout << ar[i] << " ";

    return 0;
}

No comments:

Post a Comment