Program to print series 0,6,10,17,22,30,36...N

Problem statement:- Program to print series 0,6,10,17,22,30,36...N

 Data requirement:-

   Input Data:- n

  Output Data:- a, b

  Additional Data:- i, k, p

Program in C
  
Here is the source code of the C Program to Program to print series 0,6,10,17,22,30,36...N.


Code:

#include<stdio.h>
int main()
{
    int n,i,a=0,b=6,k=10,p=11;
    printf("Enter the range of number:");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {

    if(i%2==0){
        printf("%d ",b);
        b+=p;
        p+=2;
     }
     else
     {
         printf("%d ",a);
         a+=k;
         k+=2;
         }
    }
}

Input/Output:
Enter the range of number:8
0 6 10 17 22 30 36 45

Program in C++
  
Here is the source code of the C++ Program to Program to print series 0,6,10,17, 22,30,36...N.

Code:

#include<iostream>
using namespace std;
int main()
{
    int n,i,a=0,b=6,k=10,p=11;
    cout<<"Enter the range of number:";
    cin>>n;

    for(i=1;i<=n;i++)
    {

    if(i%2==0){
        cout<<b<<" ";
        b+=p;
        p+=2;
     }
     else
     {
         cout<<a<<" ";
         a+=k;
         k+=2;
         }
    }
}

Input/Output:
Enter the range of number:8
0 6 10 17 22 30 36 45

Program in Java
  
Here is the source code of the Java Program to Program to print series 0,6,10,17,22,30, 36...N.

Code:

import java.util.Scanner;
public class p30 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,i,a=0,b=6,k=10,p=11;
    System.out.println("Enter the range of number:");
    n=cs.nextInt();
    for(i=1;i<=n;i++)
    {

    if(i%2==0){
        System.out.print(b+" ");
        b+=p;
        p+=2;
     }
     else
     {
    System.out.print(a+" ");
         a+=k;
         k+=2;
         }
    }
    cs.close();
}
}

Input/Output:
Enter the range of number:
12
0 6 10 17 22 30 36 45 52 62 70 81 

Program in Python
  
Here is the source code of the Python Program to Program to print series 0,6,10,17,22,30, 36...N.

print("Enter the range of number(Limit):")
n=int(input())
i=1
a=0
b=6
k=10
p=11
while(i<=n):
    if (i % 2 == 0):
        print(b,end=" ")
        b += p
        p += 2
    else:
        print(a,end=" ")
        a += k
        k += 2
    i+=1

Input/Output:
Enter the range of number(Limit):
30
0 6 10 17 22 30 36 45 52 62 70 81 90 102 112 125 136 150 162 177 190 206 220 237 252 270 286 305 322 342 

Post a Comment

1 Comments

  1. class ap
    {
    static void main(int n)
    {
    int a=4,b=6,f=0;
    System.out.print(f+" ");
    for (int i=2;i<=n;i++)
    {
    if(i%2==0)
    {
    System.out.print((f+=b)+" ");
    b++;
    }
    else
    {
    System.out.print((f+=a)+" ");
    a++;
    }
    }
    }
    }

    this is the better version. join my class

    ReplyDelete

Please do not Enter any spam link in the comment box