[go: up one dir, main page]

0% found this document useful (0 votes)
32 views14 pages

Lab 2

The document contains 14 Java programs (labeled Lab1 through Lab14) that demonstrate different programming concepts like input/output, loops, conditionals, methods, and arrays. Each program contains comments describing what it is doing and outputs the results of running the code to the console.

Uploaded by

Md. Ashik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

Lab 2

The document contains 14 Java programs (labeled Lab1 through Lab14) that demonstrate different programming concepts like input/output, loops, conditionals, methods, and arrays. Each program contains comments describing what it is doing and outputs the results of running the code to the console.

Uploaded by

Md. Ashik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lab1.

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package lab2pr1;

import java.util.Scanner;

/**
*
* @author Ashikur Rahman
*/
public class Lab2pr1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner reader = new Scanner(System.in);
System.out.print("Process Started\n ");
System.out.print("Enter A Number: ");
int num =reader.nextInt();
if(num%2 ==0)
System.out.println( num + " is even");
else
System.out.println(num + " is odd");
System.out.print("Process Completed: ");

Lab2

lab3.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package lab2pr3;

import java.util.Scanner;

/**
*
* @author Ashikur Rahman
*/
public class Lab2pr3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the size: ");
int userInput = input.nextInt();
for(int i = 0; i < userInput; ++i)
{
for(int j = 0; j < userInput; ++j)
{
System.out.print("#");
}
System.out.print("\n");
}
}
}

Lab4.
package lab2p4;

import java.util.Scanner;

/**
*
* @author Ashikur Rahman
*/
public class Lab2p4 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic herepackage lab3p4;
Scanner input = new Scanner(System.in);
System.out.print("Enter the rows: ");
int userInput = input.nextInt();

int k = 0;
for(int i = 1; i <= userInput; i++, k = 0)
{

for(int j = 1; j <= userInput - i; ++j)


{
System.out.print(" ");
}

while(k != 2 * i - 1)
{
System.out.print("*");
k++;
}
System.out.print("\n");
}
System.out.println();
System.out.println();

for(int i = userInput; i >= 1; i--)


{

for(int j = 1; j <= userInput - i; ++j)


{
System.out.print(" ");
}

for(int m = i; m <= 2 * i - 1; ++m)


{
System.out.print("*");
}
for(int n = 0; n < i - 1; ++n)
{
System.out.print("*");
}

System.out.print("\n");
}

}
}

lab5.
package lab2p5;

import java.util.Scanner;

/**
*
* @author Ashikur Rahman
*/
public class Lab2p5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("Enter the size: ");
int userInput = input.nextInt();

//Program A
for(int i = 1; i <= userInput; ++i)
{
for(int j = 1; j <= i; ++j)
{
System.out.print(j);
}
System.out.print("\n");
}

System.out.println();
System.out.println();

//Program B
for(int i = 0; i < userInput; ++i)
{
for(int k = 0; k < i; ++k)
{
System.out.print(" ");
}
for(int j = 1; j <= userInput - i; ++j)
{
System.out.print(j);
}
System.out.print("\n");
}

System.out.println();
System.out.println();

//Program C
int numSpaces = 0;
for(int i = 0; i < userInput; ++i)
{
//print spaces
for(int j = userInput; j > i + 1; j--)
{
numSpaces++;
System.out.print(" ");
}
//print the numbers
for(int k = userInput - numSpaces; k >= 1; k--)
{
System.out.print(k);
}
numSpaces = 0;
System.out.print("\n");
}

System.out.println();
System.out.println();

//Program D
for(int i = 0; i < userInput; ++i)
{
for(int j = userInput - i; j >= 1; j--)
{
System.out.print(j);
}
System.out.print("\n");
}

}
}

Lab6.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package lab2p6;

import java.util.Random;
import java.util.Scanner;

/**
*
* @author Ashikur Rahman
*/
public class Lab2p6 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int userInput;

Random rand = new Random();


int random = rand.nextInt();

System.out.println(random);
while(true)
{
System.out.print("Enter Your Guess: ");
userInput = input.nextInt();

if(userInput > random)


{
System.out.println("Too high, try again.");
}
else if(userInput < random)
{
System.out.println("Too low, try again.");
}
else
{
System.out.println("Congrats!");
break;
}
}
}
}

Lab7.
package lab2p7;

/**
*
* @author Ashikur Rahman
*/
public class Lab2p7 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
for(int i = 1; i <= 7; ++i)
{
for(int j = 1; j <= i; ++j)
{
System.out.print(j);
}
for(int k = 0; k < 7 - i; ++k)
{
System.out.print("*");
}
System.out.print("\n");
}
}

Lab8.
package lab2p8;

import java.util.Scanner;

/**
*
* @author Ashikur Rahman
*/
public class Lab2p8 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userInput = input.next();

boolean isPalindrome = true;

char[] arr = userInput.toCharArray();

int length = arr.length;


