M3-R4: July 2013 (O-Level)
Q. 7(a):
Define recursion. Write a complete program to evaluate the given series using recursive function sum( ). Here n is user dependent.
1 + 2 + 3 +…+ n
Ans:
Recursion: A recursive function is a function that calls itself repeatedly.
Program to evaluate sum of the given series using recursive function:
#include <stdio.h>
#include <conio.h>
int sum(int);
void main(){
int n, s;
printf("\nEnter n: ");
scanf("%d", &n);
s = sum(n);
printf("\nSum = %d", s);
getch();
}
int sum(int n){
if(n == 1)
return 1;
return (n + sum(n-1));
}
Q. 7(a):
Define recursion. Write a complete program to evaluate the given series using recursive function sum( ). Here n is user dependent.
1 + 2 + 3 +…+ n
Ans:
Recursion: A recursive function is a function that calls itself repeatedly.
Program to evaluate sum of the given series using recursive function:
#include <stdio.h>
#include <conio.h>
int sum(int);
void main(){
int n, s;
printf("\nEnter n: ");
scanf("%d", &n);
s = sum(n);
printf("\nSum = %d", s);
getch();
}
int sum(int n){
if(n == 1)
return 1;
return (n + sum(n-1));
}
No comments:
Post a Comment