c program to print the median of list of numbers in an array

Description


This program finds the median of n numbers in an array. Median is the middle most element in an array. If the number of elements in an array is even, then median is the average of the two middle elements. For example if an array consist of 10 elements then the median is the average of 5th and 6th element.


Example


Array1

5  6  8  9  10

Here the total number of elements is odd, so median is the middle most element 8.

Array2

9  5  4  6  2  1  4  6  3  7

Here the number of elements is even, so median is (2+1)/2 =1.5

Program

 
// c program to find median
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
{
int a[10],i,n;
float m;
printf("Enter the size of the array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
if(n%2==0)
{
m=(float(a[(n/2)-1])+float(a[n/2]))/2;
}
else
{
m=a[n/2];
}
printf("\n Median is %f",m);
getch();
}

c program to print the Pascals triangle

Description


This program prints the pascal triangle. Below is an example for pascal triangle. In the first row we only write 1, all the other rows start and end with a 1, the in between numbers are calculated by adding the numbers in the previous row. Eg - in the third row 2 is obtained by adding the two 1's in the previous row. In the fourth row the first 3 is obtained by adding 1 and 2, and the second 3 is obtained by adding 2 and 1 and so on.

Example

1
11
121
1331
14641

Program


#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

{

int a[50][50]={0},i,j,n;

printf("Enter the value for n");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("\n");

for(j=0;j<n;j++)

{

if(i==j|j==0)

{

a[i][j]=1;

}

if(i>2)

{

if(j<i)

{

a[i][j]=a[i-1][j-1]+a[i-1][j];

}}}}

for(i=0;i<n;i++)

{

printf("\n");

for(j=0;j<i;j++)

{

printf("%d",a[i][j]);

}}

getch();

}


c program to print the multiplication table from 1 to 10

Description


This program prints the multiplication table of numbers from 1 to 10.  The program uses two for loops. The first for loop increments numbers from 1 to 10, at each increment, the second for loop prints the multiplication table of the corresponding number.

Program


#include<stdio.h>

#include<conio.h>

void main()

{

int k;

for(int i=1;i<=10;i++)

{

for(int j=1;j<=10;j++)

{

k=i*j;

printf("%d*%d=%d\t",i,j,k);

}

printf("\n");

}

getch();

}