Write a program to print square of odd numbers between
21 and 40 ( both numbers included ) using while loop ?
Answers
Answer:
The given program is written in Java.
public class Squares {
public static void main(String args[]) {
int i=21;
System.out.println("Squares of numbers in the range (21-40) are as follows..");
while(i<=40){
System.out.print(i*i+" ");
++i;
}
}
}
Algorithm:
- Looping through numbers in the range (21-40).
- Displaying the square of each number.
Refer to the attachment for output.
•••♪
Answer:
#The following program is written in python
for I in range(21,41):
if i%2!=0 :
print(I*I)
# or print (I * * 2) both are same
Shortest approach:-
for I in range(21,41,2):
print(I * * 2)
___
//The following program is written in Java
public class OddSqrPrint{
public static void main (String ar[]){
for(I=21;I<=41;I++){
if(I%2!=0)
System.out.println(I*I);
// or Math.pow(I,2) both are same
}
}
Shortest Approach:-
public class OddSqrPrint{
public static void main (String ar[]){
for(I=21;I<=41;I+=2){
System.out.println(I*I);
}
}