Articles by "Java"
Showing posts with label Java. Show all posts
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a java program to read an integer and find whether the number is odd or even.

Program Code in JAVA:
import java.io.*;
class odd
{
public static void main(String args[])throws IOException
{
BufferedReader din = new BufferedReader(new InputStreamReader(System.in));
int n;
system.out.println(“enter a number:”);
n= Integer.parseInt(din.readLine());
if(n%2==0)
system.out.println(n+"is an even no");
else
system.out.println(n+"is an odd no:");
}
}


Program Output:
c:\java>javac odd.java
c:\java>java odd
Enter a no: 67
67 is an odd no
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a java program to find the area of the rectangle.

Program Code in JAVA:
class AreaRect
{
public static void main(String args[])
{
int len,bre,area;
len= Integer.parseInt(args[0]);
bre=Integer.parseInt(args[1]);
area=len*bre;
system.out.println(“length of rectangle=”+len);
system.out.println(“breadth of rectangle=”+bre);
system.out.println(“area of rectangle=”+area);
}
}

Program Output:
c:\java>javac AreaRect.java
c:\java>java AreaRect
12 6
length of rectangle = 12
breadth of rectangle = 6
area of rectangle= 72
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a Java program that displays the number of characters, lines, and words in a text file.

Program Code in JAVA:
import java.io.*;
import java.util.*;
class FileCount
{
public static void main(String[] args) throws Exception
{
String fname;
System.out.println("Enter a filename:");
DataInputStream dis=new DataInputStream(System.in);
fname=dis.readLine();
FileReader f=new FileReader(fname);
BufferedReader br=new BufferedReader(f);
String str;
int words=0,lines=0,chars=0;
while((str=br.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(str);
lines++;
words+=st.countTokens();
int i=0,n;
n=str.length();
for(i=0;i<n;i++)
{
char ch=str.charAt(i);
if(ch!=' ')
chars++;
}
}
System.out.println("the number of lines in the file are = "+lines);
System.out.println("the number of words in the file are = "+words);
System.out.println("the number of characters in the file are = "+chars);
}
}

Program Output:
c:\java>javac FileCount.java
c:\java>java FileCount
Enter a filename:
FileCount.java
the number of lines in the file are = 34
the number of words in the file are = 85
the number of characters in the file are = 749
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a Java program that reads a file and displays the file on the screen, with a line number before each line.

Program Code in JAVA:
import java.io.*;
class FileRead
{
public static void main(String[] args) throws Exception
{
File f=new File("FileRead.java");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String str=br.readLine();
int i=1;
while((str=br.readLine())!=null)
{
System.out.println(i++ +" "+str);
}
fr.close();
}
}

Program Output:
c:\java>javac FileRead.java
c:\java>java FileRead

1 class FileRead
2 {
3 public static void main(String[] args) throws Exception
4 {
5 File f=new File("FileRead.java");
6 FileReader fr=new FileReader(f);
7 BufferedReader br=new BufferedReader(fr);
8 String str=br.readLine();
9 int i=1;
10 while((str=br.readLine())!=null)
11 {
12 System.out.println(i++ +" "+str);
13 }
14 fr.close();
15 }
16 }
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a Java Program that reads a file name from the user, then displays information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes.

Program Code in JAVA:
import java.io.*;
class FileEx
{
public static void main(String[] args)
{
DataInputStream dis=new DataInputStream(System.in);
File f=new File("D:/Lab/abc/abc.txt");
System.out.println("File Name = "+f.getName());
System.out.println("File Path = " +f.getPath());
System.out.println("File parent = "+f.getParent());
System.out.println(f.isFile()?" is a File":"not a file");
System.out.println(f.isDirectory()?"it is a directory":"not a directory");
System.out.println(f.canRead()?" it is readable":"it is not readable");
System.out.println(f.canWrite()?"it is writable":"it is not writable");
System.out.println("length of file is"+f.length());
}
}

Program Output:
c:\java>javac FileEx.java
c:\java>java FileEx
File Name = abc.txt
File Path = D:\Lab\abc\abc.txt
File parent = D:\Lab\abc
is a File
not a directory
it is readable
it is not writable
length of file is 68
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a Java Program to make frequency count of words in a given text.

Program Code in JAVA:
import java.io.*;
import java.util.*;
class Freq
{
public static void main(String[] args) throws Exception
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter a String :");
String s=dis.readLine();
StringTokenizer st=new StringTokenizer(s);
int count=1,i=0;
int size=st.countTokens();
String words[]=new String[size];
while(st.hasMoreTokens())
{
words[i]=st.nextToken();
i++;
}
for(i=0;i<size;i++)
{
count=1;
for(int j=i+1;j<size;j++)
{
if(words[i].equals(words[j]))
{
count++;
move(words,j,size);
size--;
j--;
}
}
System.out.println(words[i]+" occurs "+count+" no of times");
}
}
static void move(String a[],int i,int size)
{
while(i<size)
{
if(i==size-1)
a[i]=null;
else
a[i]=a[i+1];
i++;
}
}
}

