0% found this document useful (0 votes)
29 views3 pages

Java Assingment 1

The document contains 3 Java programs. The first program calculates the factorial of a user-input number. The second program accepts an integer input and prints that integer's multiplication table up to 12. The third program reads a number from the user and displays the Hailstone sequence for that number, along with the number of steps to reach 1.

Uploaded by

Pratik Lad
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)
29 views3 pages

Java Assingment 1

The document contains 3 Java programs. The first program calculates the factorial of a user-input number. The second program accepts an integer input and prints that integer's multiplication table up to 12. The third program reads a number from the user and displays the Hailstone sequence for that number, along with the number of steps to reach 1.

Uploaded by

Pratik Lad
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/ 3

Q1. Write a program to calculate factorial of a number.

import java.util.Scanner;

public class factorial {

public static void main(String[] args) {


try (Scanner r = new Scanner(System.in)) {
int n=r.nextInt();
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}System.out.println("factorial of"+n+" is "+fact);
}
}

Q2. Write a program that accepts an integer as input and prints the table of that integer up to 12.

import java.util.Scanner;

public class table {

public static void main(String[] args) {


try(Scanner r= new Scanner(System.in)){
int n = r.nextInt();
for(int i=1;i<=12;i++) {
System.out.println(n+"x"+i+"="+n*i);
}
}

Q3. Write a program that reads in a number from the user and then displays the
Hailstone sequence for that number
import java.util.Scanner;

public class Hailstone_sequence {

public static void main(String[] args) {


try (Scanner r = new Scanner(System.in)) {
int n = r.nextInt();
int step = 0;
while(n!=1){
if (n%2==0) {
System.out.println(n+" is even so i take it half
");
n=n/2;
System.out.println(n);
}
else {
System.out.println(n+" is odd so i take it 3*n+1
");
n=n*3+1;
System.out.println(n);
}
step++;
}System.out.println("The process took " + step + " steps to
reach 1");
}
}

You might also like