Home / Programs / Program to display integer as sequence of digits and find sum of its digits
🚀 Programming Example

Program to display integer as sequence of digits and find sum of its digits

👁 746 Views
💻 Practical Program
📘 Step Learning
Program to display integer as the sequence of digits and find the sum of its digits

💻 Program Code

#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");
}/*End of main()*/

/*Finds the sum of digits of an integer*/
int sumdigits(long int n)
{
if( n/10 == 0 ) /* if n is a single digit number*/
	return n;
return n%10 + sumdigits(n/10);
}/*End of sumdigits()*/

/*Displays the digits of an integer*/
void display(long int n)
{
if( n/10==0 )
	{
	printf("%d",n);
	return;
	}
display(n/10);

printf("%d",n%10);
}/*End of display()*/

/*Displays the digits of an integer in reverse order*/
void Rdisplay(long int n)
{
if(n/10==0)
	{
	printf("%d",n);
	return;
	}
printf("%d",n%10);

Rdisplay(n/10);
}/*End of Rdisplay()*/
                        

🖥 Program Output

Enter number : 10
1

10
01
Press any key to continue . . .
                            

📘 Explanation

None
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.