C Program to Print Digit Sum Using While Loop

Ex: write a C program to print digit sum. How to write a C program to print digit sum. C program to print digit sum.


Input from user:

Enter the number: 145

Expected output:

Sum of digit=10





  Step by step logic of the given program:


1. Accept number from user.

2. Use while loop to check given number is greater than zero or not.

3.  Enter given digit sum logic inside the while loop.

z=no%10;
sum=sum+z;
no=no/10;


4. Print sum outside the while loop.





  C program to print digit sum using while loop:


#include<stdio.h>
void main()
{
int no,z,sum=0;

printf("Enter the number:");
scanf("%d",&no);

while(no>0)
{
z=no%10;
sum=sum+z;
no=no/10;
}
printf("sum of digit=%d",sum);

}



Above prgram shows the following output:

C program to print digit sum using while loop



No comments:

Post a Comment

If you have any doubts, please discuss here...👇