Thursday, February 1, 2024

C Language - Loop Control Statements - Do While Loop - Write a program to print the sum of digit of a given number

Question: Write a program to print the sum of digit of a given number.

Solution:

#include <stdio.h>

#include <conio.h>

void main()

{

            int digit;

            unsigned long num,sum=0;

            clrscr();

            printf("\nENTER A NUMBER: ");

            scanf("%lu",&num);

            do

            {

                        digit=num%10;

                        num=num/10;

                        sum=sum+digit;

            }

            while(num!=0);

            printf("\nSUM OF THE DIGITS IS %lu",sum);

            getch();

}

No comments: