Thursday, 14 April 2016

C And C++ Program to count total number of negative elements in array Problem Solution.

C Solution:

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

    int ar[1000],i,sz,cnt = 0;

    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]);

    for(i = 0; i < sz; i++)if(ar[i] < 0)cnt++;
    printf("\nTotal number of negative elements = %d\n", cnt);

    return 0;
}

C++ Solution:

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

    int ar[1000],i,sz,cnt = 0;

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

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

    for(i = 0; i < sz; i++)if(ar[i] < 0)cnt++;
    cout << endl << "Total number of negative elements = " << cnt << endl;;

    return 0;
}

No comments:

Post a Comment