Computer Science, asked by electricbeef1, 5 months ago

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 anindyaadhikari13
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.

  1. public class JavaBrainly {
  2. public static void main(String[] args) {
  3. for(int i=1;i<=20;i++)
  4. {
  5. if(i%2==0)
  6. continue;
  7. System.out.print(i+" ");
  8. }
  9. }
  10. }

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.
RockingStarPratheek: Awesome Answer !!
Answered by shivangiroy27
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