1.
FIBONACCI SERIES
Coding:
class FibonacciExample1{
public static void main(String args[])
int n1=0,n2=1,n3,i,count=10;
[Link](n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;
} }}
Output :
[Link] NUMBER
Coding :
public class PrimeExample {
public static void main(String args[])
int i,m=0,flag=0;
int n=3;//it is the number to be checked
m=n/2;
if(n==0||n==1){
[Link](n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
[Link](n+" is not prime number");
flag=1;
break;
if(flag==0) { [Link](n+" is prime number"); }
}}}
Output :
[Link] THE NUMBER
Coding :
class PalindromeExample {
public static void main(String args[]) {
int r,sum=0,temp;
int n=454;//It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
if(temp==sum)
[Link]("palindrome number ");
else
[Link]("not palindrome");
} }
Output :
[Link]
Coding :
class FactorialExample {
public static void main(String args[]) {
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++) {
fact=fact*i;
[Link]("Factorial of "+number+" is: "+fact);
Output :
[Link] TRIANGLE STAR
PATTERN
Coding :
public class RightTrianglePattern {
public static void main(String args[]) {
int i, j, row=6;
for(i=0; i<row; i++)
for(j=0; j<=i; j++)
[Link]("* "); }
[Link](); }
Output :
[Link] ASCII VALUES
Coding :
public class PrintAsciiValueExample1
public static void main(String[] args)
// character whose ASCII value to be found
char ch1 = 'a';
char ch2 = 'b';
// variable that stores the integer value of the character
int asciivalue1 = ch1;
int asciivalue2 = ch2;
[Link]("The ASCII value of " + ch1 + " is: " + asciivalue1);
[Link]("The ASCII value of " + ch2 + " is: " + asciivalue2);
Output :
[Link] PROGRAM TO SWAP TWO VALUES USING THIRD VARIABLE
Coding :
import [Link].*;
class swapping {
static void swapValuesUsingThirdVariable(int m, int n)
int temp = m;
m = n;
n = temp;
[Link]("Value of m is " + m + " and Value of n is " + n);
public static void main(String[] args)
int m = 9, n = 5;
swapValuesUsingThirdVariable(m, n);
Output :
[Link] CHECK THE ODD OR EVEN
Coding:
import [Link].*;
import [Link];
class check {
public static void main(String[] args)
int num = 10;
// Checking if number is even or odd number
if (num % 2 == 0) {
// If remainder is zero then this number is even
[Link]("Entered Number is Even");
else {
// If remainder is not zero then this number is
// odd
[Link]("Entered Number is Odd");
}}}
Output :
[Link] NUMBER
Coding :
import [Link];
public class LargestNumberExample1 {
public static void main(String[] args) {
int a, b, c, largest, temp;
Scanner sc = new Scanner([Link]);
//reading input from the user
[Link]("Enter the first number:");
a = [Link]();
[Link]("Enter the second number:");
b = [Link]();
[Link]("Enter the third number:");
c = [Link]();
//comparing a and b and storing the largest number in a temp variable
temp=a>b?a:b;
//comparing the temp variable with c and storing the result in the variable
largest=c>temp?c:temp;
[Link]("The largest number is: "+largest);
} }
Output :
[Link] POSITIVE OR NEGATIVE
Coding :
public class CheckPositiveOrNegativeExample1 {
public static void main(String[] args) {
//number to be check
int num=912;
//checks the number is greater than 0 or not
if(num>0)
[Link]("The number is positive.");
//checks the number is less than 0 or not
else if(num<0)
[Link]("The number is negative.");
else
[Link]("The number is zero.");
} } }
Output :
[Link] PROGRAM TO ADD TWO MATRICES
Coding :
public class MatrixAdditionExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//adding and printing addition of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j]; //use - for subtraction
[Link](c[i][j]+" ");
[Link]();//new line
} } }
Ouput :
[Link] PROGRAM TO DISPLAY EVEN NUMBERS FROM 1 TO 100
Coding :
public class DisplayEvenNumbersExample1
public static void main(String args[])
int number=100;
[Link]("List of even numbers from 1 to "+number+": ");
for (int i=1; i<=number; i++)
//logic to check if the number is even or not
//if i%2 is equal to zero, the number is even
if (i%2==0)
{
[Link](i + " ");
Out put:
list out of even number 1 to 100:
2 4 6 8 10 ...32 34 36 38 40 ...62 64 66 68 70 ..... 92 94 96 98 100
[Link] PROGRAM TO DISPLAY ODD NUMBERS FROM 1 TO 100
Coding :
public class DisplayOddNumbersExample1
public static void main(String args[])
int number=100;
[Link]("List of odd numbers from 1 to "+number+": ");
for (int i=1; i<=number; i++)
//logic to check if the number is odd or not
//if i%2 is not equal to zero, the number is odd
if (i%2!=0)
[Link](i + " ");
Output:
List of odd numbers from 1 to 100: 1 3 5 7 9 ……..21 23 25 27 29 ………51 53 55 57 59…….71 73 75 77
79 ……..91 93 95 97 99
[Link] PROGRAM TO MULTIPLY TWO MATRICES
Coding :
public class MatrixMultiplicationExample {
public static void main(String args[]){
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
[Link](c[i][j]+" "); //printing matrix element
}//end of j loop
[Link]();//new line
} } }
Output :
[Link] PROGRAM TO TRANSPOSE MATRIX
Coding :
public class MatrixTransposeExample{
public static void main(String args[]){
int original[][]={{1,3,4},{2,4,3},{3,4,5}};
//creating another matrix to store transpose of a matrix
int transpose[][]=new int[3][3]; //3 rows and 3 columns
//Code to transpose a matrix
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
} }
[Link]("Printing Matrix without transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){ [Link](original[i][j]+" "); }
[Link]("Printing Matrix After Transpose:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++) { [Link](transpose[i][j]+” ”); }
}}}
Output :
[Link] PROGRAM TO COUNT THE TOTAL NUMBER OF CHARACTERS
IN A STRING
Coding :
public class CountCharacter
public static void main(String[] args) {
String string = "The best of both worlds";
int count = 0;
//Counts each character except space
for(int i = 0; i < [Link](); i++) {
if([Link](i) != ' ')
count++;
//Displays the total number of characters present in the given string
[Link]("Total number of characters in a string: " + count);
Output :
[Link] PROGRAM TO DETERMINE WHETHER A GIVEN STRING IS
PALINDROME
Coding :
public class PalindromeString
public static void main(String[] args) {
String string = "Kayak";
boolean flag = true;
string = [Link]();
for(int i = 0; i < [Link]()/2; i++){
if([Link](i) != [Link]([Link]()-i-1)){
flag = false;
break;
} }
if(flag)
[Link]("given string is palindrome”);
Else
[Link]("given string is not palindrome”); } }
Output :
[Link] PROGRAM TO FIND THE MOST REPEATED WORD IN A TEXT
FILE
Coding :
import [Link];
import [Link];
import [Link];
public class MostRepeatedWord {
public static void main(String[] args) throws Exception {
String line, word = "";
int count = 0, maxCount = 0;
ArrayList<String> words = new ArrayList<String>();
//Opens file in read mode
FileReader file = new FileReader("[Link] ");
BufferedReader br = new BufferedReader(file);
//Reads each line
while((line = [Link]()) != null) {
String string[] = [Link]().split("([,.\\s]+) ");
//Adding all words generated in previous step into words
for(String s : string){
[Link](s);
//Determine the most repeated word in a file
for(int i = 0; i < [Link](); i++){
count = 1;
//Count each word in the file and store it in variable count
for(int j = i+1; j < [Link](); j++){
if([Link](i).equals([Link](j))){
count++;
}
//If maxCount is less than count then store value of count in maxCount
//and corresponding word to variable word
if(count > maxCount){
maxCount = count;
word = [Link](i);
[Link]("Most repeated word: " + word);
[Link](); }
Output :
19. BUBBLE SORT IN JAVA
Coding :
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = [Link];
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
} } } }
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};
[Link]("Array Before Bubble Sort");
for(int i=0; i < [Link]; i++){
[Link](arr[i] + " ");
[Link]();
bubbleSort(arr);//sorting array elements using bubble sort
[Link]("Array After Bubble Sort");
for(int i=0; i < [Link]; i++){
[Link](arr[i] + " ");
}
Output :
20. JAVA CONVERT STRING TO INT
Coding :
public class StringToIntExample1{
public static void main(String args[]){
//Declaring String variable
String s="200";
//Converting String into int using [Link]()
int i=[Link](s);
//Printing value of i
[Link](i);
}}
Output :
200