Write different programs to print the given series:
1000 81 512 49 216 25 64 9 8 1
Answers
Using JAVA:
Program:
public class oh {
public static void main(String[] args) {
/*
* 1000 81 512 49 216 25 64 9 8 1
*/
int a = 10;
int j = 9;
for (int i = 1; i < 6; i++) {
System.out.print(Math.round(Math.pow(a, 3)) + " ");
System.out.print(Math.round(Math.pow(j, 2)) + " ");
a = a - 2;
j = j - 2;
}
}
}
Using Python:
Program:
a=10
b=9
for i in range (1,6):
print(a**3, b**2, end=" ")
a-=2
b-=2
Explanation:
In JAVA I used .round method to convert decimal to int.
Required Answer:-
Question:
- Write a program to print the series:- 1000 81 512 49 216 25 64 9 8 1
Solution:
This is written in Java.
- public class JavaBrainly {
- public static void main(String[] args) {
- int a=3, b=2;
- for(int i=10;i>=1;i--)
- {
- System.out.print((int)Math.pow(i,a)+" ");
- int c=a;
- a=b;
- b=c;
- }
- }
- }
This is written in Python.
- a,b=3,2
- for i in range(10,0,-1):
- print(i**a,end=" ")
- a,b=b,a
Explanation:
Logic is as follows,
1000 = 10³
81 = 9²
512 = 8³ and so on.
So, it's like n³, n²
After each iteration, variable 'i' is raised to the power a where a = 3 and b = 2. At the same time, the values of a and b are swapped. So, variable 'i' is raised to the power 3 and then to the power 2, then again 3,2 and so on.
Output is attached for verification.