Example: Sum of Natural Numbers Using Recursion fun main(args: Array<String>) { val number = 20 val sum = addNumbers(number) println("Sum = $sum") } fun addNumbers(num: Int): Int { if (num != 0) return num + addNumbers(num - 1) else return num } When you run the program...
Below is an example of a Java program to find the sum of N numbers using recursion import java.util.Scanner; public class ArraySum { public static int RecursiveSum(int my_array[], int i,int N){ if (i == N) return 0; return my_array[i] + RecursiveSum(my_array, i + 1,N); ...
Here's the equivalent Java code: Java Program to Calculate the Sum of Natural Numbers You can also use while loop to solve this problem as follows: Example 2: Sum of Natural Numbers using while loop fun main(args: Array<String>) { val num = 50 var i = 1 var sum = 0 while (i ...
Write a program in C# Sharp to find the sum of the first n natural numbers using recursion. Visual Presentation: Sample Solution: C# Sharp Code: usingSystem;// Class definition named 'RecExercise3'classRecExercise3{// Main method, the entry point of the programstaticvoidMain(string[]args){...
«Prev - Java Program to Find the Largest Number Among Three Numbers »Next - Java Program to Reverse a Number using Recursion Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO atSanfoundry. He lives in Bangalore, and focuses on development of Lin...
Learn how to calculate the sum of squares of the first N natural numbers using C#. Step-by-step guide with examples.
Method 1: Using Loop A simple solution is to loop from 1 toN, and add their cubes tosumVal. Program to find the sum of the cubes of first N natural number # Python program for sum of the# cubes of first N natural numbers# Getting input from usersN=int(input("Enter value of N: ...