// Program is suming odd numbers from [A B] diapason
#include <stdio.h>
#include <stdlib.h>
int main(){
int r,i,a,b, sum=0;
scanf("%d %d", &a, &b); // We should write two number. A and B. Program will sum odd numbers From A to B
r=(b-a); // how many numbers do we have from 12 to 20? result=20-12=8
int mass[r]; // so we have r numbers
// if A is odd
if (a%2!=0) {
for(i=0; i<=r; i+=2)
{
mass[i]=a+i; // odd numbers by 2 step. For example if first number is 13 the next will be 13+2, the next 13+2+2
if (i>0)
printf("+%d", mass[i]); // if we write only "%d+ " the result will be, for example, 5+7+=12 the + near is not beautiful. si this code is to remove the "="
else
printf("%d", mass[i]);
sum=sum+mass[i];
}
printf("= %d \n", sum);
}
//////////////////////////////////////////////////////////////
// IF A is even number
else if (a%2==0){
for(i=0; i<=r; i+=2)
{
mass[i]=a+i+1; // odd numbers by 2 step. For example if first number is 12 the next will be 12+2+1
if (mass[i]>b) continue; // From [a,b] diapason, Array Also tooks b+1, If B is even, SO we could use function "continue;"
if (i>0)
printf("+%d", mass[i]);
else
printf("%d", mass[i]);
sum=sum+mass[i]; // we could write sum+=mass[i]; this is same;
}
printf("=%d \n", sum); // to write sum
}
}
For example, If we write 11 27, program will write:
11+13+15+17+19+21+23+25+27=171
Edited by VakhoQ, 20 December 2010 - 03:35 AM.


Sign In
Create Account


Back to top









