in Education by
a ccept number from user and calculate the sum of all number from 1 to given number Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Input: n = 5 Output: Sum of digits in numbers from 1 to 5 = 15 Input: n = 12 Output: Sum of digits in numbers from 1 to 12 = 51 Input: n = 328 Output: Sum of digits in numbers from 1 to 328 = 3241 Naive Solution: A naive solution is to go through every number x from 1 to n and compute the sum in x by traversing all digits of x. Below is the implementation of this idea. # A Simple Python program to compute sum # of digits in numbers from 1 to n # Returns sum of all digits in numbers # from 1 to n def sumOfDigitsFrom1ToN(n) : result = 0 # initialize result # One by one compute sum of digits # in every number from 1 to n for x in range(1, n+1) : result = result + sumOfDigits(x) return result # A utility function to compute sum of # digits in a given number x def sumOfDigits(x) : sum = 0 while (x != 0) : sum = sum + x % 10 x = x // 10 return sum # Driver Program n = 328 print(“Sum of digits in numbers from 1 to”, n, “is”, sumOfDigitsFrom1ToN(n)) Output: Sum of digits in numbers from 1 to 328 is 3241 please mark my ANSWER as brainliest . it is my humble request

Related questions

0 votes
    1. Take length and breadth of the rectangle from user and calculate the area of rectangle and print the area in ... box. Your answer Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    sum( ), this function is used to calculate total. is it true or false Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    write an agorithm that accepts a set of numers then calculate nd display the sum of thenuber of even vales ... this question pleasee Select the correct answer from above options...
asked Dec 11, 2021 in Education by JackTerrance
0 votes
    is _ of the computer should not be shared with anyone option number 1 user name number 2 password number 3 both Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
0 votes
    Write a menu-driven program to print the following patterns, where number of rows for the pattern should be the user ... 2 4 8 32 Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    is _ of the computer should not be shared with anyone option number 1 user name number 2 password number 3 both Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    24 ina rsa system the public key(e,n) of user a is defined as (7,187).Calculate totient(n) and private ... you encrypt message m=88? Select the correct answer from above options...
asked Dec 20, 2021 in Education by JackTerrance
0 votes
    Write pseudo code to count occurrence of a given number in the list. For example, consider the following list, if ... in the list. Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
0 votes
    Write a program to accept a filename from the user and display all the lines from the file which contain python comment character ‘#’. Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    The mathematics teacher has asked the monitor to create a worksheet that will calculate the simple interest for the ... he proceed Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    WAP to obtain 2 numbers from the user and then display the maximum number out of these 2 numbers and also display ... . (5 Points) Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    wap to obtain 2 numbers from the user and then display the maximum number out of those 2 numbers and also display ... from the number?n Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    Write a program to accept string from user and search a given character from string without using find() function Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    operating system is a program with subroutine it differences B provided liya user-friendly interface C enables the ... the above Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    Write statements in QBASIC for each of the situations described below. (ii) Assign the value represented by variable ... the variable A Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
...