Home > Back-end >  After each number and ordinal number code
After each number and ordinal number code

Time:01-22

The sum of digits 1
Here to limit the input of 0 & lt; Num=& lt;=1000, one thousand - bit processing can be unified in the expression, also can use the if statement judge 1000 cycle implementation can also be used alone, this method can calculate many digits of each sum of the:
# include
Int main ()
{
Int num=0, res=0;
The scanf (" % d ", & amp; Num);
If (num==1000)
Res=1;
The else
Res num/100 + num=10 +/10% num % 10;
Printf (" % d \ n ", res);
return 0;
}
Also can use another way oh
# include
Int main ()
{
Int num=0, res=0;
The scanf (" % d ", & amp; Num);
While (num)
{
Res +=(num % 10);
Num/=10;
}
Printf (" % d \ n ", sum);
Return0;
}
The ordinal number 2
# include
Int main ()
{
Int n=0, res=0, a=1000;
The scanf (" % d ", & amp; N);
Do {
Res +=(n % 10) * a; A/=10;
} while (n=n/10);
Printf (" % d ", res); Return0; }

CodePudding user response:

The ordinal number
Int main () {
Int n=0, res=0;
The scanf (" % d ", & amp; N);
for(; n> 0; N/a=10)
Res=res * 10 + n % 10;
Printf (" % d ", res);
return 0;
}

CodePudding user response:

CodePudding user response:

//2 the ordinal 
# include
Int main ()
{
Int n=0, res=0;
The scanf (" % d ", & amp; N);

While (n) {
Res=res * 10 + n % 10;
N/a=10;
}
Printf (" % d ", res);

return 0;
}
  • Related