Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts
C PROGRAMS : FUNCTION

C PROGRAMS : FUNCTION

C PROGRAMS : FUNCTION
6. Write a function that receives a positive integer as input and returns the leading digit in its decimal representation
// For example : the leading digit of 4567 is 4

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


int leading_digit(int n)
{
 // Declaring variable d=digit to store the digits extracted from number
 int d;
 
 // Performing digit extraction
 while(n)
 {
  d=n%10;
  n=n/10;
 }
 
 // Returning leading digit
 return (d);
}

int main()
{
 // Declaring variable "n" to input a number
 int n;
 
 // Inputting number
 printf("Enter any positve integer number : ");
 scanf("%d",&n);
 
 // Calling function and printing result
 printf("Leading digit of number : %d = %d ",n, leading_digit(n));
 getch(); // Linux user - Remove this

 return 0;
}

7. Implement a function that receives an integer value and returns the number of prime factors of that number. If the number is itself a prime number then return 0
/*
For example : 
for number 21, the function should return 2 as the prime factors of 21 is 3, 7
for number 3, 71 the function should return 0 as 3, 71 are prime numbers
*/
 
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int prime_factors(int n)
{
 // Decaring variable "i", "j" to iterate loop, c=counter
 int i, j, c;
 
 // Declaring variable cp=count prime to count the number of prime numbers
 int cp = 0;
 
 // Determining number of prime factors
 for(i=2; i<=n/2; i++)
 {
  c=0;
  if(n%i==0)
  { 
  for(j=1; j<=i/2; j++)
  {
   if(i%j==0)
   {
    c++;
   }
  }
  
  if(c<=1)
  {
   cp++;
  }
  }
  
 }
 
 // Returning number of prime factors
 return (cp);
 
}


int main()
{
 // Declaring variable "n" to input a number
 int n;
 
 // Inputting number
 printf("Enter any positve integer number : ");
 scanf("%d",&n);
 
 // Calling funtion and printing result
 printf("Number of prime factors of %d = %d ",n, prime_factors(n));
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program to find maximum number between three numbers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

// Function prototype
int maximum(int x, int y, int z);

int main()
{
 // Declaring variable n1, n2, n3 to take the input of three numbers
 int n1, n2, n3;
 
 // Inputting value
 printf("Enter first number : ");
 scanf("%d",&n1);
 
 printf("Enter second number : ");
 scanf("%d",&n2);
 
 printf("Enter third number : ");
 scanf("%d",&n3);
 
 // Calling function maximum() and printing the returned maximum value
 printf("Maximum between three numbers : %d ", maximum(n1, n2, n3));
 
 getch(); // Linux user - Remove this

 return 0;
}

int maximum(int x, int y, int z)
{
 // Decalring variable max to store max value
 int max = x; // Assuming x to be maximum
 
 if(max<y)
 {
  max=y;
 }
 
 if(max<z)
 max=z;
 
 // Returning maximum value
 return (max);
 
}

9. Program to accept a number and print the sum of given and Reverse number using function
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{

 // Declaring variable n=to store the entered number by user
 int n; 

 /* Declaring variable r=reverse to store the reverse of a number, 
 s=sum to store the sum of given and reverse number */
 int r, s;
 
 printf("Enter a number : ");
 scanf("%d",&n);
 
 // Calling funciton rev and storing the result in "r"
 r=rev(n);
 
 // Displaying reverse number
 printf("Reverse of a number = %d",r);
 
 // Calling function add
 s=add(n,r); 
 
 // Displaying result or sum
 printf("\nSum of a given and reverse number = %d",s);
 
 getch( ); // Linux user - Remove this

 return 0;
 }
 
 int rev( int n)
 {
  /* Declaring variable d=digit to store the extracted digit, 
  rev=reverse to store reversed number */
  int d,rev=0;
  
  while(n>0)
  {
   d=n%10;
   rev=rev*10+d;
   n=n/10;
  }
  
  // Returning reversed number
  return rev;
  }
  
  int add(int n, int r)
  {
   // Returning sum of given and reverse number
   return n+r;
  }
C PROGRAMS : FUNCTION

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;
}
COMMON PROGRAMMING ERROR - FUNCTION

COMMON PROGRAMMING ERROR - FUNCTION

Here is a list of few common programming errors committed by us in function. For complete list of common programming errors visit : Common Programming Errors In C
  • Forget to put a semicolon at the end of prototype or function declaration.
  • Specifying function parameters of the same type as double x,y instead of double x, double y results in a compilation error.
  • Put a semicolon at the end of function header while defining the function.
         For example:
         float division(float a, int b);  // error
        {
              return a/b;
        }
  • Type mismatch error due to difference in the types in function declaration and function definition. The types of parameter may differ.
  • Type mismatch error due to difference in the order of parameters in function declaration and function definition.
  • Type mismatch error due to difference in the number of actual arguments and the number of formal arguments.
  • Defining a function inside another function is a syntax error.
  • Defining a local variable within a function with the same name as formal argument name.
         For example:
         float division(float a, int b)
        {
                  int a; //error defining the same variable
        }
  • Not returning any value when the function return type is not valid.
        For example:
        float division(float a, int b)
       {
              return ;
       }