write a program in java to display all odd numbers from 1 to 20 by using continue statement in while loop
Attachments:
Answers
Answered by
4
Required Answer:-
Question:
- Write a Java program to display all odd numbers from 1 to 20 using continue statement.
Solution:
Here is the code.
- public class JavaBrainly {
- public static void main(String[] args) {
- for(int i=1;i<=20;i++)
- {
- if(i%2==0)
- continue;
- System.out.print(i+" ");
- }
- }
- }
Explanation:
- We are asked to print all the odd numbers from 1 to 20 using continue statement. The continue statement breaks the current iteration and moves to the next iteration. So, if a number is even, the continue statements moves to the next iteration without printing the value.
Attachments:
Anonymous:
Perfect :) Keep attaching the images as well as output.
Answered by
0
Answer:
you have different explanations on different languages here is the some.
C LANGUAGE:
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=20;i+=2)printf(“%d\n”,i);
for(i=1;i<=20;i+=2)printf(“%d\n”,i);
return 0;
}
CPP:
#include<iostream.h>
using namespace std;
int main(){
int i;
for(i=0;i<=20;i+=2)cout<<i<<endl;
for(i=1;i<=20;i+=2)cout<<i<<endl;
return 0;
}
PYTHON:
print “this is a even number list”
even_list=[]
odd_list=[]
for i in Xrange(20):
if i%2==0:
print i
even_list.append(i)
print “this is a odd number list”;
for i in Xrange(20):
if i not in even_list:
odd_list.append(i)
print odd_list
logic behind each section is same and is simple.
Similar questions