Lab Function
Lab Function
Write C program to read two integer numbers and find their sum, difference,
product and quotient using separate function.
#include<stdio.h>
void sum(int x,int y)
{
int z;
z=x+y;
printf("Sum : %d\n",z);
}
void diff(int x,int y)
{
int z;
z=x-y;
printf("Difference : %d\n",z);
}
void mult(int x,int y)
{
int z;
z=x*y;
printf("Product : %d\n",z);
}
void div(int x,int y)
{
float z;
z=x/(float)y;
printf("Division : %.2f\n",z);
}
int main()
{
int a,b;
printf("Enter Two Numbers\n");
printf("---------------------------\n");
printf("Enter First Number : ");
scanf("%d", &a);
printf("\nEnter Second Number : ");
scanf("%d",&b);
printf("---------------------------\n");
sum(a,b);
diff(a,b);
mult(a,b);
div(a,b);
return 0;
}
2. Write C Program to swap two number using call by value.
#include <stdio.h>
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and var2 is: %d, %d\n",var1, var2);
swap(var1, var2);
printf("After swap Value of var1 and var2 is: %d, %d",var1, var2);
return 0;
}
int main()
{
int var1 = 3, var2 = 2;
printf("Before swap Value of var1 and var2 is: %d, %d\n",var1, var2);
swap(&var1, &var2);
printf("After swap Value of var1 and var2 is: %d, %d",var1, var2);
return 0;
}
4. Design two functions area & perimeter which will return area and perimeter of a
rectangle.
#include<stdio.h>
int rect_area(int l,int w)
{
int area;
area=l*w;
return area;
}
int rect_perimeter(int l,int w)
{
int p;
p=2*(l+w);
return p;
}
int main()
{
int a,b,x,y;
printf("Enter Length of Rectangle : ");
scanf("%d",&a);
printf("\nEnter Width of Rectangle : ");
scanf("%d",&b);
x=rect_area(a,b); // calling function rect_area
y=rect_perimeter(a,b); // calling function perimeter
printf("\nArea of Rectangle = %d\n\nPerimeter of Rectangle = %d",x,y);
return 0;
}
5. Write a C program to calculate the x to the power y without using standard
function.
#include<stdio.h>
void power(int x,int y)
{
int ans = 1, i;
for(i=1; i<=y; i++)
ans = ans*x;
printf(" %d^%d : %d", x, y, ans);
}
int main()
{
int x,y;
printf("/*Calcualate : x^y*/\n");
printf("\nEnter Value of x : ");
scanf("%d", &x);
printf("\nEnter Value of y : ");
scanf("%d", &y);
printf("----------------------\n");
power(x,y);
return 0;
}
6. Write a C program to accept a number and check whether it is perfect or
not.(using function)
Solution:
Perfect Number - When sum of all positive divisors excluding that number is equal
to that number.
Ex. 6 is the smallest perfect number.
Divisor of 6 are 1, 2, 3. Sum of divisors is : 1+2+3 = 6.
#include<stdio.h>
void perfect(int n)
{
int i=1,sum=0;
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("\n%d is a Perfect Number.",i);
else
printf("\n%d is NOT a Perfect Number",i);
}
int main()
{
int a;
printf("Enter Number : ");
scanf("%d",&a);
perfect(a);
return 0;
}
7. Write a C program to display all perfect numbers between 1 to 50 by using
function.
#include<stdio.h>
void perfect()
{
int n,i,sum;
for(n=1;n<=50;n++)
{
i=1;
sum = 0;
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("\t%d",n);
}
}
int main()
{
printf("Perfect Numbers Between 1 to 50\n\n");
perfect();
return 0;
}
8. Write a C program to display all prime numbers between 1 to 100 by using
function.
#include<stdio.h>
void prime()
{
int i=2,j,p;
while(i<=100)
{
p=1; // Initially p=1
for(j=2;j<i;j++)
{
/*Here, we check if it is divisible by any other number
other than 1 or itself*/
if( i%j == 0)
{
p=0;
}
}
if(p)
{
printf("\t%d\n",i);
}
i++;
}
}
int main()
{
printf("Prime Numbers Between 1 to 100\n");
printf("--------------------------------\n");
prime();
return 0;
}