Computer Science, asked by anmolmohata018, 23 days ago

write a program to Print all the natural number from 1 to 15 excluding the even numbers.​

Answers

Answered by Anonymous
4

Logic:

  • We will solve the given problem using for loop.
  • Initialise a for loop with starting value 1 and ending value 15 with step size equal to 1.
  • Now using if statement, we will check if the value is divisible by 2 or not. If not, we will print the value.
  • % is an operator to find remainder when a certain number is divided by another.

Program in python:

for i in range(1, 16):

  if i%2!=0:

     print(i)

Program in java:

public class Main {

 public static void main(String[] args) {

   for (int i = 1; i <= 15; i ++) {

   if (i%2 !=0){

     System.out.println(i);

   }

  }

 }

}

Additional information:

We know that even and odd numbers are always consecutive numbers, therefore in order to solve the given problem we can initialise the loop with starting value 1 and ending value 15 with step size 2. This will also work.

Similar questions