1. Create a program using for loop that will produce each of the following sequences:
a. 6, 8, 10, 12, ..., 66
b. 7, 9, 11, 13,..., 67
Answers
Solution to question 1:
public class Main {
public static void main(String[] args) {
for(int i=6;i<=66;i+=2)
System.out.print(i+" ");
}
}
Logic:
- Loop for i = 6 to 66. STEP = 2
- Print the value of i.
Solution to question 2:
public class Main {
public static void main(String[] args) {
for(int i=7;i<=67;i+=2)
System.out.print(i+" ");
}
}
Logic:
- Loop for i = 7 to 67. STEP = 2
- Print the value of i.
The given cødes are written in Java. See the attachments for output.
•••♪
Answer:
1. Create a program using for loop that will produce each of the following sequences:
a. 6, 8, 10, 12, ..., 66
public class Forloops{
public static void main(String [] args){
for(int i=6; i<=66; i+=2){
System.out.println(i);
}
}
}
b. 7, 9, 11, 13,..., 67
public class Forloops{
public static void main(String [] args){
for(int i=7; i<=67; i+=2){
System.out.println(i);
}
}
}
Explanation: