You are on page 1of 6

1.package com.java.

myfirstjava;

public class HelloJava {


int a = 101;
static int sum() {
int a = 10;
return a;
}

public static void main(String[] args) {


int b = 10;
HelloJava obj = new HelloJava();
//int a = 101;
System.out.println(obj.a);
//System.out.println(a);
System.out.println("Hi guys Good to be with you all");
sum();

2.package com.java.myfirstjava;
import java.util.*;
public class HelloJava {

public static void main(String[] args) {

Scanner obj = new Scanner(System.in);


System.out.println("Enter a number");
int a = obj.nextInt();
System.out.println("Your number" +a);
obj.close();

3.package javaf;

public class LoopFor {


public static void main(String[] args) {
// int i;

for (int i = 0; i <= 3; i++) //++i i-- --i


{
System.out.println(i);
}

}
}
/*
i=0 0<=3 0 i=0+1=1 i=i+1

i=1 1<=3 1 i=2

i=2 2<=3 2 i=3

i=3 3<=3 3 i=4

i=4 4<=3

*/

4.package javaf;

public class ForEachExample {


public static void main(String[] args) {
int[] number = {2,7,5,8,4};

for (int i : number) {


System.out.println(i);

5.package javaf;

public class ArrayEg {


public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};//{1,2,2}
int[] a= {1,2,3};
System.out.println(a);
for (int i = 0; i < cars.length; i++) { //i=0 i<4 i=1 1<4
System.out.println(cars[i]);//car[0] car[1]
}

cars[0] = "Opel";
System.out.println(cars[0]);

System.out.println(cars.length);

for (int j = 0; j < cars.length; j++) {


System.out.println(cars[j]);
}
System.out.println();

for (String i : cars) {

System.out.println(i);

}
for (int i : a) {
System.out.println(i);
}

6.package com.java.array;

//*********Multidimensional Arrays in Java*****************


/*
SYNTAX:
data_type[1st dimension][2nd dimension][]..[Nth dimension] array_name = new
data_type[size1][size2]….[sizeN];

Two dimensional array:


int[][] twoD_arr = new int[10][20];

Three dimensional array:


int[][][] threeD_arr = new int[10][20][30];
Size of multidimensional arrays: The total number of elements that can be stored in
a multidimensional array can be calculated by multiplying the size of all the
dimensions.

For example:
The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.
Similarly, array int[][][] x = new int[5][10][20] can store a total of (5*10*20) =
1000 elements.

The elements of a 2D array are arranged in rows and columns, and the new operator
for 2D arrays specifies both the number of rows and the number of columns. For
example,

int[][] A;
A = new int[3][4];
This creates a 2D array of int that has 12 elements arranged in 3 rows and 4
columns.

// SAMPLE
public class Array3 {

public static void main(String[] args)


{

int[][] arr = new int[10][20];


arr[0][0] = 1;

System.out.println("arr[0][0] = " + arr[0][0]);


}
}

//LOOOPING TO DISPLAY ELEMENTS

public class Array3 {


public static void main(String[] args)
{
int[][] arr = { { 1, 2 }, { 3, 4 } };

for (int i = 0; i < 2; i++)


for (int j = 0; j < 2; j++)
System.out.println("arr[" + i + "][" + j + "] = "
+ arr[i][j]);
}
}
*/
/*i=0 0<2 i++ i=1

j=0 0<2 arr[0][0] j++ j=1


j=1 1<2 arr[0][1] j++ j=2
j=2 2<2

i=1 1<2 i++ i=2

j=0 0<2 arr[1][0] j++ j=1


j=1 1<2 arr[1][1] j++ j=2
j=2 2<2

i=2 2<2
*/

//Print 2D array in tabular format:

public class Array3 {


public static void main(String[] args)
{

int[][] arr = {{ 1, 2 }, { 3, 4 }};

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
System.out.print(arr[i][j] + " ");
}

System.out.println();
}
}}
6.import java.util.*;
import java.io.*;

class Main{
public static void main(String[] args) throws Exception{
double br,tr,food,logic;
double a,b,c,d,total;
Scanner sc=new Scanner(System.in);
System.out.println("Enter branding expenses");
br=sc.nextDouble();
System.out.println("Enter travel expenses");
tr=sc.nextDouble();
System.out.println("Enter food expenses");
food=sc.nextDouble();
System.out.println("Enter logistics expenses");
logic=sc.nextDouble();
total=br+tr+food+logic;
System.out.println("Total expenses: Rs."+String.format("%.2f",total));
a=(br/total)*100;
b=(tr/total)*100;
c=(food/total)*100;
d=(logic/total)*100;
System.out.println("Branding expenses percentage: "+String.format("%.2f",a)
+"%");
System.out.println("Travel expenses percentage: "+String.format("%.2f",b)
+"%");
System.out.println("Food expenses percentage: "+String.format("%.2f",c)
+"%");
System.out.println("Logistics expenses percentage:
"+String.format("%.2f",d)+"%");
}
}

You might also like