C program to find sum of first n odd positive numbers. Take a number n as input.
/**
* C program to print the sum of first n odd positive numbers
*/
#include <stdio.h>
int main()
{
/* odd numbers are integers */
int count, n, sumOfOddNum=0;
/* First positive odd numbers starts from 1 and end at n. */
printf("Enter value of n : \n");
scanf("%d", &n);
/* Find the sum of first n odd positive numbers */
/*Positive odd numbers are 1,3,5,7....
* So increment count = 1 by 2 each time */
for(count=1; count<=n; count=count+2)
{
sumOfOddNum = sumOfOddNum + count;
}
printf("Sum of first n odd numbers = %d", sumOfOddNum);
return 0;
}
Output :
Post a Comment
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.