/*Write a c program to find the sum of natural numbers using the formula sum=n(n+1)/2 by direct method */ #include <stdio.h> #include <stdlib.h> int main() { int n, sum=0; printf("Enter the value for n:\n"); scanf("%d",&n); sum =n*(n+1)/(2.0); printf("The sum of the first %d natural numbers is %d \n",n,sum); return 0; }
Enter the value for n: 6 The sum of the first 6 natural numbers is 21
No comments:
Post a Comment