Java Recursion Program Practise 5 - to input a number and print the sum of digits of the number

Java Program to input a number and print the sum of digits of the number using Recursive Function

Output


import java.util.*;

class Main {

   int digit(int n){

       if(n==0)

       return 0;

       else

       return (n%10+digit(n/10));

   }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int a;

        System.out.println("Enter the number whose sum of digit is desired using Recursive function");

        a = in.nextInt();

        Main ob = new Main();

        System.out.println(ob.digit(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.