Answers for "sum of first and last digit"

C++
0

Sum of first and last digit of a number in C++

// not my code but I figured it would be useful

int number, sum=0, firstDigit, lastDigit;

     //Reading a number from user
    cout<<"Enter any number:";
    cin>>number;

     lastDigit = number % 10;

    firstDigit = number;

    while(number >= 10)
    {
        number = number / 10;
    }
    firstDigit = number;

     //Finding sum of first and last digit
    sum = firstDigit + lastDigit;

    cout<<"Sum of first and last digit: "<<sum;
Posted by: Guest on December-30-2020
0

sum up all the first and last digit of a number until only two digits are left

function sum(num) {
  var numString = num.toString();
  var newString = "";
  while (numString.length > 1) { // (1)
    newString += (parseInt(numString[0]) + parseInt(numString[numString.length - 1])).toString(); // (2)
    numString = numString.substring(1, numString.length - 1); // (3)
  }
  newString += numString; // (4)

  if (newString.length > 2) { // (5)
    console.log(newString)
    return sum(newString);
  } else {
    return newString;
  }
}


console.log(sum(1234567));
 Run code snippet
Posted by: Guest on October-13-2021

Code answers related to "sum of first and last digit"

Browse Popular Code Answers by Language