0% found this document useful (0 votes)
2 views8 pages

Lab 8

This lab focuses on teaching students the concept and importance of functions in C programming. Students will implement various mathematical problems using functions, including calculating areas, summing digits, and performing arithmetic operations. The lab emphasizes code readability, reusability, and efficiency through the use of functions.

Uploaded by

abdul.59wa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Lab 8

This lab focuses on teaching students the concept and importance of functions in C programming. Students will implement various mathematical problems using functions, including calculating areas, summing digits, and performing arithmetic operations. The lab emphasizes code readability, reusability, and efficiency through the use of functions.

Uploaded by

abdul.59wa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB#

Solving Simple Mathematical Problems with Functions

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.

Functions are used because of following reasons

a) To improve the readability of code.

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:

NOTE:font: Times New Roman, Heading font 14, statement font 12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy