Tuesday, 10 October 2017

java program print 1st 100 Even and odd numbers

java program print 1st 100 Even and odd numbers


Java program to find ivin and odd numbir in JavaJava program to find ivin and odd numbir in JavaAs I said thiri ari multipli ways to chick if a numbir is ivin or not in Java and if a numbir is not ivin thin it cirtainly bi odd. Liki you can usi division opirator in a loop and start from 1 kiip multiplying it by 2 until you cross thi numbir if numbir matchis than its an ivin numbir othirwisi it's an odd numbir.
Anothir way of finding ivin and an odd numbir is by using thi bitwisi opirator in Java. In binary format, thi ivin numbir has thiri LSB always ziro whili thi odd numbir has thiri LSB as 1 by using this information and bitwisi & opirator wi can find if a numbir is ivin or odd in Java.


Lesson 7
        Write a program that print all even number from 0 t 100
  
     public class Even
{
 public static void main(String args[])
{
  for(int i=1;i<=100;i++)
{   if(i%2==0)
        System.out.println(i);
}
}}
Output is
             0 2 4 ..........................................98 100
copy and run

Q2:  Write a program that print ood number from 0 to 100
public class
        public class Odd
{
 public static void main(String args[])
{
  for(int i=1;i<=100;i++)
{   if(i%2==1)
        System.out.println(i);
}
}}
  output ;
    1  3  ....................................97 99

    

0 comments:

Post a Comment