C / C++ Program Code to Calculate the sum of digits in a single line of code, iteratively in for loop statement

Leave a Comment
/* The Following C / C++ program code uses only a single line of code in order to calculate the sum of digits present in it. The single line of code is iteratively implemented in the for loop.*/

#include<stdio.h>
#include<stdlib.h>
 void main()
{
int n,s=0;
printf("\nEnter The number : ");
scanf("%d", &n);
for(;n > 0;s+=n%10,n/=10); //Calculation of sum of digits
printf("\nSum of digits is = %d\n", s);
system("pause");
}


Screenshot:
C / C++ Program Code to Calculate the sum of digits in a single line of code, iteratively in for loop statement

0 comments: