#include<stdio.h>
int getDivisorForLeftmostDigit(int nVal)
{
int nDiv=1;
if(nVal<0)
nVal *= -1; //just to be sure
{
nDiv*=10;
nVal/=10;
}
return nDiv;
}
int getNumberOfDigits(int nVal)
{
int nCnt=0;
if(nVal<0)
nVal *= -1; //just to be sure
{
nCnt++;
nVal/=10;
}
return nCnt;
}
void displayWithComma(int nVal)
{
int nCnt=getNumberOfDigits(nVal);
int nMult = getDivisorForLeftmostDigit(nVal);
if(nVal<0)
{
printf("-");
nVal*=-1;
}
while(nCnt>0)
{
printf("%d", nVal/nMult);
if(nCnt>1 && nCnt%3==1)
printf(",");
nMult/=10;
nCnt--;
}
printf("\n");
}
int main()
{
displayWithComma(-23);
displayWithComma(27369);
displayWithComma(-1238246);
return 0;
}
`
I feel like I'm missing a few codes to be able to get the output that I wanted. Let me know how to fix this thankyou sm. "Write a function that displays the given number with the corresponding commas in place. The commas are used to divide each period in the number."