Wednesday, March 19, 2014

C-PROGRAMMING

Some of the C-PROGRAMMING Code:
This is my lab report


Lab No. 07
OBJECT
Functions in C-Language programming
THEORY
Functions are used normally in those programs where some specific work is required to be done repeatedly and looping fails to do the same.
Three things are necessary while using a function.
i. Declaring a function or prototype:
The general structure of a function declaration is as follows:
return_type function_name(arguments);
Before defining a function, it is required to declare the function i.e. to specify the function prototype. A function declaration is followed by a semicolon ‘ ;’. Unlike the function definition only data type are to be mentioned for arguments in the function declaration.
ii. Calling a function:
The function call is made as follows:
return_type = function_name(arguments);
ii. Defining a function:
All the statements or the operations to be performed by a function are given in the function definition which is normally given at the end of the program outside the main.
Function is defined as follows
return_type function_name(arguments)
{
Statements;
}
There are certain functions that you have already used e.g:getche( ), clrscr( ), printf( ), scanf( ) etc.
There are four types of functions depending on the return type and arguments:
• Functions that take nothing as argument and return nothing.
• Functions that take arguments but return nothing.
• Functions that do not take arguments but return something.
• Functions that take arguments and return something.
A function that returns nothing must have the return type “void”. If nothing is specified then the return type is considered as “int”.


EXERCISE
1. Write a program to print the find the sum of the given series, take first 8 terms
      Sum = 1! +2! +3! +4! +……..
Ans:
#include<stdio.h>
#include<conio.h>
long int fact(int);
int main()                               
{
 long int n;
 clrscr();
 printf("Enter any number=");
 scanf("%ld",&n);
 fact(n);
 return 0;
}
long int fact(int n)
{
long int sum=0,f;
  for(int i=1;i<=n;i++)
  {
   f=1;
   for(int j=1;j<=i;j++)
   {
    f=f*j;
   }                                                                                                          
   sum=sum+f;
  }
  printf("\nsum=%ld",sum);
  getch();
  return 0;
 }
2. Write a program to find
a. Surface area (A=4pr 2 )
b. volume(v=4/3 p 3 )
of a sphere using functions make a function for finding powers of radius.
 Ans:

A.
    #include<stdio.h>
     #include<conio.h>
     #define pi 3.14
    int radius(int);
    void main()
   {
 float r,dr,a;
 clrscr();
 printf("Enter the radius of sphere r=");         
 scanf("%f",&r);
 dr=radius(r);
 a=4*pi*dr;
 printf("\nArea of sphere=%f",a);
 getch();
}
int radius(int r)
{
float dr;
dr=r*r;
return (dr);
}

B.
 #include<stdio.h>
#include<conio.h>
#define pi 3.14
int radius(int);                                                                         
void main()
{
 float r,dr,a;
 clrscr();
 printf("Enter the radius of sphere r=");
 scanf("%f",&r);
 dr=radius(r);
 a=((4*pi*dr)/3);
printf("\nVolume of sphere=%f",a);
 getch();
}
int radius(int r)
{
float dr;
dr=r*r*r;
return (dr);
}



3. Write a program using functions to evaluate up to n terms
sin (x) = x – x3/3! + x5/5! – x7/7! …………………….. – xn/n!
Ans:

#include<stdio.h>
#include<conio.h>
#include<math.h>
int sin(float,float);
int main()
{
 float s,x,n;
 printf("Enter the value of x=");
 scanf("%f",&x);
 printf("\n Enter the value of n=");
 scanf("%f",&n);
 s=sin(x,n);
 printf("\n Sin(%f)=%f",x,s);
 getch();
 return 0;
}
int sin(float x,float n)
{
 int sum=0,k=1,f;
 for(int i=1;i<=n;i+2)
 {
  f=1;
  for(int j=1;j<=i;j++)
  {
   f=f*j;
  }
  if(k%2==0)
  {
  sum=(sum-(pow(x,i)/f));
  } else
  sum=(sum+(pow(x,i)/f));
  {
  k++;
 }
 }
  return (sum);
}


4. Write a function to compute the distance between two points and use it to develop another function that will compute the area of the triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3). Use these functions to develop a function which returns a value 1 if the point (x, y) lines inside the triangle ABC, otherwise a value 0.
Ans:

