MCS024 Session 1 and 2
MCS024 Session 1 and 2
Exercise1: Write a program in Java to implement the formula (Area= Height*Width) to find the area of
a rectangle, where height and width are the rectangle’s height and width.
import java.io.*;
class S1e1
//variable declaration
int length,width,area;
s=d.readLine();
length = Integer.parseInt(s);
s=d.readLine();
width = Integer.parseInt(s);
Exercise2: Write a program in Java to find the result of following expression (Assume a=10, b=5)
i. (a<<2) + (b>>2)
ii. (a_bol) || (b>0)
iii. (a + b * 100) / 10
iv. a&b
import java.io.*;
class S1e2
{
public static void main(String args[]) throws IOException
{
//variable declaration
int a=10,b=5;
int res1,res3,res4;
boolean a_bol,res2;
//expression 1
res1 = (a<<2) + (b>>2) ;
System.out.println("\n1. (a<<2) + (b>>2) = "+res1);
//expression 2
if(a>0)
a_bol = true;
else
a_bol = false;
//expression 3
res3 = (a + b * 100) / 10 ;
System.out.println("\n3. ((a + b * 100) / 10) = "+res3);
//expression 4
res4 = a & b;
System.out.println("\n4. a & b = "+res4);
}
}
Exercise3: Write a program in Java to explain the use of break and continue statements.
import java.io.*;
class S1e3
if(i%5 == 0)
continue;
if( i>20 )
break;
System.out.println(i);
}
Execise4: Write a program in Java to find the average of marks you obtained in your 10+2 class.
import java.io.*;
class S1e4
//variable declaration
int mat,phy,chem,elec,lang1,lang2,total;
float avg;
//input marks
S=d.readLine();
mat=Integer.parseInt(S);
S=d.readLine();
phy=Integer.parseInt(S);
S=d.readLine();
chem=Integer.parseInt(S);
S=d.readLine();
elec=Integer.parseInt(S);
S=d.readLine();
lang1=Integer.parseInt(S);
S=d.readLine();
lang2=Integer.parseInt(S);
total = mat+phy+chem+elec+lang1+lang2;
avg = total/6 ;
//display result
}
Session 2: Statements and array
Exercise1: Write a program in Java to find AxB where A is a 3x3 and B is a matrix of 3x4. Take the
values in matrixes A and B from user.
//multiplication of 2 matrices
import java.io.*;
class S2e1
int r1,c1,r2,c2;
S=d.readLine();
r1=Integer.parseInt(S);
S=d.readLine();
c1=Integer.parseInt(S);
System.out.println("Enter no of rows and columns in matrix B:");
S=d.readLine();
r2=Integer.parseInt(S);
S=d.readLine();
c2=Integer.parseInt(S);
if(c1!=r2)
else
for(int i=1;i<=r1;i++)
for(int j=1;j<=c1;j++)
S=d.readLine();
mat1[i][j]=Integer.parseInt(S);
for(int i=1;i<=r2;i++)
{
for(int j=1;j<=c2;j++)
S=d.readLine();
mat2[i][j]=Integer.parseInt(S);
System.out.println("Matrix A:");
for(int i=1;i<=r1;i++)
for(int j=1;j<=c1;j++)
System.out.print("\t");
System.out.print(mat1[i][j]);
System.out.println();
System.out.println("Matrix B:");
for(int i=1;i<=r2;i++)
for(int j=1;j<=c2;j++)
System.out.print("\t");
System.out.print(mat2[i][j]);
System.out.println();
}
//multiplication:
for(int i=1;i<=r1;i++)
for(int j=1;j<=c2;j++)
res[i][j]=0;
for(int k=1;k<=c1;k++)
res[i][j]=res[i][j]+mat1[i][k]*mat2[k][j];
//Display Output
System.out.println("Resultant Matrix:");
for(int i=1;i<=r1;i++)
for(int j=1;j<=c2;j++)
System.out.print("\t");
System.out.print(res[i][j]);
System.out.println();
}
}
Exercise2: Write a program in Java to compute the sum of the digits of a given integer. Remember,
your integer should not be less than the five digits. (e.g., if input is 23451 then sum of the digits of
23451 will be 15)
import java.io.*;
class S2e2
{
int num,sum=0,len,i;
String s;
s=d.readLine();
len=s.length();
num=Integer.parseInt(s);
if(num<=9999)
System.out.println("Invalid");
else
for(i=1;i<=len;i++)
sum=sum+num % 10;
System.out.println("The sum of all num is : " +len+ " digit is " + sum );