JAVA Exp 2
JAVA Exp 2
import java.io.*;
import java.util.Scanner;
class OddEven
{
public static void main(String[] args)
{
int n;
System.out.print("Enter any number");
Scanner r=new Scanner(System.in);
n=r.nextInt();
if(n%2==0)
{
System.out.print("Entered number is Even
Number");
}
else
{
System.out.print("Entered number is Odd
number");
}
}
}
Output:
Program:
public class ReverseNo
{
public static void main(String[] args)
{
int number = 987654, reverse = 0;
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number/10;
}
System.out.println("The reverse of the given number
is: " + reverse);
}
}
Output:
Program:
import java.util.*;
class Fibonacci
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
}
}
Output:
Program:
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of
String class
Scanner in = new Scanner(System.in);
System.out.println("Enter a string/number to
check if it is a palindrome");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("Entered string/number is
a palindrome.");
else
System.out.println("Entered string/number
isn't a palindrome.");
}
}
Output:
Program:
public class RightTrianglePattern
{
public static void main(String args[])
{
//i for rows and j for columns
//row denotes the number of rows you want to print
int i, j, row=6;
//outer loop for rows
for(i=0; i<row; i++)
{
//inner loop for columns
for(j=0; j<=i; j++)
{
//prints stars
System.out.print("* ");
}
//throws the cursor in a new line after printing
each line
System.out.println();
}
}
}
Output:
Program:
import java.util.Scanner;
public class Operations
{
public static void main(String args[])
{
Scanner y = new Scanner(System.in);
while(true)
{
System.out.println("");
System.out.println("Enter the two numbers to
perform operations ");
System.out.print("Enter the first number : ");
int x = y.nextInt();
System.out.print("Enter the second number : ");
int c = y.nextInt();
System.out.println("Choose the operation you want to
perform ");
System.out.println("Choose 1 for ADDITION");
System.out.println("Choose 2 for SUBTRACTION");
System.out.println("Choose 3 for MULTIPLICATION");
System.out.println("Choose 4 for DIVISION");
System.out.println("Choose 5 for MODULUS");
System.out.println("Choose 6 for EXIT");
int n = y.nextInt();
switch(n)
{
case 1:
int add;
add = x + c;
System.out.println("Result : "+add);
break;
case 2:
int sub;
sub = x - c;
System.out.println("Result : "+sub);
break;
case 3:
int mul;
mul = x * c;
System.out.println("Result : "+mul);
break;
case 4:
float div;
div = (float) x / c;
System.out.println("Result : "+div);
break;
case 5:
int mod;
mod = x % c;
System.out.println("Result : "+mod);
break;
case 6:
System.exit(0);
}
}
}
}
Output: