Sunday, December 12, 2010

Prime Number or Not

//PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS PRIME OR NOT
#include
#include
int check(int);
void main()
{
int num,ret;
printf("Enter the number : ");
scanf("%d",&num);
ret=check(num);
if(ret==1)
printf("%d is not Prime\n",num);
else if(ret==0)
printf("%d is Prime\n",num);
else
printf("It is neither prime nor composite\n");
getch();
}

int check(int n)
{
int i;
if(n==0)
return 1;
if(n==1)
return -1;
for(i=2;i {
if(n%i==0)
return 1;
}
return 0;
}

Find Mean ,Median And Mode of a set of Numbers

//PROGRAM TO FIND MEAN,MEDIAN AND MODE OF N GIVEN SET OF NUMBERS.
#include
#include
void main()
{
float mean,median;
int mode,sum=0,temp;
int n,i,j,pos,num[20];

int highest=0,repeat=0;
int last_num;

clrscr();
printf("\nEnter the value of n:");
scanf("%d",&n);
printf("\nEnter the %d numbers:\n",n);
for(i=0;inum[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
if(n%2==1)
{
pos=(n+1)/2;
median=num[pos];
printf("\n\nThe Median is %f",median);
}
else
{
pos=n/2;
median=(num[pos]+num[pos+1])/2;
printf("\n\nThe Median is %f",median);
}

last_num=num[0];
for(i=1;ihighest)
{
highest=repeat;
printf("%d",repeat);
mode=num[i];
}
}
else //the number was not repeated
repeat=0;
last_num=num[i];
}

printf("\n\nThe Mode is %f",mode);
getch();
}