Write a program which reads a positive integer value N, calculates and shows the sum of the odd numbers < = N.
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
int main()
{
int N, i, sum;
sum=0;
do{
printf("Enter N: ");
scanf("%d",&N);
}while(N<0);
for(i=1;i<=N; i++)
if(i%2!=0)
sum+=i;
printf("Sum of the odd numbers <= %d is equal to %d\n",N, sum);
return 0;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Asymptotic Analysis 13