Friday, March 7, 2014

WRITE A FUNCTION THAT ACCEPTS 5 INTEGERS AND RETURNS SUM,AVERAGE AND STANDARD DEVIATION OF TEHESE NUMBERS

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
void calculate(int[],int,int*,float*,float*);
int ar[10],i,s=0,n;
float sd=0,avg;
clrscr();
printf("enter the limit of array\n");
scanf("%d",&n);
printf("enter the elements of an array\n");
for(i=0;i<n;i++)
scanf("%d",&ar[i]);
calculate(ar,n,&s,&avg,&sd);
printf("the sum of numbers is %d\n",s);
printf("\nthe average of numbers is %f\n",avg);
printf("the standard deviation is %f\n",sd);
getch();
}

void calculate(int a[],int n,int *sum,float*avg,float*s_d)
{
int i;
float t;
for(i=0;i<n;i++)
{
*sum=*sum+a[i];
}
*avg=*sum/n;
for(i=0;i<n;i++)
{
t=*avg-a[i];
*s_d=*s_d+pow(t,2);
}
*s_d=*s_d/(n-1);
*s_d=sqrt(*s_d);
}



WRITE A FUNCTION TO FIND VOLUME OF A SPHERE

#include<conio.h>
#include<stdio.h>
#define PI 3.14
void main()
{
float volume(int);
int r;
float vol;
clrscr();
printf("enter the radius\n");
scanf("%d",&r);
vol=volume(r);
printf("the volume of sphere is %f",vol);
getch();
}

float volume(int r)
{
float v;
v=(4*PI*r*r*r)/3;
return(v);
}

WRITE A FUNCTION TO FIND AREA OF A SPHERE

#include<conio.h>
#include<stdio.h>
#define PI 3.14
void main()
{
void area(int);
int r;
clrscr();
printf("enter the radius\n");
scanf("%d",&r);
area(r);
getch();
}

void area(int r)
{
float a;
a=4*PI*r*r;
printf("the area of sphere is %f",a);
}

WRITE A FUNCTION POWER THAT COMPUTE X RAISED TO THE POWER Y AND RETURN DOUBLE TYPE VALUE

#include<conio.h>
#include<stdio.h>
void main()
{
double power(int,int);
int n,p;
double r;
clrscr();
printf("enter the no and power\n");
scanf("%d%d",&n,&p);
r=power(n,p);
printf("%d to the power %d is %lf",n,p,r);
getch();
}

double power(int n,int p)
{
//double r=1;
int i;
//for(i=1;i<=p;i++)
for(i=1;i<p;i++)
{
//r=r*n;
n=n*n;
}
//return(r);
return(n);
}

WRITE A FUNCTION TO CALCULATE SUM OF FIRST N INTEGERS

#include<conio.h>
#include<stdio.h>
void main()
{
void sum(int);
int n;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
sum(n);
getch();
}

void sum(int n)
{
int i,s=0;
for(i=0;i<=n;i++)
s=s+i;
printf("the sum of first %d nos is %d",n,s);
}

WRITE A FUNCTION WHICH ACCEPTS TWO INTEGERS AND DETERMINE THE LARGEST VALUE WHICH IS THEN DISPLAYED IN FUNCTION ITSELF

#include<conio.h>
#include<stdio.h>
void main()
{
void min_max(int,int);
int a,b;
clrscr();
printf("enter two nos\n");
scanf("%d%d",&a,&b);
min_max(a,b);
getch();
}

void min_max(int a,int b)
{
if(a>b)
printf("a= %d is grater than b= %d",a,b);
else
printf("b= %d is grater than a= %d",b,a);
}

Saturday, December 28, 2013

C Program to find HCF/GCD of two numbers

Definition of HCF (Highest common factor):

HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest positive numbers which can divide both numbers without any remainder.  For example HCF of two numbers 4 and 8 is 2 since 2 is the largest positive number which can dived 4 as well as 8 without a remainder. 

Logic of HCF or GCD of any two numbers:

In HCF we try to find any largest number which can divide both the number.
For example: HCF or GCD of 20 and 30
Both number 20 and 30 are divisible by 1, 2,5,10.

HCF=max (1, 2, 3, 4, 10) =10

#include<stdio.h>
int main(){
    int x,y,m,i;
    printf("Insert any two number: ");
    scanf("%d%d",&x,&y);
    if(x>y)
         m=y;
    else
         m=x;
    for(i=m;i>=1;i--)
    {
         if(x%i==0 && y%i==0)
        {
             printf("\nHCF of two number is : %d",i) ;
             break;
         }
    }
    return 0;
}