Problem I

f[x_]:=Piecewise[{{x,0<x<=(π/2)},{π-x,(π/2)<x<π}}]

Simplify[(2/π)Integrate[f[x]Sin[n x],{x,0,π}],Assumptions->n∈Integers]

b[n_]:=((4 Sin[((n π)/2)])/((n^2) π))

Table[b[n],{n,1,10}]

Plot[Sum[b[n]Sin[n x],{n,1,20}],{x,0,π}]

Plot the solution in t and x:

Plot3D[Sum[b[n]Exp[-(n^2)t]Sin[n x],{n,1,20}],{x,0,π},{t,0,3},AxesLabel->Automatic]

Manipulate[
Plot[Sum[b[n]Exp[-(n^2)t]Sin[n x],{n,1,20}],{x,0,π},AxesLabel->Automatic,PlotRange->{-2,2}]//Quiet,
{t,0,3}]

Problem II

f[x_]:=Piecewise[{{x,0<x<=(1/2)},{1-x,(1/2)<x<1}}]

Simplify[2*Integrate[f[x]Cos[n π x],{x,0,1}],Assumptions->n∈Integers]

a[n_]:=((8 Cos[((n π)/2)] ((Sin[((n π)/4)])^2))/((n^2) (π^2)))

a[0]=2*Integrate[f[x],{x,0,1}]

Table[a[n],{n,0,20}]

Plot[((a[0])/2)+Sum[a[n]Cos[n π x],{n,1,20}],{x,0,1}]

Plot the solution in t and x:

Plot3D[((a[0])/2)+Sum[a[n]Exp[-(n^2)(π^2)t]Cos[n π x],{n,1,20}],{x,0,1},{t,0,0.3},PlotRange->{0,(1/2)},AxesLabel->Automatic]

Manipulate[
Plot[((a[0])/2)+Sum[a[n]Exp[-(n^2)(π^2)t]Cos[n π x],{n,1,20}],{x,0,1},AxesLabel->Automatic,PlotRange->{0,0.5}]//Quiet,
{t,0,0.4}]