0% found this document useful (0 votes)
100 views5 pages

Nikhil 2000290100092 WTLAB2

The document contains 5 Java programs written by Nikhil Gupta (Roll No. 2000290100092) for a CSE lab assignment: 1. A program to calculate the factorial of a given number. 2. A program to check if a number is an Armstrong number. 3. A program to calculate the permutation and combination of two numbers. 4. A program to print the Fibonacci series up to a given number. 5. A program to print all prime numbers between 1 to 100.

Uploaded by

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

Nikhil 2000290100092 WTLAB2

The document contains 5 Java programs written by Nikhil Gupta (Roll No. 2000290100092) for a CSE lab assignment: 1. A program to calculate the factorial of a given number. 2. A program to check if a number is an Armstrong number. 3. A program to calculate the permutation and combination of two numbers. 4. A program to print the Fibonacci series up to a given number. 5. A program to print all prime numbers between 1 to 100.

Uploaded by

404UserNotFound
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Nikhil Gupta

Roll No. 2000290100092


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