Lab 8
Lab 8
LAB OBJECTIVE:
The objective of this lab is to help students understand the basic concept of Functions and their
importance in C programming language. Students will learn to implement different programming
problems using functions
INTRODUCTION:
A computer program cannot handle all the tasks by itself. Instead, it requests other program like entities
—called ‘functions’ in C—to get its tasks done.
b) Improves the reusability of the code, same function can be used in any program rather than writing
the same code from scratch.
c) Reduces the size of the code, duplicate set of statements are replaced by function calls.
A function is a block of statements that performs a specific task. There are two options for you.
a) Use the same set of statements every time you want to perform the task
b) Create a function to perform that task, and just call it every time you need to perform that task.
IN-LAB TASKS
Task# Input the length and width of the rectangle and radius of the circle. Write two functions to
calculate area of rectangle and area of circle.
Code:
#include<stdio.h>
int rect(int x,int y);
float circle(int r);
int main()
{
int a,b;
printf("Enter value of x & y:");
scanf("%d%d",&a,&b);
int area=rect(a,b); //area=area of rectangle
printf("\n Area of rectangle is:%d",area);
int k; //radius=k;
printf("\n Enter a radius:");
scanf("%d",&k);
float result=circle(k);
printf("\n Area of circle is:%f",result);
return 0;
}
int rect(int x,int y)
{
int z=x*y;
return z;
}
float circle(int r)
{
float ar=3.14*r*r;
return ar;
}
console window
Task# A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of
digits of the 5-digit number.
Code:
#include<stdio.h>
int sum(int num);
int main()
{
int x;
printf("Enter a number;");
scanf("%d",&x);
int add=sum(x);
printf("\n The sum of number is;%d",add);
return 0;
}
int sum(int num)
{
int rem,sum=0;
while(num)
{
rem=num%10; //rem=reminder thats the last digit stores in it.
sum=sum+rem;
num=num/10;
}
return sum;
}
console window
Task#
Write a C program to add, subtract, multiply and divide two numbers using functions
Code:
#include<stdio.h>
int sum(int a,int b);
int minus(int c,int d);
int div(int e,int f);
int mul(int g,int h);
int main()
{
int i,j;
printf("Enter a number:");
scanf("%d%d",&i,&j);
int add=sum(i,j);
printf("\n SUM IS=%d",add);
int l,m;
printf("\n Enter a number:");
scanf("%d%d",&l,&m);
int subtract=minus(l,m);
printf("\n Subtraction of number is;%d",subtract);
int o,p;
printf("\n Enter a number:");
scanf("%d%d",&o,&p);
int pro=mul(o,p);
printf("\n product is ;%d",pro);
int r,s;
printf("\n Enter a number;");
scanf("%d%d",&r,&s);
int division=div(r,s);
printf("\n division is;%d",division);
return 0;
}
int sum(int a,int b)
{
int k=a+b;
return k;
}
int minus(int c,int d)
{
int n=c-d;
return n;
}
int mul(int g,int h)
{
int q=g*h;
return q;
}
int div(int e,int f)
{
int t=e/f;
return t;
}
console window
Task# Write a program to find largest between two numbers by using functions.
Code:
#include<stdio.h>
int big(int x,int y);
int main()
{
int a,b;
printf("Enter a value:");
scanf("%d%d",&a,&b);
int bigger=big(a,b);
printf("\n %d is larger",bigger);
return 0;
}
int big(int x,int y)
{
return (x>y?x:y);
}
console window
Task# A positive integer is input through the keyboard. Write a program to obtain the prime factors of
the number through a function.
Code:
#include<stdio.h>
int prime(int num);
int main()
{
int x;
printf("Enter a number:");
scanf("%d",&x);
int factor=prime(x);
printf("\n prime factors are:%d",factor);
return 0;
}
int prime(int num)
{
int count;
for (count=2;num>1;count++)
{
while(num%count==0)
{
printf("\t%d",count);
num=num/count;
}
}
return num;
}
console window
POST-LAB TASKS
Task#
Code:
console window
Task#
Code:
console window
Task#
Code:
console window
Conclusion: