0% found this document useful (0 votes)
6 views

Java 3

WAP in Java to reverse a number and to calculate the factorial of a number
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java 3

WAP in Java to reverse a number and to calculate the factorial of a number
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Program-03

Aim: a) Write a Java program to reverse the given digits.

Code:
import java.util.Scanner;
public class ReverseDigits {
public static void main(String[] args) {
System.out.println("Name - Ankit\n" + "Roll No. - 221901206");
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();
int reversedNum = 0;
while (num != 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10; }
System.out.println("Reversed Number:" + reversedNum);
sc.close(); } }

Output:
b) Write a Java program to find the factorial of a number.
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
System.out.println("Name - Ankit\n" + "Roll No. - 221901206");
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int num = sc.nextInt();
long factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println("Factorial of " + num + " is: " + factorial);
sc.close();
}
}

Output:

You might also like