#include <stdio.h>
#include<conio.h>
int main(){
int sum(int n);
int num, result;
clrscr();
printf("Enter a positive integer:");
scanf("%d", &num);
result = sum(num);
printf("sum=%d", result);
getch();
}
int sum (int n){
if(n!=0) return n + sum(n-1); // sum() function calls itself
else return n;
}
#include<conio.h>
int main(){
int sum(int n);
int num, result;
clrscr();
printf("Enter a positive integer:");
scanf("%d", &num);
result = sum(num);
printf("sum=%d", result);
getch();
}
int sum (int n){
if(n!=0) return n + sum(n-1); // sum() function calls itself
else return n;
}
No comments:
Post a Comment