ALL > Computer and Education > courses > university courses > undergraduate courses > An Overview of Computer Science > ZSTU 2018-2019-1 class > student homework directory >
Homework_2 IHIMBAZWE KAGENZA Jean Claudien Version 0
👤 Author: by kageclaudiengmailcom 2018-10-17 16:36:33
Compute sum of digits from 1 to 10

Input: n = 10 Output: Sum of digits in numbers from 1 to 10= 55

A naive solution is to go through every number x from 1 to 10, and compute sum in x by traversing all digits of x. Below is the implementation of this idea.

C++

// A Simple C++ program to compute sum of digits in numbers from 1 to 10

#include<bits/stdc++.h>

using namespace std;

 

int sumOfDigits(int );

 

// Returns sum of all digits in numbers from 1 to 10

int sumOfDigitsFrom1To10(int n)

{

int result = 0; // initialize result

 

// One by one compute sum of digits in every number from

// 1 to n

for (int x = 1; x <= 10; x++)

result += sumOfDigits(x);

 

return result;

}

Address         Cells 2101
A0 21 2202
A1 01 2303
A2 22 2404
A3 02 2505
A4 23 2606
A5 03 2707
A6 24 2808
A7 04 2909
A8 25 2A0A
A9 05 5B12
AA 26 5C34
AB 06 5D56
AC 27 5E78
AD 07 5F9A
AE 28 51BC
AF 08 52DE
B0 29 5312
B1 09 543F
B2 2A 3400
B3 0A C000
B4 5B
B5 12
B6 5C
B7 34
B8 5D
B9 56
BA 5E
BB 78
BC 5F
BD 9A
BE 51
BF BC
C0 52
C1 DE
C2 53
C3 12
C4 54
C5 3F
C6 34
C7 00
C8 C0
C9 00

Please login to reply. Login

Reversion History

Loading...
No reversions found.