9.8 – Handling returning value – programs to practice- part-2

by subbu on November 18, 2013

Handling returning value from a function:

So far we have been assigning the value returned by a function to a variable and then used to perform other operations.
The function that returns a value can be directly printed, can be participated in arithmetic expressions, can be compared with other values and can be used to take a decision.
The following examples demonstrate how returned values from a function can be handled in different ways.

Function (returned value) can be directly printed:

Specification: Accept any integer and print its factorial using the function factorial.

Program:

#include<stdio.h>
long factorial(int n);
int main()
{
  int n;
  printf("Enter an integer:");
  scanf("%d",&n);
  printf("Factorial is %ld",factorial(n)); /* returned value is directly printing */
  return 0;
}
long factorial(int n)
{
  int i;
  long fact;
  for(fact=1,i=1;i<=n;i++)
     fact=fact*i;
  return fact;
}

Execution:
Enter an integer: 8
Factorial is 40320

Example explained:
Here factorial() is a sub function that accepts an integer and returns the factorial of the given number. The returned value is directly printed rather assigning to another variable and then printing.

Function (returned value) can be participated in arithmetic operations:

Specification: Accept any integer and print whether the number is a strong number or not using the function factorial()

Logic: If sum of factorials of individual digits is equal to the given number then the number is called strong number.
Examples: 1, 2, 145 etc.

Program:

#include<stdio.h>
long factorial(int n);
int main()
{
  int n,sum,temp;
  printf("Enter an integer:");
  scanf("%d",&n);
  temp=n;
  for(sum=0;n!=0;n=n/10)    /* logic to extract individual digits */
     sum=sum+factorial(n%10);  /* sum of factorials */
  if(temp==sum)
     printf("Strong number");
  else
     printf("Not a strong number");
  return 0;
}
long factorial(int n)
{
  int i;
  long int fact;
  for(fact=1,i=1;i<=n;i++)
     fact=fact*i;
  return fact;
}

Execution:
Enter an integer: 145
Strong number

Execution:
Enter an integer: 456
Not a strong number

Example explained:
Here the function factorial() is used to calculate the factorial of individual digits. Factorials returned by the function is directly added to the variable sum for every iteration.

Function (returned value) can be compared:

Specification: Accept any integer and print whether the number is a palindrome number or not using the function reverse().

Program:

#include<stdio.h>
int reverse(int);
int main()
{
  int n;
  printf("Enter any integer:");
  scanf("%d",&n);
  if(n==reverse(n))
     printf(" Palindrome number");
  else
     printf(" Not a palindrome number");
  return 0;
}
int reverse(int n)
{
  int rev;
  for(rev=0;n!=0;n=n/10)
     rev=rev*10+n%10;
  return rev;
}

Execution:
Enter any integer: 343
Palindrome number

Execution:
Enter any number: 567
Not a palindrome number

Example explained:
Here the function reverse() accepts an integer and returns it’s reverse value. The reverse number returned by the function is directly compared with the original number.

Function (returned value) can be used to take a decision:

Specification: Accept any number and print whether the number is a prime number or not using the function isprime()

Program:

#include<stdio.h>
int isprime(int);
int main()
{
  int n;
  printf("Enter any integer:");
  scanf("%d",&n);
  if(isprime(n))
    printf("Prime number");
  else
    printf("Not a prime number");
  return 0;
}
int isprime(int n)
{
  int count,i;
  for(count=0,i=1;i<=n;i++)
    if(n%i==0)
      count++;
    if(count==2)
       return 1;        /* returning true */
    else
       return 0;        /* returning false */
}

Execution:
Enter any integer: 18
Not a prime number

Execution:
Enter any integer: 11
Prime number

Example explained:
Here the function isprime() accepts an integer and returns 1 (true) if the given number is a prime number otherwise returns 0 (false).
Because the function isprime() returns either true (1) or false (0), the returned value is directly used to take a decision in if conditional statement.

Here the purpose of isprime() function is to return 1(true) if the given number is a prime number, 0(false) if the number is not a prime number. The same function can be defined in other ways as

int isprime(int n)
{
 int count,i;
 for(count=0,i=1;i<=n;i++)
   if(n%i==0)
     count++;
 return (count==2)?1:0;  /* returns either 1 or 0 */
}

In the above function definition, conditional statement gives 1(true) if the condition count==2 is true otherwise gives 0(false). The return statement returns the result of conditional statement that is either 1 or 0.


int isprime(int n)
{
 int count,i;
 for(count=0,i=1;i<=n;i++)
  if(n%i==0)
    count++;
 return (count==2);  /* returns either 1 or 0 */
}

We know that, any relational expression in C language gives either 1 or 0. Here count==2 gives 1(true) if the number of factors are 2 (prime number) otherwise gives 0(false). The return statement returns the result of relational expression that is either 1 or 0

Any number of functions can be defined:

Specification: Accept any integer and print the minimum and maximum digits using minimum() and maximum() functions.

Program:

#include<stdio.h>
int minimum(int);
int maximum(int);
int main()
{
  int n;
  printf("Enter any integer:");
  scanf("%d",&n);
  printf("The minimum digit %d",minimum(n));
  printf("\nThe maximum digit %d",maximum(n));
  return 0;
}
int minimum(int n)
{
  int min;
  for(min=n%10;n!=0;n=n/10)
     if(n%10<min)
        min=n%10;
  return min;
}
int maximum(int n)
{
  int max;
  for(max=n%10;n!=0;n=n/10)
     if(n%10>max)
         max=n%10;
  return max;
}

Example explained:
Here two functions are defined in a program to find minimum and maximum digits of an integer

Nested function calls:

A value returned by a function can be send as an argument into another function. Here we need to write a function calling statement in another function calling statement called nested function calls.

Example:

#include<stdio.h>
int sum(int,int);
int sub(int,int);
int main()
{
 int p;
 p=sub(120,sum(30,20)); /* nested calls */
 printf("%d",p);
 return 0;
}
int sum(int x,int y)
{
 return x+y;
}
int sub(int x,int y)
{
 return x-y;
}

Output:
70

Example explained:
Here sum(30,20) returns 50, which will be sent as the second argument to the function sub().
sub(120,50) returns 70, which will be assigned to p and then printed as output.

Specification: Accept the base, exponent and print the result of expression using the function power().

Program:

#include<stdio.h>
int power(int,int);
int main()
{
  int base,exp,res;
  printf("Base:");
  scanf("%d",&base);
  printf("Exponent:");
  scanf("%d",&exp);
  res=power(base,exp);
  printf("The result of expression %d",res);
  return 0;
}
int power(int b,int e)
{
  int r,i;
  for(r=1,i=1;i<=e;i++)
        r=r*b;
  return r;
}

Execution:
Base: 3
Exponent: 4
The result of expression: 81

pow(): It is the predefined function used for the same purpose.

Specification: Print the Armstrong numbers from 1 to the given limit.

Program:

#include<math.h>
#include<stdio.h>
int isarmstrong(int);
int main()
{
  int n,limit;
  printf("Enter the limit:");
  scanf("%d",&limit);
  for(n=1;n<=limit;n++)
    if(isarmstrong(n))
      printf("\n%d",n);
  return 0;
}
int isarmstrong(int n)
{
  int sum,temp=n;
  for(sum=0;n!=0;n=n/10)
     sum=sum+pow(n%10,3);
  return (temp==sum);                   /* it returns either 1 or 0 */
}

Execution:
Enter the limit: 1000
1
153
370
371
407

Previous post:

Next post: