/* 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:
0 comments:
Post a Comment