Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

sum of digits in c++

int x, s = 0;
   cout << "Enter the number : ";
   cin >> x;
   while (x != 0) {
      s = s + x % 10;
      x = x / 10;
   }
Comment

C++ sum the digits of an integer

// sum the digits of an integer
int getSum(long long n) {
  int sum = 0;
  int m = n;
  while(n > 0) {    
    m = n % 10;    
    sum = sum + m;    
    n = n / 10;    
  } 
  return sum;
}
Comment

digit sum c++

//1
	int n = 12345, sum = 0;
    while(n) {
        sum+=n%10;
        n/=10;
    }
    cout << sum; //15
//2
	int n = 12345, sum = 0;
    for (sum = 0; n > 0; sum += n % 10, n /= 10);
    cout << sum; //15
Comment

c++ Program for Sum of the digits of a given number

// C program to compute sum of digits in
// number.
#include <iostream>
using namespace std;
 
/* Function to get sum of digits */
class gfg {
public:
    int getSum(int n)
    {
        int sum = 0;
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }
};
 
// Driver code
int main()
{
    gfg g;
    int n = 687;
    cout << g.getSum(n);
    return 0;
}
// This code is contributed by Soumik
Comment

PREVIOUS NEXT
Code Example
Typescript :: vue typescript extend component option 
Typescript :: npm type-check nested arrays 
Typescript :: how to call two method connected 
Typescript :: adonis preload recursive 
Typescript :: typescript cloudinary api setup 
Typescript :: create react native app typescript 
Typescript :: vetur change tsconfig location 
Typescript :: typescript dictionary typing 
Typescript :: adonis validator exists 
Typescript :: microsoft outlook graph get events dates 
Typescript :: oclif table 
Typescript :: styled components conditional hover 
Typescript :: session not created: This version of ChromeDriver only supports Chrome version 85 
Typescript :: what design consideration usually taken for granted when using Ceramic 
Typescript :: throw error in typescript 
Typescript :: divide all elements of list by an integer 
Typescript :: adding headers to httpclient angular 
Typescript :: randomNumberGeneratorInRange in js 
Typescript :: Powersell execution policy 
Typescript :: typescript add to array 
Typescript :: cannot be loaded because running scripts is disabled on this system vs code 
Typescript :: replace string in typescript 
Typescript :: angular reload component 
Typescript :: useRef ts 
Typescript :: typescript key options from array values 
Typescript :: check if list of objects contains value c# 
Typescript :: see conda enviroments 
Typescript :: Socket.io bad request with response 
Typescript :: python get first n elements of list 
Typescript :: ubuntu hosts file location 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =