Hello guys, welcome to Lingarajtechhub.com. Today we are going to discuss about find out the sum of natural numbers using recursion in java.
package com.recursion.java; import java.util.Scanner; public class Sum_of_natural_no { public static void main(String[] args) { int number; Scanner Sc=new Scanner(System.in); System.out.println("enter number"); number=Sc.nextInt( ); System.out.println("sum of natural numbers:"+sum(number)); } public static int sum(int value) { if(value==0) return 0; return value+sum(value-1); } }
Line-1:
❖ Package com.recursion.java;
It is a java package( com.recursion.java) which is a collection of classes and this package is created in a java project .
Line-2:
❖ import java.util.Scanner;
Scanner is a class in java. util package used for obtaining the input of the primitive types like int, double, etc. and strings .
Line-4:
❖ public class Sum_of_natural_no {
*Here Sum_of_natural_no is defined as a class which is a template used to create objects and to define object data types and methods . >–and it’s defined as public because it can be accessed anywhere .
Line-6:
❖ public static void main(String[] args)
>>– it is a main method in which we can execute a program //public :it is an access modifier of the main function which must be public so that the JVM which is (Java Virtual Machine)can access and execute this method.
//If a method is not public, then access is restricted and its showing error //static is a keyword in which main method has must to be static in which JVM can load the class into memory and call the main method without creating an instance
//void it means no return type
//main() is function string[]args : string means collection of characters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly
Line-7:
❖ int number;
>>– here we declare a number as a variable which is integer type.
Line-8:
❖ Scanner sc=new Scanner(System.in);
>>–it means here scanner is a class and Sc is the instance of the object which is new Scanner.System.in is an argument
Line-9:
❖ System.out.println(“Enter the number “);
>>– it means System is a class name and out is an instance of the System println is print a new line .
Line-10:
- number=sc.nextInt();
>>– here number is a variable that we take and Sc is our instance name which is created for an object and nextInt() is a method method of a Scanner object that reads in a string of digits or char and converts them into an int type.
Line-11:
❖ System.out.println(“sum of natural numbers:”+sum(number)); }
>> >>– it means System is a class name and out is an instance of the System println is print a new line .
>>>>– + means concatenate or comma operator.
Line-13:
- Public static int sum(int value) {
>>– it is a main method in which we can execute a program //public :it is an access modifier of the main function which must be public so that the JVM which is (Java Virtual Machine)can access and execute this method.
//If a method is not public, then access is restricted and its showing error
//static is a keyword in which main method has must to be static in which JVM can load the class into memory and call the main method without creating an instance
//int it means return type.
//sum it to add all the numbers.
Line-14:
- if(value==0)
//if it means we are using a for loop and then the value is double equal to 0.
Line-15:
- Return 0
// it means we use int to return some value.
Line-16:
- Return value+sum(value-1);
>>it means we use int to return some value in this way.