Learn about Sum of First Odd Numbers in C from 1 to N in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Sum of First Odd Numbers in C from 1 to N
Program:
#include<stdio.h>
int main()
{
int n, sum=0;
printf("Enter n value: ");
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
if(i%2!=0) sum += i;
}
printf("Sum of odd numbers from 1 to %d is: %d", n, sum);
return 0;
}
Output:
Enter n value: 50
Sum of odd numbers from 1 to 50 is: 625
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Sum of squares of N natural numbers in C without using loop
Sum of squares of N natural numbers in C using while loop