/*Arithmetic Series up to n terms*/
/*mechatrofice*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a,d,n,i,s;
clrscr();
printf("First Term,a=");
scanf("%d",&a);
printf("Common difference,d=");
scanf("%d",&d);
printf("N term,n=");
scanf("%d",&n);
if(n==0)
{
a=0;
d=0;
}
printf("Arithematic series upto n terms =\t%d\t",a);
for(i=1;i<n;i++){
s=a+d;
printf("%d\t",s);
a=s;
}
getch();
}
|
---|
C program source code to Print Arithmetic Series up to 'n' terms.
Above for loop also can be replaced by the code below. Both have the same output.
for(i=2;i<n;i++){
s=a+(i-1)*d ;
printf("%d\t",s);
}
|
---|
Output
In this program after reading the input, conditional operator checks whether 'n' term is zero or not. In arithmetic progression terms count begin from one,for a zero 'n' term 'if ' statement assign the value zero to both common difference and first term, so the output will be zero. if 'n' term is not zero, the statement will be ignored or it will not make any change to values of 'a' and 'd' as 'n==0' is a false condition.
The value of 'a' print directly as the first term is an input assigned to 'a'. In for loop until ' i' is not less than 'n' the sum of first term 'a' and common difference 'd' print as next term, then the value of 's' assigned to 'a', it again repeats to print next value up to n terms. The loop contin ues till i <n and the value of 's' print in each loop.
See also :