Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

C program to find sum of first n odd positive numbers

The sum of the first n odd positive numbers can be calculated using the following formula:

sum = n^2

Here is an example program to find the sum of the first n odd positive numbers using the above formula:

#include <stdio.h>

int main() {
  int n, sum;
  printf("Enter the value of n: ");
  scanf("%d", &n);
  
  // calculate the sum using the formula
  sum = n * n;

  printf("The sum of the first %d odd positive numbers is %d\n", n, sum);
  return 0;
}

In this program, we first declare two integer variables n and sum. We ask the user to input the value of n using the scanf() function.

Then, we calculate the value of sum using the formula sum = n^2. Finally, we print the value of sum using the printf() function.