C program to find sum of first n odd positive numbers

October 13, 2021 · Team EasyExamNotes · 1 min read (est.) · 0 comments

Share on WhatsApp

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.

Discussion

Ask a question, share your thoughts or report an error.

Sign in to participate in the discussion.

Join the discussion