Friday, December 19, 2014

Numbers with Frequency in c lang



Print the Number of Odd & Even Numbers in an Array

#include 
#include 
void main()
{
    int array[100], i, num;
 
    printf("Enter the size of an array \n");
    scanf("%d", &num);
    printf("Enter the elements of the array \n");
    for (i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("Even numbers in the array are - ");
    for (i = 0; i < num; i++)
    {
        if (array[i] % 2 == 0)
        {
            printf("%d \t", array[i]);
        }
    }
    printf("\n Odd numbers in the array are -");
    for (i = 0; i < num; i++)
    {
        if (array[i] % 2 != 0)
        {
            printf("%d \t", array[i]);
        }
    }
 getch();
}
 
 
Output
 
Enter the size of an array
6
Enter the elements of the array
12
19
45
69
98
23
Even numbers in the array are - 12     98
 Odd numbers in the array are - 19     45     69     23

Print all the Repeated Numbers with Frequency in an Array

#include 
#include 
#include 
 
void duplicate(int array[], int num)
{
    int *count = (int *)calloc(sizeof(int), (num - 2));
    int i;
 
    printf("duplicate elements present in the given array are ");
    for (i = 0; i < num; i++)
    {
        if (count[array[i]] == 1)
            printf(" %d ", array[i]);
        else
            count[array[i]]++;
    }
}
 
int main()
{
    int array[] = {5, 10, 10, 2, 1, 4, 2};
    int array_freq = sizeof(array) / sizeof(array[0]);
    duplicate(array, array_freq);
    getchar();
    return 0;
 getch();
}
 
Output
 
duplicate elements present in the given array are  10  2

No comments:

Post a Comment