Program Output:
c:\java>javac Freq.java
c:\java>java Freq
Enter a String :
jai bolo hanuman ki jai
jai occurs 2 no of times
bolo occurs 1 no of times
hanuman occurs 1 no of times
ki occurs 1 no of times
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a Java Program for sorting a given list of names in ascending order.


Program Code in JAVA:
import java.io.*;
class SortingStr
{
public static void main(String[] args) throws Exception
{
int n,i,j;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Ente the number of Strings :");
n=Integer.parseInt(dis.readLine());
String str[]=new String[n];
for(i=0;i<n;i++)
{
System.out.println("Enter string "+(i+1)+" :");
str[i]=dis.readLine();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((str[i].compareTo(str[j]))>0)
{
String tmp=str[i];
str[i]=str[j];
str[j]=tmp;
}
}
}
System.out.println("The String after Sorting : ");
for(i=0;i<n;i++)
System.out.println(str[i]);
}
}


Program Output:
c:\java>javac SortingStr.java
c:\java>java SortingStr
Ente the number of Strings :
5
Enter string 1 : rama
Enter string 2 : krishna
Enter string 3 : raju
Enter string 4 : abc
Enter string 5 : sai
The String after Sorting :
abc
krishna
raju
rama
sai
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a Java program that checks whether a given string is a palindrome or not. Ex MADAM is a palindrome.

Program Code in JAVA:
import java.io.*;
class Palindrome
{
public static void main(String[] args) throws Exception
{
String str1, str2;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter a String");
str1=dis.readLine();
StringBuffer tmp=new StringBuffer(str1);
tmp.reverse();
str2=new String(tmp);
System.out.println("The String after Reverse is "+str2);
if(str1.equals(str2))
System.out.println("The given String "+str1+" is palindrome");
else
System.out.println("The given String "+str1+" is not palindrome");
}
}


Program Output:
c:\java>javac Palindrome.java
c:\java>java Palindrome
Enter a String
madam
The String after Reverse is madam
The given String madam is palindrome
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a Java program that reads a line of integers and then displays each integer, and the sum of all the integers.

Program Code in JAVA:
import java.io.*;
import java.util.*;
class StringToken
{
public static void main(String[] args) throws Exception
{
int i=0,sum=0;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the Line of Integer Separated by Space");
String str=dis.readLine();
int n=str.length();
int a[]=new int[n];
StringTokenizer st=new StringTokenizer(str);
while(st.hasMoreTokens())
{
a[i]=Integer.parseInt(st.nextToken());
System.out.println("a["+i+"]= "+a[i]);
i++;
}
for(i=0;i<n;i++)
sum+=a[i];
System.out.println("Sum is = "+sum);
}
}

Program Output:
c:\java>javac StringToken.java
c:\java>java StringToken
Enter the Line of Integer Separated by Space
1 2 3 4 5
a[0]= 1
a[1]= 2
a[2]= 3
a[3]= 4
a[4]= 5
Sum is = 15
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a Java program to Multiply two given matrices.