if(arr.length % 2 == 0) //even
{
for(int i = 0; i < arr.length / 2; ++i)
{
if(arr[i] != arr[length - i - 1])
{
isPalindrome = false;
break;
}
}
}
else //odd
{
for(int i = 0; i < arr.length / 2; ++i)
{
if(arr[i] != arr[arr.length - i - 1])
{
isPalindrome = false;
break;
}
}
}
if(isPalindrome)
{
System.out.println("It is a palindrome");
}
else
{
System.out.println("Not a palindrome");
}
}

Lab9
package lab2pr9;

import java.util.Scanner;

/**
*
* @author Student
*/
public class Lab2Pr9 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[] array = new int[8];
Scanner input = new Scanner(System.in);
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();

}
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 != 0) {
System.out.print(array[i] + "");
}
}
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
System.out.print(array[i] + "");
}
}
}

Lab11
package lab2p11;

import java.util.Scanner;

public class lab2p11 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);


int n = input.nextInt();

double answer = 0;

for(int i = 1; i <= n; ++i)


{
answer += 1.0 / i;
}

System.out.println(answer);
}
}

Lab12.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab2p12;

/**
*
* @author Student
*/
public class Lab2P12 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Scanner input=new Scanner(System.in);


int n1=input.nextInt();
int n2=input.nextInt();
while(n1 != n2)
{

if (n1>n2)
n1=n1-n2;
else if
System.out.println(n+ " is not a leap year");

Lab13
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab2p13;

import java.util.Scanner;

/**
*
* @author Student
*/
public class Lab2P13 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int n,c=1,b,i,j;
System.out.print("Input number of rows: ");
Scanner input = new Scanner(System.in);
n = input.nextInt();
for(i=0;i<n;i++)
{
for(b=1;b<=n-i;b++)
System.out.print(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
System.out.print(" "+c);
}
System.out.print("\n");
}
}

}
Lab14
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab2p14;

import java.util.Scanner;

/**
*
* @author Student
*/
public class Lab2P14 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Enter a number");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int i,fact=1;

for(i=1;i<=n;i++)
{
fact=fact*i;

}
System.out.println("The factorial of " +n+ " is:" +fact);
}

Lab16
package lab2p16;

import java.util.Scanner;

public class lab2p16 {

public static void main(String[] args) {

int spaces = 1;
int start = 0;
Scanner input = new Scanner(System.in);
System.out.print("Input the number: ");
int userInput = input.nextInt();

int count = 1;

for (int i = 1; i < (userInput * 2); i++)


{

for (int j = userInput - spaces; j > 0; j--)


{
System.out.print(" ");
}
if (i < userInput)
{
start = i;
spaces++;
} else
{
start = userInput * 2 - i;
spaces--;
}
for (int j = 0; j < count; j++)
{
System.out.print(start);
if (j < count / 2)
{
start--;
} else
{
start++;
}
}
if (i < userInput)
{
count = count + 2;
} else {
count = count - 2;
}

System.out.println();
}

}
}

Lab17
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
Package lab2p17;

import java.util.Scanner;

/**
*
* @author Student
*/
public class lab2p17 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Input number");
Scanner input=new Scanner(System.in);
int n=input.nextInt();

if (n==1)
System.out.println("Monday");

else if(n==2)
System.out.println("Tuesday");
else if(n==3)
System.out.println("Wednesday");
else if(n==4)
System.out.println("Thursday");
else if(n==5)
System.out.println("Friday");
else if(n==6)
System.out.println("Saturday");
else if(n==7)
System.out.println("Sunday");
else
System.out.println("Invalid");

Lab18
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab2p18;

import java.util.Scanner;

/**
*
* @author Student
*/
public class Lab2P18 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Input the year:");
Scanner input=new Scanner(System.in);
int n=input.nextInt();

if (n%4==0 && n%100 !=0 ||n%400==0)


System.out.println(n+ " is a leap year");
else
System.out.println(n+ " is not a leap year");

Lab19
package lab2p19;

import java.util.Scanner;

public class lab2p19 {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input x: ");
double x = input.nextDouble();
System.out.print("Input n: ");
int n = input.nextInt();
double result = 0.0f;

for(int i = 1; i <= n; i++)


{
result += (Math.pow(-1.0f, (double)i) / (double)fact((2 * i) + 1)) * Math.pow(x, ((2 * n) + 1));
}

System.out.println("Result: " + result);

public static int fact(int n)


{
if(n == 0)
{
return 1;
}
return n * fact(n - 1);

}
}

Lab20
package lab2p20;

import java.util.Scanner;

public class lab2p20 {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);
System.out.print("Input x: ");
double x = input.nextDouble();
System.out.print("Input n: ");
int n = input.nextInt();
double result = 0.0f;

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


{
result += (Math.pow(-1.0f, i) / (double)fact(2 * i)) * Math.pow(x, (2 * i));
}

System.out.println("Result: " + result);


}

public static int fact(int n)


{
if(n == 0)
{
return 1;
}
return n * fact(n - 1);
}
}

You might also like