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
OverLord2011 [107]
3 years ago
11

Write a recursive function to compute sum of first N integers starting with 1.

Computers and Technology
1 answer:
Citrus2011 [14]3 years ago
4 0

Answer:

// here is code in C++.

#include <bits/stdc++.h>

using namespace std;

// recursive function to find sum from 1 to n

int recur_Sum(int n)

{ // base condition

if (n <= 1)

 return n;

// recursive call

return n + recur_Sum(n - 1);

}

// main function

int main()

{

   // variables

   int n;

   cout<<"Enter a number:";

   // read the number

   cin>>n;

   // print the sum

   cout<<"Sum of first "<<n<<" integer from 1 to "<<n<<" is:"<<recur_Sum(n);

return 0;

}

Explanation:

Read a number from user and assign it to variable "n".Call function recur_Sum() with parameter "n".This function will recursively call itself and find the Sum of first n numbers from 1 to n.Then function will return the sum.

Output:

Enter a number:10

Sum of first 10 integer from 1 to 10 is:55

You might be interested in
Write a program that will input a list of test scores in from the keyboard. When the user enters -1, print the average.
aivan3 [116]

Answer: -1 usually represents infinity.

Explanation:

It would keep going forever and not be able to stop without manually stopping it.

3 0
3 years ago
Read 2 more answers
Consider the recursive method whose definition appear below. Why? public static String mysteryString (String s){ if(s.length()==
Marizza181 [45]

Answer:

retupmoc

Explanation:

1.) Anwser will be retupmoc

because

public static String mysteryString(String s){

if(s.length() == 1){

return s;

}

else{

return s.substring(s.length() -1) + mysteryString(s.substring(0, s.length()-1));

}

}

In this program input is "computer" . So the function mysteryString(String s) it does

return s.substring(s.length() -1) + mysteryString(s.substring(0, s.length()-1));

so when it enters the first time ??s.substring(s.length() -1) and it will be give you 'r' then it calls the function recursively by reducing the string length by one . So next time it calls the mysteryString function with string "compute" and next time it calls return s.substring(s.length()-1)? + mysteryString(s.substring(0,s.length-1)) so this time it gives "e" and calls the function again recursively . It keeps on doing till it matched the base case.

so it returns "retupmoc".

6 0
3 years ago
At one college, the tuition for a full-time student is $6,000 per semester. It has been announced that the tuition will increase
xxMikexx [17]

Answer:grhgrt

Explanation:

4 0
3 years ago
What’s the screen that displays results from typing texts, preforming calculations,or running programs
marshall27 [118]

A monitor is the screen.

5 0
3 years ago
When you call one of the Scanner class's methods to read a primitive value, such as nextInt or nextDouble, and then call the nex
Rzqust [24]

The answer is true.

Explanation:

The scanner class's methods are the methods in java.util, which allows the user to read values of various types. If the nextLine is issued after a numeric read and the numeric value is at the end of the line, nextLine returns the empty string.

The problem occurs when you click the enter key which is a new line \n character. nextInt() has only the integer but skips the new line \n.

To solve this problem, you have to add the input.nextLine() after reading the int it will consume the \n.

Hence, make input.nextLine(); call after input.nextint(); which reads till end of life.

5 0
3 years ago
Other questions:
  • Melissa and Sue want to show a presentation to twenty employees using a presentation while using a projector. They learn that th
    12·2 answers
  • Which of the following statements is false?
    10·1 answer
  • 2. Which of the following best describes the protocols used on the Internet?
    15·1 answer
  • The set of rules for how computers talk to one another
    6·1 answer
  • The correct order of operations is _____. brackets, exponents, division, multiplication, addition, and subtraction subtraction,
    5·2 answers
  • Select the correct answer.
    10·2 answers
  • What is the first step that you have to perform before you can add a windows package to a wim file?
    9·1 answer
  • What when can you expect technology to be effective?
    12·1 answer
  • When defining a system
    15·1 answer
  • How to send an email to multiple recipients individually outlook
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!