Display Integer as Sequence of Digits and Find Sum of its Digits


/*
Write a program to display integer as sequence of digits and find sum of its digits

*/
 
#include<stdio.h>

void display(long int n);
void Rdisplay(long int n);
int sumdigits( long int n);


main()
{
long int num;
printf("Enter number : ");
scanf("%ld", &num);
printf("%d\n",sumdigits(num));
printf("\n");
display(num);
printf("\n");
Rdisplay(num);
printf("\n");
}


/*sumdigits*/

int sumdigits(long int n)
{
if( n/10 == 0 ) /* if n is a single digit number*/
return n;
return n%10 + sumdigits(n/10);
}




/*Display*/

void display(long int n)
{
if( n/10==0 )
{
printf("%d",n);
return;
}
display(n/10);
printf("%d",n%10);
}



/*Rdisplay*/
void Rdisplay(long int n)
{
if(n/10==0)
{
printf("%d",n);
return;
}
 

printf("%d",n%10);
Rdisplay(n/10);

}

Post a Comment

0 Comments