Fa23-Bse-004 (Lab 7)
Fa23-Bse-004 (Lab 7)
Fa23-Bse-004 (Lab 7)
LAB: 07
ACTIVITY#1:
package lab7;
int i = 5;
int j = 2;
int result;
result = num1;
else
result = num2;
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 {
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);
n1 = n2;
n2 = temp;
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);
}
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 {
return 366;
OUTPUT:
TASK#05:
package lab7;
import java.util.*;
public class T5 {
return length;
String s = sc.nextLine();
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.println();
}
import java.util.Scanner;
public class T7 {
System.out.print("Enter n: ");
printMatrix(scanner.nextInt());
System.out.println();
OUTPUT:
TASK#08:
package lab7;
import java.util.Scanner;
public class T8 {
int vowels = 0;
string = string.toUpperCase();
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);
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: