Decimal to Octal conversion

#include
using namespace std;
int dec(int n)
{
int sum=0,rem,rem2,sum2=0;

while(n!=0){
rem=n%8;
sum=sum10+rem;
n=n/8;
}
if(sum<0){
while(sum!=0){
rem2=sum%10;
sum2=sum2
10+rem2;
sum=sum/10;
}
return sum2;
}
return sum;
}

int main(){
int n;
cin>>n;
cout<<dec(n)<<endl;
}

working fine with all inputs but showing wrong answer

You are not using correct logic for base conversion . Try using divide by destination and multiply by source concept.