Write a program to display all odd numbers between 100 and 200
Answers
Answer:
public class odd
{
Public static void main(String args[]}
{
Int n=100;
Int p=0;
System.out.println("The odd numbers are");
While(n<=198)
{
If(n%2==0)
{
p=n+1;
System.out.println(p);
}
n++;
}
}
}
Explanation:
Answer:
Program in JAVA:
public class OddNumbers
{
public static void main (String args[])
{
int x;
for ( x = 100; x < = 200; x ++)
{
if ( x % 2 != 0)
System.out.print ( x + ", ");
}
}
}
Program in C++:
#include<iostream.h>
#include<conio.h>
void main ( ) {
int x;
for ( x = 100; x < = 200; x ++)
{
if ( x % 2 != 0)
cout<< x<< " ";
}
getch ( );
}
Explanation:
In this question, it is said to display all the ODD numbers between 100 and 200. Now using FOR LOOP we can initialize (assign first value) 100 in variable 'X' and keep the loop running till the values in variable 'X' is being 200 by increasing the value of X by 1 (i.e x++), so that we can get all the numbers from 100 to 200 in variable X one by one. Then we need to check the each value in variable 'X' as to whether it is ODD or not. For that, we need to use modulus (%) i.e it finds remainder. So, the statement " if ( x % 2 ! = 0) " checks whether value in variable X when divided by 2, produces the remainder other than '0' (zero) or not. As we know that the ODD numbers are not divisible by 2. So, if an odd number is divided by 2, it produces 1 as remainder and not '0'. Therefore, the statement ' if (x%2!=0)' will either gives the result as TRUE or FALSE based on the value of variable 'x'. So, if the result is TRUE that means the value is not divisible by 2 and it is an ODD number. So we can easily print the value of X.