Professional Documents
Culture Documents
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 {
Output:-
Q3)
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;
}
}
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();
}
}
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();
} 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 {
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)
public class Q7 {
}
}
Output:-
Q8)
class miller {
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)
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();
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) {
int m = input.nextInt();
modInverse(a, m);
}
}
Output:-