You are on page 1of 13

Name:-Aayush Kumar

Roll no.:- 12112126

Section:- CSB-06

Q1) /* Write a program that reads an integer between 0 and 1000 and multiplies
all the digits in

the integer. For example, if an integer is 932, the multiplication of all its
digits is 54.*/
import java.util.Scanner;

class Q1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter Integer:- ");
       
        int integer = input.nextInt();
       
        int product = 1;
        if( 0 <= integer && integer <= 1000) {
            while(integer != 0) {
                product = product*(integer%10);
                integer = integer/10;
            }
            System.out.println("Product of the digits:- " + product);
        } else {
            System.out.println("Input is out of the testcase");
        }

    }
}

Output:-

Q2)

/* Write a program that prompts the user to enter the minutes (e.g., 1
billion), and displays
the number of years and remaining days for the minutes. For simplicity,
assume that a
year has 365 days.*/
import java.util.Scanner;
public class Q2 {

    public static void main(String[] Strings) {

        double minutesInYear = 60 * 24 * 365;


        Scanner input = new Scanner(System.in);
        System.out.print("Input the number of minutes: ");

        double min = input.nextDouble();


        long years = (long) (min / minutesInYear);
        int days = (int) (min / 60 / 24) % 365;

        System.out.println((int) min + " minutes is approximately " + years +


" years and " + days + " days");
    }
}

Output:-

Q3)

/* Body Mass Index (BMI) is a measure of health on weight. It can be


calculated by taking
your weight in kilograms and dividing, by the square of your height in
meters. Write a
program that prompts the user to enter a weight in pounds and height in
inches and
displays the BMI. Note one pound is 0.45359237 kilograms and one inch is
0.0254
meters. */

import java.util.Scanner;

public class Q3 {
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your weight in pounds:- ");
       
        double weight = input.nextDouble();        
       
        System.out.println("Enter your height in inches:- ");
        double height = input.nextDouble();        

        weight = weight*0.45359237;
        height = height*0.0254;

        double BMI = weight/(height*height);


        System.out.println("Your BMI is:- " + String.format("%.2f", BMI));

    }
   
}

Output:-

Q4)

/*If you know the balance and the annual percentage interest rate, you can
compute the
interest on the next monthly payment using the following formula:
interest = balance * (annualInterestRate/1200)
Write a program that reads the balance and the annual percentage interest
rate and
displays the interest for the next month.*/
import java.util.Scanner;

public class Q3 {
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your Balance:- ");
       
        double balance = input.nextDouble();        
       
        System.out.println("Enter your annualInterestRate:- ");
        double annualInterestRate = input.nextDouble();        

        double interest = balance/(annualInterestRate/1200);


        System.out.println("Your Total Interest is:- " + String.format("%.2f",
interest));

    }
   
}
Output:-

Q5)

/*Write an application that reads two integers, determines whether the first
is a multiple of
the second and prints the result. [Hint: Use the remainder operator.]*/
import java.util.Scanner;

public class Q3 {
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
        System.out.println("Enter First Number:- ");
       
        double First_int = input.nextDouble();        
       
        System.out.println("Enter Second Number:- ");
        double Second_int = input.nextDouble();        

        double multiple = First_int % Second_int;


        if( multiple == 0) {

            System.out.println("Yes, It is a multiple of second number");

        } else {
            System.out.println("No, It is not a multiple of second number");

        }

    }
   
}

Output:-
Q6)

/*Write a program that reads an integer and prints it in binary, octal, and
hexadecimal.*/
import java.util.Scanner;

public class Q3 {
   

    public static void octal(int num) {


        int[] binary = new int[35];
        int id = 0;
 
        while (num > 0) {
            binary[id++] = num % 8;
            num = num / 8;
        }
        for (int i = id - 1; i >= 0; i--)
            System.out.print(binary[i] + "");
       
    }

    public static void binary(int num) {


        int[] binary = new int[35];
        int id = 0;
 
        while (num > 0) {
            binary[id++] = num % 2;
            num = num / 2;
        }
        for (int i = id - 1; i >= 0; i--)
            System.out.print(binary[i] + "");
       
    }
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
        System.out.println("Enter Number:- ");
       
        int num = input.nextInt();        

        System.out.println("Binary Form:-\n");
        binary(num);
        System.out.println("\nOctal Form:- \n");
        octal(num);
        System.out.println("\nHexadecimal Form:- ");
        System.out.println(Integer.toHexString(num));
    }
   
}

Output:-

Q7)

// Design and implement an application that reads a sequence of up to 25


pairs of names and mobile numbers for individuals. Store the data in an
object designed to store a first name (string), last name (string), and
mobile number (integer). Assume each line of input will contain two strings
followed by an integer value, each separated by a tab character. Then, after
the input has been read in, print the list in an appropriate format to the
import java.util.*;

public class Q7 {

    static class Student {


        String name;
        String last_name;
        int mobile_no;
   
