Friday, March 7, 2014

WRITE A FUNCTION TO CALCULATE THE BINARY EQUIVALENT OF A NUMBER WITH DECIMAL BASE USING RECURSION

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int binary(int*,int,int);
int num,a[10],i=0;
clrscr();
printf("enter the number\n");
scanf("%d",&num);
i=binary(a,num,i);
i--;
printf("the binary equivalennt of %d is ",num);
for(;i>=0;i--)
printf("%d",a[i]);
getch();
}

int binary(int*p,int n,int i)
{
if(n>=1)
{
*(p+i)=n%2;
i++;
binary(p,n/2,i);
}
else
return(i);
}

WRITE A FUNCTION TO CALCULATE THE BINARY EQUIVALENT OF A NUMBER IN DECIMAL BASE

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int binary(int*,int);
int num,a[10],i;
clrscr();
printf("enter the number\n");
scanf("%d",&num);
i=binary(a,num);
i--;
printf("the binary equivalennt of %d is ",num);
for(;i>=0;i--)
printf("%d",a[i]);
getch();
}

int binary(int*p,int n)
{
int i;
for(i=0;n>=1;i++)
{
*(p+i)=n%2;
n=n/2;
}
return(i);
}

WRITE A FUNCTION TO CALCULATE THE SUM OF DIGITS OF A NUMBER USING RECURSION

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

int sum(int n)
{
int s=0;
if(n>0)
{
s=s+n%10;
return(s+sum(n/10));
}
else
return(0);
}

WRITE A FUNCTION TO CALCULATE THE SUM OF DIGITS OF A NUMBER

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

int sum(int n)
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return(s);
}

WRITE A FUNCTION POWER TO CALCULATE A FLOATING POINT NUMBER TO BE RAISED TO AN INTEGER POWER

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
void power(float,int);
int p;
float n;
clrscr();
printf("enter the number\n");
scanf("%f",&n);
printf("\nenter the power\n");
scanf("%d",&p);
power(n,p);
getch();
}
void power(float n,int p)
{
float r=1;
int i;
for(i=1;i<=p;i++)
{
r=r*n;
}
printf("%f to the power %d is %f",n,p,r);
}

WRITE A FUNCTION TO CALCULATE AND DISPLAY REAL ROOTS OF A QUADRATIC EQUATION

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
void root(int,int,int);
int a,b,c,d;
clrscr();
printf("enter the values of constants\n");
scanf("%d%d%d",&a,&b,&c);
d=(b*b-4*a*c);
if(d>0)
{
printf("the real roots of equation are\n");
root(a,b,d);
}
else
printf("roots are imaginary");
getch();
}
void root(int a,int b,int d)
{
int r1,r2;
r1=(-b+sqrt(d))/2*a;
r2=(-b-sqrt(d))/2*a;
printf("r1=%d r2=%d",r1,r2);
}

WRITE A FUNCTION TO CALCULATE FACTORIAL OF A NUMBER USING RECURSION

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

int fact(int num)
{
if(num==1||num==0)
return(1);
else
return(num*fact(num-1));
}

WRITE A FUNCTION TO CONVERT A LOWERCASE CHARACTER TO UPPERCASE

#include<conio.h>
#include<stdio.h>
void main()
{
char convert(char);
char ch,r;
clrscr();
printf("enter the character\n");
scanf("%c",&ch);
r=convert(ch);
printf("the uppercase conversion of %c is %c",ch,r);
getch();
}

char convert(char ch)
{
return (ch-32);
}

WRITE A FUNCTION TO CALCULATE FACTORIAL OF A NUMBER

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

int fact(int n)
{
int f=1,i;
for(i=1;i<=n;i++)
f=f*i;
return f;
}

WRITE A FUNCTION TO FIND LARGEST OF TWO VALUES AND PRINT THE RESULT IN CALLING FUNCTION

#include<conio.h>
#include<stdio.h>
void main()
{
int large(int,int);
int a,b,r;
clrscr();
printf("enter the numbers\n");
scanf("%d%d",&a,&b);
r=large(a,b);
printf("the largest value is %d",r);
getch();
}

int large(int a,int b)
{
if(a>b)
return a;
else
return b;
}

WRITE A FUNCTION THAT ACCEPTS MARKS OBTAINED BY A STUDENT IN 3 SUBJECTS AND RETURNS THE AVERAGE AND PERCENTAGE OF THE MARKS

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
void cal(int*,float*,float*);
int ar[3],i;
float avg,per;
clrscr();
printf("enter the marks out of 50\n");
for(i=0;i<3;i++)
{
scanf("%d",&ar[i]);
}
cal(ar,&avg,&per);
printf("\nthe avg of marks is %f",avg);
printf("\nthe percentage of marks is %f",per);
getch();
}

void cal(int *a,float *avg,float*per)
{
int i,s=0;
for(i=0;i<3;i++)
{
s=s+*(a+i);
}
*avg=s/3;
*per=s*100/150;
}




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);
}