C PROGRAMS : FUNCTION

C PROGRAMS : FUNCTION
1.Program to find sum of two numbers using function sum(int,int) that returns sum value
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int sum( int a, int b )
{
return a+b;
}

int main()
{
 
// Declaring variable x, y to take the input of two numbers
int x, y;

// Inputting value
printf("Enter the value of x:");
scanf("%d", &x);
printf("Enter the value of y:");
scanf("%d", &y);

// Displaying value by calling sum function
 printf("Sum of %d and %d : %d", x, y, sum(x, y));
 
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to print the area of a rectangle
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

void area()
{
 // Declaring variable l=length, b = breadth
 int l,b;
 
 // Inputting length and breadth 
 printf("Enter length of rectangle : ");
 scanf("%d",&l);
 
 printf("Enter breadth of rectangle : ");
 scanf("%d",&b);
 
 // Calculating and printing area
 printf("Area of rectangle %d * %d = %d ", l, b, l*b);
 
}

int main()
{
 area();
 
 getch(); // Linux user - Remove this

 return 0;
}

3. Program to find the factorial of two numbers and add them and print the result
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// Function prototype 
int factorial(int);

int main()
{
 // Decalring variable n1, n2 to input two number
 int n1, n2;
 
 // Declaring variable f1 and f2 to store the factoial of n1, n2 respectively
 int f1, f2;
 
 // Declaring variable sum to store the sum of f1 and f2
 int sum=0;
 
 // Inputting two numbers
 printf("Enter first number : ");
 scanf("%d", &n1);
 
 printf("Enter second number : ");
 scanf("%d", &n2);
 
 // Calling function factorial
 f1 = factorial(n1);
 f2 = factorial(n2);
 
 // Calculating sum and displaying result
 sum = f1 + f2;

 printf("Factorial of first number %d = %d \n", n1, f1);
 printf("Factorial of second number %d = %d \n", n2, f2);
 printf("Sum of factorial of two numbers %d and %d = %d ",n1, n2, sum);
  
 getch(); // Linux user - Remove this

 return 0;
}

int factorial(int n)
{
 // Declaring variable "i" to iterate loop
 int i;
 
 // Declaring variabl fact to store factorial
 int fact=1;
 
 // Calculating factorial
 for(i=1;i<=n;i++)
 {
  fact = fact * i;
 }
 
 // Returning factorial
 return (fact);
}

4. Program to implement functions that returns HCF OR GCD and LCM of two numbers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int calculatehcf(int x, int y)
{
 // Declaring varibale "i' to iterate loop
 int i;
 
 // Declaring variable hcf to store calculated hcf
 int hcf=1;
 
 for(i=1; i<=x;i++)
 {
  if(x%i==0 && y%i==0)
  {
   hcf=i;
  }
 }
 
 // Returnig hcf
 return hcf;
}

int calculatelcm(int x, int y, int h) // h is passed value of hcf
{
 // Declaring variable lcm to store calculated lcm
 int lcm=1;
 
 // lcm is calculated by formula : hcf * lcm =1
 lcm = (x*y)/h;
 
 // Returning lcm
 return lcm;
}

int main()
{
 // Declaring variable "a" and "b" to input two numbers
 int a, b;
 
 /* Declaring variable hcf=to hold hcf returned to it, 
 lcm=to hold lcm returned to it */
 int hcf, lcm;
 
 // Inputting two numbers
 printf("Enter first number : ");
 scanf("%d", &a);
 
 printf("Enter first number : ");
 scanf("%d", &b);
 
 // Calling function calculatehcf
 hcf=calculatehcf(a, b);
 
 // Calling function calculatelcm
 lcm=calculatelcm(a, b, hcf);
 
 // Printing hcf and lcm
 printf("hcf = %d ",hcf);
 printf("\nlcm = %d ",lcm);
 
 getch(); // Linux user - Remove this

 return 0;
}

5. Implement a function that takes two values of integer type and interchange them
// 1. without using Third variable. You may use arithmetic operators
// 2. With the use of third variable. You should not use arithmetic operator

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

void swap_without_third_variable(int a, int b)
{
 // performing swapping
 a=a+b;
 b=a-b;
 a=a-b;
 
 // Printing interchange value
 printf("\nInterchange value without use of third variable : a=%d, b=%d", a, b);
 
}

void swap_with_third_variable(int a, int b)
{
 // Declaring varibale swap to help in interchanging value
 int swap;
 
 // Performing swapping
 swap=a;
 a=b;
 b=swap;
 
 // Printing interchange value
 printf("\nInterchange value without use of third variable : a=%d, b=%d", a, b);
}

int main()
{
 // Declaring variable a, b to input two numbers
 int a, b;
 
 // Inputting number
 printf("Enter value of a : ");
 scanf("%d",&a);
 
 printf("Enter value of b : ");
 scanf("%d",&b);
 
 // Printing original value
 printf("Orginal value a = %d, b = %d ",a, b);
 
 // Calling functions
 swap_without_third_variable(a, b);
 swap_with_third_variable(a, b);
 
 getch(); // Linux user - Remove this

 return 0;
}

Share this

Related Posts

Previous
Next Post »