Code:
#include<stdio.h>
int main()
{
int n,sum,i;
printf("Enter here:");
scanf("%d",&n);
sum=0;
for(i=1; i<=n; sum=sum+i,i++)
printf("%d\n",sum);
return 0;
}
output:
Enter here:5 0 1 3 6 10
another :
#include<stdio.h>
int main()
{
int n,sum,i;
printf("Enter here:");
scanf("%d",&n);
sum=0;
for(i=1; i<=n+1; sum=sum+i,i++)
printf("%d\n",sum);
return 0;
}
output:
Enter here:5 0 1 3 6 10 15
another code smart for loop : 😀
#include<stdio.h>
int main()
{
int n,sum,i;
printf("Enter here:");
scanf("%d",&n);
for(sum=0,i=1; i<=n; sum+=i,i++); //it is smart loop because everything is here and it is doing comma operator from left to right precedence
printf("sum of %d natural number is %d\n",n,sum);
return 0;
}
output:
Enter here:5 sum of 5 natural number is 15