Program Code in JAVA:
import java.io.*;
class Matrix
{
int a[5][5],b[5][5],c[5][5];
int i,j,k,m,n,p,q;
DataInputStream dis=new DataInputStream(System.in);
void readA()
{
try
{
System.out.println("Enter the Size of Matrix A (Rows and Columns) ");
m=Integer.parseInt(dis.readLine());
n=Integer.parseInt(dis.readLine());
a=new int[m][n];
System.out.println("Enter the Matrix A values:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=Integer.parseInt(dis.readLine());
}
catch(Exception e){}
}
void readB()
{
try
{
System.out.println("Enter the Size of Matrix B (Rows and Columns) ");
p=Integer.parseInt(dis.readLine());
q=Integer.parseInt(dis.readLine());
b=new int[p][q];
System.out.println("Enter the Matrix A values:");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
b[i][j]=Integer.parseInt(dis.readLine());
}
catch(Exception e){}
}
void printA()
{
System.out.println("Matrix A \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println(" ");
}
}
void printB()
{
System.out.println("Matrix B \n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
}
System.out.println(" ");
}
}
void mulAB()
{
c=new int[m][q];
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
void printC()
{
System.out.println("Matrix Multiplication is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println(" ");
}
}
}
class MatrixMul
{
public static void main(String[] args) throws Exception
{
Matrix mat=new Matrix();
mat.readA();
mat.readB();
if(mat.n==mat.p)
{
mat.printA();
mat.printB();
mat.mulAB();
mat.printC();
}
else
System.out.println("Matrix Multiplication is Not Possible");
}
}



Program Output:
c:\java>javac MatrixMul.java
c:\java>java MatrixMul
Enter the Size of Matrix A (Rows and Columns)
2
2
Enter the Matrix A values:
1 2
1 2
Enter the Size of Matrix B (Rows and Columns)
2
2
Enter the Matrix A values:
2 1
2 1
Matrix A
1 2
1 2
Matrix B
2 1
2 1
Matrix Multiplication is
6 3
6 3
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer.

Program Code in JAVA:
import java.io.*;
class Primenos
{
public static void main(String[] args) throws Exception
{
int n,fact;
System.out.println("Enter the range of Prime number you want");
DataInputStream dis=new DataInputStream(System.in);
n=Integer.parseInt(dis.readLine());
System.out.println("Prime No's from 1 to "+n+" is");
for(int i=1;i<=n;i++)
{
fact=1;
for(int x=1;x<i;x++)
if(i%x==0)
fact++;
if(fact==2)
System.out.print(i+"\t");
}
}
}

Program Output:
c:\java>javac Primenos.java
c:\java>java Primenos
Enter the range of Prime number you want 50
Prime No's from 1 to 50 is
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
The Fibonacci sequence is defined by the following rules: The first two values in the sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. Write a java program that uses both recursive and non-recursive functions to print the nth value in the Fibonacci sequence.

Program Code in JAVA:
import java.io.*;
import java.math.*;
class Fibonacci
{
int Fib(int n)
{
if(n==0)
return 0;
else if(n==1) return 1;
else
return Fib(n-1)+Fib(n-2);
}
}
class Fibrec
{
public static void main(String[] args) throws Exception
{
int n;
long r=0;
System.out.println("Enter N value:");
DataInputStream dis=new DataInputStream(System.in);
n=Integer.parseInt(dis.readLine());
Fibonacci f=new Fibonacci();
for(int i=1;i<=n; i++)
{
System.out.print(f.Fib(i)+"\t");
r=f.Fib(n);
}
System.out.println("\nThe "+n+" the value in the Fibonacci Series : "+r);
}
}

Program Output:
c:\java>javac Fibrec.java
c:\java>java Fibrec
Enter N value:
8
1 1 2 3 5 8 13 21 
The 8 the value in the Fibonacci Series : 21