C++ Program to Find the sum of the first and last digit of a number using a user-defined function


February 1, 2023, Learn eTutorial
1348

How to write a C++ program to find the sum of the first and last digit of a number using a user-defined function

Define a function sumfun() to add the first and last digit of a number


int sumfun(int n)
{
   int t=0, l, r, s=0;
   while(n!=0)
   {
      if(t==0)
      {
         l = n;
         t++;
      }
   r = n;
   n = n/10;
}
s = r + l;
return s;
}


The user is asked to enter a number. This number is passed as the argument to the function.
Within the function, the condition n!=0 evaluates to be true within the while loop. The program flow goes inside the loop and the condition of the if statement gets evaluated. 
Condition t=0; initially value of t is set to be 0; the condition evaluates to be true and the program flow goes inside the body of if and The last digit of the number is initialized to the variable l by performing the computation n, And the value of t gets incremented. So t=1. Because the value of t is 1 from now, therefore the condition of if always gets evaluated to be False from now. Now after exiting from the if, n  gets initialized to r. This statement is created to get the last remainder at the last execution, And the last statement of the loop gets executed, that is n/10  gets initialized to n. Since all statements are available in the body while loop gets executed, therefore program flow evaluates its condition again. That is, the condition n!=0  again evaluates to be True. Therefore program flow again goes inside the loop. This process continues until the condition evaluates to be False. In this way, after exiting the loop, we will have l= last digit and r= first digit. Therefore just add them and print the addition result of the first and last digit of a given number. That's it

Algorithm

Step 1: Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Declare a user defined function int sumfun(int);

Step 4: Open the integer type main function; int main().

Step 5: Declare integer type variable num, sum;

Step 6: Ask the user to enter a  number,

Step 7:Get the number into the variable num;

Step 8: invokes the function  int sumfun(int);

Step 8.1: Pass the num as an argument and get the result to the variable sum;

Step 8.2: Set t= 0 initially;

Step 8.3: Use the % operator to find the last digit of the number.

Step 8.4: l= num;

Step 8.5: Divide the number by 10 until the number is greater than 10. In the end, we get the first digit.

Step 8.6: n= n/10;

Step 8.7: Calculate the sum of the first and last digit and return the value.

Step 9: Print the value that the function returns

Step 10::Exit

 

C++ Source Code

                                          #include<iostream>
using namespace std;

int sumfun(int);
int main()
{
   int num, sum;
   cout<<"Enter a Number: ";
   cin>>num;
   sum = sumfun(num);
   cout<<"\nSum of First and Last Digit = "<<sum;
   cout<<endl;
   return 0;
}
int sumfun(int n)
{
   int t=0, l, r, s=0;
   while(n!=0)
   {
      if(t==0)
      {
         l = n;
         t++;
      }
      r = n;
      n = n/10;
   }
   s = r + l;
   return s;
}
                                      

OUTPUT

Enter a Number: 475
Sum of First and Last Digit = 9