Hem 068WTLAB2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Name: Hem Kishan

Roll No. 2000290100068


CSE VI-B

WT LAB 2

1. WAJP to find out the factorial of a given number:

import java.util.Scanner;

public class Factorial {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = input.nextInt();

int factorial = 1;
for (int i = 2; i <= num; i++) {
factorial *= i;
}

System.out.println("Factorial of " + num + " is " + factorial);


}
}

2. WAJP to check whether a number is Armstrong or not:


import java.util.Scanner;

public class ArmstrongNumber {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = input.nextInt();

int temp = num;


int sum = 0;

while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, 3);
temp /= 10;
}

if (sum == num) {
System.out.println(num + " is an Armstrong number");
} else {
System.out.println(num + " is not an Armstrong number");
}
}
}

3. Write a Java program to calculate Permutation and Combination of


2 numbers:
import java.util.Scanner;

public class PermutationCombination {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter n: ");
int n = input.nextInt();
System.out.print("Enter r: ");
int r = input.nextInt();

int permutation = factorial(n) / factorial(n - r);


int combination = factorial(n) / (factorial(n - r) * factorial(r));

System.out.println("Permutation of " + n + " and " + r + " is " + permutation);


System.out.println("Combination of " + n + " and " + r + " is " + combination);
}

public static int factorial(int num) {


int factorial = 1;
for (int i = 2; i <= num; i++) {
factorial *= i;
}
return factorial;
}
}

4. Write a Java program to Print Fibonacci Series up to n numbers:


Java
import java.util.Scanner;

public class FibonacciSeries {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter n: ");
int n = input.nextInt();

int first = 0, second = 1;


System.out.print(first + " " + second + " ");

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


int next = first + second;
System.out.print(next + " ");
first = second;
second = next;
}
}

5. Write a Java program to print prime numbers between 1 to 100:

public class PrimeNumbers {


public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}

You might also like