Tuesday, March 1, 2016

Sum up to n th number using user define function in C

1:  #include <stdio.h>  
2:  int sum(int n){  
3:    if(n==0)  
4:      return n;  
5:    else  
6:      return n+sum(n-1);  
7:  }  
8:    
9:  int main(){  
10:    int num,add;  
11:    printf("Enter a positive integer:\n");  
12:    scanf("%d",&num);  
13:    add=sum(num);  
14:    printf("sum=%d",add);  
15:  }  
16:    

No comments:

Post a Comment

Thank you for commenting. Please wait for response :)