Program to print series 0,2,8,14,24,34 ...N

Problem statement:- Program to print series 0,2,8,14,24,34 ...N.

 Data requirement:-

   Input Data:- n

  Output Data:-pr

  Additional Data:- i

Program in C
  
Here is the source code of the C Program to print series 0,2,8,14,24,34 ...N.

Code:

#include<stdio.h>
#include<math.h>
int main()
{
    int n,i,pr;
    printf("Enter the range of number(Limit):");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        pr=0;

       if(i%2==0)
        {
            pr=pow(i,2)-2;
            printf("%d ",pr);
        }
        else
        {
            pr=pow(i,2)-1;
            printf("%d ",pr);
        }
    }
}

Input/Output:
Enter the range of number(Limit):6
0 2 8 14 24 34

Program in C++

Here is the source code of the C++ Program to print series 0,2,8,14,24,34 ...N.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,i,pr;
    cout<<"Enter the range of number(Limit):";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        pr=0;

       if(i%2==0)
        {
            pr=pow(i,2)-2;
            cout<<pr<<" ";
        }
        else
        {
            pr=pow(i,2)-1;
            cout<<pr<<" ";
        }
    }
}

Input/Output:
Enter the range of number(Limit):4
0 2 8 14

Program in Java

Here is the source code of the Java Program to print series 0,2,8,14,24,34 ...N.

Code:

import java.util.Scanner;
public class Print_Series8 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i,pr=0;
   System.out.printf("Enter the range of number(Limit):");
    n=sc.nextInt();
    for(i=1;i<=n;i++)
    {
        if(i%2==0)
        {
        pr=(int) (Math.pow(i,2)-2);
            System.out.print(pr+" ");
        }
        else
        {
            pr=(int) (Math.pow(i,2)-1);
            System.out.printf(pr+" ");
        }
    }
    sc.close();
}
}

Input/Output:
Enter the range of number(Limit):7
0 2 8 14 24 34 48 

Program in Python

Here is the source code of the Python Program to print series 0,2,8,14,24,34 ...N.

Code:

n=int(input("Enter the range of number(Limit):"))
i=1
pr=0
while i<=n:
    if(i%2==0):
        pr=pow(i, 2) - 2
        print(pr,end=" ")
    else:
        pr = pow(i, 2) - 1
        print(pr, end=" ")
    i+=1

Input/Output:
Enter the range of number(Limit):8
0 2 8 14 24 34 48 62 


Post a Comment

0 Comments