C program to display sum of digits till it becomes single digits

 

C program to display sum of digits till it becomes single digits


/***********************************************

C program to display sum of digits till it becomes single digits

for example 1234

        sum=1+2+3+4

        sum=1

**********************************************/

# include<stdio.h>

#include<conio.h>

void main()

{

    int n,rem,sum=0;

    //clrscr();

    printf("Enter number\n");

    scanf("%d",&n);

    printf("\nThe sum of digits of %d is:",n);

    while(n/10!=0)

    {

    sum=0;

    while(n!=0)

    {

        rem=n%10;

        sum=sum+rem;

        n=n/10;

    }

    n=sum;

    }

    printf("%d\n",sum);

    printf("End of program");

}

Enter number

1234


The sum of digits of 1234 is:1

End of program


टिप्पणी पोस्ट करा

0 टिप्पण्या