Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
55 views7 pages

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

This document contains code for 4 programming exercises - the first calculates Fibonacci numbers, the second sums the digits of a number, the third compares left-to-right and right-to-left harmonic sums, and the fourth finds the nth prime number. Each exercise includes the code, labeled inputs and outputs. The document provides code solutions to 4 programming problems involving recursion, digit summing, series comparison, and prime number identification.

Uploaded by

Khushal Israni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views7 pages

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

This document contains code for 4 programming exercises - the first calculates Fibonacci numbers, the second sums the digits of a number, the third compares left-to-right and right-to-left harmonic sums, and the fourth finds the nth prime number. Each exercise includes the code, labeled inputs and outputs. The document provides code solutions to 4 programming problems involving recursion, digit summing, series comparison, and prime number identification.

Uploaded by

Khushal Israni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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