write a program to print all the odd numbers between 1 to 21
urgent please
Answers
Answer:
public class anyname{
int i;
public void method(){
for(i = 1; i <= 21; i++){
if(i%2 == 0){
i = i + 1;
System.out.println(i);
} else {
System.out.println(i);
}
}
}
}
Explanation:
ok so here what I did is I took integer variables i cuz all even and odd numbers will be in integer form
after that, I used for loop method as it will keep on printing and checking conditions till it doesn't reach the last number limit
for loop syntax
for(variable = value; condition; calculate)
in for loop, I kept the syntax i = 1 so I am storing some values here,
i <= 21 is a condition and if it's true it will keep on printing and adding
now it will check if the number is divisible by 2, if it is then it will add + 1 to the number and make it odd and if the condition is not divisible by 2 then it will print till it reaches 21 number
If u understood it then pls put 5 or 4 stars and heart the answer :D
it will mean a lot to me
★
public class Main { // Line 1
public static void main(String [] args) { // Line 2
for(int i = 1; i < 21; i++) { // Line 3
if(i % 2 == 0) { // Line 4
System.out.println(i); // Line 5
} // Line 6
} // Line 7
} // Line 8
} // Line 9
★
✦ Line 1: We declare a public class called Main.
✦ Line 2: The Main Method.
✦ Line 3: We run a for loop for variable i from 1 to 21.
✦ Line 4: If i is divisible by two, the below result gets executed.
✦ Line 5: We print i.
✦ Line 6: End of if block.
✦ Line 7: End of for loop.
✦ Line 8: End of Main method.
✦ Line 9: End of class.