Oops With Java
Oops With Java
Oops With Java
PRACTICAL FILE
Output:
(B) Write a Program to calculate the area of rectangle using command prompt.
Code:
Public static void main(String[] args){
int length =30;
int bredth =40;
int area = length*breadth;
System.out.println(“Area of the rectangle is:”+area);
}
}
Output:
LAB-2
Aim:
(A) Write a Program to find whether the number is prime number or not using
‘for loop’.
Code:
public class Main {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Output:
(B) Write a Program to find whether the number is prime number or not using
‘while loop’.
Code:
public class Main {
public static void main(String[] args) {
int num = 33, i = 2;
boolean flag = false;
while (i <= num / 2) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
}
++i;
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Output:
Lab-3
Aim:
(A) Arithmetic operators
Code:
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
Output:
Output:
Output:
Output:
Lab-4
Aim: Write a program to print the star, Number and Character pattern
using Java packages
(I) Star Pattern:
(I.A) Pyramidal Pattern
Code:
public class PyramidPattern
{
public static void main(String args[])
{
//i for rows and j for columns
//row denotes the number of rows you want to print
int i, j, row = 6;
//Outer loop work for rows
for (i=0; i<row; i++)
{
//inner loop work for space
for (j=row-i; j>1; j--)
{
//prints space between two stars
System.out.print(" ");
}
//inner loop for columns
for (j=0; j<=i; j++ )
{
//prints star
System.out.print("* ");
}
//throws the cursor in a new line after printing each line
System.out.println();
}
}
}
OUTPUT: