Excess Numbers

Given ‘n’ integers, write an algorithm and the subsequent ‘C’ program to print all Excess numbers in it. A number is said to an ‘Excess number’ if it is greater than the average of the elements in its right. Last number is always considered to be ‘Excess number’. For example, given seven numbers 23, 34, 12, 21, 14, 26, 33, then the numbers 34, 33 are excess numbers.
Input format
First line contains the number of elements, n
Next ‘n’ lines contain the elements
Output format
Print all excess numbers in the collection
Input:
the number of elements
the n elements
Processing:
void main()
{
int n;
int numbers[n];
for(int i =0;i<n-1;i++)
    {
        int sum = 0;
        for(int j = i+1;j<n;j++)
            sum+=numbers[j];
        if ((float)numbers[i]>(sum/(n-i-1)))
            printf("%d\n",numbers[i]);
    }
printf("%d",numbers[n-1]);
}
Output:
Display the excess numbers

Program:
#include<stdio.h>
void main()
{
    int n;
    scanf("%d", &n);
    int numbers[n];
    for (int i = 0; i<n; i++)
        scanf("%d", &numbers[i]);
    for (int i = 0; i<n - 1; i++)
    {
        int sum = 0;
        for (int j = i + 1; j<n; j++)
            sum += numbers[j];
        if ((float)(numbers[i]) > (sum / (n - i - 1)))
            printf("%d\n", numbers[i]);
    }
    printf("%d",numbers[n-1]);

}

Pseudocode:
Step1. get the number n
Step2. declare numbers as an integer array to store n numbers
Step3. get the n numbers using a for loop and storing the numbers in their respective positions
Step4. repeat till i goes from 0 to n-1
Step4.1 declare sum as an integer
Step4.2 repeat till j goes from i+1 to n-1
Step4.2.1 assign sum as the sum of sum and numbers[j]
Step4.3 if the numbers[i] is greater than the average of the numbers to the right of it then display the number else continue
Step5. display the last number
Step6. End

No comments:

Post a Comment

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...