Saturday, 5 March 2022

5. Write a program to find sum of the digits entered by the user.


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

void main()
{
    long int no,i,sum=0,rem,ary[99],k=0;
    clrscr();

    printf("enter digit: ");
    scanf("%ld",&no);

    for(i=0; no>0; i++)
    {
        rem = no % 10;
        ary[i] = rem;
        sum = sum + rem;
        no = no / 10;
        k++;
    }

    printf("\n\n");
    for(i=k-1; i>=0; i--)
    {
        printf("%ld + ",ary[i]);
    }
    printf("\b\b= %ld",sum);

    getch();
}

/*      output:

enter digit: 123456


1 + 2 + 3 + 4 + 5 + 6 = 21

*/ 

No comments:

Post a Comment

python programs

1. sum of two number