C program to Calculate 1 2 3 6 9 18 27 54... series

In c language you can print any number series. Here i will show you how to print number series in c language with explanation. In case of print number series you need to focus on common difference between two numbers..

C program to Calculate 1 2 3 6 9 18 27 54... series




#include<stdio.h>
int main()
{
    int a=1,b=2,i,n=10;
    printf("%d %d ",a, b);
    for(i=3;i<=n;i++)
    {
        if(i%2==1)
        {
            a=a*3;
            printf("%d ",a);
        }
        else
        {
            b=b*3;
            printf("%d ",b);
        }
    }
    return 0;
}



Output:


1 2 3 6 9 18 27 54 81 162




#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b,i,n;
    printf("Size of series:");
    scanf("%d",&n);
    printf("Enter a value:");
    scanf("%d",&a);
    printf("Enter b value:");
    scanf("%d",&b);
    printf("%d %d ",a, b);
    for(i=3;i<=n;i++)
    {
        if(i%2==1)
        {
            a=a*3;
            printf("%d ",a);
        }
        else
        {
            b=b*3;
            printf("%d ",b);
        }
    }
    return 0;
}




Output:

Size of series:20
Enter a value:2
Enter b value:3
2 3 6 9 18 27 54 81 162 243 486 729 1458 2187 4374 6561 13122 19683 39366 59049
Process returned 0 (0x0) execution time : 5.522 s




Instagram