Home / Programs / Program to display numbers from 1 to n and their sum
🚀 Programming Example

Program to display numbers from 1 to n and their sum

👁 796 Views
💻 Practical Program
📘 Step Learning
Program to display numbers from 1 to n and their sum

💻 Program Code

#include<stdio.h>
int summation(int n);
void display1(int n);
void display2(int n);
main( )
{
int n;
printf("Enter number of terms : ");
scanf("%d", &n);
display1(n);
printf("\n");
display2(n);
printf("\n");
printf("sum = %d\n", summation(n));
}/*End of main()*/

int summation( int n)
{
if(n==0)
	return 0;
return ( n + summation(n-1) );
}/*End of summation()*/

/*displays in reverse order*/
void display1(int n)
{
if( n==0 )
	return;
printf("%d ",n);

display1(n-1);

}/*End of display1()*/


void display2(int n)
{
if( n==0 )
	return;
	
display2(n-1);

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

🖥 Program Output

Enter number of terms : 10
10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10
sum = 55
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.