Video 35(ch7)Ps7

Yash
0

 








Program:-

public class PracticeSet7V35 {
    //Q.1 write a java program to print multiplication table of a number n.
    static void mul(int n) {
        for(int i=1;i<=10;i++) {
            System.out.format("%d * %d = %d \n",n,i,n*i);
        }
    }
    /*Q2. write a program using function to print the following pattern.
    *
    **
    ***
    ****
     */
    static void star(int n){
        for(int i = 0;i<n;i++){
          for(int j = 0;j<i+1;j++){
              System.out.print("*");
          }
            System.out.println();
        }
    }
    //Q3.write a recursive function to calculate sum of first n natural numbers.
    /* n + number(n-1)
     3 + number(2) = 3 + 2 + number(1) = 3 + 2 + 1 = 6*/
    static int number(int n){
        if(n==1){
            return 1;
        }
        else{
            return n + number(n-1);
        }
    }
    /*Q4.write a program to print the following pattern.
    ****
    ***
    **
    *
     */
    static void star_r(int n){
        for(int i = n;i>0;i--){
            for(int j = 0;j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
    //Q5.write a function to print Nth term of fibonacci series using recursion.
    static int fibonacci(int n){
        if(n==1 || n==2){
            return n-1;
        }
        else{
            return fibonacci(n-1) + fibonacci(n-2);
        }
    }
   //Q6.write a function to find average of a set of numbers passed as arguments.
    static int Average(int ...arr){
        int a = 0;
        System.out.print("The entered elements are: ");
        for(int element:arr){
            System.out.print(element+ ", ");
            a += element;
        }
        System.out.print("\nThe average of set of numbers passed as a argument is: ");
       return a;
    }

    //Q7. Repeat 4 using Recursion.
    static void star1(int n ){
        if(n>0){
            star1(n-1);
            for (int i = 0; i <n; i++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    //Q8., Repeat 2using Recursion.
    static void star2(int n){
        if(n>0){
            for(int i = 0;i<n;i++){
                System.out.print("*");
            }
            System.out.println();
            star2(n-1);
        }
    }

    //Q9.Write a function to convert Celsius temperature into Fahrenheit.
    static float temp(float n ){
        //float a = 33.8f;
        float b =(n * 9/5)+32;
        return b;
    }

    //Q10. Repeat 3 using iterative approach.
    static void number1(int n){
        int a = 0;
        for(int i = 1;i<=n;i++){
            a +=i;
        }
        System.out.println(a);
    }
    public static void main(String[] args) {
        System.out.println("Q1.");
        mul(4);
        System.out.println("Q2.");
        star(4);
        System.out.println("Q3.");
        int a = 4;
        System.out.println("The sum of first "+ a +" natural is: "+number(a));
        System.out.println("Q4.");
        star_r(4);
        System.out.println("Q5.");
        //fibonacci series : 0 1 1 2 3 5 8 13 21 34
        System.out.println("The tenth number of fibonacci number is"+fibonacci(10)+"\n");
        System.out.println("Q6.");
        System.out.println(Average(1, 2, 3));
        System.out.println("Q7.");
        star1(4);
        System.out.println();
        System.out.println("Q8.");
        star2(4);
        System.out.println("Q9.");
        float x = 45.6f;
        System.out.print("Celsius to fahrenheit of "+ x + " is: ");
        System.out.println(temp(x)+"\n");
        System.out.println("Q10.");
        int z =4;
        System.out.print("The sum of first "+ z + " natural no is: ");
        number1(z);
    }
}


Post a Comment

0 Comments
Post a Comment (0)
To Top