java program
java program
1. Print hello
int c=a+b;
System.out.println("sum of a + b is " +c);
}
Output is : sum of a + b is 10
import java.util.Scanner;
}
4.Three digit sum & reverse
import java.util.Scanner;
import java.util.*;
class IfElse
{
public static void main(String[] args)
{
import java.util.Scanner;
public class leapyear
{
public static void main (String []args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter the year");
int input=sc.nextInt();
if (input%4==0){
System.out.println("this is leap year");
}
else{
System.out.println("this is not leap year");
}
import java.util.Scanner;
public class printday
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int day=sc.nextInt();
switch(day)
{
case 1:
System.out.println("monday");
break;
case 2:
System.out.println("tuesday");
break;
case 3:
System.out.println("wednesday");
break;
case 4:
System.out.println("thrusday");
break;
case 5:
System.out.println("friday");
break;
case 6:
System.out.println("saturday");
break;
case 7:
System.out.println("saturday");
break;
default:
System.out.println("invalid day");
break;
}
}
}
class GFG
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
{
System.out.println(i);
}
}
Output
1
2
3
4
5
6
7
8
9
10
class forLoopDemo
{
public static void main(String args[])
{
for(i=1;i<=number;i++)
{
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
10 fibonacci program
import java.util.Scanner;
class FibonacciExample1
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i;
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
int count = s.nextInt();
System.out.print(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;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
import java.util.Scanner;
public class Prime
{
public static void main(String args[])
{
int min, max, flag = 0, i, j;
Scanner s = new Scanner(System.in);
System.out.println ("Enter the lower limit :");
min= s.nextInt();
System.out.println ("Enter the upper limit :");
max = s.nextInt();
System.out.println ("The prime numbers in between the entered limits are :");
for(i = min; i <=max; i++)
{
for( j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if(flag == 0)
{
System.out.println(i);
}
flag = 0;
}
}
}
}
}
Ouput is
11
12
13
14
15
// create an array
int[] age = {12, 4, 5, 2, 5};
Output
import java.util.Scanner;
public class arraywithui
{
public static void main(String args[])
{
System.out.println("enter the size of array");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter the element of array");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("array elements are -- ");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
import java.util.Scanner;
public class ararysumofel
{
public static void main(String args[])
{
System.out.println("enter the size of array");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int sum=0;
System.out.println("enter the element of array");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("array elements are -- ");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
sum=sum+a[i];
}
}
}
16.largest element in array among three number
import java.util.Scanner;
public class arraylargestel
{
public static void main(String[] args)
{
import java.io.*;
import java.util.Scanner;
import java.io.*;
import java.util.Scanner;
}
}
import java.util.Scanner;
class GFG {
arr[0][0] = 1;
Output
arr[0][0] = 1
class GFG {
{
int[][] arr = { { 1, 2 }, { 3, 4 } };
+ arr[i][j]);
Output
arr[0][0] = 1
arr[0][1] = 2
arr[1][0] = 3
arr[1][1] = 4
Elements in two-dimensional arrays are commonly referred by x[i][j] where ‘i’ is the row number and
‘j’ is the column number.
Syntax:
x[row_index][column_index]
For example:
int[][] arr = new int[10][20];
arr[0][0] = 1;
The above example represents the element present in first row and first column. Note: In arrays if size
of array is N. Its index will be from 0 to N-1. Therefore, for row_index 2, actual row number is 2+1 =
3. Example:
Java
class GFG {
{
int[][] arr = { { 1, 2 }, { 3, 4 } };
Output
arr[0][0] = 1
Representation of 2D array in Tabular Format:
A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’ columns where the row number
ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A two – dimensional array ‘x’ with 3
rows and 3 columns is shown below:
class GFG {
int[][] arr = { { 1, 2 }, { 3, 4 } };
for (int i = 0; i < 2; i++) {
System.out.println();
Output
1 2
3 4
Three – dimensional Array (3D-Array)
Three – dimensional array is a complex form of a multidimensional array. A three – dimensional array
can be seen as an array of two – dimensional array for easier understanding.
Indirect Method of Declaration:
Declaration – Syntax:
data_type[][][] array_name = new data_type[x][y][z];
For example: int[][][] arr = new int[10][20][30];
Initialization – Syntax:
array_name[array_index][row_index][column_index] = value;
For example: arr[0][0][0] = 1;
Example:
Java
class GFG {
{
int[][][] arr = new int[10][20][30];
arr[0][0][0] = 1;
Output
arr[0][0][0] = 1
Direct Method of Declaration: Syntax:
data_type[][][] array_name = {
{
{valueA1R1C1, valueA1R1C2, ....},
{valueA1R2C1, valueA1R2C2, ....}
},
{
{valueA2R1C1, valueA2R1C2, ....},
{valueA2R2C1, valueA2R2C2, ....}
}
};
For example: int[][][] arr = { {{1, 2}, {3, 4}}, {{5, 6}, {7,
8}} };
Example:
Java
class GFG {
int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
for (int i = 0; i < 2; i++)
System.out.println("arr[" + i
+ "]["
+ j + "]["
+ z + "] = "
+ arr[i][j][z]);
Output
arr[0][0][0] = 1
arr[0][0][1] = 2
arr[0][1][0] = 3
arr[0][1][1] = 4
arr[1][0][0] = 5
arr[1][0][1] = 6
arr[1][1][0] = 7
arr[1][1][1] = 8
Elements in three-dimensional arrays are commonly referred by x[i][j][k] where ‘i’ is the array
number, ‘j’ is the row number and ‘k’ is the column number.
Syntax:
x[array_index][row_index][column_index]
For example:
int[][][] arr = new int[10][20][30];
arr[0][0][0] = 1;
The above example represents the element present in the first row and first column of the first array in
the declared 3D array.
Note: In arrays if size of array is N. Its index will be from 0 to N-1. Therefore, for row_index 2, actual
row number is 2+1 = 3.
Example:
Java
class GFG {
int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
Output
arr[0][0][0] = 1
Representation of 3D array in Tabular Format:
A three – dimensional array can be seen as a tables of arrays with ‘x’ rows and ‘y’ columns where the
row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A three – dimensional
array with 3 array containing 3 rows and 3 columns is shown below:
class GFG {
int[][][] arr = { { { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } } };
System.out.println();
System.out.println();
}
Output
1 2
3 4
5 6
7 8
Inserting a Multi-dimensional Array during Runtime:
This topic is forced n taking user-defined input into a multidimensional array during runtime. It is
focused on the user first giving all the input to the program during runtime and after all entered input,
the program will give output with respect to each input accordingly. It is useful when the user wishes
to make input for multiple Test-Cases with multiple different values first and after all those things
done, program will start providing output. As an example, let’s find the total number of even and odd
numbers in an input array. Here, we will use the concept of a 2-dimensional array.
Here are a few points that explain the use of the various elements in the upcoming code:
Row integer number is considered as the number of Test-Cases and Column values are considered
as values in each Test-Case.
One for() loop is used for updating Test-Case number and another for() loop is used for taking
respective array values.
As all input entry is done, again two for() loops are used in the same manner to execute the
program according to the condition specified.
The first line of input is the total number of TestCases.
The second line shows the total number of first array values.
The third line gives array values and so on.
Implementation:
Java
import java.util.Scanner;
class GFGTestCase {
// totalTestCases = total
// number of TestCases
// eachTestCaseValues =
// an Array values
totalTestCases = scanner.nextInt();
eachTestCaseValues = scanner.nextInt();
arrayMain[i] = new int[eachTestCaseValues];
arrayMain[i][j] = scanner.nextInt();
+ arrayMain[i].length
+ " values:");
if (arrayMain[i][j] % 2 == 0) {
nEvenNumbers++;
else {
nOddNumbers++;
System.out.println();
System.out.println(
Output:
Input:
2
2
1 2
3
1 2 3
Output:
TestCase 0 with 2 values:
1 2
Total Even numbers: 1, Total Odd numbers: 1
TestCase 1 with 3 values:
1 2 3
Total Even numbers: 1, Total Odd numbers: 2
Input:
3
8
1 2 3 4 5 11 55 66
5
100 101 55 35 108
6
3 80 11 2 1 5
Output:
TestCase 0 with 8 values:
1 2 3 4 5 11 55 66
Total Even numbers: 3, Total Odd numbers: 5
TestCase 1 with 5 values:
100 101 55 35 108
Total Even numbers: 2, Total Odd numbers: 3
TestCase 2 with 6 values:
3 80 11 2 1 5
Total Even numbers: 2, Total Odd numbers: 4