C Program to print all the numbers which are less than given key element from a given array.

Get an element and print the elements of array which is less than the element.

Sample Input 1:

5 5 7 9 3 1 4

Sample Output 1:

3 1

Sample Input 2:

5 5 7 9 3 1 8

Sample Output 2:

5 7 3 1

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#include<stdio.h> int main() { //write your code here }

Program or Solution

				
			
					
#include <stdio.h>
#include <stdlib.h>


int main(void)

{
	
	int *a,n,i,pos=-1,element;

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

	a=malloc(sizeof(int)*n);
	printf("Enter %d Elements:",n);
	for(i=0;i<n;i++)

	{

		scanf("%d",&a[i]);

	}

	printf("Enter a number:");
	scanf("%d",&element);

	printf("Elements less than %d are:\n",element);
	for(i=0;i<n;i++)
	
	{

		if(a[i]<element)
		{
			printf("%d ",a[i]);	
		}
	}

	
	return 0;

}
			
				
			

Program Explanation

i is initialized to 0 and incremented by 1 at each iteration of both the for loops. 

First for loop reads n input numbers from user and stores them in array a[] from location 0 to n-1 using second for loop visit each location serially from 1 to n-1.

if the value located in a position is lesser than the element given by user, then print the value using printf statement.

Comments