Here’s a simple C program to calculate the sum of odd natural numbers.This program prompts the user to enter a positive integer (n
). It then calculates the sum of odd natural numbers up to n
using a for
loop. The result is then displayed on the screen. If the user enters a non-positive integer, the program asks for a positive integer and terminates with an error code.
Program :
#include<stdio.h>
#include<conio.h>
int main()
{
int number,min,max,sum=0;
printf(“Enter the minimum range: “);
scanf(“%d”,&min);
printf(“Enter the maximum range: “);
scanf(“%d”,&max);
for(number=min;number<=max;number++)
{
if(number%2!=0)
{
printf(” %d “,number);
sum=sum+number;
}
}
printf(“\nSum of Odd Numbers : %d”,sum);
}
Â