Computer Science, asked by anindyaadhikari13, 6 months ago

Challenge of the week.
Write a Java program to display the following series.
1 -2 3 -4 5 -6..... N terms.


Input:-
Enter the value of n - 10

Output:-
1 -2 3 -4 5 -6 7 -8 9 -10

Restrictions:-
1. Not to use if-else.
2. Not to use ternary operator.
3. Not to use switch case.
4. Maximum number of variables you can use:- 3
5. Maximum number of print statement you can use:- 2(1 for taking input and other for displaying the series)
6. Not to use more than 1 loop.

Solve it if you can but don't post irrelevant things.

*User chinnu123488, stay away. Don't dare to give nonsense answers otherwise I will report all your answer.*​

Answers

Answered by Vyomsingh
9

CODE:-

import java.util.*;

class sereis2

{

   public static void main(String[] args)

   {

       Scanner sc=new Scanner(System.in);

       System.out.println("Input the value of n");

       int n=sc.nextInt();

       int b=1;

       for(int a=1;a<=n;a++)

       {

           System.out.print(b+"\t");

           b=Math.abs(b)+1;

           b*=Math.pow(-1,b+1);

       }

   }

}

______________________________

Another Logic:-

import java.util.*;

class sereis2

{

   public static void main()

   {

       Scanner sc=new Scanner(System.in);

       System.out.println("Input the value of n");

       int n=sc.nextInt();

      int b=1;

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

    {

    System.out.print(i*b + " ");

    b*=-1;

    }  

}

}

Output:

Input the value of n

10

1 -2 3 -4 5 -6 7 -8 9 -10

Input the value of n

11

1 -2 3 -4 5 -6 7 -8 9 -10 11

_______________________________________________

Restriction:-

1. Not to use if-else.

2. Not to use ternary operator.  ✔

3. Not to use switch case.  ✔

4. Maximum number of variables you can use:- 3  ✔

5. Maximum number of print statement you can use:- 2(1 for taking input and other for displaying the series)  ✔

6. Not to use more than 1 loop.

Important Point:-

  • I used Scanner class here to insure that we ask user for value of n through keyboard.

  • Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings through keyboard. It is the easiest way to read input in a Java program.

  • Always divide double variable from integer or integer variable from double data type variable or double variable from double variable,To ensure 100% result.
Answered by akumar41864
0

Answer:

\huge\blue{✎﹏Question}✎﹏Question

Q. What conditions should be followed during boiling of vegetables so that vitamins present in them are not destroyed?

\huge\purple{✎﹏Answer}✎﹏Answer

\green{━━━━━━━━━━━━━✯✰★✰✯}━━━━━━━━━━━━━✯✰★✰✯

Water is the enemy when it comes to nutrient losses during cooking. That's why steaming is one of the best methods to preserve easily damaged nutrients, such as vitamin C and many B vitamins. Since vegetables don't come in contact with cooking water during steaming, more vitamins are retained.

\green{✯✰★✰✯━━━━━━━━━━━━━}✯✰★✰✯━━━━━━━━━━━━━

❥Hope \: it\: Helps !!࿐❥HopeitHelps!!࿐

Similar questions