Program to print series -1 4 -7 10 -13 16 -19...n

Problem statement:- Program to print series -1 4 -7 10 -13 16 -19...n

 Data requirement:-

   Input Data:- n

  Output Data:-se

  Additional Data :-i

Program in C
  
Here is the source code of the C Program to print series -1 4 -7 10 -13 16 -19...n

Code:

#include<stdio.h>
int main()
{
    int n,i=1,se=1;
    printf("Enter the range of number(Limit):");
    scanf("%d",&n);
    while(i<=n)
    {
        if(i%2==0)
        {
            printf("%d ",se);
        }
        else
        {
            printf("%d ",-1*se);
        }
     se+=3;
     i++;
    }
}

Input/Output:
Enter the range of number(Limit):7
-1 4 -7 10 -13 16 -19

Program in C++
  
Here is the source code of the C++ Program to print series -1 4 -7 10 -13 16 -19...n

Code:

#include<iostream>
using namespace std;
int main()
{
    int n,i=1,se=1;
    cout<<"Enter the range of number(Limit):";
    cin>>n;
    while(i<=n)
    {
        if(i%2==0)
        {
            cout<<se<<" ";
        }
        else
        {
           cout<<-1*se<<" ";
        }
     se+=3;
     i++;
    }
}

Input/Output:
Enter the range of number(Limit):5
-1 4 -7 10 -13

Program in Java

Here is the source code of the Java Program to print series -1 4 -7 10 -13 16 -19...n

Code:

import java.util.Scanner;
public class p16 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in); 
int n,i=1,se=1;
    System.out.println("Enter the range of number(Limit):");
    n=cs.nextInt();
   
    while(i<=n)
    {
        if(i%2==0)
        {
          System.out.print(se+" ");
        }
        else
        {
        System.out.print(-1*se+" ");
        }
     se+=3;
     i++;
    }
     cs.close();
}
}

Input/Output:
Enter the range of number(Limit):
10
-1 4 -7 10 -13 16 -19 22 -25 28 

Program in Python
  
Here is the source code of the Python Program to print series -1 4 -7 10 -13 16 -19...n

Code:

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

Input/Output:
Enter the range of number(Limit):
16
-1 4 -7 10 -13 16 -19 22 -25 28 -31 34 -37 40 -43 46 

Post a Comment

0 Comments