1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
yaroslaw [1]
2 years ago
11

Write code for a function that uses a loop to compute the sum of all integers from 1 to n. Do a time analysis, counting each bas

ic operation (such as assignment and ++) as one operation.
Computers and Technology
1 answer:
tekilochka [14]2 years ago
8 0

Answer:

#include<bits/stdc++.h>  

using namespace std;  

int sumOfinteger(int );  

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

int sumOfintegersFrom1ToN(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 <= n; x++)  

       result += sumOfinteger(x);  

   return result;  

}  

  // A utility function to compute sum of digits in a  

// given number x  

int sumOfinteger(int x)  {  

   int sum = 0;  

   while (x != 0)  {  

      sum += x %10;  

       x   = x /10;  }  

   return sum;  }  

  // Driver Program  

int main()  {  

   int n ;  

   cout<<"enter a number between 1 and n : ";

   cin>>n;

   cout << "Sum of digits in numbers from 1 to " << n << " is " << sumOfDigitsFrom1ToN(n);  

   return 0;  }

You might be interested in
Write a q basic program to find the sum of all the even numbers from 1 to 50​
Artist 52 [7]

Answer:

CLS

M=2

SUM=0

DO WHILE M<=50

SUM=SUM+M

M=M+2

LOOP

PRINT " The sum of all even numbers between 1 and 50 is"; sum

5 0
3 years ago
Why is there no ide length or timing for study breaks?​
Ghella [55]

Answer:

Could you add more to ur question?

4 0
3 years ago
Does the Main Content (MC) of a web page include searchboxes?
Tanzania [10]
Yes it does use search boxes
8 0
3 years ago
Read 2 more answers
Climatologist use weather balloons, stethoscopes, and satellites true or false
Nonamiya [84]

Answer:

False.

Explanation:

A stethoscope is a medical instrument which is used for listening to the work of the heart and lungs.  It consists of a microphone that rests on the patient's chest, and rubber tubes that bring the sound to the earphones. An ordinary stethoscope does not have any moving or electrical parts, as it only mechanically conducts sound from the patient's chest to the doctor's ear.   One of the simplest examinations that doctors perform today is listening to the lungs and heart with a stethoscope. With the help of this instrument, noises are heard during the work of the lungs and heart.

8 0
2 years ago
Which of the following sentences uses correct punctuation? A. I am a good communicator, and I am a strong team member. B. I comm
adelina 88 [10]
A has a comma then and so that isn't correct
B should be and I am
D should have an and in it
C is correct
7 0
3 years ago
Read 2 more answers
Other questions:
  • Which of the following is the part of a digital camera that acts as the film?
    11·1 answer
  • When paying bills online, a payee is:
    9·1 answer
  • True / False
    5·1 answer
  • Select the correct answer. Linda has written a program that works well on various operating systems, but she needs to increase t
    10·1 answer
  • What is the correct process for selecting an entire row in a spreadsheet?
    8·2 answers
  • The program is to be answered in Java Programming not C++ Please answer the following: Modify the BarChart program to accept the
    6·1 answer
  • What icons in the toolbar change the look of the presentation text? It’s either Drawing, Formatting,Presentation, or Standard
    9·2 answers
  • If you can name this you get 15 points: ↑↑↓↓←→←→βα
    10·1 answer
  • Now that you have explored several different languages (Python, HTML, CSS, and JavaScript), what can you do to keep straight whi
    7·1 answer
  • What will the following program display in the console?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!