Program in C to find the sum of digits of a three digit number

Write a program in C to find the sum of digits of a three digit number


#include <stdio.h>
#include <math.h>
#include <conio.h>

main()
{
            int d,d1,d2,d3,r1,r2,sum;
            clrscr();
            printf("\n enter any three digit no's");
            scanf("%d",&d);
            d1=d/100;
            r1=d%100;
            if(r1!=0)
            {
                        d2=r1/10;
                        r2=r1%10;
                        if(r2!=0)
                                    d3=r2;
                        else
                                    d3=0;
            }          
            else
            {
                        d2=0;
                        d3=0;
            }
            sum=d1+d2+d3;
            printf("\n sum of 3 digit no is %d",sum);
            getch();
}


Post a Comment