Java Programs PDF
Java Programs PDF
Java Programs PDF
Method Description
next() to get string without space from the user
nextLine() to get string with spaces from the user
nextInt() to get integer value from the user
nextFloat() to get float value from the user
nextByte() to get byte value from the user
Programs:
Swap of numbers with using third variable and without using third variable
class SwapTwoNumbers
{
public static void main(String []s)
{
int a,b;
//Scanner class to read value
Scanner sc=new Scanner(System.in);
class SwapTwoNumbers
{
public static void main(String []s)
{
int a,b;
//Scanner class to read value
Scanner sc=new Scanner(System.in);
System.out.println("Lagest Number is :
"+largest);
}
}
Simple interest
}
}
Factorial
Factorial Program in Java: Factorial of n is the product of all positive descending integers.
Factorial of n is denoted by n!. For example:
1. 4! = 4*3*2*1 = 24
2. 5! = 5*4*3*2*1 = 120
//find factorial
factorial=1;
for(int loop=num; loop>=1; loop--)
factorial*=loop;
Palindrome:
int num,tNum,sum=0,r;
while(tNum>0)
{
r=tNum%10;
tNum=tNum/10;
sum = (sum*10) + r;
121 true 1 0 * 10 + 1 = 1
12 true 2 1 * 10 + 2 = 12
1 true 1 12 * 10 + 1 = 121
0 false - 121
Reverse of number:
//Read Number
System.out.print("Enter an integer number: ");
number=sc.nextInt();
}
}
Reverse of string
The java string length() method length of the string. It returns count of total number of
characters. The length of java string is same as the unicode code units of the string.
Ex:String S1=”java”;
S1.length()=4
The java.lang.String.charAt() method returns the char value at the specified index. An index
ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index
1, and so on, as for array
//Reversing String
rStr="";
for(int loop=str.length()-1; loop>=0; loop--)
rStr= rStr + str.charAt(loop);
Regrex:Regular expression
class ExtractNumberFromString
{
public static void main(String[] args)
{
String str;
String numbers;
//extracting string
numbers=str.replaceAll("[^0-9]", "");
class CountWords
{
//word count
for(int i=0; i<text.length()-1; i++)
{
if(text.charAt(i)==' ' && text.charAt(i+1)!='
')
countWords++;
}
}
}
Multiplication table:
int number;
Scanner SC=new Scanner(System.in);
//print table
System.out.println("Table of " + number + " is ");
for(int loop=1; loop<=10; loop++){
System.out.println(number*loop);
}
}
}
armstromg number
1. 153 = (1*1*1)+(5*5*5)+(3*3*3)
2. where:
3. (1*1*1)=1
4. (5*5*5)=125
5. (3*3*3)=27
6. So:
7. 1+125+27=153
class ArmstrongExample
{
public static void main(String[] args)
{
int c=0,a,temp;
int n=153;//It is the number to check armstrong
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number");
else
System.out.println("Not armstrong number");
}
}
Perfect Number:
Any number can be a Java Perfect Number, If the sum of its positive divisors
excluding the number itself is equal to that number. For example, 28 is a
perfect number because 28 is divisible by 1, 2, 4, 7, 14 and 28 and the sum of
these values are: 1 + 2 + 4 + 7 + 14 = 28 (Remember, we have to exclude the
number itself. That’s why we haven’t added 28 here). Some of the perfect
numbers are 6, 28, 496, 8128 and 33550336 so on
public class PerfectNumberUsingFor {
Fibnocci series:
The Fibonacci series is a series where the next term is the sum of pervious two terms.
The first two terms of the Fibonacci sequence is 0 followed by 1.
First method:
This method compares this string to the specified object. The result is true if
and only if the argument is not null and is a String object that represents the
same sequence of characters as this object.
String s2 = "tutorial";
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
Second method:
String s1 = "tutorial";
String s2 = "tutorial";
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.print(text.contains("the"));
Result
The above code sample will produce the following result.
true
Result
The above code sample will produce the following result.
Original String: string abc touppercase
String changed to upper case: STRING ABC TOUPPERCASE
Concatination of string:
String s = "Hello";
s = s.concat("word");
System.out.print(s);
Result
The above code sample will produce the following result. The result may vary.
Helloword
string split
for(String w:words) {
System.out.println(w);
Result
The above code sample will produce the following result.
t
u
t
o
r
i
a
l
s
1. class Demo
2. {
3. public static void main(String args[]){
4. //Declaring String variable
5. String s="200";
6. //Converting String into int using Integer.parseInt()
7. int i=Integer.parseInt(s);
8. //Printing value of i
9. System.out.println(i);
10. }}
Duplicate in string:
if(myArray[i] == myArray[j]){
Output