C program to display the series x+3x+5x+7x+....

Problem:

Write a c program to display the series x+3x+5x+7x+.... and to find the corresponding sum of all elements.

Solution:

Here is the solution of c program to display the series x+3x+5x+7x+....
  1. Start
  2. Accept the value of x from user.
  3. Accept how many terms to display in the series.
  4. Use for loop and initialize the counter variable i=1, increment it by 2 every time to get odd numbers.
  5. Multiply counter and variable 'X' to get corresponding element in the series.
  6. Display element every time.
  7. Add element to sum and assign it to sum as sum=sum+r, where r is the element.
  8. Lastly display addition.
  9. Stop.

C Program / Source Code :

Here is the source code of C program to display series x+3x+5x+7x+....

/* Aim: Write a program to accept real number x and integer n and calculate sum of 
first n numbers of the series x+3x+5x+7x+..... */

#include<stdio.h>
void main()
{ 
	int x,r,i,sum,n;
 
// Accept value of x
	printf("\n Enter any number:- ");
	scanf("%d",&x);
 
// Accept how many terms to display in the series
	printf("\n Numbers of terms in series:- ");
	scanf("%d",&n);

		for(i=1;i<=(n+4);i=i+2)
		{  
		// calculate the element
		r=i*x;
		if(i==1)
		{
		printf("\n The Series is as below:\n \n");
		}
		// Display the element
		printf(" %d  ",r);
		// Add element to the sum varible
		sum+=r; 
		}
	printf("\n"); 
		// Display the final sum
	printf("\n The sum of all terms in series is \'%d\' \n \n",sum);

} // End of the code 

/* Output of above code:-

 Enter any number:- 1

 Numbers of terms in series:- 10

 The Series is as below:
 
 1   3   5   7   9   11   13  

 The sum of all terms in series is '49' 
 
*/