Fa23-Bse-004 (Lab 7)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

NAME: KAMAL HASSAN

REGISTRATION NUMBER: FA23-BSE-047

LAB: 07
ACTIVITY#1:
package lab7;

public class A1{

public static void main(String[] args) {

int i = 5;

int j = 2;

int k = max(i, j); //Method calling

System.out.println("The maximum of " + i + " and " + j + "is " + k);

public static int max(int num1, int num2) {

int result;

if (num1 > num2)

result = num1;
else

result = num2;

return result; // returning the value

OUTPUT:
ACTIVITY#2:

package lab7;
public class A2{
public static void main(String[] args){
System.out.print("The grade is ");
printGrade(78.5);
System.out.print("The grade is ");
printGrade(59.5);
}
public static void printGrade(double score){
if(score >= 90.0){
System.out.println('A');
}
else if(score >= 80.0){
System.out.println('B');
}
else if (score >= 70.0){
System.out.println('C');
}
else if (score >= 60.0){
System.out.println('D');
}
else{
System.out.println('F');
}
}
}

OUTPUT:
ACTIVITY#3:
package lab7;

public class A3 {

public static void main(String[] args){

int num1 = 1;

int num2 = 2;

System.out.println("Before invoking the swap method, num1 is " +num1 + " and
num2 is " + num2);

swap(num1, num2);

System.out.println("After invoking the swap method, num1 is" + num1 + " and
num2 is " + num2);

public static void swap(int n1, int n2) {

System.out.println("\tInside the swap method");

System.out.println("\t\tBefore swapping, n1 is " + n1 + " and n2 is " + n2);

int temp = n1;

n1 = n2;

n2 = temp;

System.out.println("\t\tAfter swapping, n1 is " + n1+ " and n2 is " + n2);

OUTPUT:
ACTIVITY#4:

package lab7;
public class A4 {
public static void main(String[] args) {
System.out.println("The maximum of 3 and 4 is " + max(3, 4));
System.out.println("The maximum of 3.0 and 5.4 is "+
max(3.0,5.4));
System.out.println("The maximum of 3.0, 5.4, and 10.14 is " + max(3.0, 5.4,
10.14));
}
public static int max(int num1, int num2){
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, double num2){
if (num1 > num2)
return num1;
else
return num2;
}
public static double max(double num1, double num2, double num3){
return max(max(num1, num2), num3);
}
}
OUTPUT:
ACTIVITY#5:
package lab7;
import java.util.Scanner;
public class A5{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a nonnegative integer: ");
int n = input.nextInt();
System.out.println("Factorial of " + n + " is " + factorial(n));
}
public static long factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
}

OUTPUT:
ACTIVITY#6:
package lab7;
import java.util.Scanner;
public class A6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter index for a Fibonacci number: ");
int index = input.nextInt();
System.out.println("The Fibonacci number at index " + index + " is " +
fib(index));
}
public static long fib(long index) {
if (index == 0)
return 0;
else if (index == 1)
return 1;
else
return fib(index - 1) + fib(index - 2);
}
}

OUTPUT:
GRADED TASKS
TASK# 1 (A):

package lab7;
public class T1 {
public static int sumDigits(long isLong){
int sum = 0;
while (isLong>0) {
sum+=isLong%10;
isLong/=10;
}
return sum;
}
public static void main(String[] args) {
System.out.println(sumDigits(12345));
}
}

OUTPUT:
TASK#01 (B):

package lab7;
public class T1b {
public static void Reverse(int number){
String reverse = "";
while (number>0) {
reverse+=number%10;
number/=10;
}
System.out.println(reverse);
}
public static void main(String[] args) {
Reverse(12345);
}
}

OUTPUT:
TASK#02:

package lab7;
import java.util.Scanner;
public class T2 {
public static int Reverse(int number){
String reverse = "";
while (number>0) {
reverse+=number%10;
number/=10;
}
return Integer.parseInt(reverse);
}
public static boolean isPalindrome(int number){
return number == Reverse(number);
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter A Number: ");
int number = sc.nextInt();
System.out.println(isPalindrome(number));
}
}

OUTPUT:
TASK#03:

package lab7;
import java.util.*;
public class T3 {
public static void displaySortedNumbers(double num1, double num2,double
num3){
double[] numarray = {num1,num2,num3};
Arrays.sort(numarray);
for (int i = 0; i < numarray.length; i++) {
System.out.print(numarray[i]+" ");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers seperated by spaces: ");
double num1 = sc.nextDouble();
double num2 = sc.nextDouble();
double num3 = sc.nextDouble();
displaySortedNumbers(num1,num2,num3);
}
}

OUTPUT:
TASK#04:
package lab7;

public class T4 {

public static int numberOfDaysInAYear(int year){

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

return 366;

else return 365;

public static void main(String[] args) {

for (int i = 2000; i < 2021; i++) {

System.out.println("The year "+i+" has "+numberOfDaysInAYear(i)+" days");

OUTPUT:
TASK#05:

package lab7;

import java.util.*;

public class T5 {

public static int countLetters(String s){

int length = s.length();

return length;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a string: ");

String s = sc.nextLine();

System.out.println("The number of letters in the string is "+countLetters(s));

OUTPUT:
TASK#06:

package Lab7;
import java.util.Scanner;

public class T6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a sentence: ");


String sentence = scanner.nextLine();

String[] chunks = sentence.split("\\s+");

for (int i = 0; i < chunks.length; i++){


System.out.printf("%s ", capitalize(chunks[i]));
}

System.out.println();
}

public static String capitalize(String word) {


return word.substring(0, 1).toUpperCase() + word.substring(1);
}
}
OUTPUT:
TASK#07:
package lab7;

import java.util.Scanner;

public class T7 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter n: ");

printMatrix(scanner.nextInt());

public static void printMatrix(int n) {

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

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

System.out.printf("%d ", Math.round(Math.random()));

System.out.println();

OUTPUT:
TASK#08:
package lab7;

import java.util.Scanner;

public class T8 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");

String string = scanner.nextLine();

System.out.println("Number of vowels in string: " + countVowels(string));

public static int countVowels(String string) {

int vowels = 0;

string = string.toUpperCase();

for (int i = 0; i < string.length(); i++) {

char current = string.charAt(i);

if ((current == 'A') || (current == 'E') || (current == 'I') || (current == 'O') ||


(current == 'U'))

vowels += 1;

}
return vowels;

OUTPUT:
TASK#9:

package lab7;

import java.util.Scanner;
public class T9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter base: ");


double base = scanner.nextDouble();
System.out.print("Enter exponent: ");
int exponent = scanner.nextInt();

System.out.printf("\n%.2f^%d is: %.2f\n", base, exponent, power(base,


exponent));
}
public static double power(double base, int exponent) throws
ArithmeticException {
if ((base < 0) || (exponent < 0))
throw new ArithmeticException("Invalid values for base and/or exponent
specified.");
if (exponent == 0) return 1;
if (exponent == 1) return base;
return base * power(base, exponent - 1);
}
}
OUTPUT:
TASK#10 (Pattern 1):

package lab7;
public class T10_1 {
public static void main(String[] args) {
int n = 5;
printPyramid(n);
}
public static void printPyramid(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}

OUTPUT:
TASK#10 (Pattern 2):

package lab7;
public class T10_2 {
public static void main(String[] args) {
int n = 5;
printLeftAngledPyramid(n, 1);
}
public static void printLeftAngledPyramid(int n, int row) {
if (row > n) {
return;
}
printStars(row);
System.out.println();
printLeftAngledPyramid(n, row + 1);
}
public static void printStars(int count) {
if (count <= 0) {
return;
}
System.out.print("* ");
printStars(count - 1);
}
}

OUTPUT:
TASK#10 (Pattern 3):

package lab7;
public class T10_3{
public static void print_space(int space){
if (space == 0) {
return;
}
System.out.print(" " + " ");
print_space(space - 1);
}
public static void print_asterisk(int asterisk) {
if (asterisk == 0)
{
return;
}
System.out.print("* ");
print_asterisk(asterisk - 1);
}
public static void pattern(int n, int num){
if (n == 0)
{
return;
}
print_space(n - 1);
print_asterisk(num - n + 1);
System.out.println();
pattern(n - 1, num);
}
public static void main(String[] args) {
int n = 5;
pattern(n, n);
}
}

OUTPUT:
TASK#10 (Pattern 4):

package lab7;
public class T10_4{
public static void printInvertedRightHalfPyramidStarPattern(int rows) {
if (rows <= 0) {
return;
}
for (int i = rows; i > 0; i--) {
System.out.print("*");
}
System.out.println();
printInvertedRightHalfPyramidStarPattern(rows - 1);
}
public static void main(String[] args) {
int rows = 5;
printInvertedRightHalfPyramidStarPattern(rows);
}
}

OUTPUT:
TASK#10 (A):

package lab7;
import java.util.Scanner;
public class T10A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int number = sc.nextInt();
System.out.print("In reverse: ");
reverseInt(number);
System.out.println();
}
public static void reverseInt(int number) {
if (number == 0) return;
System.out.print(number % 10);
reverseInt(number / 10);
}
}

OUTPUT:
TASK#10 (B):

package lab7;
import java.util.Scanner;
public class T10B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int number = sc.nextInt();
System.out.print("In binary: ");
dec2bin(number);
System.out.println();
}
public static void dec2bin(int number) {
if (number == 0) return;
dec2bin(number / 2);
System.out.print(number % 2);
}
}

OUTPUT:

You might also like