0% found this document useful (0 votes)
4 views7 pages

Javapgmlab

Java and DBMS lab programs

Uploaded by

Durga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Javapgmlab

Java and DBMS lab programs

Uploaded by

Durga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Write a java program to check whether a number is even or odd

import java.io.*;

public class EvenOdd {

public static void main(String[] args) throws IOException

int num ;

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

System.out.println(“Enter a number”);

num=Integer.parseInt(br.readLine());

if(num % 2 == 0)

System.out.println(num + ” is even.”);

else

System.out.println(num + ” is odd.”);

Write a java program to check whether a number is positive,negative or zero

import java.io.*;

public class PosNeg {

public static void main(String[] args) throws IOException

int num ;

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

System.out.println(“Enter a number”);

num=Integer.parseInt(br.readLine());

if(num > 0)

System.out.println(num + ” is positive”);

else if(num < 0)


System.out.println(num + ” is negative”);

else

System.out.println(num + ” is zero”);

Write a java program to execute arithmetic operations based on choice

import java.io.*;

public class choice {

public static void main(String[] args) throws IOException

int a,b,x;

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

System.out.println(“Enter 2 numbers”);

a=Integer.parseInt(br.readLine());

b=Integer.parseInt(br.readLine());

System.out.println(“Enter arithmetic operator:”);

char c=(char)br.read();

switch(c)

case ‘+’:

x=a+b;

break;

case ‘-’:

x=a-b;

break;

case ‘*’:
x=a*b;

break;

case ‘/’:

x=a/b;

break;

case ‘%’:

x=a%b;

break;

default:

System.out.println(“Wrong choice”);

System.out.println(“Value of x=”+x);

Write a java program to find the factorial of a number

import java.io.*;

public class Factorial {

public static void main(String[] args) throws IOException

int num , factorial = 1;

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

System.out.println(“Enter a number”);

num=Integer.parseInt(br.readLine());

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

factorial *= i;

}
System.out.println(“Factorial of ” + num + ” is: ” + factorial);

Write a java program to find the reverse of a number

import java.io.*;

public class Reverse {

public static void main(String[] args) throws IOException

int n , rev=0, rem;

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

System.out.println(“Enter a number”);

n=Integer.parseInt(br.readLine());

while(n>0)

rem=n%10;

rev=rev*10+rem;

n=n/10;

System.out.println(“Reverse is: ” + rev);

Write a java program to print pattern: *

**

***

import java.io.*;

public class PrintPattern {


public static void main(String[] args) throws IOException

int rows ;

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

rows=Integer.parseInt(br.readLine());

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

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

System.out.print(“* “);

System.out.println();

Write a java program to check whether a number is palindrome or not

import java.io.*;

public class Palindrome{

public static void main(String[] args) throws IOException

int n , rev=0, rem,temp;

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

System.out.println(“Enter a number”);

n=Integer.parseInt(br.readLine());

temp=n;

while(n>0)

rem=n%10;

rev=rev*10+rem;
n=n/10;

if(temp==rev)

System.out.println(temp+“is palindrome”);

else

System.out.println(temp+“is not palindrome”);

Write a java program to print Fibonacci series

import java.io.*;

public class Palindrome{

public static void main(String[] args) throws IOException

int n1=0,n2=1,n3,l;

BufferedReader br=new BufferedReader(new InputStreamReader (System.in));

System.out.println(“Enter the limit”);

l=Integer.parseInt(br.readLine());

System.out.print(n1+“ ”+n2+” “);

for(int i=3;i<=l;i++)

n3=n1+n2;

System.out.print(n3+“ ”);

n1=n2;

n2=n3;

}}

You might also like