Sum of first n positive integers in C

#include <stdio.h>
 
 int sum(int);
 
 int main()
 {
    int n=3,s;
 
    printf("%dn",n);    //Prints 3
    s=sum(n);
    printf("%dn",n);    //Prints 3
    printf("%dn",s);    //Prints 6
    return 0;
 
 }
 int sum(int n)
 {
    int s1=0;
 
    for(;n>0;--n)
        s1+=n;
    printf("%dn",n);    //Prints 0
    return s1;
}
End Program

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.