/* 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:
#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 C / C++ Program Code to Calculate the sum of digits in a single line of code, iteratively in for loop statement](http://1.bp.blogspot.com/-0bxFUslW09s/UqhFLvH7joI/AAAAAAAAKDc/zvdFNZtcrKo/s1600/sum+of+digits.png)
0 comments:
Post a Comment