/*
1.1から10までの整数のうち、偶数の合計を求めなさい。
2.1から100までの整数で、奇数だけの合計、偶数だけの合計、総合計をもとめよ;
3.y=5x+8の値をxが0~10まで、1ずつ増やした時のyの値
4.10^3+30^3+50^3+70^3+90^3の計算
*/
#include <stdio.h>
typedef enum {
EVEN = 0,
ODD,
ALL
} sum_types;
int get_sum(int min, int max, sum_types type)
{
int i, sum = 0;
for (i=min; i<max+1; i++) {
if (type == EVEN || type==ODD)
sum += i%2==type ? i : 0;
else
sum += i;
}
return sum;
}
int pow_int(int x, int y)
{
int i, mult = 1;
for (i=0; i<y; i++)
mult *= x;
return mult;
}
int main(void)
{
int i, j, pow_sum = 0;
/* 1 */
fprintf(stdout
, "1) %d\n", get_sum
(1, 10, EVEN
));
/* 2 */
fprintf(stdout
, "2) Odd: %d, Even: %d, All: %d\n", get_sum
(1, 100, ODD
), get_sum(1, 100, EVEN),
get_sum(1, 100, ALL));
/* 3 */
fprintf(stdout
, "3) y = 5x + 8;\n"); for (i=0; i<=10; i++)
fprintf(stdout
, "y=%d [x=%d]\n", 5*i
+8, i
);
/* 4 */
for (j=10; j<=90; j+=20)
pow_sum += pow_int(j, 3);
fprintf(stdout
, "4) 10^3+30^3+50^3+70^3+90^3 = %d\n", pow_sum
);
return 0;
}
LyoKICAxLjHjgYvjgokxMOOBvuOBp+OBruaVtOaVsOOBruOBhuOBoeOAgeWBtuaVsOOBruWQiOioiOOCkuaxguOCgeOBquOBleOBhOOAggogIDIuMeOBi+OCiTEwMOOBvuOBp+OBruaVtOaVsOOBp+OAgeWlh+aVsOOBoOOBkeOBruWQiOioiOOAgeWBtuaVsOOBoOOBkeOBruWQiOioiOOAgee3j+WQiOioiOOCkuOCguOBqOOCgeOCiO+8mwogIDMueT01eCs444Gu5YCk44KS772Y44GM77yQ772e77yR77yQ44G+44Gn44CB77yR44Ga44Gk5aKX44KE44GX44Gf5pmC44Gu772Z44Gu5YCkCiAgNC4xMF4zKzMwXjMrNTBeMys3MF4zKzkwXjPj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