Computer Science, asked by Rik3026, 20 days ago

Write a program in java to print the given output
a. 1 2 4 8 16 32 64
128
256 512​

Answers

Answered by anindyaadhikari13
1

Solution:

The given program is written in Java.

import java.util.*;

public class Series {

   public static void main(String args[])  {

       int a=1,n=10,i=1;

       for(;i<=n;i++,a*=2)

           System.out.print(a+" ");

   }

}

Logic:

  • Initialise a = 1.
  • Iterate loop ten times.
  • Display the value of a.
  • Multiply a with 2 after each iteration.

See the attachment for output.

Attachments:
Answered by kamalrajatjoshi94
1

Answer:

Program:-

public class Main

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

int i,temp=2,n;

System.out.println("Enter the limit:");

n=in.nextInt();

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

{

System.out.print(temp+" ");

temp=temp*2;

}

}

}

Output is attched.

Logic:-

  • A loop for the number of iterations and a variable temp=2 for the series
  • Each term of the series term=term*2 and it prints 2 4 8 16 32..upto n terms.

Attachments:
Similar questions