Lab 2
Lab 2
/*
* 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)
{
while(k != 2 * i - 1)
{
System.out.print("*");
k++;
}
System.out.print("\n");
}
System.out.println();
System.out.println();
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) {
int userInput;
System.out.println(random);
while(true)
{
System.out.print("Enter Your Guess: ");
userInput = input.nextInt();
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();
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;
double answer = 0;
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) {
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;
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;
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();
Lab19
package lab2p19;
import java.util.Scanner;
}
}
Lab20
package lab2p20;
import java.util.Scanner;