Find the sum of all odd numbers between 1 to n with C.
#include <stdio.h>
int main()
{
int i, first, last, sum = 0;
printf("Enter first Number: ");
scanf("%d", &first);
printf("Enter last Number: ");
scanf("%d", &last);
for (i = first; i <= last; i ++)
{
if (i % 2 != 0)
{
sum += i;
}
}
printf("Sum of all even number between %d to %d = %d", first, last, sum);
return 0;
}
Output:-
Enter first Number : 10
Enter last Number : 20
Sum of all even number between 10 to 20 = 75
0 Comments