c program to find sum of first n natural numbers

C Program To find the sum of first n natural numbers where n is entered by user.                          

Code :
  
#include <stdio.h>

int main()
{

    int n;
    int sum=0;
    printf("Enter the value of n:");
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        sum=sum+i;
    }
    printf("Sum of first n natural number:%d",sum);
    return 0;

}


 

Sample Input and Output:

Enter the value of n:5                                                                                                               
Sum of first n natural number:15    

 

If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  

  Previous Next

Post a Comment

Previous Post Next Post