Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Core Java Practical Slip 22 Answers

 Write a ‘java’ program to display

Slip 22 A) Write a Java program to calculate factorial of a number using recursion.

Answer :

public class Slip22A {

    public static void main(String[] args) {

        int num = 6;

        long factorial = multiplyNumbers(num);

        System.out.println("Factorial of " + num + " : " + factorial);

    }

    public static long multiplyNumbers(int num)

    {

        if (num >= 1)

            return num * multiplyNumbers(num - 1);

        else

            return 1;

    } 

}

Output :

Slip 22 B) Write a java program for the following:

1. To create a file.

2. To rename a file.

3. To delete a file.

4. To display path of a file.

Answer :

import java.io.*;

import java.util.*;

class Slip22B {

    public static void main(String args[]) throws IOException{

        Scanner  br = new Scanner(System.in);

 

        System.out.println("1. Press 1 Create File\n2. Press 2 Rename a File\n3. Press 3 Delete a File\n4. Press 4 Display Path of a File");

 

        System.out.print("Enter File Name : ");

        String str = br.nextLine();

        File file = new File(str);

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

        int num = br.nextInt();

 

        switch(num){

 

            case 1 :

                if (file.createNewFile()) {

                    System.out.println("File created : " + file.getName());

                } else {

                    System.out.println("File already exists.");

                }

 

            case 2 :

                System.out.print("Enter New File Name : ");

                String newone = br.nextLine();

                File newfile =new File(newone);

                if(file.renameTo(newfile)){

                    System.out.println("File renamed");a

                }else{

                    System.out.println("Sorry! the file can't be renamed");

                }

            break;

 

            case 3 :

                if (file.delete()) {

                    System.out.println("Deleted the file: " + file.getName());

                } else {

                    System.out.println("Failed to delete the file.");

                }

            break;

 

            case 4 :

                System.out.println("File Location : " +file.getAbsolutePath());

            break;

 

            default :

            System.out.println("Wrong Number ..!");

            break;

        }

    }

}

Output :

Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Core Java Practical Slip 22 Answers Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Core Java Practical Slip 22 Answers Reviewed by technical_saurabh on December 30, 2021 Rating: 5

No comments:

Powered by Blogger.