0% found this document useful (0 votes)
26 views

Lab Function

The document contains 8 code snippets that demonstrate various functions in C programming. The snippets show functions to: 1) Calculate sum, difference, product and quotient of two numbers. 2) Swap two numbers using call by value and call by reference. 3) Return the area and perimeter of a rectangle. 4) Calculate x to the power of y without a standard function. 5) Check if a number is a perfect number. 6) Display all perfect numbers between 1-50. 7) Display all prime numbers between 1-100.

Uploaded by

Swapnal Swami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lab Function

The document contains 8 code snippets that demonstrate various functions in C programming. The snippets show functions to: 1) Calculate sum, difference, product and quotient of two numbers. 2) Swap two numbers using call by value and call by reference. 3) Return the area and perimeter of a rectangle. 4) Calculate x to the power of y without a standard function. 5) Check if a number is a perfect number. 6) Display all perfect numbers between 1-50. 7) Display all prime numbers between 1-100.

Uploaded by

Swapnal Swami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

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>

void swap(int var1, int var2)


{
int temp = var1;
var1 = var2;
var2 = temp;
}

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

3. Write C Program to swap two number using call by reference.

void swap(int *var1, int *var2)


{
int temp = *var1;
*var1 = *var2;
*var2 = temp;
}

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

You might also like