        public Student(String name1, String last_name1, int mobileNo) {
   
            name = name1;
            last_name = last_name1;
            mobile_no = mobileNo;
   
        }
   
        public void display() {
            System.out.println("First Name:- " + name);
            System.out.println("Last Name:- " + last_name);
            System.out.println("Mobile No.:- " + mobile_no);
   
        }
    }
    public static void main(String[] args) {
        System.out.println("Name:- Aayush Kumar \n Section:- CS B-06 \n
Rollno.:- 12112126");
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the number of students:- ");
        int n = input.nextInt();
        Student[] info = new Student[n];
        System.out.println("Input Student's First Name, Last Name,
Mobile_no.");

        for (int i = 0; i < n; i++) {


            String name = input.next();
            String last_name = input.next();
            int mobile_no = input.nextInt();
            info[i] = new Student(name,last_name,mobile_no);
        }

        for (int j = 0; j < n; j++) {


            System.out.println("Student No.:- " + (j+1) + "\n");
            info[j].display();
        }

    }
}

Output:-
Q8)

// Java program Miller-Rabin primality test


import java.io.*;
import java.math.*;

class miller {

    static int power(int x, int y, int p) {


       
        int res = 1; // Initialize result
        x = x % p;

        while (y > 0) {
           
            if ((y & 1) == 1)
                res = (res * x) % p;
       
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }
       
        return res;
    }
   
    static boolean miillerTest(int d, int n) {
       
        int a = 2 + (int)(Math.random() % (n - 4));
   
        int x = power(a, d, n);
   
        if (x == 1 || x == n - 1)
            return true;
   
        while (d != n - 1) {
            x = (x * x) % n;
            d *= 2;
       
            if (x == 1)
                return false;
            if (x == n - 1)
                return true;
        }
   
        return false;
    }
   
    static boolean isPrime(int n, int k) {
       
        if (n <= 1 || n == 4)
            return false;
        if (n <= 3)
            return true;
   
        int d = n - 1;
       
        while (d % 2 == 0)
            d /= 2;
   
        for (int i = 0; i < k; i++)
            if (!miillerTest(d, n))
                return false;
   
        return true;
    }
   
    public static void main(String args[]) {
       
        int k = 4; // Number of iterations
   
        System.out.println("All primes smaller "
                                + "than 100: ");
                               
        for (int n = 1; n < 100; n++)
            if (isPrime(n, k))
                System.out.print(n + " ");
    }
}

Output:-

Q9)

//Implement Rabin-Karp algorithm for pattern matching. Show at least three


different
patterns to be tested in a given string.

import java.util.Scanner;

public class Q7 {
    public final static int d = 256;
     
    static void rabin_karp (String pat, String txt, int q)
    {
        int M = pat.length();
        int N = txt.length();
        int i, j;
        int p = 0;
        int t = 0;
        int h = 1;
     
        for (i = 0; i < M-1; i++)
            h = (h*d)%q;
     
        for (i = 0; i < M; i++)
        {
            p = (d*p + pat.charAt(i))%q;
            t = (d*t + txt.charAt(i))%q;
        }
     
        for (i = 0; i <= N - M; i++)
        {
     
            if ( p == t )
            {
                for (j = 0; j < M; j++)
                {
                    if (txt.charAt(i+j) != pat.charAt(j))
                        break;
                }
     
                if (j == M)
                    System.out.println("Pattern found at index " + i);
            }
     
            if ( i < N-M )
            {
                t = (d*(t - txt.charAt(i)*h) + txt.charAt(i+M))%q;
     
                if (t < 0)
                t = (t + q);
            }
        }
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       
        System.out.println("Enter sentence:- ");
        String text = input.next();

        System.out.println("Enter pattern to find:- ");


        String pattern = input.next();

        int q = 101;
        rabin_karp(pattern, text, q);

    }
}

Output:-

Q10)

// Read more about Fermat’s Little Theorem. Write a code to verify it and
implement
// Fermat’s method for primality testing.
import java.util.Scanner;

class Fermets_little {
    static int __gcd(int a, int b)
    {
 
        if (b == 0) {
            return a;
        }
        else {
            return __gcd(b, a % b);
        }
    }
 
    static int power(int x, int y, int m)
    {
        if (y == 0)
            return 1;
        int p = power(x, y / 2, m) % m;
        p = (p * p) % m;
 
        return (y % 2 == 0) ? p : (x * p) % m;
    }
 
    static void modInverse(int a, int m)
    {
        if (__gcd(a, m) != 1)
            System.out.print("Inverse doesn't exist");
 
        else {
 
            System.out.print(
                "Modular multiplicative inverse is "
                + power(a, m - 2, m));
        }
    }
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        System.out.println("Enter an Integer:- ");
        int a = input.nextInt();
        System.out.println("Enter a prime number:- ");

        int m = input.nextInt();

        modInverse(a, m);
    }
}

Output:-

You might also like