#include<stdio.h>
#include<conio.h>
#include<math.h>
int dis(int,int,int,int);
int area(int,int,int,int,int,int);
int check(int,int,int,int);
int main()
{
int x1,x2,y1,y2,x3,y3,d1,d2,d3,s,a,x,y,a1,a2,a3;
clrscr();
 printf("Enter a first poins");
 printf("(x1,y1)=");              
 scanf("%d %d",&x1,&y1);
 printf("\nenter a second point");
 printf("(x2,y2)=");
 scanf("%d %d",&x2,&y2);
 printf("\nEnter a third point");
 printf("(x3,y3)=");
 scanf("%d %d",&x3,y3);
 d1=dis(x1,y1,x2,y2);
 d2=dis(x1,y1,x3,y3);
 d3=dis(x2,y2,x3,y3);
 printf("\nd1=%d",d1);
 printf("\nd2=%d",d2);
 printf("\nd3=%d",d3);
 printf("\ns=%d",s);
 a=area(x1,y1,x2,y2,x3,y3);                                                  
 printf("\narea=%d",a);
 printf("\n Enter another point (x,y)=");
 scanf("%d %d",&x,&y);
 a1=area(x,y,x2,y2,x3,y3);
 a2=area(x1,y1,x,y,x3,y3);
 a3=area(x1,y1,x2,y2,x,y);
 check(a,a1,a2,a3);
 getch();
 return 0;
}
 int dis(int a,int b,int c,int d )
 {
   int l;
   l=sqrt(((c-a)*(c-a))+((d-b)*(d-b)));
   return (l);
  }
 int area(int a,int b,int c,int d,int f,int g)
 {
int ar;
 ar=(a*(d-g))+(c*(g-b))+(f*(b-d));
 return (ar);
}
int check(int a,int a1,int a2,int a3)
{
   int d;
   d=(a1+a2+a3);
   if(a==d)
   {
   printf("\n the point is inside the triangle");
   return 1;
   } else
   {
   printf("\n the point is outside the triangle");
   return 0;
   }
}

5. Given three variables x, y, z write a function to circularly shift their values to right. In other words if x = 5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10 after circular shift y = 5, z = 8 and x = 10. Call the function with variables a, b, c to circularly shift values.
Ans:

#include<stdio.h>
#include<conio.h>
void swap(int,int,int);
int main()
{
 int x,y,z;
 clrscr();
 printf("Enter three number");
 printf("\nx=");
 scanf("%d",&x);
 printf("\ny=");
 scanf("%d",&y);
 printf("\nz=");
 scanf("%d",&z);
 printf("\nthe value before swap"); 
 printf("\nx=%d",x);
 printf("\ny=%d",y);
 printf("\nz=%d",z);
 swap(x,y,z);
 printf("\nthe value after swap");
 printf("\nx=%d",x);
 printf("\ny=%d",y);
 printf("\nz=%d",z);
 getch();
 return 0;
 }
 void swap(int a,int b,int c)                                                                          
 {
   int temp;
   temp=a;
   a=b;
   b=c;
   c=temp;
 }

6. Make a function that takes the length of a side as argument and prints a square with that length on the sides. You can use #. For example if the side length is 4:
####
####
####
####
Ans:

#include<stdio.h>
#include<conio.h>                   
void square(int);
void main()
{
 int l;
 clrscr();
 printf("Enter the length of sequre=");
 scanf("%d",&l);
 printf("\n");
 square(l);
}
void square(int a)
{
 for(int i=1;i<=a;i++)
 {
   for(int j=1;j<=a;j++)
   {
     printf("#");
   }                                                                                                                  

   printf("\n");
  }
 getch();
}


7. Make a function
void drawTriangle(int lengthOfbase, int rightAngleUpLeft)
that prints a right-angled triangle with the right-angle up to the left if the parameter rightAngleUpLeft is 1 and down to the left if the value is 0. Examples of two triangles with base 4 and different values in rightAngleUpLeft:
####
###
##
#

#
##
###
####
Ans:

#include<stdio.h>
#include<conio.h>
void drawTriangle(int,int);
void main()
{
 int b,a;
 clrscr();
printf("Enter the base of the triangle b=");
printf("\n The right angle triangle with base %d and right angle of UP left % is\n",length Of base, right Angle Up Left);

for(int j=1;j<=i;j++)
  {
   printf("#");
  }
  printf("\n");
 }
 for(int l=1;l<=lengthOfbase;l++)
{
scanf("%d",&b);
 printf("Enter the angle of tirngle of up left a=");
 scanf("%d",&a);
 drawTriangle(b,a);
}
void draw Triangle(int length Of base,int right Angle Up Left)
{

for(int i=lengthOfbase;i>=1;i--)
 {

for(int k=1;k<=l;k++)
  {
   printf("#");
  }
  printf("\n");
 }
 getch();
}

 


No comments:

Post a Comment