Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Name - Khushal - Issrani Reg No 209302027 Sec E Lab 1

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

Name_khushal_issrani

Reg no=209302027
Sec=E
LAB 1

Exercise 1
Code:
import java.util.Scanner;

public class exercise1 {

        static int num(int n) {

            if (n <= 1)

                return n;

            return num(n - 1) + num(n - 2);

    }

    public static void main(String[] args){

            int number,i;

            Scanner s=new Scanner(System.in);

        System.out.println(" enter the range: ");

        number=s.nextInt();

            for(i=1;i<=number;i++){

                System.out.println(num(i));

      }

            double total=0;

        for(i=1;i<=number;i++){

           total=total+num(i);

    }
        double avg=total/number;

        System.out.println( " the avg is "+avg);

  }

Output

Exercise 2
Code:
public class exercise2 {

    static int digit_sum(int n) {

        int sum = 0;

        while (n != 0) {

            sum = sum + n % 10;


            n = n / 10;

    }

        return sum;

  }

    public static void main(String[] args) {

        System.out.println(digit_sum(12345));

  }

  

Output :

Exercise 3
Code:
public class HarmonicSum

    public static void main(String[] args)

  {
        int n = 50000;

        HarmonicSum aHarmonicSum = new HarmonicSum();

        double sL2R = aHarmonicSum.printLeftToRightSum(n);

        double sR2L = aHarmonicSum.printRightToLeftSum(n);

        System.out.printf("Difference: %.15f", (sL2R - sR2L));

        System.out.println();

  }

    private double printLeftToRightSum(int n)

  {

        double sum = 0.0;

        for (int i = 1; i <= n; i++)

    {

            sum += (double) 1/i;

    }

        System.out.printf("Left-to-right harmonic sum %.15f", sum);

        System.out.println();

        return sum;

  }

    private double printRightToLeftSum(int n)

  {

        double sum = 0.0;

        for (int i = n; i >= 1; i--)

    {

            sum += (double) 1/i;

    }

        System.out.printf("Right-to-left harmonic sum %.15f", sum);

        System.out.println();
        return sum;

  }

Output:

Exercise 4
Code:
import java.util.Scanner;

public class NthPrime

    public static void main(String[] args)

  {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter n to compute the nth prime number: ");

        int nth = sc.nextInt();

        int num, flag, i;

        num=1;

        flag=0;

        while (flag < nth)

    {
            num=num+1;

            for (i = 2; i <= num; i++){ // we will loop from 2 to num

                if (num % i == 0)

        {

                    break;

        }

      }

            if ( i == num)

            {//if it is a prime number

                flag = flag+1;

      }

    }

        System.out.println("Value of "+nth+ "th prime: " + num);

  }

Output:

You might also like