Java Recursion Program Practise 4 - to input a number and display sum from 1 to that number

Java Program to input a number and display sum from 1 to that number using Recursion

Output


import java.util.*;

class Main {

    int add(int n){

        if(n>0){

            return (n + add(n-1));

        }

        else

        return 0;

    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int a;

        System.out.println("Enter the number of which the sum is desired from 1 to the number n");

        a = in.nextInt();

        Main ob = new Main();

      System.out.println( ob.add(a));

        

    }

}

Comments

Translate

Popular posts from this blog

Letter to SBI Bank Manager of Local Branch to reduce the interest rate on home loan

Discuss the impact of remote work on modern society. Amplify the following points in your composition: Increased flexibility and work-life balance Reduced commute times Flexible working hours More time for personal activities Changes in workplace dynamics Virtual communication and collaboration tools Shift in team management strategies Impact on workplace culture Economic implications Cost savings for businesses Economic benefits for employees Effects on local businesses and real estate Environmental impact Reduction in carbon footprint due to less commuting Decreased office energy consumption Potential increase in home energy usage Challenges and drawbacks Issues with maintaining productivity Potential feelings of isolation among employees Security concerns with remote networks

Abhisara - the Tryst Notes

Write a short story entitled "Advice not taken"

Narrate an experience that shows appearances can be deceiving

One word Essay: Adventure

“Every person must have some skill in life.” Describe an important skill that you are learning, giving the various advantages that will accrue to you after learning it.

Argumentative Essay: Is the advancement of autonomous vehicles a positive development for society?

Macbeth Personal Notes: Act 1 Scene 3 (Part 2/2)

Be the change that you wish to be. Reflect on the statement.