Wednesday 26 February 2014

9: series programms

//Write a c program to find out the sum of series 1 + 2 +  + n.
Sum of 1 + 2 +  + n series in c programming language
#include<stdio.h>
int main(){
    int n,i;
    int sum=0;
    printf("Enter the n i.e. max values of series: ");
    scanf("%d",&n);
    sum = (n * (n + 1)) / 2;
    printf("Sum of the series: ");
    for(i =1;i <= n;i++){
         if (i!=n)
             printf("%d + ",i);
         else
             printf("%d = %d ",i,sum);
         }
 
    return 0;
}
Sample output:
Enter the n i.e. max values of series: 5
Sum of the series: 1 + 2 + 3 + 4 + 5 = 15//Write a c program to find out the sum of series 1^2 + 2^2 +  + n^2.
#include<stdio.h>
int main(){
    int n,i;
    int sum=0;
    printf("Enter the n i.e. max values of series: ");
    scanf("%d",&n);
    sum = (n * (n + 1) * (2 * n + 1 )) / 6;
    printf("Sum of the series : ");
    for(i =1;i<=n;i++){
         if (i != n)
             printf("%d^2 + ",i);
         else
             printf("%d^2 = %d ",i,sum);
    }
    return 0;
}
Sample output:
Enter the n i.e. max values of series: 5
Sum of the series: 1^2 + 2^2 + 3^2 + 4^2 + 5^2 = 55//Write a c program to find out the sum of in A.P. series
#include<stdio.h>
#include<math.h>
int main(){
    int a,d,n,i,tn;
    int sum=0;
    printf("Enter the first number of the A.P. series: ");
    scanf("%d",&a);
    printf("Enter the total numbers in the A.P. series: ");
    scanf("%d",&n);
    printf("Enter the common difference of A.P. series: ");
    scanf("%d",&d);
    sum = ( n * ( 2 * a + ( n -1 ) * d ) )/ 2;
    tn = a + (n-1) * d;
    printf("Sum of the series A.P.: ");
    for(i=a;i<=tn; i= i + d ){
         if (i != tn)
             printf("%d + ",i);
         else
             printf("%d = %d ",i,sum);
    }
    return 0;
}
Sample output:
Enter the first number of the A.P. series: 1
Enter the total numbers in the A.P. series: 5
Enter the common difference of A.P. series: 3
Sum of the series: 1 + 4 + 7 + 10 + 13 = 35//Write a c program to find out the sum of G.P series
#include<stdio.h>
#include<math.h>
int main(){
    float a,r,i,tn;
    int n;
    float sum=0;
    printf("Enter the first number of the G.P. series: ");
    scanf("%f",&a);
    printf("Enter the total numbers in the G.P. series: ");
    scanf("%d",&n);
    printf("Enter the common ratio of G.P. series: ");
    scanf("%f",&r);
    sum = (a*(1 - pow(r,n+1)))/(1-r);
       tn = a * (1 -pow(r,n-1));
    printf("tn term of G.P.: %f",tn);
    printf("\nSum of the G.P.: %f",sum);
    return 0;
}
Sample output:
Enter the first number of the G.P. series: 1
Enter the total numbers in the G.P. series: 5
Enter the common ratio of G.P. series: 2
tn term of G.P. : 16.000000
Sum of the G.P. : 63.000000//Write a c program to find out the sum of infinite G.P. series
Definition of geometric progression (G.P.):
A series of numbers in which ratio of any two consecutive numbers is always a same number that is constant. This constant is called as common ratio.
#include<stdio.h>
int main(){
    float a,r;
    float sum=0;
    printf("Enter the first number of the G.P. series: ");
    scanf("%f",&a);
    printf("Enter the common ratio of G.P. series: ");
    scanf("%f",&r);
    if(1 > r)
         sum = a/(1-r);
    else
         sum = a/(r-1);
    printf("\nSum of the infinite G.P. series: %f",sum);
    return 0;
}
Sample output:
Enter the first number of the G.P. series: 1
Enter the common ratio of G.P. series: .5
Sum of the infinite G.P. series: 2.000000
Enter the first number of the G.P. series: 5
Enter the common ratio of G.P. series: 2
Sum of the infinite G.P. series: 5.000000//Write a program to calculate the tan series
#include
#include
int main(void)
{
double x,pi=3.1415926535;
clrscr();
x=tan(pi/4);
printf("\n\t*****TAN SERIES*****");
printf("\n\n\tTan(%f)=%f\n",pi/4,x);
getch();
return;
}
OUTPUT:
*****TAN SERIES*****
Tan(0.785398)=1.000000

No comments:

Post a Comment