C program to print sum up-to each individual preceding number until the entered number

Here is a C program which accepts an integer and prints out sum from 1 to 1, 1 to 2, 1 to 3 …… 1 up-to that number –

main(){
       int n,i,s=0;
       printf("\nEnter any integer>>");
       scanf("%d",&n);
       
       for(i = 1; i <= n; i++){
          s += i;
          printf("\nSum up-to %d is %d.", i, s);
       }      
}

Add a Comment

Your email address will not be published. Required fields are marked *