Problem statement:- Program to find the sum of Even numbers using recursion.
Data requirement:-
Input Data:- num2
Output Data:-SumEven(num1,num2)
Output Data:-SumEven(num1,num2)
Program in C
Here is the source code of the C Program to find the sum of Even numbers using recursion.
Code:
#include<stdio.h>
int SumEven(int num1, int num2)
{
if(num1>num2)
return 0;
return num1+SumEven(num1+2,num2);
}
int main()
{
int num1=2,num2;
printf("Enter your Limit:");
scanf("%d",&num2);
printf("Sum of all Even numbers in the given range is: %d",SumEven(num1,num2));
}
Enter your Limit:12
Sum of all Even numbers in the given range is: 42
Program in C++
Here is the source code of the C++ Program to find the sum of Even numbers using recursion.
Code:
#include<iostream>
using namespace std;
int SumEven(int num1, int num2)
{
if(num1>num2)
return 0;
return num1+SumEven(num1+2,num2);
}
int main()
{
int num1=2,num2;
cout<<"Enter your Limit:";
cin>>num2;
cout<<"Sum of all Even numbers in the given range is:"<<SumEven(num1,num2);
}
Enter your Limit:24
Sum of all Even numbers in the given range is:156
Program in Java
Here is the source code of the Java Program to find the sum of Even numbers using recursion.
Code:
import java.util.Scanner;
public class FindSumOEvenNumber {
static int SumEven(int num1, int num2)
{
if(num1>num2)
return 0;
return num1+SumEven(num1+2,num2);
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1=2,num2;
System.out.print("Enter your Limit:");
num2=cs.nextInt();
System.out.print("Sum of all Even numbers in the given range is:"+SumEven(num1,num2));
cs.close();
}
}
Enter your Limit:34
Sum of all Even numbers in the given range is:306
Program in Python
Here is the source code of the Python program to find the sum of Even numbers using recursion.
Code:
def SumEven(num1,num2):
if num1>num2:
return 0
return num1+SumEven(num1+2,num2)
num1=2
print("Enter your Limit:")
num2=int(input())
print("Sum of all Even numbers in the given range is:",SumEven(num1,num2))
Enter your Limit:
54
Sum of all Even numbers in the given range is: 756
Most Recommend Questions:-
More:-
0 Comments
Please do not Enter any spam link in the comment box